Kubernetes offers a robust and efficient way to manage containerized applications through various controllers. One of the most powerful controllers is the Deployment Controller, which simplifies the deployment, scaling, and updating of applications in Kubernetes clusters. In this article, we’ll delve into the intricacies of the Deployment Controller, exploring its functionalities, internal workings, deployment strategies, scaling capabilities, and mechanisms for rolling updates and rollback.

Make sure you have a good understanding of various Controllers in Kubernetes and when to use them.

What is Deployment Controller in Kubernetes?

The Deployment Controller in Kubernetes is a higher-level abstraction that manages Workloads. Workloads are basically the deployment and scaling of application Pods. It ensures that the desired state of the application is maintained across the cluster by creating and managing ReplicaSets. It enables users to declaratively define and manage application deployments, facilitating easy updates, scaling, and rollback mechanisms. We have discuused all key features and Functionalities of it next.

Features and Functionalities of Deployment Controllers:

1. Managing Application Deployments:

  • Declarative Configuration: The Deployment Controller allows users to define the desired state of their application deployments declaratively using YAML manifests.
  • Desired State Reconciliation: It continuously monitors the current state of the cluster and reconciles it with the desired state defined in the Deployment manifests.

Here’s an example of a Deployment that creates a ReplicaSet with 3 replicas of an Nginx Pod:

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

By now you know to use the kubectl command to create the Deployment resource:

$ kubectl apply -f nginx-deployment.yml

2. Ensuring High Availability and Reliability:

  • ReplicaSets: Internally, the Deployment Controller manages ReplicaSets, which ensure the desired number of Pods (replicas) are running at all times. ReplicaSets act as a template for creating and managing identical Pods.
  • Automatic Pod Replacement: In case of Pod failures or node disruptions, the Deployment Controller automatically replaces failed Pods to maintain the desired number of replicas, ensuring high availability of the application.

The Deployment Controller uses a ReplicaSet to ensure that the desired number of replica Pods are running at any given time. A ReplicaSet is a resource (another Controller) that ensures that a specified number of Pods are running and available. In the same example, we can see that  Deployment creates a ReplicaSet with 3 replicas of an Nginx Pod:

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  ....
  ....
  ## Explained in the first example

3. Flexible Deployment Strategies:

  • Rolling Updates: The Deployment Controller supports rolling updates, allowing users to update their application deployments gradually without causing downtime. During a rolling update, the Controller creates a new ReplicaSet with the updated application version and gradually scales down the old ReplicaSet while scaling up the new one.
  • Recreate Strategy: Alternatively, users can opt for a recreate strategy where all existing Pods are terminated before new Pods are created with the updated configuration. While this approach may result in downtime, it ensures a clean transition to the new version of the application.
  • Blue/Green: This strategy creates a new set of Pods alongside the existing set, allowing for easy rollbacks and A/B testing.

The Deployment Controller supports several strategies for updating Pods, including RollingUpdate, Recreate, and Blue/Green. 

Rolling Updates Example:

Here is an example of a Deployment that uses the RollingUpdate strategy. In this example, the Deployment specifies a RollingUpdate strategy with a maxSurge of 1 and a maxUnavailable of 0. This means that at most one new Pod will be created at a time, and no Pods will be unavailable during the deployment.

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest

Recreate Strategy Example:

In this example, the strategy field is set to Recreate, which means that Kubernetes will terminate all existing Pods before creating new Pods with the new version. This strategy is simple, but can result in downtime for your application.

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 3
  strategy:
   type: Recreate
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app-container
          image: my-app:1.0.0
          ports:
            - containerPort: 80

4. Scalability and Autoscaling:

  • Scaling: The Deployment Controller enables horizontal scaling of application Pods by allowing users to scale the number of replicas up or down based on workload demands. This ensures that applications can handle increased traffic or scale down during periods of low demand.
  • Autoscaling: Additionally, the Deployment Controller integrates with the Horizontal Pod Autoscaler (HPA), which automatically adjusts the number of replicas based on observed CPU or custom metrics, enabling efficient resource utilization and cost optimization.

To demonstrate scaling and autoscaling of Pods in Kubernetes using the Deployment controller with the nginx:latest image, let’s create a simple example Deployment manifestas below. We are initially creating 3 replicas of the Nginx pod by deploying it as kubectl apply -f nginx-deployment

nginx-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3   # Initial number of replicas
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx-container
        image: nginx:latest
        ports:
        - containerPort: 80

Scaling Pods:

Using the Deployment controller we can just use the scale command to scale up/down as below. Initially 3 pods created, then we scale up to 5 pods and then scale down to 4.

$ kubectl apply -f nginx-deployment.yml
deployment.apps/nginx-deployment created

$ kubectl scale deployment nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled

$ kubectl scale deployment nginx-deployment --replicas=4
deployment.apps/nginx-deployment scaled

Autoscaling of Pods:

Autoscaling allows Kubernetes to automatically adjust the number of Pods based on resource utilization. We make use of another controller called, Horizontal Pod Autoscaler (HPA). We can set up Horizontal Pod Autoscaler (HPA) to enable autoscaling:

nginx-deploy-hpa.yml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: nginx-deploy-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx-deployment
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50  # Target average CPU utilization

Command to deploy/apply the HPA config:

$ kubectl apply -f nginx-deploy-hpa.yml
horizontalpodautoscaler.autoscaling/nginx-deploy-hpa created

This HPA configuration scales the Deployment to maintain an average CPU utilization of 50%. Kubernetes automatically adjusts the number of replicas based on observed CPU utilization.

With the above configuration, Kubernetes will scale the number of Pods in the nginx-deployment Deployment up or down based on CPU utilization. If the CPU usage exceeds the target threshold, Kubernetes will increase the number of replicas, and if it falls below the threshold, Kubernetes will decrease the number of replicas.

By combining manual scaling and autoscaling capabilities of the Deployment controller, you can efficiently manage the number of Pods in your Kubernetes cluster to meet changing workload demands while ensuring optimal resource utilization.

5. Rollback and Revision History:

  • Rollback: In case of issues or errors during an application update, the Deployment Controller provides the ability to rollback to a previous version of the application. This ensures quick recovery and minimizes downtime.
  • Revision History: The Deployment Controller maintains a revision history of all changes made to the application deployment, allowing users to track and revert to specific revisions if needed.

One powerful feature of the Deployment Controller is its ability to roll out new versions of your application and roll back to previous versions if something goes wrong. This is done through the use of ReplicaSets, which are managed by the Deployment Controller. We will deploy the following using kubectl command and explore how to rollback.

ngix-rolling-rollback-v1.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  revisionHistoryLimit: 5
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.25.3
          ports:
            - containerPort: 80
$ kubectl apply -f ngix-rolling-rollback-v1.yml

Now lets say we want to upgrade the deployment with the new nginx:1.25.5 version.

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  revisionHistoryLimit: 5
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.25.5
          ports:
            - containerPort: 80
$ kubectl apply -f ngix-rolling-rollback-v2.yml

1. To view the Revision History

This command will display a list of all revisions along with details such as revision number, image, and timestamp. Each revision represents a different state of the Deployment, allowing us to track changes over time.

$ kubectl rollout history deployment/nginx-deployment

2. Rollback to previous Revision

Rollack to previous version:

$ kubectl rollout undo deployment/nginx-deployment

Rollback to any previous version, replace 2 with the actual revision number you want to rollback to.

$ kubectl rollout undo deployment/nginx-deployment --to-revision=2

3. Automatic Rollback 

Kubernetes can also automatically rollback a deployment if it detects that something has gone wrong. You can define the rollback conditions in the Deployment spec:

spec:
  progressDeadlineSeconds: 600
  rollbackTo:
    revision: 3

Conslusion:

This article provides a comprehensive overview of the Deployment controller in Kubernetes, covering its fundamental concepts, internal workings, deployment strategies, scaling capabilities, and mechanisms for rollback and revision history. By exploring each aspect in detail and providing practical examples, readers gain a thorough understanding of how to effectively manage application deployments in Kubernetes environments.

From understanding the role of the Deployment controller in maintaining the desired state of applications to learning about the flexible deployment strategies like rolling updates and recreate, this article equips readers with the knowledge to deploy, scale, and update their applications with confidence. Additionally, the demonstration of scaling and autoscaling using real-world examples enhances the practicality of the content.

Furthermore, the explanation of rollback and revision history demonstrates the importance of maintaining a reliable deployment process and troubleshooting issues effectively. With the ability to perform seamless rollbacks and track changes over time, Kubernetes users can ensure the stability and resilience of their applications in production environments.

Overall, this article serves as a valuable resource for Kubernetes practitioners, providing them with the insights and techniques needed to leverage the Deployment controller effectively and manage application deployments with efficiency and reliability

By |Last Updated: April 18th, 2024|Categories: Kubernetes|