[Kubernetes] Deployments

A Deployment provides declarative updates for Pods and ReplicaSets.

  • A deployment defines the desired state for replica pods.
  • The cluster constantly maintains the desired state by creating, removing, and modifying replica pods.

Deployments | Kubernetes

Creating deployments

Deployment definition file need 3 sections:

  • spec.replicas: The number of replica pods
  • spec.template: A template definition which defines a pod
    • spec.template.metadata
    • spec.template.spec
  • spec.selector: the deployment will manage all pods whose labels match this selector

(Example) A simple deployment of 2 nginx pods

  • Create a YAML file
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:1.16.1
        ports:
        - containerPort: 80
  • Create/Apply
kubectl create -f <yaml-file>

# Use --save-config 
# when you want to use 'kubectl apply' in the future
kubectl create -f <yaml-file> --save-config

kubectl apply -f <yaml-file>

Checking deployments

kubectl get deployments

kubectl get pods –l app=my-nginx

kubectl get deployment <deployment-name>

kubectl describe deployment <deployment-name>

kubectl delete deployment <deployment-name>

Editing deployments

With deployments, you can easily edit any field/property of the pod template such as the number of replicas.

kubectl edit deployment <deployment-name>

# modify spec.replicas

# check the pods
kubectl get pods

Checking replica sets

kubectl get replicasets
kubectl get rs

# check 'NewReplicaSet' property
kubectl describe deploy <deploy-name>

kubectl describe rs <replicaset-name>

Scaling

kubectl scale deploy <deploy-name> --replicas=5

kubectl describe deploy <deploy-name>
kubectl get pods

Deployment Options

  • Rolling update
    • Gradually update replicas so that there is no downtime
  • Blue-green deployment
  • Canary deployment
  • Rollback

Performing a rolling update

# Create a deployment with nginx:1.16.1
kubectl apply –f my-deployment.yaml

# Peform a rolling update
kubectl set image deployment/<deployment-name> \ 
  <container-name>=nginx:1.19.7 --record

Checking the rollout status/history

kubectl rollout status deploy/<deploy-name> 
kubectl rollout status deploy/<deploy-name> --revision=2

kubectl rollout history deploy/<deploy-name> 
kubectl rollout history deploy/<deploy-name> --revision=2

Performing the rollback

kubectl rollout undo deploy/<deploy-name> 

kubectl rollout undo deploy/<deploy-name> --to-revision=2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s