跳到主要内容

发布策略(扩展)(仅收藏)

发布策略

image-20230921083321559

目录

[toc]

滚动更新

apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1 ## 最多1个pod处于不可工作状态
maxSurge: 2 ## 升级时可以比预期多出2个pod
minReadySeconds: 5 ## 容器启动后等待5秒

蓝绿发布

创建新的部署,然后修改service的标签来将流量指向新的部署。

[root@master b-g]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginxapp-1.19-bdb8dc6c4-2jfsw 1/1 Running 0 12m
nginxapp-1.19-bdb8dc6c4-79qrh 1/1 Running 0 12m
nginxapp-1.19-bdb8dc6c4-hbwkx 1/1 Running 0 12m
nginxapp-1.20-786464458-4zh5t 1/1 Running 0 91s
nginxapp-1.20-786464458-5bfwq 1/1 Running 0 91s
nginxapp-1.20-786464458-fwgxb 1/1 Running 0 91s

v1.1.4-blue.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
labels:
k8s-app: devops4-ops-service
version: "1.1.4"
name: devops4-ops-service-blue
namespace: devops4
spec:
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: devops4-ops-service
version: "1.1.4"
template:
metadata:
labels:
k8s-app: devops4-ops-service
version: "1.1.4"
namespace: devops4
name: devops4-ops-service
spec:
containers:
- name: devops4-ops-service
image: 192.168.1.200:8088/devops4/devops4-ops-service:RELEASE-1.1.4-c6b176ad
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: web
protocol: TCP
serviceAccountName: devops4-ops-service
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: devops4-ops-service
name: devops4-ops-service

service.yaml

apiVersion: v1
kind: Namespace
metadata:
name: devops4
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: devops4
spec:
rules:
- host: devops4.ops.service
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: devops4-ops-service
port:
number: 8080
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: devops4-ops-service
name: devops4-ops-service
namespace: devops4
spec:
type: ClusterIP
ports:
- name: web
port: 8080
targetPort: 8080
selector:
k8s-app: devops4-ops-service
version: "1.1.4"

v1.1.5-green.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
labels:
k8s-app: devops4-ops-service
version: "1.1.5"
name: devops4-ops-service-green
namespace: devops4
spec:
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: devops4-ops-service
version: "1.1.5"
template:
metadata:
labels:
k8s-app: devops4-ops-service
version: "1.1.5"
namespace: devops4
name: devops4-ops-service
spec:
containers:
- name: devops4-ops-service
image: 192.168.1.200:8088/devops4/devops4-ops-service:RELEASE-1.1.5-bc630338
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: web
protocol: TCP
serviceAccountName: devops4-ops-service
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: devops4-ops-service
name: devops4-ops-service

启动busybox容器调试(废弃)

kubectl run --restart=Never -it --image \
infoblox/dnstools dnstools

修改流量(service)

kubectl patch svc devops4-ops-service -p '{"spec":{"selector":{"version": "1.1.5"}}}' -n devops4

测试验证

curl http://devops4.ops.service/hello

灰度发布

https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#canary

1.1.4

apiVersion: v1
kind: Namespace
metadata:
name: devops4
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress-1-4
namespace: devops4
spec:
rules:
- host: devops4.ops.service
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: devops4-ops-service-1-4
port:
number: 8080
---
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
k8s-app: devops4-ops-service
name: devops4-ops-service-1-4
namespace: devops4
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: devops4-ops-service
template:
metadata:
labels:
k8s-app: devops4-ops-service
namespace: devops4
name: devops4-ops-service
spec:
containers:
- name: devops4-ops-service
image: 192.168.1.200:8088/devops4/devops4-ops-service:RELEASE-1.1.4-c6b176ad
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: web
protocol: TCP
serviceAccountName: devops4-ops-service
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: devops4-ops-service
name: devops4-ops-service
namespace: devops4
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: devops4-ops-service
name: devops4-ops-service-1-4
namespace: devops4
spec:
type: ClusterIP
ports:
- name: web
port: 8080
targetPort: 8080
selector:
k8s-app: devops4-ops-service

1.1.5

apiVersion: v1
kind: Namespace
metadata:
name: devops5
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: devops5
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-by-cookie: "from"
spec:
rules:
- host: devops4.ops.service
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: devops4-ops-service-1-5
port:
number: 8080
---
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
k8s-app: devops4-ops-service
name: devops4-ops-service
namespace: devops5
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: devops4-ops-service
template:
metadata:
labels:
k8s-app: devops4-ops-service
namespace: devops5
name: devops4-ops-service
spec:
containers:
- name: devops4-ops-service
image: 192.168.1.200:8088/devops4/devops4-ops-service:RELEASE-1.1.5-bc630338
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
name: web
protocol: TCP
serviceAccountName: devops4-ops-service
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: devops4-ops-service
name: devops4-ops-service
namespace: devops5
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: devops4-ops-service
name: devops4-ops-service-1-5
namespace: devops5
spec:
type: ClusterIP
ports:
- name: web
port: 8080
targetPort: 8080
selector:
k8s-app: devops4-ops-service

创建资源:

[root@zeyang-nuc-service canary]# kubectl get all -n devops4
NAME READY STATUS RESTARTS AGE
pod/devops4-ops-service-1-4-7f97cb665c-vcntl 1/1 Running 0 16m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/devops4-ops-service-1-4 ClusterIP 10.96.65.65 <none> 8080/TCP 16m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/devops4-ops-service-1-4 1/1 1 1 16m

NAME DESIRED CURRENT READY AGE
replicaset.apps/devops4-ops-service-1-4-7f97cb665c 1 1 1 16m


[root@zeyang-nuc-service canary]# kubectl get all -n devops5
NAME READY STATUS RESTARTS AGE
pod/devops4-ops-service-5fbfb57777-6hc62 1/1 Running 0 15m

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/devops4-ops-service-1-5 ClusterIP 10.96.64.251 <none> 8080/TCP 15m

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/devops4-ops-service 1/1 1 1 15m

NAME DESIRED CURRENT READY AGE
replicaset.apps/devops4-ops-service-5fbfb57777 1 1 1 15m

测试:

## 编辑hosts文件
192.168.10.100 devops4.ops.service

for i in {1..10};do curl devops4.ops.service/hello ;done

基于权重的发布

<1.19.0

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: example-ingress
namespace: devops4
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
rules:
- host: devops4.ops.service
http:
paths:
- path: /
backend:
serviceName: devops4-ops-service
servicePort: 8080

>1.19.0

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: devops4
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
rules:
- host: devops4.ops.service
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: devops4-ops-service
port:
number: 8080

Header报头流量分发

<1.19.0

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: example-ingress
namespace: devops4
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-by-header: "Region"
nginx.ingress.kubernetes.io/canary-by-header-value: "bj"
spec:
rules:
- host: devops4.ops.service
http:
paths:
- path: /
backend:
serviceName: devops4-ops-service
servicePort: 8080

>1.19.0

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: devops4
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-by-header: "Region"
nginx.ingress.kubernetes.io/canary-by-header-value: "bj"
spec:
rules:
- host: devops4.ops.service
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: devops4-ops-service
port:
number: 8080
for i in {1..10};do curl -H "region: bj" devops4.ops.service/hello;done

根据cookie流量分发

<1.19.0

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: example-ingress
namespace: devops4
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-by-cookie: "from"
spec:
rules:
- host: devops4.ops.service
http:
paths:
- path: /
backend:
serviceName: devops4-ops-service
servicePort: 8080

>1.19.0

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: devops4
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-by-cookie: "from"
spec:
rules:
- host: devops4.ops.service
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: devops4-ops-service
port:
number: 8080
[root@master ingress-gray]# curl devops4.ops.service/hello --cookie "from=always"
1.1.5
[root@master ingress-gray]# curl devops4.ops.service/hello --cookie "from=a"
1.1.4
[root@master ingress-gray]# curl devops4.ops.service/hello --cookie "from=a"
1.1.4

关于我

我的博客主旨:

  • 排版美观,语言精炼;
  • 文档即手册,步骤明细,拒绝埋坑,提供源码;
  • 本人实战文档都是亲测成功的,各位小伙伴在实际操作过程中如有什么疑问,可随时联系本人帮您解决问题,让我们一起进步!

🍀 微信二维码 x2675263825 (舍得), qq:2675263825。

image-20230107215114763

🍀 微信公众号 《云原生架构师实战》

image-20230107215126971

🍀 个人博客站点

http://47.97.48.237/ (即将上线域名:onedayxyy.cn)

image-20230917111843405

🍀 语雀

https://www.yuque.com/xyy-onlyone

image-20230912072007284

🍀 csdn https://blog.csdn.net/weixin_39246554?spm=1010.2135.3001.5421

image-20230107215149885

🍀 知乎 https://www.zhihu.com/people/foryouone

image-20230107215203185

最后

好了,关于本次就到这里了,感谢大家阅读,最后祝大家生活快乐,每天都过的有意义哦,我们下期见!