k8s之滚动更新、金丝雀发布、蓝绿发布
k8s Pod三种升级方式哪几种发布方式滚动更新金丝雀发布蓝绿发布演示时间滚动更新金丝雀发布蓝绿发布哪几种发布方式滚动更新滚动更新通过设置MaxSurge和MaxUnavailable来规定可以有多少个额外的Pod和可以忍受多少个Pod无法提供服务(两个参数可以为0,但是不能同时为0),在整个更新的过程中都是平滑无缝衔接。金丝雀发布金丝雀发布,又称:灰度发布,在k8s中通过暂停滚动跟新来实现灰度发
哪几种发布方式
滚动更新
滚动更新通过设置MaxSurge和MaxUnavailable来规定可以有多少个额外的Pod和可以忍受多少个Pod无法提供服务(两个参数可以为0,但是不能同时为0),在整个更新的过程中都是平滑无缝衔接。
金丝雀发布
金丝雀发布,又称:灰度发布,在k8s中通过暂停滚动跟新来实现灰度发布。灰度发布,是将一个新的版本放入到整个集群中,当没有发现问题后再将其他Pod进行更新。
蓝绿发布
蓝绿发布相当于两个不相关的deployment,访问完全通过流量控制来达到目的,当在使用过程中出现问题,在通过流量切换切换回老的版本。
演示时间
滚动更新
- 更新准备
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 5
revisionHistoryLimit: 10
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 2
template:
metadata:
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 60
containers:
- name: nginx
image: nginx:1.11
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
readinessProbe:
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 15
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
ports:
- containerPort: 80
name: nginx
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: nginx
[root@master k8s]# kubectl apply -f svc.yaml
[root@master k8s]# kubectl apply -f deployment.yaml
[root@master k8s]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-7c66bcdf4-dtvcr 1/1 Running 0 43s
nginx-7c66bcdf4-qp7jb 1/1 Running 0 43s
nginx-7c66bcdf4-qxn8f 1/1 Running 0 43s
nginx-7c66bcdf4-rqckm 1/1 Running 0 43s
nginx-7c66bcdf4-tvk4f 1/1 Running 0 43s
修改yaml文件将nginx镜像版本修改为最新版本
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 5
revisionHistoryLimit: 10
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 2
template:
metadata:
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 60
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
readinessProbe:
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 15
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
ports:
- containerPort: 80
name: nginx
新开一个shell窗口执行
[root@master k8s]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
nginx-7c66bcdf4-dtvcr 1/1 Running 0 3m28s
nginx-7c66bcdf4-qp7jb 1/1 Running 0 3m28s
nginx-7c66bcdf4-qxn8f 1/1 Running 0 3m28s
nginx-7c66bcdf4-rqckm 1/1 Running 0 3m28s
nginx-7c66bcdf4-tvk4f 1/1 Running 0 3m28s
开始滚动更新
[root@master k8s]# kubectl apply -f deployment.yaml
可以在新开窗口看到滚动更新过程
金丝雀发布
这里将已经更新到最新版本的nginx再通过金丝雀的方式还原回1.11版本
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 5
revisionHistoryLimit: 10
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 3
template:
metadata:
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 60
containers:
- name: nginx
image: nginx:1.11
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
readinessProbe:
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 15
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
ports:
- containerPort: 80
name: nginx
保持新开窗口不要动,观察新开窗口
[root@master k8s]# kubectl apply -f deployment.yaml && kubectl rollout pause deployment/nginx
[root@master k8s]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6ff78767cc-97lg7 1/1 Running 0 5m59s
nginx-6ff78767cc-lhxx2 1/1 Running 0 5m59s
nginx-7c66bcdf4-fdt2h 1/1 Running 0 27s
nginx-7c66bcdf4-k8kvv 1/1 Running 0 27s
nginx-7c66bcdf4-mcwbr 1/1 Running 0 27s
nginx-7c66bcdf4-xfh9c 1/1 Running 0 27s
通过上面可以开出新起了三个Pod还有两个老的Pod,这时候可以利用curl命令多次访问服务来查看效果
[root@master k8s]# curl -I 192.168.0.201:31219
HTTP/1.1 200 OK
Server: nginx/1.11.13
Date: Fri, 04 Dec 2020 18:32:00 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Apr 2017 15:01:57 GMT
Connection: keep-alive
ETag: "58e3b565-264"
Accept-Ranges: bytes
[root@master k8s]# curl -I 192.168.0.201:31219
HTTP/1.1 200 OK
Server: nginx/1.19.5
Date: Fri, 04 Dec 2020 18:32:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 24 Nov 2020 13:02:03 GMT
Connection: keep-alive
ETag: "5fbd044b-264"
Accept-Ranges: bytes
当测试没有问题时继续完成更新
[root@master k8s]# kubectl rollout resume deployment/nginx
更新完成后继续利用curl命令多次访问发现nginx版本已经全部更改
[root@master k8s]# curl -I 192.168.0.201:31219
HTTP/1.1 200 OK
Server: nginx/1.11.13
Date: Fri, 04 Dec 2020 18:34:27 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Apr 2017 15:01:57 GMT
Connection: keep-alive
ETag: "58e3b565-264"
Accept-Ranges: bytes
[root@master k8s]# curl -I 192.168.0.201:31219
HTTP/1.1 200 OK
Server: nginx/1.11.13
Date: Fri, 04 Dec 2020 18:34:31 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Apr 2017 15:01:57 GMT
Connection: keep-alive
ETag: "58e3b565-264"
Accept-Ranges: bytes
[root@master k8s]# curl -I 192.168.0.201:31219
HTTP/1.1 200 OK
Server: nginx/1.11.13
Date: Fri, 04 Dec 2020 18:34:33 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Apr 2017 15:01:57 GMT
Connection: keep-alive
ETag: "58e3b565-264"
Accept-Ranges: bytes
蓝绿发布
准备两套yaml文件
- nginx-v1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-v1
spec:
selector:
matchLabels:
app: nginx
version: "1.11"
replicas: 5
revisionHistoryLimit: 10
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 3
template:
metadata:
labels:
app: nginx
version: "1.11"
spec:
terminationGracePeriodSeconds: 60
containers:
- name: nginx-v1
image: nginx:1.11
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
readinessProbe:
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 15
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
ports:
- containerPort: 80
name: nginx
- nginx-v2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-v2
spec:
selector:
matchLabels:
app: nginx
version: "1.19"
replicas: 5
revisionHistoryLimit: 10
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 3
template:
metadata:
labels:
app: nginx
version: "1.19"
spec:
terminationGracePeriodSeconds: 60
containers:
- name: nginx-v2
image: nginx
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
readinessProbe:
httpGet:
path: /
port: 80
scheme: HTTP
initialDelaySeconds: 15
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
ports:
- containerPort: 80
name: nginx
修改service的yaml文件
- svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: nginx
version: "1.11"
将流量指向新版本的nginx上
- 修改svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: nginx
version: "1.19"
通过curl命令可发现流量已经全部转到新版本的nginx上,此时将老版本的deployment删除即可
更多推荐
所有评论(0)