wiki/运维/容器/Kubernetes/相关实验/2.金丝雀发布实验.md
2025-01-02 10:46:09 +08:00

133 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

金丝雀发布又称为灰度发布。它能够缓慢的将修改推广到一小部分用户,验证没有问题后,再推广到全部用户,以降低生产环境引入新功能带来的风险。
主要实现思想是SVC 通过标签 将生产流量逐步转发到新版本的POD中不断扩容新版本Deployment的副本数缩减旧版本的副本数
## 工作负载概览
| **工作负载** | **名称** | **标签** | **标签选择器** | **作用** |
| --- | --- | --- | --- | --- |
| service | web-service | 无 | app: nginx
env: prod | 负载均衡通过筛选pod 的标签进行流量转发 |
| deployment | web-nginx-v1(旧版本使用nginx1.14镜像)
web-nginx-v2(新版本使用nginx1.20镜像) | 随意为方便管理这里我们与下面的pod 的标签保持一致 | app: nginx
env: prod
releasev1
releasev2 | 扩展管理、管理pod等 |
| pod | web-nginx | app: nginx
env: prod
releasev1
releasev2 | 无 | 管理容器,最小调度单位 |
通过nginx 的版本进行验证,举个例子 svc 的Cluster IP为 1.1.1.1那我们就通过curl 1.1.1.1/net 查看404 显示中的nginx版本即可快速区分
## YAML
以下为详细yaml配置文件
### SVC
```yaml
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: nginx
env: prod
ports:
- protocol: TCP
port: 80
targetPort: 80
```
### 旧Deployment
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-nginx-v1
labels:
app: nginx
env: prod
release: v1
spec:
replicas: 10
selector:
matchLabels:
app: nginx
env: prod
release: v1
template:
metadata:
labels:
app: nginx
env: prod
release: v1
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
```
### 新Deplyment
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-nginx-v2
labels:
app: nginx
env: prod
release: v2
spec:
replicas: 1
selector:
matchLabels:
app: nginx
env: prod
release: v2
template:
metadata:
labels:
app: nginx
env: prod
release: v2
spec:
containers:
- name: nginx
image: nginx:1.20.1
ports:
- containerPort: 80
```
## 具体操作
运行svc 和deployment
```yaml
kubectl apply -f canary-dp-old.yaml -f canary-svc.yaml
```
查看SVC的CLUSTER-IP
```yaml
kubectl get svc
```
查看运行状态并测试服务可用性
```yaml
kubectl get pod
curl SVC的CLUSTER-IP/net
```
开始金丝雀部署
```yaml
kubectl apply -f canary-dp-new.yaml
```
查看运行状态,并测试金丝雀的服务情况
```yaml
kubectl get pod
curl SVC的CLUSTER-IP/net
```
逐步扩容金丝雀的副本数量减少v1版本的副本数量
```yaml
kubectl scale deployment web-nginx-v2 --replicas 数量(建议逐步加大)
kubectl scale deployment web-nginx-v1 --replicas 数量(建议逐步减小)
```