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
- Creating Deployments: Use
kubectl create deployment
or apply a deployment YAML file usingkubectl 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
-
Listing Deployments: Use
kubectl get deployments
. -
Updating Deployments: Modify the deployment YAML and use
kubectl apply -f <filename.yaml>
or use imperative commands likekubectl set image
. -
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
.
- Use:
-
Rollback: Undo an update and revert to a previous version.
- Use:
kubectl rollout undo deployment/my-deployment
.
- Use:
-
History: View rollout history with
kubectl rollout history deployment/my-deployment
.
Deployment Strategies: Blue-Green, Canary
-
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.
-
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 usekubectl 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
.
- Example:
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:
- Create a New Deployment:
kubectl create deployment <deployment-name> --image=<image-name>
- Update Existing Deployment Image:
kubectl set image deployment/<deployment-name> <container-name>=<new-image>
- Scale a Deployment:
kubectl scale deployment/<deployment-name> --replicas=<number-of-replicas>
- Expose a Deployment:
kubectl expose deployment <deployment-name> --port=<port> --target-port=<target-port> --name=<service-name> --type=<service-type>
Exercise - Deployment Troubleshooting
Setup:
-
Create a new deployment using:
kubectl create deployment ckad-app --image=nginx:1.17
-
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:
-
Check Deployment: Confirm if the desired number of replicas matches the current number.
kubectl get deployments
-
Check Pods: Ensure pods are in a RUNNING state.
kubectl get pods -l app=ckad-app
-
Check Service: Verify if the service is correctly set up and note the node port assigned.
kubectl get svc ckad-app
-
Access the Application: Use
curl
or browser to access the app using the node’s IP and the noted node port. -
Check Pod Logs: If there’s an issue with the app itself.
kubectl logs <pod-name>
-
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
-
Practice Imperative Commands: CKAD is time-constrained. Imperative commands can save a lot of time versus writing YAML files from scratch.
-
Always Monitor Rollouts: After making changes to a deployment, use
kubectl rollout status
to ensure changes are being applied correctly. -
Know How to Rollback: Mistakes happen. Be comfortable using the
kubectl rollout undo
command. -
Use Dry-Run: Before applying a change, use
--dry-run=client
withkubectl
commands to see what changes would occur without actually applying them.