Kubernetes – Deployment object – simplifying concepts

Kubernetes - Deployment object
Kubernetes - Deployment object

Hi All,

GREETINGS FOR THE DAY 🙂 LIFE IS BEAUTIFUL 🙂

We are continuing series on Kubernetes – simplifying the concepts.

In previous few articles we discussed

  1. What is Kubernetes ? – https://knowledge-junction.in/2021/09/03/kubernetes-introduction-starting-point-for-beginners-helps-to-prepare-the-interview/
  2. Kubernetes – Components / Cluster architecture – simplifying concepts – detailed article – https://knowledge-junction.in/2022/04/27/kubernetes-components-cluster-architecture-simplifying-concepts-detailed-article/
  3. Setting up local environment for Kubernetes – https://knowledge-junction.in/2021/09/11/kubernetes-simplifying-setting-up-local-environment-creating-kubernetes-cluster-locally-helps-in-interview-preparation-kubernetes-for-beginners/
  4. Yaml file guide / reference – https://knowledge-junction.in/2021/09/04/complete-guide-to-yaml/
  5. Kubernetes – Setting up kubernetes cluster for development on Windows 10 box using minikube – https://knowledge-junction.in/2022/05/06/kubernetes-setting-up-kubernetes-cluster-for-development-on-windows-10-box-using-minikube/
  6. Kubernetes – POD – https://knowledge-junction.in/2022/05/05/kubernetes-pod/
  7. Kubernetes – ReplicaSet object – simplifying concepts – https://knowledge-junction.in/2022/07/07/kubernetes-replicaset-object-simplifying-concepts/

Take away from this article

  • What is Kubernetes Deployment object
  • YAML specification of Deployment object
  • kubectl CMDLETS for Deployment object
  • What happens when Deployment object is created
  • KuberNetes Deployment object states

What is Kubernetes – Deployment?

Kubernetes - Deployment object
fig : Kubernetes – Deployment object
  • In last article we discussed about PODs and ReplicaSet objects
  • Each container (our application / docker image) is encapsulated in POD
  • To have our containerized application in our Kubernetes cluster we use Kubernetes deployment object
  • The Deployment object tells our Kubernetes cluster
    • How to create and update instance of our application
  • Once we have deployment, Kubernetes control plane schedule (Scheduler component of Kubernetes Control Plane) application instances from deployment to run on nodes in cluster
  • Once the application instances are created, Kubernetes Deployment Controller monitors those instances
    • If any node goes down, deployment controller replaces application instances with an instance on another node – This is a self-healing mechanism to address machine failure or maintenance
  • Kubernetes Deployment object ensures required scalability and availability of our appliection

Deployment Yaml file

apiVersion: apps/v1
kind: Deployment
metadata:

  name: <deployment name>

spec:
  selector:
    matchLabels:
      app: <label defined in pod template - these must match>
  replicas: <number of replicas> #deployment to run 2 pods matching template
  template: <POD specification>

    metadata:
      labels:
        app: <Pods label - used in deployment selector>
    spec:#indicates pod runs one container
      containers:

      - name: <Container name>
        image: <container image>
        ports:
        - containerPort: 80
  • Kind – Deployment
  • apiVersion : apps/v1
  • POD Template – Here we have spec.selector and spec.template are the required fields
  • Here. spec.selector field how Deployment finds the Pods
  • template field contains
    • .template.spec –
      • POD template – exactly the same schema as POD except api and kind
      • Pods run one container and having respective given image
  • .spec.selector must match .spec.template.metadata.labels, or it will be rejected by the API.

Sample Deployment YAML file – to create the deployment for Registry image. This can be use case where we need to create our private registry. In next article we will discuss about Kubernetes registries

apiVersion: apps/v1
kind: Deployment
metadata:
  name: registry-deployment-knowledjejunction
  labels:
    app: registry
spec:
  replicas: 2
  selector:
    matchLabels:
      app: registry
  template:
    metadata:
      labels:
        app: registry
    spec:
      containers:
      - name: registry
        image: registry:2
        ports:
        - containerPort: 5000
          protocol: TCP

kubectl CMDLETS for Deployment

  • Here I am using minikube utility in my local development environment
  • Start the minikube – minikube start

minikube start

Kubernetes - starting minikube - minikube start
fig : Kubernetes – starting minikube – minikube start
  • Create new deployment using imperative command
kubectl create deployment <deployment name> --image=<image name>

Example:

kubectl create deployment KnowledgeJunction-Deployment --image=KnowledgeJunction

kubectl create deployment test-deploy --image=KnowledgeJunction --replicas=3

  • We could also create Deployment object by using YAML file as

kubectl -f apply <Deployment YAML specification file>

  • Get all Deployments

kubectl get deployments

  • Get details of Deployment

kubectl describe deployments

Scaling a Deployment

  • We could scale deployment using – scale parameter to kubectl CMDLET

kubectl scale <deployment-name> --replicas=5

  • We could also autoscale the deployment – specify minimum and maximum number of PODs we want to run based on CPU utilization of existing PODs – we could do this using – autoscale parameter of kubectl CMDLET

kubectl autoscale <deployment-name> --min=15 --max=25 --cpu-percent=90

What exactly happen when we create Deployment object :

  • Node will be searched to run the our application instance
  • Our application will be scheduled to run
  • Configure cluster to have new node to run application in case existing node fails or get down
  • Each time new deployment found, deployment controller, ReplicaSets are created
  • ReplicaSet creation event is monitored by ReplicaSet controller
  • Next PODs objects are created, monitored by the Scheduler
  • Scheduler selects the appropriate node for the POD based on scheduling algorithms

Deployment States

  • Progressing Deployment
    • Deployment is in progressing state when
      • Deployment creates new ReplicaSet
      • Deployment is scaling up OR scaling down ReplicaSets
      • New PODs are ready or available
  • Complete Deployment
    • Deployment is complete when
      • Any update requested to Deployment is completed
      • All replicas associated with Deployment are available
  • Failed Deployment
    • Deployment may failed when
      • Insufficient quota
      • Image pull errors
      • Insufficient permissions

Thanks for reading !!! Please feel free to discuss / suggestions / share thoughts !!!

HAVE A GREAT TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.