Updating Your App
The goal of this scenario is to update a deployed application with kubectl set image and to rollback with the rollout undo command.
Update the version of the app
kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.extensions "kubernetes-bootcamp" scaled
kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4 4 4 4 1h
kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5c69669756-7665t 1/1 Running 0 28s
kubernetes-bootcamp-5c69669756-jbdr4 1/1 Running 0 23m
kubernetes-bootcamp-5c69669756-vdwgm 1/1 Running 0 1h
kubernetes-bootcamp-5c69669756-zx597 1/1 Running 0 28s
To update the image of the application to version 2, use the set image command, followed by the deployment name and the new image version
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.apps "kubernetes-bootcamp" image updated
The command notified the Deployment to use a different image for your app and initiated a rolling update.
Check the status of the new Pods, and view the old one terminating with the get pods command
kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5c69669756-7665t 1/1 Terminating 0 2m
kubernetes-bootcamp-5c69669756-jbdr4 1/1 Running 0 25m
kubernetes-bootcamp-5c69669756-vdwgm 1/1 Running 0 1h
kubernetes-bootcamp-5c69669756-zx597 1/1 Running 0 2m
kubernetes-bootcamp-7799cbcb86-gg62f 0/1 ContainerCreating 0 8s
kubernetes-bootcamp-7799cbcb86-hr9js 0/1 ContainerCreating 0 7s
kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5c69669756-7665t 1/1 Terminating 0 2m
kubernetes-bootcamp-5c69669756-jbdr4 1/1 Terminating 0 25m
kubernetes-bootcamp-5c69669756-vdwgm 1/1 Terminating 0 1h
kubernetes-bootcamp-5c69669756-zx597 1/1 Terminating 0 2m
kubernetes-bootcamp-7799cbcb86-gg62f 1/1 Running 0 19s
kubernetes-bootcamp-7799cbcb86-h6rn7 1/1 Running 0 8s
kubernetes-bootcamp-7799cbcb86-hr9js 1/1 Running 0 18s
kubernetes-bootcamp-7799cbcb86-kq5xr 1/1 Running 0 5s
kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7799cbcb86-gg62f 1/1 Running 0 1m
kubernetes-bootcamp-7799cbcb86-h6rn7 1/1 Running 0 54s
kubernetes-bootcamp-7799cbcb86-hr9js 1/1 Running 0 1m
kubernetes-bootcamp-7799cbcb86-kq5xr 1/1 Running 0 51s
Verify an update
kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: run=kubernetes-bootcamp
Annotations: <none>
Selector: run=kubernetes-bootcamp
Type: NodePort
IP: 10.101.238.220
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 31431/TCP
Endpoints: 172.17.0.10:8080,172.17.0.11:8080,172.17.0.8:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
NODE_PORT=31431
curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7799cbcb86-h6rn7 | v=2
curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7799cbcb86-kq5xr | v=2
curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7799cbcb86-gg62f | v=2
curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7799cbcb86-hr9js | v=2
We hit a different Pod with every request and we see that all Pods are running the latest version (v2).
The update can be confirmed also by running a rollout status command
kubectl rollout status deployments/kubernetes-bootcamp
deployment "kubernetes-bootcamp" successfully rolled out
Rollback an update
Let’s perform another update, and deploy image tagged as v10
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
deployment.apps "kubernetes-bootcamp" image updated
See the status of the deployment
kubectl get deployments -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
kubernetes-bootcamp 4 5 2 3 1h kubernetes-bootcamp gcr.io/google-samples/kubernetes-bootcamp:v10 run=kubernetes-bootcamp
And something is wrong… We do not have the desired number of Pods available. List the Pods again
kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5f76cd7b94-b4ldz 0/1 ErrImagePull 0 1m
kubernetes-bootcamp-5f76cd7b94-j9rcs 0/1 ErrImagePull 0 1m
kubernetes-bootcamp-7799cbcb86-gg62f 1/1 Running 0 7m
kubernetes-bootcamp-7799cbcb86-h6rn7 1/1 Running 0 7m
kubernetes-bootcamp-7799cbcb86-hr9js 1/1 Running 0 7m
There is no image called v10 in the repository.
Let’s roll back to our previously working version.
kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps "kubernetes-bootcamp"
kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7799cbcb86-55pkx 1/1 Running 0 16s
kubernetes-bootcamp-7799cbcb86-gg62f 1/1 Running 0 9m
kubernetes-bootcamp-7799cbcb86-h6rn7 1/1 Running 0 9m
kubernetes-bootcamp-7799cbcb86-hr9js 1/1 Running 0 9m
kubectl get deployments -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
kubernetes-bootcamp 4 4 4 4 1h kubernetes-bootcamp jocatalin/kubernetes-bootcamp:v2 run=kubernetes-bootcamp
We see that the deployment is using a stable version of the app (v2). The Rollback was successful.