1. Deployment Creation
Imperative Command
kubectl create deployment <deployment-name> --image=<image-name> --replicas=<number-of-replicas>
Example:
kubectl create deployment my-app --image=nginx:1.21 --replicas=3
Declarative Approach (YAML File)
Create a file deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
Apply the YAML file:
kubectl apply -f deployment.yaml
2. Update Deployment
Rolling Update
Modify the YAML file to update the image:
containers:
- name: nginx
image: nginx:1.22
Apply the changes:
kubectl apply -f deployment.yaml
OR, using imperative command:
kubectl set image deployment/<deployment-name> <container-name>=<new-image>
Example:
kubectl set image deployment/my-app nginx=nginx:1.22
Rolling Update Status
Check the status of a rolling update:
kubectl rollout status deployment/<deployment-name>
Example:
kubectl rollout status deployment/my-app
Rollback Deployment
Rollback to the previous version:
kubectl rollout undo deployment/<deployment-name>
Example:
kubectl rollout undo deployment/my-app
3. Update Strategies
Recreate Strategy
All existing pods are terminated before new ones are created.
spec:
strategy:
type: Recreate
RollingUpdate Strategy
Default strategy where pods are updated incrementally.
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # Extra pods allowed during update
maxUnavailable: 1 # Pods allowed to be unavailable during update
4. Useful Deployment Commands
Describe a Deployment
Provides detailed information:kubectl describe deployment <deployment-name>
Example:
kubectl describe deployment my-app
List All Deployments
kubectl get deployments
Get Pods Managed by a Deployment
kubectl get pods -l app=<app-label>
Example:
kubectl get pods -l app=my-app
Scale a Deployment
Increase or decrease replicas:kubectl scale deployment <deployment-name> --replicas=<number>
Example:
kubectl scale deployment my-app --replicas=5
View Deployment History
kubectl rollout history deployment/<deployment-name>
Example:
kubectl rollout history deployment/my-app
Delete a Deployment
kubectl delete deployment <deployment-name>
Example:
kubectl delete deployment my-app
5. YAML for Various Strategies
Recreate Deployment Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: recreate-deployment
spec:
replicas: 2
strategy:
type: Recreate
selector:
matchLabels:
app: recreate-app
template:
metadata:
labels:
app: recreate-app
spec:
containers:
- name: nginx
image: nginx:1.21
Rolling Update Deployment Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: rolling-update-deployment
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 2
maxUnavailable: 1
selector:
matchLabels:
app: rolling-app
template:
metadata:
labels:
app: rolling-app
spec:
containers:
- name: nginx
image: nginx:1.21
Summary of Key Commands
Action | Command |
Create Deployment (imperative) | kubectl create deployment ... |
Apply Deployment (YAML) | kubectl apply -f deployment.yaml |
Update Image (imperative) | kubectl set image deployment/... |
Check Update Status | kubectl rollout status deployment/... |
Rollback Deployment | kubectl rollout undo deployment/... |
Describe Deployment | kubectl describe deployment <deployment-name> |
Scale Deployment | kubectl scale deployment <deployment-name> --replicas=X |
These commands and YAML files will help you manage deployments effectively in Kubernetes.