Deployment Overview

  • Deployment is a higher-level Kubernetes object designed to manage stateless applications.
  • Ensures desired number of replicas for your app are maintained, updating them as needed.
  • Supersedes the ReplicationController for deploying stateless apps.

Creating and Managing Deployments

  1. Creating Deployments: Use kubectl create deployment or apply a deployment YAML file using kubectl apply -f <filename.yaml>.
apiVersion: 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: my-container
        image: my-image:v1
  1. Listing Deployments: Use kubectl get deployments.

  2. Updating Deployments: Modify the deployment YAML and use kubectl apply -f <filename.yaml> or use imperative commands like kubectl set image.

  3. Deleting Deployments: Use kubectl delete deployment <deployment-name>.

Rolling Updates and Rollbacks

  • Rolling Update: Default strategy for updates. One replica is updated at a time.

    • Use: kubectl set image deployment/my-deployment my-container=my-image:v2.
  • Rollback: Undo an update and revert to a previous version.

    • Use: kubectl rollout undo deployment/my-deployment.
  • History: View rollout history with kubectl rollout history deployment/my-deployment.

Deployment Strategies: Blue-Green, Canary

  1. Blue-Green Deployment:

    • Two environments: Blue (current) and Green (new).
    • Switch traffic from Blue to Green once Green is tested and ready.
    • Implemented using two separate deployments and service selectors.
  2. Canary Deployment:

    • Release new version to a subset of users.
    • If successful, gradually increase the number of users seeing the new version.
    • Can be done using multiple deployments and managing replicas or with service mesh solutions like Istio.

Scaling Deployments

  • Manual Scaling: Adjust the replicas field in the deployment YAML or use kubectl scale deployment/my-deployment --replicas=5.

  • Auto-scaling: Use Horizontal Pod Autoscaler (HPA) to adjust replicas based on CPU or memory usage.

    • Example: kubectl autoscale deployment my-deployment --min=2 --max=10 --cpu-percent=80.

Imperative Commands for Deployments

When you need to directly perform actions without using configuration files, you’d rely on imperative commands. Here are some of the common imperative commands related to deployments:

  1. Create a New Deployment: kubectl create deployment <deployment-name> --image=<image-name>
  2. Update Existing Deployment Image: kubectl set image deployment/<deployment-name> <container-name>=<new-image>
  3. Scale a Deployment: kubectl scale deployment/<deployment-name> --replicas=<number-of-replicas>
  4. Expose a Deployment: kubectl expose deployment <deployment-name> --port=<port> --target-port=<target-port> --name=<service-name> --type=<service-type>

Exercise - Deployment Troubleshooting

Setup:

  1. Create a new deployment using:

    kubectl create deployment ckad-app --image=nginx:1.17
    
  2. Expose the deployment on port 80:

    kubectl expose deployment ckad-app --port=80 --type=NodePort
    

Exercise Task:

You’ve been informed that the ckad-app is not accessible. Troubleshoot and fix the issue.

Steps:

  1. Check Deployment: Confirm if the desired number of replicas matches the current number.

    kubectl get deployments
    
  2. Check Pods: Ensure pods are in a RUNNING state.

    kubectl get pods -l app=ckad-app
    
  3. Check Service: Verify if the service is correctly set up and note the node port assigned.

    kubectl get svc ckad-app
    
  4. Access the Application: Use curl or browser to access the app using the node’s IP and the noted node port.

  5. Check Pod Logs: If there’s an issue with the app itself.

    kubectl logs <pod-name>
    
  6. Describe Resources: This provides detailed information which often includes errors or issues.

    kubectl describe deployment ckad-app
    kubectl describe pod <pod-name>
    

Tips for CKAD - Deployments

  1. Practice Imperative Commands: CKAD is time-constrained. Imperative commands can save a lot of time versus writing YAML files from scratch.

  2. Always Monitor Rollouts: After making changes to a deployment, use kubectl rollout status to ensure changes are being applied correctly.

  3. Know How to Rollback: Mistakes happen. Be comfortable using the kubectl rollout undo command.

  4. Use Dry-Run: Before applying a change, use --dry-run=client with kubectl commands to see what changes would occur without actually applying them.