Deployments in Kubernetes

  • In prod, we should have multiple instances of app running. If we want to update app, we cant update live apps because users will be accessing it. So we update one after other — This is called Rolling Update.

  • Deployments provides the capabilities to upgrade the instances using rolling updates.

A ReplicaSet ensures a specified number of pod replicas are running at all times. A Deployment manages ReplicaSets, allowing for declarative updates, rollbacks, and versioning of applications. Use Deployments for easier management and updates.

ReplicaSet Example

yamlCopy codeapiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: nginx
        image: nginx:1.17
  • Ensures 3 replicas of the nginx pod are always running.

  • No built-in mechanism for updates or rollbacks.


Deployment Example

yamlCopy codeapiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: nginx
        image: nginx:1.17
  • Manages a ReplicaSet under the hood.

  • Supports rolling updates, version history, and rollbacks. For example, updating the image to nginx:1.18 would trigger a smooth update across pods.


Lets create a deployment (MSST)

M:

metadata:
  name: myreplica
  labels: 
    app: replica-set
    env: prod
    tier: frontend

SST: spec-selector-template

pec:
  selector:
    matchLabels:
      app: replica-set
      env: prod
      tier: frontend
  template:
    metadata:
      labels: 
        name: replica-set
        env: prod
        tier: frontend
    replicas: 3

sample file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
  labels:
    app: mywebsite
    tier: frontend
spec:
  replicas: 4
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
    spec:
      containers:
        - name: nginx
          image: nginx
  selector:

linking the pods to the Deployment by updating selectors.

Instruction: Add a property "matchLabels" under selector and copy the labels defined in the pod-definition under it.

Questions:

deployment queries:
What is the flavor and version of Operating System on which the Kubernetes nodes are running?
cat /etc/os-release

Create a new Deployment with the below attributes using your own deployment definition file.


Name: httpd-frontend;
Replicas: 3;
Image: httpd:2.4-alpine


 k create deploy httpd-frontend --image=httpd:2.4-alpine --replicas=3
deployment.apps/httpd-frontend created

controlplane ~ ➜  k get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment-1          0/2     2            0           28m
frontend-deployment   0/4     4            0           29m
httpd-frontend        3/3     3            3           10s
my-app-dep            3/3     3            3           7m9s