Upgrade the application
Goals
- Upgrade a deployment from 1 version to another
- Rollback the deployment to the 1st version
Upgrade a deployment from 1 version to another
helloworld-black.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: navbar-deployment
spec:
selector:
matchLabels:
app: helloworld
replicas: 3 # tells deployment to run 3 pods matching the template
template: # create pods using pod definition in this template
metadata:
labels:
app: helloworld
spec:
containers:
- name: helloworld
image: karthequian/helloworld:black
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: navbar-service
spec:
# if your cluster supports it, uncomment the following to automatically create
# an external load-balanced IP for the frontend service.
type: NodePort
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: helloworld
localhost:~ xunyang$ kubectl create -f helloworld-black.yaml
deployment.apps "navbar-deployment" created
service "navbar-service" created
localhost:~ xunyang$ kubectl get all
NAME READY STATUS RESTARTS AGE
navbar-deployment-7d5f55b5f-84zv6 1/1 Running 0 9m
navbar-deployment-7d5f55b5f-l5crm 1/1 Running 0 9m
navbar-deployment-7d5f55b5f-tfg9x 1/1 Running 0 9m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h
navbar-service NodePort 10.97.12.250 <none> 80:32540/TCP 9m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
navbar-deployment 3 3 3 3 9m
NAME DESIRED CURRENT READY AGE
navbar-deployment-7d5f55b5f 3 3 3 9m
check the application run minikube service navbar-service
--record
is used to record rollout history
Looking at the deployment, we see that there are 3 desired, current and ready replicas.
As developers, we're required to make changes to our applications and get these deployed. The rollout functionality of Kubernetes assists with upgrades because it allows us to upgrade the code without any downtime.
now it is going to update the nav bar to a blue color which is black now.
To update the image, run kubectl set image deployment/navbar-deployment helloworld=karthequian/helloworld:blue
localhost:~ xunyang$ kubectl set image deployment/navbar-deployment helloworld=karthequian/helloworld:blue
deployment.apps "navbar-deployment" image updated
Check the upgrade status by run kubectl get rs
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-7d5f55b5f 0 0 0 10m
navbar-deployment-c8b7fcb86 3 3 3 10m
One result set with 3 desired, current and ready pods, and another with 0.
We can also take a look at the rollout history by typing kubectl rollout history deployment/navbar-deployment
.
Rollback the deployment to the 1st version
run kubectl rollout undo deployment/navbar-deployment
. This will revert our changes back to the previous version.
localhost:~ xunyang$ kubectl rollout undo deployment/navbar-deployment
deployment.apps "navbar-deployment"
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-7d5f55b5f 2 2 1 13m
navbar-deployment-c8b7fcb86 2 2 2 12m
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-7d5f55b5f 3 3 3 13m
navbar-deployment-c8b7fcb86 0 0 0 12m
more
add another deployment upgrade
localhost:~ xunyang$ kubectl set image deployment/navbar-deployment helloworld=karthequian/helloworld:lionel
deployment.apps "navbar-deployment" image updated
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-77dbd4458f 1 1 0 9m
navbar-deployment-7d5f55b5f 3 3 3 14m
navbar-deployment-c8b7fcb86 0 0 0 14m
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-77dbd4458f 2 2 1 9m
navbar-deployment-7d5f55b5f 2 2 2 14m
navbar-deployment-c8b7fcb86 0 0 0 14m
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-77dbd4458f 3 3 2 9m
navbar-deployment-7d5f55b5f 1 1 1 14m
navbar-deployment-c8b7fcb86 0 0 0 14m
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-77dbd4458f 3 3 3 9m
navbar-deployment-7d5f55b5f 0 0 0 14m
navbar-deployment-c8b7fcb86 0 0 0 14m
Our webpage will be change to the Lionel version of the deployment. check the rollout versions. noticed here, the enties number is same as the deployment slot.
localhost:~ xunyang$ kubectl rollout history deployment/navbar-deployment
deployments "navbar-deployment"
REVISION CHANGE-CAUSE
2 kubectl set image deployment/navbar-deployment helloworld=karthequian/helloworld:blue
3 kubectl create --filename=helloworld-black.yaml --record=true
4 kubectl set image deployment/navbar-deployment helloworld=karthequian/helloworld:lionel
In a real world setting, you might have a longer history, and might want to rollback to a specific version. To do this, add a --to-revision=version
to the specific version you want to.
localhost:~ xunyang$ kubectl rollout undo deployment/navbar-deployment --to-revision=2
deployment.apps "navbar-deployment"
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-77dbd4458f 1 1 1 15m
navbar-deployment-7d5f55b5f 0 0 0 20m
navbar-deployment-c8b7fcb86 3 3 2 20m
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-77dbd4458f 1 1 1 15m
navbar-deployment-7d5f55b5f 0 0 0 20m
navbar-deployment-c8b7fcb86 3 3 2 20m
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-77dbd4458f 1 1 1 15m
navbar-deployment-7d5f55b5f 0 0 0 20m
navbar-deployment-c8b7fcb86 3 3 2 20m
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-77dbd4458f 1 1 1 15m
navbar-deployment-7d5f55b5f 0 0 0 20m
navbar-deployment-c8b7fcb86 3 3 2 20m
localhost:~ xunyang$ kubectl get rs
NAME DESIRED CURRENT READY AGE
navbar-deployment-77dbd4458f 0 0 0 15m
navbar-deployment-7d5f55b5f 0 0 0 21m
navbar-deployment-c8b7fcb86 3 3 3 20m