1. 滚动升级
-
滚动升级架构图
-
滚动升级注释:
K8s对Pod升级的默认策略,通过使用新版本Pod逐步更新旧版本Pod,实现零停机发布,用户无感知。
-
滚动升级更新策略:
[root@k8s-master deployment]# vim web.yaml [root@k8s-master deployment]# cat web.yaml apiVersion: apps/v1 kind: Deployment metadata: name: web namespace: default annotations: # 记录回滚参数 kubernetes.io/change-cause: "web.v1-nginx-1.19" #记录到revision中的内容,记录版本号 spec: replicas: 9 # Pod副本预期数量 revisionHistoryLimit: 10 # RS历史版本保存数量 selector: matchLabels: app: web strategy: rollingUpdate: maxSurge: 25% # 滚动更新过程最大pod副本数 maxUnavailable: 25% # 滚动更新过程中最大不可用pod副本数, type: RollingUpdate template: metadata: labels: app: web # Pod副本的标签 spec: containers: - name: web image: nginx:1.19 readinessProbe: # 存活检查,如果失败,将杀死容器,来重启 httpGet: port: 80 path: /index.html initialDelaySeconds: 10 #启动容器后多少秒健康检查 periodSeconds: 10 #以后间隔多少秒检查一次 livenessProbe: # 就绪检查,失败就会剔除 service httpGet: port: 80 path: /index.html
- 注释
- maxSurge:滚动更新过程中最大Pod副本数,确保在更新时启动的Pod数量比期望(replicas)Pod数量最大多出25%
- maxUnavailable:滚动更新过程中最大不可用Pod副本数,确保在更新时最大25%Pod数量不可用,即确保75%Pod数量是可用状态。
- 注释
2. 滚动升级操作
kubectl apply -f xxx.yaml
kubectl set image deployment/web nginx=nginx:1.16
kubectl edit deployment/web
3. 案例
3.1 编写deployment的web.yaml文件
[root@k8s-master deployment]# vim web.yaml
[root@k8s-master deployment]# cat web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: default
annotations: # 记录回滚参数
kubernetes.io/change-cause: "web.v1-nginx-1.19" #记录到revision中的内容,记录版本号
spec:
replicas: 9 # Pod副本预期数量
revisionHistoryLimit: 10 # RS历史版本保存数量
selector:
matchLabels:
app: web
strategy:
rollingUpdate:
maxSurge: 25% # 滚动更新过程最大pod副本数
maxUnavailable: 25% # 滚动更新过程中最大不可用pod副本数,
type: RollingUpdate
template:
metadata:
labels:
app: web # Pod副本的标签
spec:
containers:
- name: web
image: nginx:1.16
readinessProbe: # 存活检查,如果失败,将杀死容器,来重启
httpGet:
port: 80
path: /index.html
initialDelaySeconds: 10 #启动容器后多少秒健康检查
periodSeconds: 10 #以后间隔多少秒检查一次
livenessProbe: # 就绪检查,失败就会剔除 service
httpGet:
port: 80
path: /index.html
3.2 启动服务
[root@k8s-master deployment]# kubectl apply -f web.yaml
deployment.apps/web created
3.3 查看服务
[root@k8s-master deployment]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-548b65b8c6-4k8n7 1/1 Running 0 2m24s
web-548b65b8c6-7wvhj 1/1 Running 0 2m24s
web-548b65b8c6-vpmpg 1/1 Running 0 2m24s
[root@k8s-master deployment]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-548b65b8c6-4k8n7 1/1 Running 0 2m56s 10.244.36.96 k8s-node1 <none> <none>
web-548b65b8c6-7wvhj 1/1 Running 0 2m56s 10.244.169.159 k8s-node2 <none> <none>
web-548b65b8c6-vpmpg 1/1 Running 0 2m56s 10.244.36.95 k8s-node1 <none> <none>
3.4 deployment端口暴露
[root@k8s-master deployment]# kubectl expose deployment web --port=80 --target-port=80 --type=NodePort
service/web exposed
3.5 查询服务是否端口暴露
[root@k8s-master deployment]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6d
probe-demo ClusterIP 10.104.161.168 <none> 80/TCP 2d
web NodePort 10.98.32.199 <none> 80:30082/TCP 14s
3.6 curl请求测试
[root@k8s-master deployment]# curl -I http://192.168.0.202:30082
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Tue, 01 Dec 2020 08:28:06 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 13 Aug 2019 10:05:00 GMT
Connection: keep-alive
ETag: "5d528b4c-264"
Accept-Ranges: bytes
3.7 修改deployment的web.yaml文件实现滚动更新
[root@k8s-master deployment]# vim web.yaml [root@k8s-master deployment]# cat web.yaml apiVersion: apps/v1kind: Deploymentmetadata: name: web namespace: default annotations: # 记录回滚参数 kubernetes.io/change-cause: "web.v1-nginx-1.19" #记录到revision中的内容,记录版本号spec: replicas: 9 # Pod副本预期数量 revisionHistoryLimit: 10 # RS历史版本保存数量 selector: matchLabels: app: web strategy: rollingUpdate: maxSurge: 25% # 滚动更新过程最大pod副本数 maxUnavailable: 25% # 滚动更新过程中最大不可用pod副本数, type: RollingUpdate template: metadata: labels: app: web # Pod副本的标签 spec: containers: - name: web image: nginx:1.18 readinessProbe: # 存活检查,如果失败,将杀死容器,来重启 httpGet: port: 80 path: /index.html initialDelaySeconds: 10 #启动容器后多少秒健康检查 periodSeconds: 10 #以后间隔多少秒检查一次 livenessProbe: # 就绪检查,失败就会剔除 service httpGet: port: 80 path: /index.html
3.8 执行配置文件更新
[root@k8s-master deployment]# kubectl apply -f web.yaml deployment.apps/web configured
3.9 查看pod服务
[root@k8s-master deployment]# kubectl get podsNAME READY STATUS RESTARTS AGEweb-65798c9f74-2r266 0/1 Terminating 0 39mweb-65798c9f74-5hl4p 1/1 Running 0 39mweb-65798c9f74-5qhgg 1/1 Running 0 39mweb-79c4bc4bd-9j4pw 0/1 ContainerCreating 0 3sweb-79c4bc4bd-pbz5m 1/1 Running 0 30s
3.10 curl 请求是否nginx是1.18版本
[root@k8s-master deployment]# curl -I http://192.168.0.202:30082HTTP/1.1 200 OKServer: nginx/1.18.0Date: Tue, 01 Dec 2020 08:31:09 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 21 Apr 2020 14:09:01 GMTConnection: keep-aliveETag: "5e9efe7d-264"Accept-Ranges: bytes