112 lines
2.6 KiB
Markdown
112 lines
2.6 KiB
Markdown
> 本文作者:丁辉
|
|
|
|
# 通过Ingress实现金丝雀发布
|
|
|
|
**通过 Ingress 实现基于权重的金丝雀**
|
|
|
|
部署测试示例 Nginx Deployment v1和v2
|
|
|
|
1. 部署 v1 版本
|
|
|
|
```bash
|
|
kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment-v1.yaml
|
|
```
|
|
|
|
2. 部署 v2 版本
|
|
|
|
```bash
|
|
kubectl apply -f https://gitee.com/offends/Kubernetes/raw/main/File/Yaml/nginx-deployment-v2.yaml
|
|
```
|
|
|
|
3. 创建两个 Service
|
|
|
|
```yaml
|
|
cat <<EOF | kubectl apply -f -
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: nginx-deployment-service-1
|
|
namespace: default
|
|
spec:
|
|
selector:
|
|
app: nginx
|
|
version: v1
|
|
ports:
|
|
- port: 80
|
|
targetPort: 80
|
|
---
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: nginx-deployment-service-2
|
|
namespace: default
|
|
spec:
|
|
selector:
|
|
app: nginx
|
|
version: v2
|
|
ports:
|
|
- port: 80
|
|
targetPort: 80
|
|
EOF
|
|
```
|
|
|
|
4. 创建 Ingress 资源
|
|
|
|
```yaml
|
|
cat <<EOF | kubectl apply -f -
|
|
# 金丝雀 Ingress 对象为 v1 版本
|
|
apiVersion: networking.k8s.io/v1
|
|
kind: Ingress
|
|
metadata:
|
|
name: nginx-ingress-1
|
|
namespace: default
|
|
spec:
|
|
ingressClassName: nginx # 通过 kubectl get ingressclass 查看
|
|
rules:
|
|
- host: example.com # 修改为自己的对外域名
|
|
http:
|
|
paths:
|
|
- backend:
|
|
service:
|
|
name: nginx-deployment-service-1 # Nginx Serice 名称
|
|
port:
|
|
number: 80 # Nginx 端口
|
|
path: /
|
|
pathType: Prefix
|
|
tls:
|
|
- hosts:
|
|
- example.com # 替换为你的域名
|
|
secretName: example-tls # 替换为你的证书
|
|
---
|
|
# 金丝雀 Ingress 对象为 v2 版本
|
|
apiVersion: networking.k8s.io/v1
|
|
kind: Ingress
|
|
metadata:
|
|
name: nginx-ingress-2
|
|
namespace: default
|
|
annotations:
|
|
nginx.ingress.kubernetes.io/canary: "true"
|
|
nginx.ingress.kubernetes.io/canary-weight: "10" # 10%流量到金丝雀
|
|
spec:
|
|
ingressClassName: nginx # 通过 kubectl get ingressclass 查看
|
|
rules:
|
|
- host: example.com # 修改为自己的对外域名
|
|
http:
|
|
paths:
|
|
- backend:
|
|
service:
|
|
name: nginx-deployment-service-2 # Nginx Serice 名称
|
|
port:
|
|
number: 80 # Nginx 端口
|
|
path: /
|
|
pathType: Prefix
|
|
tls:
|
|
- hosts:
|
|
- example.com # 替换为你的域名
|
|
secretName: example-tls # 替换为你的证书
|
|
EOF
|
|
```
|
|
|
|
5. 慢慢调整 Nginx-Ingress-2 流量到 100%,即可完成更新
|
|
|