K8 Configuration Files
Every Kubernetes configuration file has 3 sections:
- metadata - name, labels, info about what you're deploying
- specification - all the details about how and what you want deploy attributes - dependent on the kind/type of deployment
- status - automatically generated by K8 (desired vs actual) etcd stores/holds the current status of K8 component
The first two lines of every config consist of an API version and the type of configuration (i.e. deployment, service, etc). The API version is dependent on the type and can be looked up.
Configuration Format
- it's in YAML
- strict indentation
- human friendly
Blueprints (templates)
- blueprints or templates are defined in the specification section of the configuration
- templates section will have its own metadata and specification sections
- basically a config inside a config
- blueprints apply to Pod
- contains information such as
- base image name
- ports
- name of pod
Connecting Components
Labels and Selectors make the connections between the K8 component
- labels in the metadata section
- matchLabels in the spec section
- labels again in the metadata of the template
when creating the configuration file you can separate different components (i.e. service, deployment) by specifying three dashes which means new configuration but in the same physical file.
---
apiVersion: 1
kind: Deployment
... start deployment configuration ...
... end deployment configuration ...
---
apiVersion: 1
kind: Service
... start service configuration ...
... end service configuration ...
Service exmaple:
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service # this is just a name could be anything
spec:
selector:
app: mongodb # this should match what has been specified in the deployment metadata
# type: LoadBalancer # uncomment if you want to make this service an external one
ports:
- protocol: TCP
port: 27017 # externally exposed port by the service
targetPort: 27017 # pod/container port specified in the pod specification
# nodePort: 30000 # only used when service type is external, port range: 30000-32767
ConfigMap exmaple:
apiVersion: v1
kind: ConfigMap
metadata:
name: mongodb-configmap
data:
database_url: mongodb-service # this is the service name defined in service config
deployment example:
apiVersion: apps/v1
kind: Deployment # what I am creating
metadata:
name: nginx-deployment
labels:
app: nginx
spec: # blueprint/specificaiton for deployment
replicas: 1 # number of replicas
selector:
matchLabels:
app: nginx
template: # specification for pod
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16
ports:
- containerPort: 80
When using minikube, to get the external service's external IP you have to run the following command:
minikube servcie <serviceName>