learning kubernetes by minikube, 6

Upgrade the application

Goals

  1. Upgrade a deployment from 1 version to another
  2. 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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,436评论 0 10
  • 那榆阴下的一潭,不是清泉,是天上虹。怎样指导学生理解、运用此类句式?这个时候的清泉,不再仅仅是清泉,这一潭清泉融入...
    半夏33阅读 172评论 0 0
  • 题目:test普通用户尝试读取/root/README 首先发现root目录是其他用户不可读的 [test@ALI...
    Ryans阅读 297评论 0 1
  • 说实话,关于什么是智商,感觉一直是一个比较模糊的概念,原先的理解,智商就是人的聪明的程度,本应该就是天生的嘛,天生...
    戒得草堂阅读 182评论 0 0