kubernetes ingress 蓝绿发布
1.蓝绿发布介绍
蓝绿发布就是现网运行的业务在系统1,计划升级的版本部署了系统2。
新版本的系统2部署出来以后将进行充分的测试、验证。通过之后,将业务切换到系统2,即完成了版本更新。
Kubernetes本身是不支持蓝绿发布的,当前主要是通过新的deployment文件配合service label完成。
- 优点:
更新过程不用停机,在线完成,风险低;
回滚方便,效率高;
- 缺点:
两套一样的系统,占用资源,成本较高。
负载均衡、代理、路由等处理不当,可能导致业务没正常切换。
本文演示通过service的selector切换服务进行蓝绿发布
2.部署green的service
green_service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-green
spec:
replicas: 4
selector:
matchLabels:
app: nginx-green
template:
metadata:
labels:
app: nginx-green
spec:
containers:
- name: nginx-green
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
configMap:
name: nginx-green
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-green
data:
index.html: |
green green green --> If you see this message --> The route to green,bye ~~~;
---
apiVersion: v1
kind: Service
metadata:
name: nginx-green
labels:
app: nginx-green
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
selector:
app: nginx-green
部署:
kubectl apply -f green_service.yaml
查看pods:
qiteck@server:~/program/k8s/blue_green_split$ sudo kubectl get pod
nginx-green-78968cd575-bngtx 1/1 Running 0 79m
nginx-green-78968cd575-cv986 1/1 Running 0 79m
nginx-green-78968cd575-pkpcr 1/1 Running 0 79m
nginx-green-78968cd575-xz4dj 1/1 Running 0 79m
查看service:
qiteck@server:~/program/k8s/blue_green_split$ sudo kubectl get service
nginx-green ClusterIP 10.96.255.252 <none> 80/TCP 79m
3. 部署blue的service
blue_service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-blue
spec:
replicas: 4
selector:
matchLabels:
app: nginx-blue
template:
metadata:
labels:
app: nginx-blue
spec:
containers:
- name: nginx-blue
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
configMap:
name: nginx-blue
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-blue
data:
index.html: |
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
---
apiVersion: v1
kind: Service
metadata:
name: nginx-blue
labels:
app: nginx-blue
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
selector:
app: nginx-blue
部署:
kubectl apply -f blue_service.yaml
查看pods:
qiteck@server:~/program/k8s/blue_green_split$ sudo kubectl get pods |grep nginx
nginx-blue-766b9f6df4-4ldzx 1/1 Running 0 38m
nginx-blue-766b9f6df4-bmd85 1/1 Running 0 38m
nginx-blue-766b9f6df4-h5bdd 1/1 Running 0 38m
nginx-blue-766b9f6df4-qsn5w 1/1 Running 0 38m
nginx-green-78968cd575-bngtx 1/1 Running 0 97m
nginx-green-78968cd575-cv986 1/1 Running 0 97m
nginx-green-78968cd575-pkpcr 1/1 Running 0 97m
nginx-green-78968cd575-xz4dj 1/1 Running 0 97m
查看service:
qiteck@server:~/program/k8s/blue_green_split$ sudo kubectl get service |grep nginx
nginx-blue ClusterIP 10.96.95.252 <none> 80/TCP 39m
nginx-green ClusterIP 10.96.255.252 <none> 80/TCP 98m
目前可以看到blue和green两套服务都是存在的
4.部署选择green的ingress
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- host: nginx.test.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-green
port:
number: 80
域名nginx.test.com映射到nginx_green服务:
部署:
sudo kubectl apply -f ingress.yaml
查看ingress:
qiteck@server:~/program/k8s/blue_green_split$ sudo kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
nginx <none> nginx.test.com 10.96.180.91 80 49s
测试访问一下:
qiteck@server:~/program/k8s/blue_green_split$ for i in `seq 20`;do curl nginx.test.com;done
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
green green green --> If you see this message --> The route to green,bye ~~~;
可以看到green服务正常提供服务
5.部署选择blue的ingress
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- host: nginx.test.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-blue
port:
number: 80
域名nginx.test.com映射到nginx_blue服务:
部署:
sudo kubectl apply -f ingress.yaml
查看ingress:
qiteck@server:~/program/k8s/blue_green_split$ sudo kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
nginx <none> nginx.test.com 10.96.180.91 80 49s
测试访问一下:
qiteck@server:~/program/k8s/blue_green_split$ for i in `seq 20`;do curl nginx.test.com;done
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
blue blue blue --> If you see this message --> The route to blue,bye ~~~;
可以看到现在是blue服务在提供服务
这样就实现了蓝绿切换