panchalhimself

Deployments in kubernetes

20 June 2023

deployment defines a desired state for a replica set.

deployment controller ensures the desired state by creating, deleting and replacing pods with new configs.

replicas

Specifies no of pod replicas in the cluster

defined under deployment spec

selector

in case of deployment selector selects the template matching the lables that are defined in the selector via matchLabels

defined under deployment spec

template

defined under deployment spec

defines spec for all containers in the pod and metadata for the pod template.


Why use deployments ?

Easy to scale up and down by changing the number of replicas.

For rolling updates of new version of app.

Roll back to previous version of app

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

scaling a deployment is straightforward

kubectl scale deployment DEPLOYMENT_NAME --replicas=NO_OF_REPLICAS

Pods can also be autoscaled

kubectl autoscale deployment DEPLOYMENT_NAME --min=10 --max=15 --cpu-percent=80

when you autoscale using kubectl autoscale deployment command k8s creates hpa HorizontalPodScaler

kubectl get hpa

hpa stays up even after you destroy the deployment.

so you need to delete the hpa separately in order to stop the autoscaling of the deployment.

search_query: deployment

ref: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/