K8S RollingUpdate 滚动升级机制实例

一、准备

1、编写一个简单的http server

package main

import (
        "net/http"
        "log"
        "fmt"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
        fmt.Println("This is version 1 say hello!")
        fmt.Fprintf(w, "Hello World Version 1!")
}

func main()  {
        fmt.Println("Server Start...")
        http.HandleFunc("/hello", sayHello)
        err := http.ListenAndServe(":9999", nil)
        if err != nil {
                log.Fatal("Http Server Err:", err)
        }
}

2、编译得到web1.0的二进制程序,编写Dockerfile如下:

FROM scratch
COPY web1.0 /
EXPOSE 9999
CMD ["/web1.0"]

3、编写升级版本的http server

package main

import (
        "net/http"
        "log"
        "fmt"
)

func sayHello(w http.ResponseWriter, r *http.Request) {
        fmt.Println("This is version 2 say hello!")
        fmt.Fprintf(w, "Hello World Version 2!")
}

func main()  {
        fmt.Println("Server Start...")
        http.HandleFunc("/hello", sayHello)
        err := http.ListenAndServe(":9999", nil)
        if err != nil {
                log.Fatal("Http Server Err:", err)
        }
}

4、编译得到web2.0的二进制程序,编写Dockerfile如下:

FROM scratch
COPY web2.0 /
EXPOSE 9999
CMD ["/web2.0"]

5、根据Dockerfile打包Docker镜像,分别得到web:1.0和web:2.0的Docker镜像

docker build -t web:1.0 --no-cache=true -f Dockerfile .
docker build -t web:2.0 --no-cache=true -f Dockerfile2 .

二、
1、创建Deployment,记录版本历史(–record=true)

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webapp
  namespace: webapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: web:1.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9999
      dnsPolicy: ClusterFirst
      nodeSelector:
        caas_cluster: szd-dev-9c18da5c

---
apiVersion: v1
kind: Service
metadata:
  name: webapp-svc
  namespace: webapp
spec:
  ports:
  - name: http
    port: 9999
    nodePort: 32222
    protocol: TCP
    targetPort: 9999
  selector:
    app: webapp
  type: NodePort
    kubectl create -f web-health.yaml --record=true

2、查看版本历史

    [root@SZD-L0072834 rollingUpdate]# kubectl rollout history deployment/webapp -n webapp
    deployments "webapp"
    REVISION  CHANGE-CAUSE
    1         kubectl create --filename=web-health.yaml --record=true

    [root@SZD-L0072834 rollingUpdate]#

3、触发RollingUpdate

    [root@SZD-L0072834 rollingUpdate]# kubectl set image deployment/webapp webapp=web:2.0 -n webapp
    deployment "webapp" image updated
    [root@SZD-L0072834 rollingUpdate]# kubectl rollout history deployment/webapp -n webapp
    deployments "webapp"
    REVISION  CHANGE-CAUSE
    1         kubectl create --filename=web-health.yaml --record=true
    2         kubectl set image deployment/webapp webapp=web:2.0 --namespace=webapp

    [root@SZD-L0072834 rollingUpdate]# kubectl get pod -n webapp -w
    NAME                      READY     STATUS        RESTARTS   AGE
    webapp-57c89bb7d8-65qc6   1/1       Running       0          20m
    webapp-57c89bb7d8-fxmmg   1/1       Terminating   0          20m
    webapp-58f95c5775-2wpbz   0/1       Running       0          9s
    webapp-58f95c5775-v5kqj   0/1       Running       0          9s
    [root@SZD-L0072834 rollingUpdate]# kubectl rollout status deployment/webapp -n webapp
    Waiting for rollout to finish: 1 old replicas are pending termination...
    Waiting for rollout to finish: 1 old replicas are pending termination...
    Waiting for rollout to finish: 1 old replicas are pending termination...
    Waiting for rollout to finish: 1 of 2 updated replicas are available...
    deployment "webapp" successfully rolled out
    [root@SZD-L0072834 rollingUpdate]#
    [root@SZD-L0072834 rollingUpdate]#
    [root@SZD-L0072834 rollingUpdate]# kubectl get pod -n webapp
    NAME                      READY     STATUS        RESTARTS   AGE
    webapp-57c89bb7d8-65qc6   1/1       Terminating   0          21m
    webapp-58f95c5775-2wpbz   1/1       Running       0          59s
    webapp-58f95c5775-v5kqj   1/1       Running       0          59s
    [root@SZD-L0072834 rollingUpdate]#
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐