Kubernetes 是一个开源容器编排系统,可简化软件部署、扩展和管理。它最初由 Google 设计,现在由云原生计算基金会监管。
蓝绿部署概述:
蓝绿部署是一种部署模式,通过运行两个相同的生产环境(蓝色和绿色)来最大限度地减少停机时间。一次只有一个环境运行,确保更改既向前又向后兼容。该方法包括设置并行基础设施、将新版本部署到新环境、验证,然后将所有流量切换到绿色环境。确认后,旧的基础设施(蓝色)可以移除或停止。
先决条件:
- Kubernetes集群
- kubectl 命令行工具
- 待部署应用程序的 Docker 镜像
分步指南:
第1步:创建命名空间
代码语言:javascript复制kubectl create namespace blue-green-deployment
第2步:创建部署
创建蓝色部署清单 (blue-deployment.yaml):
代码语言:javascript复制# blue-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapp-blue
labels:
app: demoapp
env: blue
spec:
replicas: 3
selector:
matchLabels:
app: demoapp
env: blue
template:
metadata:
labels:
app: demoapp
env: blue
spec:
containers:
- name: demo
image: demoapp:v1.0
ports:
- containerPort: 80
应用部署:
代码语言:javascript复制kubectl apply -f blue-deployment.yaml -n blue-green-deployment
创建类似的绿色部署清单 (green-deployment.yaml) 并应用它。
第3步:创建服务
创建服务清单(service.yaml):selector: app: demoapp
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer
申请服务:
代码语言:javascript复制kubectl apply -f service.yaml -n blue-green-deployment
获取服务的外部IP地址:
代码语言:javascript复制kubectl get service demoapp-service -n blue-green-deployment
第4步:路由规则
在服务清单 (service.yaml) 中定义路由规则,将流量路由到蓝色或绿色部署:
代码语言:javascript复制# service.yaml
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer
sessionAffinity: ClientIP
externalTrafficPolicy: Local
loadBalancerSourceRanges:
- 0.0.0.0/0
应用更新后的服务:
代码语言:javascript复制kubectl apply -f service.yaml -n blue-green-deployment
第5步:基于主机和路径的路由
扩展服务清单 (service.yaml) 以包含基于主机和路径的路由规则:
代码语言:javascript复制# service.yaml
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer
sessionAffinity: ClientIP
externalTrafficPolicy: Local
loadBalancerSourceRanges:
- 0.0.0.0/0
externalIPs:
- <external_ip_address>
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demoapp-ingress
spec:
rules:
- host: "demoapp.example.com"
http:
paths:
- path: /blue
backend:
serviceName: demoapp-blue
servicePort: 80
- path: /green
backend:
serviceName: demoapp-green
servicePort: 80
应用更新后的服务:
代码语言:javascript复制kubectl apply -f service.yaml -n blue-green-deployment
第6步:执行蓝绿部署
在蓝色和绿色部署同时运行的情况下,将流量从蓝色部署路由到绿色部署。
第7步:更新绿色部署
使用新版本的应用程序更新绿色部署 (green-deployment-v2.yaml):
代码语言:javascript复制# green-deployment-v2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demoapp-green
labels:
app: demoapp
env: green
spec:
replicas: 3
selector:
matchLabels:
app: demoapp
env: green
template:
metadata:
labels:
app: demoapp
env: green
spec:
containers:
- name: myapp
image: myapp:v2.0
ports:
- containerPort: 80
应用更新的绿色部署:
代码语言:javascript复制kubectl apply -f green-deployment-v2.yaml -n blue-green-deployment
第8步:更新服务
更新服务以将流量路由到绿色部署 (service.yaml)。
代码语言:javascript复制# service.yaml
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp
env: green
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer
sessionAffinity: ClientIP
externalTrafficPolicy: Local
loadBalancerSourceRanges:
- 0.0.0.0/0
externalIPs:
- <external_ip_address>
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demoapp-ingress
spec:
rules:
- host: "demoapp.example.com"
http:
paths:
- path: /blue
backend:
serviceName: demoapp-blue
servicePort: 80
- path: /green
backend:
serviceName: demoapp-green
servicePort: 80
应用更新后的服务:
代码语言:javascript复制kubectl apply -f service.yaml -n blue-green-deployment
第9步:验证部署成功
通过使用外部 IP 地址在 Web 浏览器中访问应用程序来验证部署是否成功。
第10步:回滚(如果需要)
如果出现问题,请通过更新服务清单中的标签选择器以选择蓝色部署并重新应用服务清单来回滚到以前的版本。
代码语言:javascript复制# service.yaml
apiVersion: v1
kind: Service
metadata:
name: demoapp-service
spec:
selector:
app: demoapp
env: blue
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer
sessionAffinity: ClientIP
externalTrafficPolicy: Local
文章原文:
https://ashishnoob.medium.com/blue-green-deployment-with-host-and-path-based-routing-in-kubernetes-9c9c19a19e10