[kubernetes]9-4 部署策略详解 --- 重建、滚动、蓝绿、金丝雀

不羁岁月 提交于 2020-01-29 05:08:20

 

9-4 部署策略详解 --- 重建、滚动、蓝绿、金丝雀

部署策略实践

这两个是kubernetes 层面支持的策略

Rolling update

Recreate 需要先停止旧服务

这两个是需要通过service的一些特征(labelselector)结合deployment一起完成

蓝绿部署

金丝雀

 

 

1 测试Recreate

创建web-recreate.yaml 

#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-recreate
  namespace: dev
spec:
# 策略为Recreate
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: web-recreate
  replicas: 2
  template:
    metadata:
      labels:
        app: web-recreate
    spec:
      containers:
      - name: web-recreate
        image: harbor.pdabc.com/kubernetes/web:v3
        ports:
        - containerPort: 8080
        livenessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 2
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /examples/index.html
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 5
          failureThreshold: 1
          successThreshold: 1
          timeoutSeconds: 5
---
#service
apiVersion: v1
kind: Service
metadata:
  name: web-recreate
  namespace: dev
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: web-recreate
  type: ClusterIP


---
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web-recreate
  namespace: dev
spec:
  rules:
# 需要在host中先配置好域名
  - host: web-recreate.pdabc.com
    http:
      paths:
      - path: /
        backend:
          serviceName: web-recreate
          servicePort: 80

 

创建pod

启动后访问

http://web-recreate.pdabc.com/hello?name=jiaminxu

 

修改web-recreate.yaml 的label字段

  template:
    metadata:
      labels:
        app: web-recreate
        type: webappp

 

再apply一下

发现服务会先处于 Terminating 并且服务不可用,等到两个pod都停止之后才会新起两个pod,期间服务均不可用。这种场景在资源不足,或者服务需要在不同的node的情况下可以使用。

 

 

2 测试Rollingupdate

先删除刚刚创建的pod

kubectl  delete -f web-recreate.yaml

创建web-rollingupdate.yaml 

#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-rollingupdate
  namespace: dev
spec:
  strategy:
    rollingUpdate:
# 如果pod为4个,最多多启动1个。也可以设置为数值
      maxSurge: 25%
# 如果pod为4个,最多去掉1个,保证3个可用。也可以设置为数值
      maxUnavailable: 25%
# 表示类型为Rolling update
    type: RollingUpdate
  selector:
    matchLabels:
      app: web-rollingupdate
  replicas: 2
  template:
    metadata:
      labels:
        app: web-rollingupdate
    spec:
      containers:
      - name: web-rollingupdate
        image: harbor.pdabc.com/kubernetes/web:v3
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: 1024Mi
            cpu: 500m
          limits:
            memory: 2048Mi
            cpu: 2000m
        livenessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 3
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /hello?name=test
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 1
          successThreshold: 1
          timeoutSeconds: 5
---
#service
apiVersion: v1
kind: Service
metadata:
  name: web-rollingupdate
  namespace: dev
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: web-rollingupdate
  type: ClusterIP


---
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web-rollingupdate
  namespace: dev
spec:
  rules:
  - host: web-rollingupdate.pdabc.com
    http:
      paths:
      - path: /
        backend:
          serviceName: web-rollingupdate
          servicePort: 80

 

kubectl  apply -f web-rollingupdate.yaml 

通过健康检查之后显示为running状态

 

配制好本机hosts之后 访问http://web-rollingupdate.pdabc.com/hello?name=jiaminxu

 

修改web-rollingupdate.yaml 修改镜像为harbor.pdabc.com/kubernetes/springboot-web:v1

模拟镜像升级的情况

kubectl  apply -f web-rollingupdate.yaml 

在 /etc/hosts添加192.168.10.150 web-rollingupdate.pdabc.com

并通过脚本去检测 服务是否出现不可用

while  sleep 0.2;do curl "http://web-rollingupdate.pdabc.com/hello?name=jiaminxu";echo "";done (这里没做好 最好是新pod起来的时候 输出的内容有变化 这样比较直观)

 

 

尝试滚动部署的另一种方法

将镜像改回image: harbor.pdabc.com/kubernetes/web:v3 并apply

在pod启动一个 的时候执行

kubectl rollout pause deploy web-rollingupdate  -n dev

使滚动更新停止 再去访问服务 会有负载均衡

 

恢复使用

kubectl rollout resume deploy web-rollingupdate -n dev

 

回滚版本测试

kubectl rollout undo deploy web-rollingupdate -n dev 

 

 

正在回滚会spring版本

 

全部回滚完成

 

蓝绿部署

通过selector 切换新旧的deployment 将流量切换过来

 

删除上面测试的pod

kubectl  delete -f web-rollingupdate.yaml

 

创建 web-bluegreen.yaml 

#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-bluegreen
  namespace: dev
spec:
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  selector:
    matchLabels:
      app: web-bluegreen
  replicas: 2
  template:
    metadata:
      labels:
        app: web-bluegreen
# 用于
        version: v1.0
    spec:
      containers:
      - name: web-bluegreen
        image: harbor.pdabc.com/kubernetes/web:v3
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: 1024Mi
            cpu: 500m
          limits:
            memory: 2048Mi
            cpu: 2000m
        livenessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 3
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /hello?name=test
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 1
          successThreshold: 1
          timeoutSeconds: 5

 

kubectl  apply -f web-bluegreen.yaml

 

 

启动完成之后为其创建一个service

添加host web-bluegreen.pdabc.com

 

创建bluegreen-service.yaml 

---
#service
apiVersion: v1
kind: Service
metadata:
  name: web-bluegreen
  namespace: dev
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: web-bluegreen
    #app: web-blue
# 表示需要选择app为 web-bluegreen 且version为1.0的pod
    version: v1.0
  type: ClusterIP


---
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web-bluegreen
  namespace: dev
spec:
  rules:
  - host: web-bluegreen.pdabc.com
    http:
      paths:
      - path: /
        backend:
          serviceName: web-bluegreen
          servicePort: 80

通过前面相同的脚本去检测

 while  sleep 0.2;do curl "http://web-bluegreen.pdabc.com/hello?name=jiaminxu";echo "";done 

 

并且创建service

 

 

测试升级 修改web-bluegreen.yaml 配置文件

#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-bluegreen-v2
  namespace: dev
spec:
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  selector:
    matchLabels:
      app: web-bluegreen
  replicas: 2
  template:
    metadata:
      labels:
        app: web-bluegreen
        version: v2.0
    spec:
      containers:
      - name: web-bluegreen
        image: harbor.pdabc.com/kubernetes/springboot-web:v1
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: 1024Mi
            cpu: 500m
          limits:
            memory: 2048Mi
            cpu: 2000m
        livenessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 3
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /hello?name=test
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 1
          successThreshold: 1
          timeoutSeconds: 5


 

应用并启动

kubectl apply -f web-bluegreen.yaml

 

此时启动了2个deploy 每个deploy 2个pod 还没有切流量 访问还没变

 

修改bluegreen-service.yaml 将版本改为2.0

 

 kubectl apply -f bluegreen-service.yaml

 

没有交替的过程 直接全部切换成新版本

 

kubectl get pods -n dev 

所有的版本都在

蓝绿方便切换版本.可以保留旧版本一段时间,在删除.或者再次发布 才会去掉.

第一次发布deployment的name为bule;第二次发布deployment的name修改为green,第三次发布deployment的name再修改为bule.保持线上有2个版本的deployment

 

 

金丝雀发布

蓝绿发布的基础上 简单修改selector 就是金丝雀发布了

 

修改bluegreen-service.yaml 

---
#service
apiVersion: v1
kind: Service
metadata:
  name: web-bluegreen
  namespace: dev
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
# selector 中删除了version
  selector:
    app: web-bluegreen
  type: ClusterIP


---
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web-bluegreen
  namespace: dev
spec:
  rules:
  - host: web-bluegreen.pdabc.com
    http:
      paths:
      - path: /
        backend:
          serviceName: web-bluegreen
          servicePort: 80

 

kubectl apply -f bluegreen-service.yaml

版本在进行交替 是4个pod负载.线上环境可以v1版本10个pod 测试的功能1个pod 占10%流量

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!