Kubernetes Deployment Tutorial
Kubernetes Deployment Tutorial
Deployment implements the features like rolling updates, roll-back of updates. Deployments have the capability to update the the docker image which you are using in the pods and are also capable of rolling back to the previous docker image versions. In this blog post i will clearly explain about how to create kubernetes deployment and how to update the deployment and how to rollback deployments.
If you create a deployment with name 'mydep' it will create a ReplicaSet with name mydep-<replica-set-id>, which will further create a Pod with name mydep-<replica-set->-<pod-id>. so using deployment we can create the repicaset. so no need to create replicaset again manually for this pods.
cat abcd.yml
apiVersion: apps/v1 kind: Deployment metadata: name: mydep labels: dep: nginxdep spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.16.1 ports: - containerPort: 80
Create Deployment
using kubectl create command we can create the deployment.
master $ kubectl create -f abcd.yml deployment.apps/mydep created
List the deployments
using kubectl get command we can list the deployments
master $ kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE mydep 3/3 3 3 87s
Describe the deployment:
To know more about deployment, deployment metadata, pods status and replicas we can use describe command.
kubectl describe deployment <deployment-name>
master $ kubectl describe deployment mydep Name: mydep Namespace: default CreationTimestamp: Sat, 07 Sep 2019 18:02:36 +0000 Labels: dep=nginxdep Annotations: deployment.kubernetes.io/revision: 1 Selector: app=nginx Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.16.1 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: mydep-59777878f8 (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 5m1s deployment-controller Scaled up replica set mydep-59777878f8 to 3
Update Deployment or Rolling Update
To edit or update existed deployment edit your yml file. Here update means changing the docker images/labels/... not replicas numbers. after updating the deployment yml file execute kubectl apply -f ymlfile command to update your deployments. For every update one replicaset will be created. Rolling updates incrementally replace your resource's Pods with new ones. whenever you update the deployment using RollingUpdate(default) strategy, a new ReplicaSet is created and the Deployment moves the Pods from the old ReplicaSet to the new one.
vi abcd.yml change the docker image version/docker image kubectl apply -f abcd.yml
here abcd.yml is my deployment yml file. Here i am updating my deployment from old nginx:1.16.1 to new nginx 1.17.3
version. and using kubectl apply -f command we can update our deployment.
Rollout History
we can verify the roll-out history using kubectl rollout history command. It will give the all the information about your updates/rollouts
kubectl rollout history deployment/mydep
master $ kubectl rollout history deployment/mydep deployment.extensions/mydep REVISION CHANGE-CAUSE 1 <none> 2 <node>
Hear you can see the two versions. we got the first one when we creating the deployment. after changing the image from nginx:1.16.1 to nginx 1.17.3
we got second version.
Rollout status
verify the status of rollout using kubectl rollout status command. it will tell you whether the new update is completed or still in progress. update will take some time to change pods from old version to new versions.
kubectl rollout status deployment/<deploymentname>
master $ kubectl rollout status deployment/mydep deployment "mydep" successfully rolled out
Rollback deployment
as we discussed in rollout history we have two versions, in first version we are using nginx 1.16.1. in second version we are using ngin 1.17.3. kubernetes deployment provides feature like rollout to previous deployments
using kubectl rollout undo command we can rollback to previous versions.
kubectl rollout undo deployment/mydep --to-revision=1
master $ kubectl rollout undo deployment/mydep --to-revision=1 deployment.extensions/mydep rolled back
so you will get previous version of deployment. you can verify with kubectl describe deployment mydep. if you don't mention revision number it will roll out to recent deployment or last deployment.
Scale the deployment
using kubectl scale command we can upscale or descale your deployment, i,e we can increase or decrease pods running under this deployment.
Upscale:
master $ kubectl scale deployment mydep --replicas=5 deployment.extensions/mydep scaled
Downscale:
master $ kubectl scale deployment mydep --replicas=1 deployment.extensions/mydep scaled
we use same command to upscale and downscale, just we have increase or decrease the number of replicas
verify the number of pods running under this deployment with kubectl get deployment mydep
master $ kubectl get deployment mydep NAME READY UP-TO-DATE AVAILABLE AGE mydep 1/1 1 1
Here you can see after down scaling to 1, only 1 pod is running under this deployment.
- kubernetes deployment tutorial
- kubernetes deployment tutorial
- deployment tutorial kubernetes