Kubernetes Deployment Deep Dive

Chaithanya Kopparthi
3 min readJun 27, 2021

Deployments in kubernetes are a great way to deploy your stateless applications efficiently. It also offers capabilities like auto-healing, autoscaling, controlled upgrades for the deployed stateless applications.

Deployments can be created using a declarative approach using YAML files. When we create a deployment, the below objects will be created in the background.

  • Deployments
  • Replica Set
  • Pod

Pod:

The pod is the smallest entity in kubernetes. It consists of single or multiple dependent containers packed to form a stateless microservice. There can be different types of containers in a pod.

Init Container: Init container is generally shortlived and used for initial configuration before the entry-point script in the container is invoked.

Sidecar: Sidecar’s are helpers generally used for routing, scraping metrics, monitoring the microservice.

Containers: This is the place where your microservice is deployed.

Replica Set:

Replicaset allows replication. It makes sure’s the correct amount of pods are maintained.

  • It can scale up and scale down the pods efficiently.
  • The selector in the spec file defines which pods it should manage.
  • Replicas count defined in the spec file specifies how many pods it should be running.

Deployment:

Deployment allows managing the application lifecycle, like the image used for the application, the number of replicas, what upgrade strategies to be used.

Creating Deployments:

Below is the example spec file for creating a deployment.

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: nginx
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
run: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx:stable-alpine-perl
imagePullPolicy: Always
name: nginx
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- name: secret-volume
mountPath: "/etc/secret"
readOnly: true
- name: config-volume
mountPath: "/etc/config"
env:
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
volumes:
- name: secret-volume
secret:
secretName: mysecret
- name: config-volume
configMap:
name: game-config
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 30

For creating the deployment, we can use the below command.

kubectl create -f deployment.yaml

Upgrade Strategy:

spec.strategy.type defines the process to delete the old pods and replace them with the new pods. spec.strategy.type can be one of RollingUpdate or Recreate.

Recreate: Deletes the existing replica set and creates a new replica, resulting in the deletion of all the old pods before the updated pods get created.

RollingUpdate: Upgrades to the pods are performed in an orderly fashion based on spec.strategy.maxSurge and spec.strategy.maxUnavilable.

MaxSurge defines the number of pods that can be created and MaxUnavilable defines the number of pods that can be unavailable in a deployment at a given point in time.

Rollout Deployment:

Deployment can be rolled out to a previous version using the below command.

kubectl rollout undo nginx-deployment --to-version=2

--

--