[Kubernetes] Declarative Kubernetes YAML
Declarative Kubernetes YAML
- Kubectl apply
- Kubernetes Configuration YAML
- Building YAML Files
- Building YAML spec
- Dry Runs and Diff’s
- Labels and Annotations
Kubectl apply
kubectl apply -f filename.yaml
Kubernetes Configuration YAML
- Each file contains one or more manifests
- Each manifest describe an API object(e.g. deployment, job, secret, etc.)
- Each manifest needs four parts(
apiVersion
,kind
,metadata
,spec
)
Pod
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.17.3
ports:
- containerPort: 80
Deployment
apiVersion: apps/v1 # api version is different with above since it is related with the kind(in this case deployment)
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.17.3
ports:
- containerPort: 80
Deployment & Service
apiVersion: v1
kind: Service
metadata:
name: app-nginx-service
spec:
type: NodePort
ports:
- port: 80
selector:
app: app-nginx
--- # way of telling k8s that the code above and below are entities
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-nginx-deployment
spec:
replicas: 3
selector: # how one object find the resources that it wants to talk to
matchLabels:
app: app-nginx
template:
metadata:
labels:
app: app-nginx
spec:
containers:
- name: nginx
image: nginx:1.17.3
ports:
- containerPort: 80
Building YAML Files
kind
: get a list of resources the cluster supports(kubectl api-resources
)- List will get longer when third party tools gets included
- Some Resource have multiple API’s
- Find the documents of the corresponding
APIVERSION
apiVersion
: get the api version the cluster supports(kubectl api-versions
)kind
&apiVersion
together decides which resource to get and which api version will be used for the servicemetadata
: only name is requiredspec
: where all the action is at
Building YAML spec
# get all the keys each kind supports
kubectl explain services --recursive
kubectl explain deployment --recursive
kubectl explain services.spec # see only 'spec' service
kubectl explain services.spec.type # see only the 'type' key (of spec service)
Dry Runs and Diff’s
- Supported after k8s 1.15
kubectl apply -f app.yml --dry-run=client
kubectl apply -f app.yml --dry-run=server
# show changes between the one running on the server and the current YAML file
# does not check errors
kubectl diff -f app.yml
Labels and Annotations
- Labels goes under
metadata
in the YAML, which are optional - List of
key: value
pair for identifying resources later by selecting, grouping, or filtering - Common examples include
tier: backend
,app: api
,env: prod
- Annotations are used often to store config data(proxy, ingress)
# filter a get command
kubectl get pods -l app=nginx
# apply only matching labels
kubectl apply -f myfile.yaml -l app=nginx
Label Selectors
- Telling
Services
andDeployments
which pod are theirs - Many resources use
Label Selectors
to “link” resources dependencies- Which is why
Service
, andDeployment
kind YAML have theseLabel Selectors
- Which is why
- controls which pods go to which nodes
apiVersion: v1
kind: Service
metadata:
name: app-nginx-service
spec:
type: NodePort
ports:
- port: 80
selector:
app: app-nginx # key, value of a label for pods that it needs to direct traffic to
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-nginx-deployment
spec:
replicas: 3
selector: # how one object find the resources that it wants to talk to
matchLabels:
app: app-nginx # selecting pods based on app-nginx match
template:
metadata:
labels:
app: app-nginx
spec:
containers:
- name: nginx
image: nginx:1.17.3
ports:
- containerPort: 80