Kubernetes YAML for a deployment
Below is the Kubernetes YAML file for creating a deployment. The deployment would control 3 replicas of nginx app.
~/kubernetes$ cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-nginx
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Explanation of each field in YAML
Below is the explanation of each of the lines in the YAML file.
kind: Deployment name: deployment-nginx | Create a deployment with the name “deployment-nginx“. |
labels: app: nginx | The label for the deployment is “nginx“. Anytime we want to select this deployment among different deployments, we can use this label. |
replicas: 3 | Create 3 replicas of the deployment spec. Here, it includes the creation of pods with the name “nginx“; hence create 3 of them. |
template: metadata: labels: app: nginx | The pods are labeled as nginx. |
selector: matchLabels: app: nginx | The deployment would control pods that are labeled as “nginx“. |
Running the YAML file
We have created a Kubernetes 2 node cluster with the client machine where we issue the kubectl command as below. We deploy the YAML file using “kubectl create” and check the pods, replicas, and deployment status using the below commands.
~/kubernetes$ kubectl create -f deployment.yaml
deployment.apps/deployment-nginx created
~/kubernetes$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
deployment-nginx 0/3 3 0 10s
~/kubernetes$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
deployment-nginx 3/3 3 3 17s
~/kubernetes$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-nginx-cd55c47f5-8lpgm 1/1 Running 0 25s
pod/deployment-nginx-cd55c47f5-zfwqk 1/1 Running 0 25s
pod/deployment-nginx-cd55c47f5-zxhjq 1/1 Running 0 25s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 8d
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment-nginx 3/3 3 3 26s
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-nginx-cd55c47f5 3 3 3 26s