Kubernetes deployments are the resource objects that are used as an abstraction over the pods and replicasets. They are useful when we want to our application to be highly available. Kubernetes deployments always try to maintain the minimum number of replicas that have been declared in the deployment object.
Advantages of kubernetes deployment
- Deployment Strategy
- Rollout
- Rollback
- Auto Healing
Deployment Strategy:
There are two kinds on deployent startegy that kubernetes provides out of the box. They are Recreate and RollingUpdate. Rolling Update is the default deployment selected when we create any deployment object.
There are two more deployment startegies which are commonly know such as Canary deployments and Blue green deployments.
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:1.25
ports:
- containerPort: 80
There are some kubernetes imperative commands that are very helpful when we want to manage the deployments like updating the image version, restarting the deployment, check the status of the deployment, rollback the previous deployment or rollback to a specific version of the deployment.
Command to create a deployment and save it in a yaml format:
kubectl create deployment nginx-deployment --image=nginx:1.14.2 --dry-run=client -o yaml > deployment.yaml
Command to create a deployment
kubectl create deployment nginx-deployment --image=nginx:1.14.2
Command to update the image of the deployment
kubectl set image deployment nginx-deployment nginx=nginx:latest
Command to restart the deployment
Kubectl rollout restart deployment nginx-deployment
Command to rollback the deployment
kubectl rollout undo deployment nginx-deployment
Command to rollback the deployment to a specific revision
kubectl rollout undo deployment nginx-deployment --to-revison=3
Command to check the status of the deployment
kubectl rollout status deployment nginx-deployment
Command to check the history of the deployment
kubectl rollout history deployment nginx-deployment