Understanding Kubernetes Deployments on Azure AKS

Deployments are a fundamental Kubernetes resource that provides declarative updates for Pods and ReplicaSets. A Deployment manages a desired state for your applications, ensuring that a specified number of Pod replicas are running and available at all times.

Tip: Deployments are the recommended way to manage stateless applications on Kubernetes.

Core Concepts of Deployments

A Deployment consists of the following key components:

Creating a Deployment

You can create a Deployment using a YAML manifest file. Here's a simple example for deploying a basic Nginx web server:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3 # Specifies the desired number of pods
  selector:
    matchLabels:
      app: nginx # Selects pods with this label
  template:
    metadata:
      labels:
        app: nginx # Labels applied to the pods created by this deployment
    spec:
      containers:
      - name: nginx
        image: nginx:1.21.6 # The container image to use
        ports:
        - containerPort: 80 # The port the container listens on
            

To apply this manifest, save it to a file (e.g., nginx-deployment.yaml) and run:

kubectl apply -f nginx-deployment.yaml

Deployment Strategies

Deployments support different update strategies to manage application rollouts:

1. Rolling Update (Default)

This strategy gradually updates Pods by deleting old Pods and creating new ones. It ensures that there are always a specified number of available Pods during the update. You can control the pace of the rollout using maxUnavailable and maxSurge parameters.


spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25% # Max pods unavailable during rollout
      maxSurge: 25%       # Max pods that can be created over the desired amount
            

2. Recreate

This strategy terminates all existing Pods before creating new ones. This can lead to downtime.


spec:
  strategy:
    type: Recreate
            
Note: Rolling updates are generally preferred for minimizing downtime.

Managing Deployments

You can use kubectl commands to manage your Deployments:

Kubernetes Deployment Lifecycle
A simplified diagram of the Kubernetes Deployment lifecycle.

Key Benefits of Using Deployments

Warning: Ensure your container images are tagged correctly to avoid deploying unintended versions. Use immutable tags for production.

Conclusion

Deployments are a cornerstone of managing applications on Azure Kubernetes Service. By leveraging Deployments, you can achieve robust, reliable, and scalable application deployments with ease.