Mastering Kubernetes Deployments: A Comprehensive Guide π
Written by Sivaranjan
As a professional DevOps engineer, managing Kubernetes deployments efficiently is crucial. This article will guide you through creating and managing deployment.yaml
files, scaling replicas, setting images at runtime, and more. Let's dive in! π
1. Creating deployment.yaml
π
First, let's create a basic deployment.yaml
file for an Nginx deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
2. Scaling Up & Down Replicas ππ
To scale your deployment replicas up or down, use the following commands:
Scale Up:
kubectl scale deployment nginx-deployment --replicas=4
Scale Down:
kubectl scale deployment nginx-deployment --replicas=2
3. Setting Image Through Runtime π
You can update the container image of your deployment at runtime:
kubectl set image deployment/nginx-deployment nginx=nginx:1.19.0
4. Rollout History π
To check the rollout history of your deployment, use:
kubectl rollout history deployment/nginx-deployment
5. Record Cause π
To record the reason for a deployment update, add the --record
flag:
kubectl set image deployment/nginx-deployment nginx=nginx:stable-perl --record
6. Annotations π·οΈ
Annotations can provide additional metadata to your resources. Add annotations to your deployment.yaml
like this:
metadata:
name: nginx-deployment
annotations:
description: "This deployment runs Nginx"
7. Rollout Previous π
To rollback to the previous deployment, use:
kubectl rollout undo deployment/nginx-deployment
8. Rollout Registered First Version π
To rollback to a specific revision, use:
kubectl rollout undo deployment/nginx-deployment --to-revision=1
Checking All Resources & Types π οΈ
To see all your resources and their types:
kubectl get all
Executing deployment.yaml
π
To create the deployment from your deployment.yaml
file:
kubectl apply -f deployment.yaml
Checking Pod & Node Status π
To check the status of your pods and nodes:
kubectl get pods
kubectl get nodes
Checking Deployment & ReplicaSet π
To check the status of your deployment and its replicaset:
kubectl get deployments
kubectl get replicasets
By mastering these commands and techniques, you'll be well-equipped to manage Kubernetes deployments like a pro. Happy deploying! πβ¨
Feel free to share your thoughts or ask any questions in the comments below! π