Video Thumbnail for Lesson
4.4: Deployment

Deployment

In Kubernetes, a Deployment provides declarative updates to applications. It manages the creation and scaling of ReplicaSets, and allows for controlled, rolling updates to your applications. Deployments are a higher-level concept that includes many additional features beyond what ReplicaSets offer.

If you are deploying stateless applications on Kubernetes, a Deployment is generally the resource type you should use.

Official docs: https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/

Hands-On: Working with Deployments

We will create and examine a Deployment to understand its behavior.

1. Create a Namespace for the Examples

First, we'll create a namespace for these examples and set it as the default.

# task 01-create-namespace
# - Create a namespace for these examples and set as default.
kubectl apply -f Namespace.yaml
kubens 04--deployment

2. Apply a Deployment Configuration

Next, we'll apply Deployment configuration containing 3 replicas of a Pod containing an nginx container image.

# task 03-apply-better
# - Apply the better Deployment configuration.
kubectl apply -f Deployment.nginx-better.yaml

You will notice that within the Deployment definition, the spec looks identical to that of our ReplicaSet from the previous lesson. This is because a Deployment ends up deploying a ReplicaSet but adds the following functionality:

  1. Declarative Updates: Deployments allow you to declaratively update your applications. You can specify the desired state in the Deployment configuration, and the Deployment controller will ensure that the current state matches the desired state.
  2. Rolling Updates: Deployments support rolling updates, which update the pods in a controlled manner with zero downtime. This allows you to update your application to a new version while keeping the old version running until the new version is ready.
  3. Rollback: If an update fails, you can easily roll back to a previous revision of the Deployment. Kubernetes keeps track of Deployment revisions for easy rollback.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-better
  namespace: 04--deployment
  labels:
    app: nginx-better
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-better
  template:
    metadata:
      labels:
        app: nginx-better
    spec:
      containers:
        - name: nginx
          image: cgr.dev/chainguard/nginx:latest
          ports:
            - containerPort: 8080
              protocol: TCP
          readinessProbe:
            httpGet:
              path: /
              port: 8080
          resources:
            limits:
              memory: "50Mi"
            requests:
              memory: "50Mi"
              cpu: "250m"
          securityContext:
            allowPrivilegeEscalation: false
            privileged: false
      securityContext:
        seccompProfile:
          type: RuntimeDefault
        runAsUser: 1001
        runAsGroup: 1001
        runAsNonRoot: true

3. Rollout Restart

To demonstrate a rolling update, we'll roll the pods in one of the Deployments. This simulates updating your application to a new version.

# task 04-rollout-restart
# - Roll the pods in one of the deployments
kubectl rollout restart deployment nginx-better
watch "kubectl get pods"

4. Delete the Namespace to Clean Up

Finally, clean up by deleting the namespace, which will also delete all resources within it.

# task 05-delete-namespace
# - Delete the namespace to clean up.
kubectl delete -f Namespace.yaml