This guide provides a comprehensive overview of Kubernetes Deployment objects, a crucial component for the CKAD exam. Deployments are a key Kubernetes API object type used for deploying and managing stateless workloads in a declarative manner.
A Deployment is a higher-level Kubernetes resource that manages ReplicaSets and Pods. It provides the following key functionality:
Deployments add an abstraction layer over ReplicaSets, making it easier to update applications. In production environments, workloads are rarely deployed directly via ReplicaSets because they lack the functionality necessary for easily updating Pods.
Deployments excel at managing stateless applications by:
Kubernetes objects, including Deployments, are typically defined in manifest files using YAML or JSON format. Here’s a basic Deployment manifest structure:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:1.0.0
ports:
- containerPort: 8080
You can create a Deployment in several ways:
kubectl apply -f deployment.yaml
kubectl create deployment my-app --image=my-app:1.0.0 --replicas=3
kubectl create deployment my-app --image=my-app:1.0.0 --dry-run=client -o yaml > deployment.yaml
The RollingUpdate strategy gradually replaces old Pods with new ones, ensuring application availability during updates. You can configure:
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
The Recreate strategy terminates all existing Pods before creating new ones, causing application downtime:
spec:
strategy:
type: Recreate
You can scale a Deployment to change the number of Pod replicas:
kubectl scale deployment my-app --replicas=5
Or by updating the replicas field in the YAML and applying the changes:
kubectl apply -f updated-deployment.yaml
You can update a Deployment by:
kubectl apply -f updated-deployment.yaml
kubectl set image deployment/my-app my-app=my-app:2.0.0
kubectl edit deployment my-app
kubectl rollout status deployment/my-app
kubectl rollout undo deployment/my-app
To roll back to a specific revision:
kubectl rollout undo deployment/my-app --to-revision=2
kubectl rollout history deployment/my-app
Deployments manage Pods, which are the smallest deployable units in Kubernetes. You can configure various aspects of the Pods:
Override the default command in the container image:
spec:
containers:
- name: my-app
image: my-app:1.0.0
command: ["/bin/sh"]
args: ["-c", "echo Hello Kubernetes!"]
Set environment variables for each container:
spec:
containers:
- name: my-app
image: my-app:1.0.0
env:
- name: DB_HOST
value: "mysql"
- name: DB_PORT
value: "3306"
Use ConfigMaps for configuration data and Secrets for sensitive data:
spec:
containers:
- name: my-app
image: my-app:1.0.0
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: app-config
key: db_host
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: app-secrets
key: db_password
Attach volumes to Pods:
spec:
containers:
- name: my-app
image: my-app:1.0.0
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: app-config
Once Pods are running via a Deployment, they can be exposed using a Service:
apiVersion: v1
kind: Service
metadata:
name: my-app
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
type: ClusterIP
Create a Service to expose the Deployment:
kubectl expose deployment my-app --port=80 --target-port=8080
Organize Deployments and other resources into namespaces:
metadata:
name: my-app
namespace: my-namespace
Labels help identify which objects belong to which application:
metadata:
labels:
app: my-app
tier: frontend
environment: production
kubectl create deployment my-app --image=nginxkubectl scale deployment my-app --replicas=3kubectl set image deployment/my-app my-app=nginx:1.19kubectl create deployment my-app --image=nginx --dry-run=client -o yaml > deployment.yaml
kubectl rollout status deployment/my-appkubectl rollout history deployment/my-appkubectl rollout pause deployment/my-appkubectl rollout resume deployment/my-appkubectl rollout undo deployment/my-appPractice deployment updates and rollbacks as these are common exam scenarios
When facing issues with deployments, refer to the troubleshooting guide in the Deployment Strategies and Troubleshooting document for detailed steps and commands.
Kubernetes Deployments are a crucial resource for managing stateless applications in Kubernetes. Understanding how to create, update, and manage Deployments is essential for the CKAD exam and real-world Kubernetes administration. Practice creating and manipulating Deployments to build proficiency and prepare for the CKAD exam.