一、实验环境准备:
kubectl get pods -o wide
kubectl get nodes -o wide
kubectl get svc
准备两个nginx镜像,版本号一个是V3,一个是V4
二、准备一个nginx.yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
release: v1
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: mynginx
image: 192.168.206.137/library/nginx:v3
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginxservice
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30080
selector:
app: nginx1
在这个文件中,定义了:
元数据的名称为:nginx-deploy
副本数量为:1
滚动升级的策略为:RollingUpdate
滚动升级最低保留的副本数量为:25%
镜像为:192.168.206.137/library/nginx:v3
集群内部访问端口为:80
外部访问端口为:30080
三、部署这个yaml,并查看部署状态
kubectl apply -f nginx.yaml
部署状态:
kubectl rollout status deploy nginx-deploy
查看历史升级状态:
kubectl rollout history deploy nginx-deploy
查看历史升级状态为1的详细信息:
kubectl rollout history deploy nginx-deploy --revision=1
查看发布完成后的svc和pod状态:
kubectl get svc,pods -o wide
此时可以看到,nginx.yaml已经部署完成。
四、更新nginx版本,重新发布
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
release: v2
spec:
replicas: 2
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: mynginx
image: 192.168.206.137/library/nginx:v4
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginxservice
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30080
selector:
app: nginx2
比较上面的V1版本,V2更新了几点:
副本数量增加为:2
镜像使用:v4
更新发布这个新的版本:
kubectl apply -f nginx.yaml
kubectl rollout status deploy nginx-deploy
跟新后的SVC和POD状态:
可以看到POD增加到了2个
再次查看历史更新记录:
kubectl rollout history deploy nginx-deploy
指定查看V2的详细信息:
kubectl rollout history deploy nginx-deploy --revision=2
这里可以看到,两次的版本更新操作,这里都有详细记录。但是这里最多只能记录2个。
五、回滚版本到V1:
v1只有一个pod并且镜像使用的是nginx:v3
撤销上一次升级,并查看升级状态:
kubectl rollout undo deploy nginx-deploy
kubectl rollout status deploy nginx-deploy
kubectl get svc,pods -o wide
这里会发现,原本1个的POD,回滚后变成了2个。其实这里是k8s滚动升级的机制导致的。前面设置了最低保留25%的可用性,那么至少也有1个POD。所以K8s首先新增了一个POD,然后再升级版本,最后保持两个POD一致。
再次查看升级历史信息:
kubectl rollout history deploy nginx-deploy
查看3的详细信息:
kubectl rollout history deploy nginx-deploy --revision=3
对比2:
kubectl rollout history deploy nginx-deploy --revision=2
查看当前POD的详细信息:
kubectl describe pods nginx-deploy-6b6f9ff985-xpz94
Name: nginx-deploy-6b6f9ff985-xpz94
Namespace: default
Priority: 0
Node: master/192.168.206.138
Start Time: Sat, 27 Jan 2024 12:56:23 +0800
Labels: app=nginx
pod-template-hash=6b6f9ff985
Annotations: cni.projectcalico.org/containerID: 1b2e98f76f8f8e7da439c4a767ec4b297035ca582bcdd445e816e1c13c3d891a
cni.projectcalico.org/podIP: 10.224.219.80/32
cni.projectcalico.org/podIPs: 10.224.219.80/32
Status: Running
IP: 10.224.219.80
IPs:
IP: 10.224.219.80
Controlled By: ReplicaSet/nginx-deploy-6b6f9ff985
Containers:
mynginx:
Container ID: docker://2e6181cee6a4886c0892af0b92b0f31bde4d3513b0d34a654a9a0e0438274c73
Image: 192.168.206.137/library/nginx:v3
Image ID: docker-pullable://192.168.206.137/library/nginx@sha256:37bb5a5a2078bd67b1be281165e9f82f6f267d6404e0ae11b3a524b8fff8354e
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Sat, 27 Jan 2024 12:56:24 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-gd949 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-gd949:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-gd949
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 5m17s Successfully assigned default/nginx-deploy-6b6f9ff985-xpz94 to master
Normal Pulled 5m17s kubelet, master Container image "192.168.206.137/library/nginx:v3" already present on machine
Normal Created 5m17s kubelet, master Created container mynginx
Normal Started 5m17s kubelet, master Started container mynginx
Image这个地方,可以看到当前POD使用的版本号为V3,可以说明撤销成功,已经回退都发布之前的版本。
六、回滚到指定版本:
kubectl rollout history deploy nginx-deplo
目前有两个版本,在使用的是3,可以回滚到指定版本2,镜像回滚到nginx:v4状态,副本数量为2个
kubectl rollout undo deploy nginx-deploy --to-revision=2
kubectl rollout status deploy nginx-deploy
kubectl get svc,pods -o wide
kubectl describe pods nginx-deploy-76d4c4c684-kvtx8