灰度發布
灰度發布也叫金絲雀發布,是指一種平滑過渡的發布方式,可以實作兩個版本的共存,服務的新版本先在小范圍內上線,可以避免很多風險,灰度發布可以保證整體系統的穩定,在初始灰度的時候就可以發現、調整問題,以保證其影響度,
基于Service實作的灰度發布的原理
Service是基于標簽去選擇負載均衡的Pod的范圍,Deployment也是基于標簽選擇控制的Pod的范圍,兩個版本的Deployment部署的時候可以都使用兩個標簽,一個標簽用來保證控制的Pod都可以被Service選中,另外一個標簽用來標識版本號,然后可以調整這兩個版本的replicas來實作兩個版本在整個對外服務中占有的權重,
基于Service實作的灰度發布功能比較單一,如果使用Ingress的話可以提供更多的功能,具體可以參考我的這篇文章:
https://blog.csdn.net/weixin_46040684/article/details/122143612
具體實作
創建一個service
這個service選中標簽為grayscale的Pod進行負載均衡,在Pod上面開放的埠為80,
[root@master data]# cat svc.yaml
apiVersion: v1
kind: Service
metadata:
name: grayscale-svc
namespace: test
spec:
selector:
app: grayscale
type: NodePort
ports:
- name: grayscale-test
port: 80
targetPort: 80
protocol: TCP
nodePort: 31111
[root@master 1225]# kubectl apply -f svc.yaml
service/grayscale-svc created
[root@master 1225]# kubectl get svc -n test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
grayscale-svc NodePort 10.96.102.213 <none> 80:31111/TCP 2m31s
創建v1版本Deployment
這個Deployment使用的是nginx鏡像,創建了兩個副本,標簽app=grayscale保證可以被上面創建的Service負載均衡,
[root@master 1225]# cat deploy-v1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: grayscale-deploy-v1
namespace: test
spec:
selector:
matchLabels:
app: grayscale
version: v1
replicas: 2
template:
metadata:
labels:
app: grayscale
version: v1
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
[root@master 1225]# kubectl apply -f deploy-v1.yaml
deployment.apps/grayscale-deploy-v1 created
創建v2版本的Deployment
這個Deployment使用了一個初始化容器,在初始化容器中通過掛載volumes修改了nginx的默認頁,以便分辨和v1版本的區別,
[root@master 1225]# cat deploy-v2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: grayscale-deploy-v2
namespace: test
spec:
selector:
matchLabels:
app: grayscale
version: v2
replicas: 1
template:
metadata:
labels:
app: grayscale
version: v2
spec:
volumes:
- name: for-input
emptyDir: {}
initContainers:
- name: init
image: alpine
volumeMounts:
- name: for-input
mountPath: /test
command: ["/bin/sh","-c","echo this is version 2 > /test/index.html;"]
containers:
- name: nginx
image: nginx
volumeMounts:
- name: for-input
mountPath: /usr/share/nginx/html
ports:
- containerPort: 80
[root@master 1225]# kubectl apply -f deploy-v2.yaml
deployment.apps/grayscale-deploy-v2 created
實作效果
先查看一下目前Pod的狀態,都已經就緒
[root@k8s-master 1225]# kubectl get all -n test
NAME READY STATUS RESTARTS AGE
pod/grayscale-deploy-v1-649fb966f9-7mk2x 1/1 Running 0 4m55s
pod/grayscale-deploy-v1-649fb966f9-vm5qg 1/1 Running 0 4m55s
pod/grayscale-deploy-v2-7bc797d8d9-lbhkc 1/1 Running 0 2m31s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/grayscale-svc NodePort 10.96.102.213 <none> 80:31111/TCP 11m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/grayscale-deploy-v1 2/2 2 2 4m55s
deployment.apps/grayscale-deploy-v2 1/1 1 1 2m31s
通過集群ip訪問發現回應有v1版本的nginx的默認頁,也有v2版本修改后的內容,
[root@master 1225]# curl 10.96.102.213
this is version 2
[root@master 1225]# curl 10.96.102.213
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/393084.html
標籤:其他
上一篇:簡單聊聊 OSPF
