[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.19.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 Strategies

  • Rolling Update
    • (default) gradually updates replicas so that there is no downtime
    • maxSurge:
      • how many pods to add at a time
      • number or percentage (25% default)
    • maxUnavailable
      • how many pods can be unavailable during the rolling update
      • number or percentage (25% default)
  • Recreate
    • All existing Pods are killed before new ones are created.
    • All applications are entirely updated but there might be some downtime.
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 2    
      maxUnavailable: 0  

spec:
  replicas: 3
  strategy:
    type: Recreate

Performing an update

# Update the deployment yaml file with a new image
kubectl apply –f my-deployment.yaml

# Or 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 Comment

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 )

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