kubernetes Deployment 金丝雀发布

创建应用

depoly-demoapp-v10.yaml

apiVersion: v1
kind: Namespace
metadata:
    name: demoapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demoapp-deployment
  namespace: demoapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: demoapp
      controller: demoapp
  template:
    metadata:
      labels:
        app: demoapp
        controller: demoapp
    spec:
      containers:
      - name: demoapp
        image: ikubernetes/demoapp:v1.0
        ports:
        - containerPort: 80
          name: http
        resources:
          requests:
            memory: "256Mi"
            cpu: "50m"
          limits:
            memory: "256Mi"
            cpu: "50m"

demo-svc.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: demoapp-svc
  namespace: demoapp
spec:
  selector:
    app: demoapp
    controller: demoapp
  ports:
  - name: http
    port: 80
    targetPort: 80

创建demo v10资源

# kubectl apply -f depoly-demoapp-v10.yaml -f demo-svc.yaml
namespace/demoapp created
deployment.apps/demoapp-deployment created
service/demoapp-svc created

查看demo v10资源

# kubectl get deploy -n demoapp
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
demoapp-deployment   3/3     3            3           51s

查看 Deployment 滚动更新状态 

# kubectl rollout status deploy/demoapp-deployment -n demoapp
deployment "demoapp-deployment" successfully rolled out

查看ReplicaSet 

# kubectl get rs -n demoapp
NAME                           DESIRED   CURRENT   READY   AGE
demoapp-deployment-96994cd5f   3         3         3       108s

查看pod 标签

# kubectl get pods -n demoapp --show-labels
NAME                                 READY   STATUS    RESTARTS   AGE     LABELS
demoapp-deployment-96994cd5f-gwfh4   1/1     Running   0          2m10s   app=demoapp,controller=demoapp,pod-template-hash=96994cd5f
demoapp-deployment-96994cd5f-mmhsb   1/1     Running   0          2m10s   app=demoapp,controller=demoapp,pod-template-hash=96994cd5f
demoapp-deployment-96994cd5f-qrjw8   1/1     Running   0          2m10s   app=demoapp,controller=demoapp,pod-template-hash=96994cd5f

查看Deployment 修订历史

# kubectl rollout history deployment/demoap-depolyment -n demoapp
deployment.apps/demoappv10 
REVISION  CHANGE-CAUSE
1         <none>

Deployment 添加注解

# kubectl annotate deployment/demoapp-deployment kubernetes.io/change-cause="kubectl apply -f depoly-demoapp-v10.yaml" -n demoapp
deployment.apps/demoapp-deployment annotated

确认Deployment 添加注解

# kubectl rollout history deployment/demoapp-deployment -n demoapp
deployment.apps/demoapp-deployment 
REVISION  CHANGE-CAUSE
1         kubectl apply -f depoly-demoapp-v10.yaml

查看svc

# kubectl get svc -n demoapp
NAME          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
demoapp-svc   ClusterIP   10.100.95.59   <none>        80/TCP    5m16s

访问服务

# curl `kubectl get svc/demoapp-svc -n demoapp -o jsonpath="{.spec.clusterIP}"`:80
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-mmhsb, ServerIP: 172.20.154.229!

金丝雀发布 方法一

设置参数

# kubectl patch deployments/demoapp-deployment -p '{"spec": {"strategy":{"rollingUpdate":{"maxSurge": 1, "maxUnavailable":0}}}}' -n demoapp
deployment.apps/demoapp-deployment patched

更新应用并暂停

# kubectl set image deployment/demoapp-deployment demoapp=ikubernetes/demoapp:v1.1 -n demoapp && kubectl rollout pause deployments/demoapp-deployment -n demoapp
deployment.apps/demoapp-deployment image updated
deployment.apps/demoapp-deployment paused

查看更新状态

# kubectl rollout status deploy demoapp-deployment -n demoapp
Waiting for deployment "demoapp-deployment" rollout to finish: 1 out of 3 new replicas have been updated...

查看rs控制器

# kubectl get rs -n demoapp
NAME                            DESIRED   CURRENT   READY   AGE
demoapp-deployment-6f798c7578   1         1         1       112s
demoapp-deployment-96994cd5f    3         3         3       35m

查看pod

# kubectl get pod -n demoapp
NAME                                  READY   STATUS    RESTARTS   AGE
demoapp-deployment-6f798c7578-6vk69   1/1     Running   0          6m13s
demoapp-deployment-96994cd5f-gwfh4    1/1     Running   0          39m
demoapp-deployment-96994cd5f-mmhsb    1/1     Running   0          39m
demoapp-deployment-96994cd5f-qrjw8    1/1     Running   0          39m

查看deployment日志

# kubectl describe deployments -n demoapp
Name:                   demoapp-deployment
Namespace:              demoapp
CreationTimestamp:      Mon, 19 Jun 2023 16:19:48 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 2
                        kubernetes.io/change-cause: kubectl apply -f depoly-demoapp-v10.yaml
Selector:               app=demoapp,controller=demoapp
Replicas:               3 desired | 1 updated | 4 total | 4 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  0 max unavailable, 1 max surge
Pod Template:
  Labels:  app=demoapp
           controller=demoapp
  Containers:
   demoapp:
    Image:      ikubernetes/demoapp:v1.1
    Port:       80/TCP
    Host Port:  0/TCP
    Limits:
      cpu:     50m
      memory:  256Mi
    Requests:
      cpu:        50m
      memory:     256Mi
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status   Reason
  ----           ------   ------
  Available      True     MinimumReplicasAvailable
  Progressing    Unknown  DeploymentPaused
OldReplicaSets:  demoapp-deployment-96994cd5f (3/3 replicas created)
NewReplicaSet:   demoapp-deployment-6f798c7578 (1/1 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  36m    deployment-controller  Scaled up replica set demoapp-deployment-96994cd5f to 3
  Normal  ScalingReplicaSet  2m37s  deployment-controller  Scaled up replica set demoapp-deployment-6f798c7578 to 1

访问服务

# while sleep 1;do curl `kubectl get svc/demoapp-svc -n demoapp -o jsonpath="{.spec.clusterIP}"`:80;done
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-qrjw8, ServerIP: 172.20.89.158!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-gwfh4, ServerIP: 172.20.44.194!
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-6f798c7578-6vk69, ServerIP: 172.20.154.220!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-mmhsb, ServerIP: 172.20.154.229!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-qrjw8, ServerIP: 172.20.89.158!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-gwfh4, ServerIP: 172.20.44.194!
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-6f798c7578-6vk69, ServerIP: 172.20.154.220!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-mmhsb, ServerIP: 172.20.154.229!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-qrjw8, ServerIP: 172.20.89.158!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-gwfh4, ServerIP: 172.20.44.194!
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-6f798c7578-6vk69, ServerIP: 172.20.154.220!
相关的Pod列表也能够显示出旧版本ReplicaSet的所有Pod副本仍在正常运行,而同时新版本ReplicaSet对象也有了一个Pod实例,相关Service对象能够在其就绪后将一定比例的客户端流量引入到该Pod之上。运行足够长的一段时间后,若确认新版本应用没有必须通过回滚才能解决的问题,随后即可使用kubectl rollout resume命令继续后续更新步骤,以完成滚动更新过程。
你不可以回滚处于暂停状态的 Deployment,除非先恢复其执行状态。

恢复deployment更新

# kubectl rollout resume deployments/demoapp-deployment -n demoapp
deployment.apps/demoapp-deployment resumed

查看 Deployment 滚动更新状态

# kubectl rollout status deploy/demoapp-deployment -n demoapp
deployment "demoapp-deployment" successfully rolled out

查看ReplicaSet 

# kubectl get rs -n demoapp
NAME                            DESIRED   CURRENT   READY   AGE
demoapp-deployment-6f798c7578   3         3         3       14m
demoapp-deployment-96994cd5f    0         0         0       48m

查看pod

# kubectl get pod -n demoapp
NAME                                  READY   STATUS    RESTARTS   AGE
demoapp-deployment-6f798c7578-6vk69   1/1     Running   0          15m
demoapp-deployment-6f798c7578-hfxjz   1/1     Running   0          2m51s
demoapp-deployment-6f798c7578-v2gb8   1/1     Running   0          2m48s

访问服务

# while sleep 1;do curl `kubectl get svc/demoapp-svc -n demoapp -o jsonpath="{.spec.clusterIP}"`:80;done
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-6f798c7578-hfxjz, ServerIP: 172.20.89.156!
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-6f798c7578-6vk69, ServerIP: 172.20.154.220!
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-6f798c7578-v2gb8, ServerIP: 172.20.44.219!
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-6f798c7578-hfxjz, ServerIP: 172.20.89.156!
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-6f798c7578-6vk69, ServerIP: 172.20.154.220!
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-6f798c7578-v2gb8, ServerIP: 172.20.44.219!
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-6f798c7578-hfxjz, ServerIP: 172.20.89.156!

更新Deployment 修订历史

# kubectl annotate deployment/demoapp-deployment kubernetes.io/change-cause="kubectl set image deployment/demoapp-deployment demoapp=ikubernetes/demoapp:v1.1 -n demoapp" -n demoapp
deployment.apps/demoapp-deployment annotated

查看 Deployment 修订历史

# kubectl rollout history deploy/demoapp-deployment -n demoapp
deployment.apps/demoapp-deployment 
REVISION  CHANGE-CAUSE
1         kubectl apply -f depoly-demoapp-v10.yaml
2         kubectl set image deployment/demoapp-deployment demoapp=ikubernetes/demoapp:v1.1 -n demoapp

版本回滚

回滚到上一个版本

# kubectl rollout undo deployment/demoapp-deployment -n demoapp
deployment.apps/demoapp-deployment rolled back

访问服务

# while sleep 1;do curl `kubectl get svc/demoapp-svc -n demoapp -o jsonpath="{.spec.clusterIP}"`:80;done
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-ndntl, ServerIP: 172.20.89.162!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-grvn7, ServerIP: 172.20.44.220!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-tjn85, ServerIP: 172.20.154.217!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-ndntl, ServerIP: 172.20.89.162!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-grvn7, ServerIP: 172.20.44.220!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-tjn85, ServerIP: 172.20.154.217!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-ndntl, ServerIP: 172.20.89.162!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-grvn7, ServerIP: 172.20.44.220!
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoapp-deployment-96994cd5f-tjn85, ServerIP: 172.20.154.217!

金丝雀发布 方法二

depoly-demoapp-v11.yaml

apiVersion: v1
kind: Namespace
metadata:
    name: demoapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    version: v1.1
  name: demoapp-deployment
  namespace: demoapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: demoapp
      controller: demoapp
      version: v1.1
  template:
    metadata:
      labels:
        app: demoapp
        controller: demoapp
        version: v1.1
    spec:
      containers:
      - name: demoapp
        image: ikubernetes/demoapp:v1.0
        ports:
        - containerPort: 80
          name: http
        resources:
          requests:
            memory: "256Mi"
            cpu: "50m"
          limits:
            memory: "256Mi"
            cpu: "50m"

创建demo v11资源

# kubectl apply -f depoly-demoapp-v11.yaml

访问demo 资源

此时demo服务 v10和v11版本共存

更新demo-svc.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: demoapp-svc
  namespace: demoapp
spec:
  selector:
    app: demoapp
    controller: demoapp
    version: v1.1
  ports:
  - name: http
    port: 80
    targetPort: 80

更新demo-svc资源

# kubectl apply -f demo-svc.yaml

访问demo服务

此时demo-svc只能匹配到v11版本

清理旧版本

# kubectl delete -f depoly-demoapp-v10.yaml

参考文档

https://kubernetes.io/zh-cn/docs/concepts/cluster-administration/manage-deployment/#canary-deployments

posted @ 2023-06-19 17:18  小吉猫  阅读(106)  评论(0编辑  收藏  举报