Deployments in kubernetes

Yash Panchal
2 min readJun 22, 2023

Scale your applications using deployments in kubernetes.

Let us understand some of the important components and their use cases :-

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

Following is a basic nginx-deployment.yaml defined that creates a deployment with 3 replica set of nginx :-

nginx-deployment.yaml

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

We use the above deployment using

kubectl apply -f nginx-deployment.yaml

scaling a deployment is as follows

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/

space art ai generated

--

--