kubernetes pod升级与回滚扩容与缩容
运行一个容器:
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2020-09-19T02:41:11Z"
generation: 1
labels:
app: nginx
name: nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 3 # 副本数
revisionHistoryLimit: 10 # 历史记录保留的个数
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.4
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
# 保存为deployment.yaml 并创建nginx pod:
[root@k8s-master01 yaml]# kubectl create -f deployment-nginx.yaml
deployment.apps/nginx created
#检查:
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6cdd5dd489-d7kll 1/1 Running 0 17s
nginx-6cdd5dd489-dblw2 1/1 Running 0 17s
nginx-6cdd5dd489-zc8fl 1/1 Running 0 17s
#检查nginx版本:
[root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-d7kll -- sh
# nginx -v
nginx version: nginx/1.15.4
镜像升级与回滚
#升级:
root@k8s-master01[11:10:24]:~/yaml$ kubectl set image deploy nginx nginx=nginx:1.15.5 --record=true
deployment.apps/nginx image updated
#检查升级过程:
kubectl describe pod nginx-7c4b6d99fd-csh5s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 76s default-scheduler Successfully assigned default/nginx-df9789dc8-6h7p7 to k8s-node02
Normal Pulling 76s kubelet Pulling image "nginx:1.15.5"
Normal Pulled 51s kubelet Successfully pulled image "nginx:1.15.5" in 24.828362289s
Normal Created 51s kubelet Created container nginx
Normal Started 51s kubelet Started container nginx
#检查现在的版本:
[root@k8s-master01 yaml]# kubectl exec -it nginx-df9789dc8-trtwq -- sh
# nginx -v
nginx version: nginx/1.15.5
#检查更新过程
kubectl rollout status deploy nginx
[root@k8s-master01 yaml]# kubectl rollout status deploy nginx
Waiting for deployment "nginx" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
deployment "nginx" successfully rolled out
[root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-bsb7f -- sh
# nginx -v
nginx version: nginx/1.15.5
#为了实验效果 在升级一次
[root@k8s-master01 yaml]# kubectl set image deploy nginx nginx=nginx:1.15.6 --record=true
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-5455fbc855-64brv 1/1 Running 0 2m48s
nginx-5455fbc855-nblks 1/1 Running 0 3m16s
nginx-5455fbc855-ns4kz 1/1 Running 0 2m47s
[root@k8s-master01 yaml]# kubectl exec -it nginx-5455fbc855-64brv -- sh
# nginx -v
nginx version: nginx/1.15.6
#回滚:
1. 查看历史更新
kubectl rollout history deploy nginx
[root@k8s-master01 yaml]# kubectl rollout history deploy nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
2 kubectl set image deploy nginx nginx=nginx:1.15.5 --record=true
3 kubectl set image deploy nginx nginx=nginx:1.15.6 --record=true
2. 回滚到上一个版本[kubectl rollout undo deploy {name} ]
[root@k8s-master01 yaml]# kubectl rollout undo deploy nginx
deployment.apps/nginx rolled back
3. 检查回滚状态:
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6cdd5dd489-bzdgd 1/1 Running 0 21s
nginx-6cdd5dd489-cwxv5 1/1 Running 0 22s
nginx-6cdd5dd489-xb6xj 1/1 Running 0 20s
3.1 进入容器检查:
[root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-bzdgd -- sh
# nginx -v
nginx version: nginx/1.15.4
4. 回滚到指定版本
[root@k8s-master01 yaml]# kubectl rollout history deploy nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 kubectl set image deploy nginx nginx=nginx:1.15.5 --record=true
2 kubectl set image deploy nginx nginx=nginx:1.15.6 --record=true
# 查看指定版本信息
kubectl rollout history deploy nginx --revision=1
#回滚到指定版本
kubectl rollout undo deploy nginx --to-revision=1
[root@k8s-master01 yaml]# kubectl exec -it nginx-6cdd5dd489-q44cr -- sh
# nginx -v
nginx version: nginx/1.15.5
使用pod扩容,缩容
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-df9789dc8-gqk5j 1/1 Running 0 11m
nginx-df9789dc8-vhqkd 1/1 Running 0 11m
#修改资源:
[root@k8s-master01 yaml]# kubectl edit deploy nginx
deployment.apps/nginx edited
找到: replicas: 2
改为: replicas: 3
#检查状态:
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-df9789dc8-c47mc 1/1 Running 0 3s
nginx-df9789dc8-gqk5j 1/1 Running 0 11m
nginx-df9789dc8-vhqkd 1/1 Running 0 11m
#缩容就是继续把 replicas: 3 改为: replicas: 2即可:
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-df9789dc8-gqk5j 1/1 Running 0 13m
nginx-df9789dc8-vhqkd 1/1 Running 0 13m
使用yaml文件进行升级
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2020-09-19T02:41:11Z"
generation: 1
labels:
app: nginx
name: nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 3 # 副本数
revisionHistoryLimit: 10 # 历史记录保留的个数
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.4
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
#修改:
spec:
containers:
- image: nginx:1.15.5 #改为想要的镜像版本
#改为 1.15.5
[root@k8s-master01 yaml]# kubectl apply -f nginx-1.yaml
deployment.apps/nginx configured
[root@k8s-master01 yaml]# kubectl get pod
#注意升级的时候 他会起新的rs(replicas),通过命令可以看到,他会新起来一个pod 并把老副本数设置为2,新副本起来2个的时候
#老副本数会设置为1,直到变成0 最后才会真正的完成滚动更新。
[root@k8s-master01 yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-5455fbc855 3 3 3 4m46s
nginx-565979ccb4 1 1 0 5s
[root@k8s-master01 yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-5455fbc855-j552c 1/1 Running 0 17s
nginx-5455fbc855-sl8m8 1/1 Running 0 15s
nginx-5455fbc855-sq4mr 1/1 Running 0 14s
[root@k8s-master01 yaml]# kubectl exec -it nginx-5455fbc855-j552c -- sh
# nginx -v
nginx version: nginx/1.15.5
## 查看滚动更新过程:
NewReplicaSet: nginx-565979ccb4 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set nginx-6cdd5dd489 to 3
Normal ScalingReplicaSet 9m53s deployment-controller Scaled up replica set nginx-df9789dc8 to 1
Normal ScalingReplicaSet 9m51s deployment-controller Scaled down replica set nginx-6cdd5dd489 to 2
Normal ScalingReplicaSet 9m51s deployment-controller Scaled up replica set nginx-df9789dc8 to 2
Normal ScalingReplicaSet 9m50s deployment-controller Scaled down replica set nginx-6cdd5dd489 to 1
Normal ScalingReplicaSet 9m50s deployment-controller Scaled up replica set nginx-df9789dc8 to 3
Normal ScalingReplicaSet 9m48s deployment-controller Scaled down replica set nginx-6cdd5dd489 to 0
Normal ScalingReplicaSet 7m47s deployment-controller Scaled up replica set nginx-5455fbc855 to 1
Normal ScalingReplicaSet 7m45s deployment-controller Scaled down replica set nginx-df9789dc8 to 2
Normal ScalingReplicaSet 2m18s (x10 over 7m45s) deployment-controller (combined from similar events): Scaled down replica set nginx-5455fbc855 to 0

微信赞赏

支付宝赞赏

【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
2020-07-05 docker创建容器数据持久化资源限制基础命令