This document provides practical examples of Kubernetes Deployments to complement the theoretical knowledge in the Master Deployment Guide. These examples are designed to help you prepare for the CKAD exam.
Here’s a complete example of a basic Deployment manifest:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
resources:
limits:
cpu: "500m"
memory: "256Mi"
requests:
cpu: "100m"
memory: "128Mi"
Apply this deployment:
kubectl apply -f nginx-deployment.yaml
This example shows how to configure a Deployment with a specific rolling update strategy:
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
labels:
app: frontend
spec:
replicas: 5
selector:
matchLabels:
app: frontend
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend
image: frontend:v1
ports:
- containerPort: 80
This example shows how to configure environment variables in a Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-deployment
labels:
app: backend
spec:
replicas: 2
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: backend:v1
ports:
- containerPort: 8080
env:
- name: DATABASE_URL
value: "postgres://user:password@postgres:5432/db"
- name: API_KEY
value: "your-api-key"
- name: DEBUG_MODE
value: "false"
First, create a ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
database_url: "postgres://user:password@postgres:5432/db"
api_url: "https://api.example.com"
log_level: "info"
Then reference it in the Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-deployment
labels:
app: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1
ports:
- containerPort: 8080
env:
- name: DATABASE_URL
valueFrom:
configMapKeyRef:
name: app-config
key: database_url
- name: API_URL
valueFrom:
configMapKeyRef:
name: app-config
key: api_url
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: log_level
First, create a Secret:
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
data:
db_password: cGFzc3dvcmQxMjM= # base64 encoded "password123"
api_key: dGhpc2lzYXNlY3JldGtleQ== # base64 encoded "thisisasecretkey"
Then reference it in the Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: secure-app
labels:
app: secure-app
spec:
replicas: 3
selector:
matchLabels:
app: secure-app
template:
metadata:
labels:
app: secure-app
spec:
containers:
- name: secure-app
image: secure-app:v1
ports:
- containerPort: 8443
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: app-secrets
key: db_password
- name: API_KEY
valueFrom:
secretKeyRef:
name: app-secrets
key: api_key
This example shows how to set resource limits and requests:
apiVersion: apps/v1
kind: Deployment
metadata:
name: resource-limited-app
labels:
app: resource-app
spec:
replicas: 2
selector:
matchLabels:
app: resource-app
template:
metadata:
labels:
app: resource-app
spec:
containers:
- name: resource-app
image: resource-app:v1
resources:
limits:
cpu: "1"
memory: "512Mi"
requests:
cpu: "500m"
memory: "256Mi"
ports:
- containerPort: 8080
This example shows a Deployment with multiple containers in a Pod:
apiVersion: apps/v1
kind: Deployment
metadata:
name: multi-container-app
labels:
app: multi-app
spec:
replicas: 2
selector:
matchLabels:
app: multi-app
template:
metadata:
labels:
app: multi-app
spec:
containers:
- name: web
image: nginx:1.14.2
ports:
- containerPort: 80
- name: log-collector
image: fluentd:v1.11
volumeMounts:
- name: logs
mountPath: /var/log/nginx
volumes:
- name: logs
emptyDir: {}
This example demonstrates using init containers to perform setup tasks:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-with-init
labels:
app: init-app
spec:
replicas: 3
selector:
matchLabels:
app: init-app
template:
metadata:
labels:
app: init-app
spec:
initContainers:
- name: init-db-check
image: busybox:1.28
command: ['sh', '-c', 'until nslookup mysql; do echo waiting for mysql; sleep 2; done;']
containers:
- name: app
image: app:v1
ports:
- containerPort: 80
nginx image
kubectl create deployment nginx-test --image=nginx --replicas=2
kubectl get deployments
kubectl get pods
kubectl scale deployment nginx-test --replicas=5
kubectl get pods
nginx:1.14.2 image
kubectl create deployment nginx-update --image=nginx:1.14.2
nginx:1.19.0
kubectl set image deployment/nginx-update nginx-update=nginx:1.19.0
kubectl rollout status deployment/nginx-update
kubectl rollout history deployment/nginx-update
kubectl set image deployment/nginx-update nginx-update=nginx:invalid_tag
kubectl rollout status deployment/nginx-update
kubectl rollout undo deployment/nginx-update
kubectl get pods
kubectl describe deployment nginx-update
kubectl create deployment yaml-demo --image=nginx --dry-run=client -o yaml > yaml-demo.yaml
Edit the YAML file to add resource limits and labels
kubectl apply -f yaml-demo.yaml
kubectl create deployment env-demo --image=nginx --dry-run=client -o yaml > env-demo.yaml
spec:
template:
spec:
containers:
- name: env-demo
image: nginx
env:
- name: APP_ENV
value: "production"
- name: LOG_LEVEL
value: "info"
kubectl apply -f env-demo.yaml
kubectl exec -it $(kubectl get pods -l app=env-demo -o jsonpath="{.items[0].metadata.name}") -- env | grep APP_ENV
kubectl get deployments can be shortened to kubectl get deploykubectl describe deployment can be shortened to kubectl describe deploykubectl create deployment name --image=imagekubectl scale deployment name --replicas=3kubectl create deployment name --image=image --dry-run=client -o yaml > deployment.yaml
kubectl set image deployment/name container=image:tagkubectl scale deployment name --replicas=3kubectl edit deployment namekubectl rollout status deployment/namekubectl rollout history deployment/namekubectl rollout undo deployment/nameBy practicing these examples and exercises, you’ll be well-prepared for the Deployment-related questions in the CKAD exam.