涉及参考文档
一、什么是蓝绿部署?
蓝绿(blue/green):新版本与旧版本一起存在,然后切换流量
二、K8S中如何实现蓝绿部署
1、k8s service label标签来实现蓝绿发布(本次实验)
2、Ingress 控制器来实现蓝绿发布
3、Istio来实现蓝绿发布,或者像Istio类似的服务
三、kubernets 蓝绿部署 yaml 配置
1)、蓝——yaml
vim blue.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: blue
namespace: default
labels:
app: blue
version: v1 #定义标签版本
spec:
replicas: 1 #启动副本数
minReadySeconds: 5 #等待设置的时间后才进行升级,(如果没有设置该值,在某些极端情况下可能会造成服务不正常运行)
revisionHistoryLimit: 3 #要保留以允许回滚的旧复制集数
strategy: #策略
type: RollingUpdate #默认为滚动更新(可以不写)
rollingUpdate: #滚动更新
maxSurge: 30% #升级过程中最多可以比原先设置多出30%的POD数量
maxUnavailable: 30% #升级过程中最多有30%POD处于无法提供服务的状态(该不为0)
selector: #设置标签
matchLabels:
app: blue #这个标签相当于分组,查看(kubectl get pods --show-labels)
version: v1 #定义标签版本
template: #模板(相当于定义好的一个python中的模块)
metadata:
labels:
app: blue #这个标签需要selector定义的标签一个,划分在同一个组
version: v1 #定义标签版本
spec:
containers:
- name: blue #node节点启动docker的容器名字(kind控制器名字+标签名)
image: mritd/demo #镜像名
livenessProbe: #判断容器是否存活、如果livenessProbe检查不健康就杀死该容器
httpGet: #探针方式
path: / #请求
port: 80 #端口
scheme: HTTP #http 还是 https
initialDelaySeconds: 30 #初始化探测的延迟时间(容器启动立即探测会报错—容器启动也需要时间哇
timeoutSeconds: 5 #探测时候始终没有响应等待多久时长(默认为一秒)
periodSeconds: 30 #认周期探测时长,多久去探测一次(默认为10秒一次)
successThreshold: 1 #探测多少次成功才成功(默认为3次)
failureThreshold: 5 #探测多少次失败才失败(默认为3次)
readinessProbe: #判断容器是否可用性, 达到ready状态的pod才可以接收请求,当为false时候,这个容器>将从ep的列表中移除,这样就不会照成用户访问返回不正常的状态码(3xx、4xx、5xx)
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
periodSeconds: 10
successThreshold: 1
failureThreshold: 5
ports:
- name: http #对应svc的名称(targetPort 可使用 名称指定 服务端口)
containerPort: 80 #容器开放的监听端口
protocol: TCP #使用TCP协议
resources: #资源请求
limits: #最高允许资源大小
cpu: 100m
memory: 400Mi
requests: #最小允许资源大小
cpu: 50m
memory: 200Mi
2)绿——yaml
vim green.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: green
namespace: default
labels:
app: green
version: v2 #定义标签版本
spec:
replicas: 1 #启动副本数
minReadySeconds: 5 #等待设置的时间后才进行升级,(如果没有设置该值,在某些极端情况下可能会造成服务不正常运行)
revisionHistoryLimit: 3 #要保留以允许回滚的旧复制集数
strategy: #策略
type: RollingUpdate #默认为滚动更新(可以不写)
rollingUpdate: #滚动更新
maxSurge: 30% #升级过程中最多可以比原先设置多出30%的POD数量
maxUnavailable: 30% #升级过程中最多有30%POD处于无法提供服务的状态(该不为0)
selector: #设置标签
matchLabels:
app: green #这个标签相当于分组,查看(kubectl get pods --show-labels)
version: v2 #定义标签版本
template: #模板(相当于定义好的一个python中的模块)
metadata:
labels:
app: green #这个标签需要selector定义的标签一个,划分在同一个组
version: v2 #定义标签版本
spec:
containers:
- name: green #node节点启动docker的容器名字(kind控制器名字+标签名)
image: mritd/demo #镜像名
livenessProbe: #判断容器是否存活、如果livenessProbe检查不健康就杀死该容器
httpGet: #探针方式
path: / #请求
port: 80 #端口
scheme: HTTP #http 还是 https
initialDelaySeconds: 30 #初始化探测的延迟时间(容器启动立即探测会报错—容器启动也需要时间哇
timeoutSeconds: 5 #探测时候始终没有响应等待多久时长(默认为一秒)
periodSeconds: 30 #认周期探测时长,多久去探测一次(默认为10秒一次)
successThreshold: 1 #探测多少次成功才成功(默认为3次)
failureThreshold: 5 #探测多少次失败才失败(默认为3次)
readinessProbe: #判断容器是否可用性, 达到ready状态的pod才可以接收请求,当为false时候,这个容器>将从ep的列表中移除,这样就不会照成用户访问返回不正常的状态码(3xx、4xx、5xx)
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
periodSeconds: 10
successThreshold: 1
failureThreshold: 5
ports:
- name: http #对应svc的名称(targetPort 可使用 名称指定 服务端口)
containerPort: 80 #容器开放的监听端口
protocol: TCP
resources: #资源请求
limits: #最高允许资源大小
cpu: 100m
memory: 400Mi
requests: #最小允许资源大小
cpu: 50m
memory: 200Mi
3)蓝绿部署访问yaml
访问蓝服务
vim service.yaml
apiVersion: v1 #api版本
kind: Service #kind名称
metadata:
name: blue-green #svc名称
namespace: default #使用默认的名称空间
spec:
ports: #暴露服务端口
- port: 80 #暴露端口号
targetPort: http #对应pod暴露端口(可使用deployment的容器定义的名称,也可以使用deployment对外提供服务端口)
protocol: TCP #使用TCP协议
name: http #定义pod的名称
selector:
app: blue #对应deployment对应的标签(对对连游戏)
version: v1 #对应deployment对应的标签(对对连游戏),可以定义多个标签
4)部署蓝、绿 deployment和service服务
kubectl apply -f blue.yaml -f green.yaml -f service.yaml
5)查看部署pod、svc、ep
kubectl get pod,svc,ep -o wide |grep -E 'blue|green'
四、修改部署蓝绿pod
1)、进入蓝pod,修改访问index.html
kubectl exec -it blue-7bb8b448db-brrq8 /bin/bash
echo blue > /usr/share/nginx/html/index.html
2)、进入绿pod,修改访问index.html
kubectl exec -it green-69b8784957-47vjs /bin/bash
echo blue > /usr/share/nginx/html/index.html
五、部署绿服务,流量是否转流
1)、查看当前流量转向
kubectl get svc |grep blue-green
2)、修改svc服务标签
vim service.yaml
apiVersion: v1 #api版本
kind: Service #kind名称
metadata:
name: blue-green #svc名称
namespace: default #使用默认的名称空间
spec:
ports: #暴露服务端口
- port: 80 #暴露端口号
targetPort: http #对应pod暴露端口(可使用deployment的容器定义的名称,也可以使用deployment对外提>供服务端口)
protocol: TCP #使用TCP协议
name: http #定义pod的名称
selector: #重点修改对应的green中pod1中的标签
app: green #对应deployment对应的标签(对对连游戏)
version: v2 #对应deployment对应的标签(对对连游戏),可以定义多个标签
3)部署绿标签svc
kubectl apply -f service.yaml
4)重新访问svc