滚动更新是Kubernetes中确保应用零停机更新的核心机制。本指南将详细介绍滚动更新的概念、操作步骤和最佳实践。

什么是滚动更新?

滚动更新是一种渐进式更新策略,它允许您逐步用新版本的Pod替换旧版本的Pod,从而确保在整个更新过程中应用始终可用。

核心特点

  • 零停机时间:通过逐步替换Pod实例,确保服务持续可用

  • 自动负载均衡:Service在更新期间仅将流量路由到可用的Pod

  • 版本控制:所有更新都被版本化,可以轻松回滚到任何先前状态

  • 灵活配置:可以配置最大不可用Pod数量和最大新Pod创建数量

滚动更新操作流程

1. 准备工作

在开始滚动更新前,先检查当前部署状态:

# 查看部署状态
kubectl get deployments
ccc@cccs-MBP ~ % kubectl get deployments

NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   2/2     2            2           3h12m
# 查看运行的Pod
kubectl get pods
ccc@cccs-MBP ~ % kubectl get pods

NAME                                   READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-75c5d958ff-flfdd   1/1     Running   0          3h12m
kubernetes-bootcamp-75c5d958ff-wzbdr   1/1     Running   0          21m
# 查看当前应用版本(关注Image字段)
kubectl describe pods
ccc@cccs-MBP ~ % kubectl describe pods

Name:             kubernetes-bootcamp-75c5d958ff-flfdd
Namespace:        default
Priority:         0
Service Account:  default
Node:             docker-desktop/192.168.65.4
Start Time:       Fri, 30 Jan 2026 09:18:24 +0800
Labels:           app=kubernetes-bootcamp
                  pod-template-hash=75c5d958ff
                  version=v1
Annotations:      <none>
Status:           Running
IP:               10.1.0.17
IPs:
  IP:           10.1.0.17
Controlled By:  ReplicaSet/kubernetes-bootcamp-75c5d958ff
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://42746c7a21be846354d7c4f0f33a364efcfd9cc4f5353907c1b9912e106beeb0
    Image:          gcr.io/google-samples/kubernetes-bootcamp:v1
    Image ID:       docker-pullable://gcr.io/google-samples/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 30 Jan 2026 09:18:25 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-s64m8 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-s64m8:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
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:                      <none>


Name:             kubernetes-bootcamp-75c5d958ff-wzbdr
Namespace:        default
Priority:         0
Service Account:  default
Node:             docker-desktop/192.168.65.4
Start Time:       Fri, 30 Jan 2026 12:09:31 +0800
Labels:           app=kubernetes-bootcamp
                  pod-template-hash=75c5d958ff
Annotations:      <none>
Status:           Running
IP:               10.1.0.18
IPs:
  IP:           10.1.0.18
Controlled By:  ReplicaSet/kubernetes-bootcamp-75c5d958ff
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://a1e21149860e65abe87b6debb7e9b3349941253dad81108ed439c3e3a9bd4816
    Image:          gcr.io/google-samples/kubernetes-bootcamp:v1
    Image ID:       docker-pullable://gcr.io/google-samples/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 30 Jan 2026 12:09:32 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-hjf88 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-hjf88:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
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  21m   default-scheduler  Successfully assigned default/kubernetes-bootcamp-75c5d958ff-wzbdr to docker-desktop
  Normal  Pulled     21m   kubelet            Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
  Normal  Created    21m   kubelet            Created container kubernetes-bootcamp
  Normal  Started    21m   kubelet            Started container kubernetes-bootcamp
ccc@cccs-MBP ~ %

2. 执行滚动更新

更新应用到新版本(例如v2):

kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=docker.io/jocatalin/kubernetes-bootcamp:v2
ccc@cccs-MBP ~ % kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=docker.io/jocatalin/kubernetes-bootcamp:v2

deployment.apps/kubernetes-bootcamp image updated

此命令会通知Deployment使用新的容器镜像,并自动启动滚动更新过程。更新期间,您可以观察Pod的替换过程:

# 查看新旧Pod的交替过程
kubectl get pods
ccc@cccs-MBP ~ % kubectl get pods
NAME                                   READY   STATUS        RESTARTS   AGE
kubernetes-bootcamp-5478c6c664-nxc5h   1/1     Running       0          8s
kubernetes-bootcamp-5478c6c664-vk6gn   1/1     Running       0          7s
kubernetes-bootcamp-75c5d958ff-flfdd   1/1     Terminating   0          3h18m
kubernetes-bootcamp-75c5d958ff-wzbdr   1/1     Terminating   0          27m

ccc@cccs-MBP ~ % kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-5478c6c664-nxc5h   1/1     Running   0          41s
kubernetes-bootcamp-5478c6c664-vk6gn   1/1     Running   0          40s
ccc@cccs-MBP ~ % 

3. 验证更新

更新完成后,验证应用是否正常运行:

# 检查服务状态(如服务已被删除,重新创建)
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080

# 设置环境变量获取NodePort
export NODE_PORT="$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')"
echo "NODE_PORT=$NODE_PORT"

# 测试应用访问
curl http://localhost:8080

ccc@cccs-MBP ~ % curl http://localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5478c6c664-vk6gn | v=2
ccc@cccs-MBP ~ % curl http://localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5478c6c664-vk6gn | v=2
ccc@cccs-MBP ~ % curl http://localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5478c6c664-nxc5h | v=2
ccc@cccs-MBP ~ %

每次运行curl命令,可能会命中不同的Pod。所有Pod现在都应该运行最新版本(v2)。

使用以下命令确认更新状态:

# 查看滚动更新状态
kubectl rollout status deployments/kubernetes-bootcamp
ccc@cccs-MBP ~ % kubectl rollout status deployments/kubernetes-bootcamp

deployment "kubernetes-bootcamp" successfully rolled out
# 验证Pod使用的镜像版本
kubectl describe pods
ccc@cccs-MBP ~ % kubectl describe pods

Name:             kubernetes-bootcamp-5478c6c664-nxc5h
Namespace:        default
Priority:         0
Service Account:  default
Node:             docker-desktop/192.168.65.4
Start Time:       Fri, 30 Jan 2026 12:36:46 +0800
Labels:           app=kubernetes-bootcamp
                  pod-template-hash=5478c6c664
Annotations:      <none>
Status:           Running
IP:               10.1.0.21
IPs:
  IP:           10.1.0.21
Controlled By:  ReplicaSet/kubernetes-bootcamp-5478c6c664
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://4989f6c40f95afecad9c3001fefd8c24d665920112e21dbf864eace95a31ac67
    Image:          docker.io/jocatalin/kubernetes-bootcamp:v2
    Image ID:       docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 30 Jan 2026 12:36:47 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6mvpg (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-6mvpg:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
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  76m   default-scheduler  Successfully assigned default/kubernetes-bootcamp-5478c6c664-nxc5h to docker-desktop
  Normal  Pulled     76m   kubelet            Container image "docker.io/jocatalin/kubernetes-bootcamp:v2" already present on machine
  Normal  Created    76m   kubelet            Created container kubernetes-bootcamp
  Normal  Started    76m   kubelet            Started container kubernetes-bootcamp


Name:             kubernetes-bootcamp-5478c6c664-vk6gn
Namespace:        default
Priority:         0
Service Account:  default
Node:             docker-desktop/192.168.65.4
Start Time:       Fri, 30 Jan 2026 12:36:47 +0800
Labels:           app=kubernetes-bootcamp
                  pod-template-hash=5478c6c664
Annotations:      <none>
Status:           Running
IP:               10.1.0.22
IPs:
  IP:           10.1.0.22
Controlled By:  ReplicaSet/kubernetes-bootcamp-5478c6c664
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://1f00674f52fec884f55e53641d430aae76d7bbdb01d9a8b8903c40aeb5afce3c
    Image:          docker.io/jocatalin/kubernetes-bootcamp:v2
    Image ID:       docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 30 Jan 2026 12:36:48 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-l9rz7 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-l9rz7:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
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  76m   default-scheduler  Successfully assigned default/kubernetes-bootcamp-5478c6c664-vk6gn to docker-desktop
  Normal  Pulled     76m   kubelet            Container image "docker.io/jocatalin/kubernetes-bootcamp:v2" already present on machine
  Normal  Created    76m   kubelet            Created container kubernetes-bootcamp
  Normal  Started    76m   kubelet            Started container kubernetes-bootcamp
ccc@cccs-MBP ~ %

4. 处理更新失败和回滚

如果更新出现问题(例如使用不存在的镜像标签v10):

# 尝试更新到不存在的版本
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
ccc@cccs-MBP ~ % kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10

deployment.apps/kubernetes-bootcamp image updated
# 查看部署状态(会显示Pod不可用)
kubectl get deployments
ccc@cccs-MBP ~ % kubectl get deployments

NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   2/2     1            2           4h36m
# 查看Pod详情(部分Pod状态为ImagePullBackOff)
kubectl get pods
ccc@cccs-MBP ~ % kubectl get pods

NAME                                   READY   STATUS             RESTARTS   AGE
kubernetes-bootcamp-5478c6c664-nxc5h   1/1     Running            0          78m
kubernetes-bootcamp-5478c6c664-vk6gn   1/1     Running            0          78m
kubernetes-bootcamp-5c9dcf5899-sd77g   0/1     ImagePullBackOff   0          28s
ccc@cccs-MBP ~ % kubectl get pods

NAME                                   READY   STATUS         RESTARTS   AGE
kubernetes-bootcamp-5478c6c664-nxc5h   1/1     Running        0          78m
kubernetes-bootcamp-5478c6c664-vk6gn   1/1     Running        0          78m
kubernetes-bootcamp-5c9dcf5899-sd77g   0/1     ErrImagePull   0          39s

遇到问题时,查看详细错误信息:

# 查看Pod详细信息和事件
kubectl describe pods

ccc@cccs-MBP ~ % kubectl describe pods

Name:             kubernetes-bootcamp-5478c6c664-nxc5h
Namespace:        default
Priority:         0
Service Account:  default
Node:             docker-desktop/192.168.65.4
Start Time:       Fri, 30 Jan 2026 12:36:46 +0800
Labels:           app=kubernetes-bootcamp
                  pod-template-hash=5478c6c664
Annotations:      <none>
Status:           Running
IP:               10.1.0.21
IPs:
  IP:           10.1.0.21
Controlled By:  ReplicaSet/kubernetes-bootcamp-5478c6c664
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://4989f6c40f95afecad9c3001fefd8c24d665920112e21dbf864eace95a31ac67
    Image:          docker.io/jocatalin/kubernetes-bootcamp:v2
    Image ID:       docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 30 Jan 2026 12:36:47 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6mvpg (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-6mvpg:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
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  79m   default-scheduler  Successfully assigned default/kubernetes-bootcamp-5478c6c664-nxc5h to docker-desktop
  Normal  Pulled     79m   kubelet            Container image "docker.io/jocatalin/kubernetes-bootcamp:v2" already present on machine
  Normal  Created    79m   kubelet            Created container kubernetes-bootcamp
  Normal  Started    79m   kubelet            Started container kubernetes-bootcamp


Name:             kubernetes-bootcamp-5478c6c664-vk6gn
Namespace:        default
Priority:         0
Service Account:  default
Node:             docker-desktop/192.168.65.4
Start Time:       Fri, 30 Jan 2026 12:36:47 +0800
Labels:           app=kubernetes-bootcamp
                  pod-template-hash=5478c6c664
Annotations:      <none>
Status:           Running
IP:               10.1.0.22
IPs:
  IP:           10.1.0.22
Controlled By:  ReplicaSet/kubernetes-bootcamp-5478c6c664
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://1f00674f52fec884f55e53641d430aae76d7bbdb01d9a8b8903c40aeb5afce3c
    Image:          docker.io/jocatalin/kubernetes-bootcamp:v2
    Image ID:       docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 30 Jan 2026 12:36:48 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-l9rz7 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-l9rz7:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
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  79m   default-scheduler  Successfully assigned default/kubernetes-bootcamp-5478c6c664-vk6gn to docker-desktop
  Normal  Pulled     79m   kubelet            Container image "docker.io/jocatalin/kubernetes-bootcamp:v2" already present on machine
  Normal  Created    79m   kubelet            Created container kubernetes-bootcamp
  Normal  Started    79m   kubelet            Started container kubernetes-bootcamp


Name:             kubernetes-bootcamp-5c9dcf5899-sd77g
Namespace:        default
Priority:         0
Service Account:  default
Node:             docker-desktop/192.168.65.4
Start Time:       Fri, 30 Jan 2026 13:54:50 +0800
Labels:           app=kubernetes-bootcamp
                  pod-template-hash=5c9dcf5899
Annotations:      <none>
Status:           Pending
IP:               10.1.0.23
IPs:
  IP:           10.1.0.23
Controlled By:  ReplicaSet/kubernetes-bootcamp-5c9dcf5899
Containers:
  kubernetes-bootcamp:
    Container ID:   
    Image:          gcr.io/google-samples/kubernetes-bootcamp:v10
    Image ID:       
    Port:           <none>
    Host Port:      <none>
    State:          Waiting
      Reason:       ImagePullBackOff
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-7749j (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  kube-api-access-7749j:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
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  92s                default-scheduler  Successfully assigned default/kubernetes-bootcamp-5c9dcf5899-sd77g to docker-desktop
  Warning  Failed     66s                kubelet            Failed to pull image "gcr.io/google-samples/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: Head "https://gcr.io/v2/google-samples/kubernetes-bootcamp/manifests/v10": EOF
  Normal   Pulling    43s (x3 over 91s)  kubelet            Pulling image "gcr.io/google-samples/kubernetes-bootcamp:v10"
  Warning  Failed     40s (x2 over 86s)  kubelet            Failed to pull image "gcr.io/google-samples/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: Get "https://gcr.io/v2/": EOF
  Warning  Failed     40s (x3 over 86s)  kubelet            Error: ErrImagePull
  Normal   BackOff    2s (x5 over 86s)   kubelet            Back-off pulling image "gcr.io/google-samples/kubernetes-bootcamp:v10"
  Warning  Failed     2s (x5 over 86s)   kubelet            Error: ImagePullBackOff
ccc@cccs-MBP ~ %

执行回滚到上一个稳定版本:

# 回滚到上一个版本
kubectl rollout undo deployments/kubernetes-bootcamp
ccc@cccs-MBP ~ % kubectl rollout undo deployments/kubernetes-bootcamp

deployment.apps/kubernetes-bootcamp rolled back
# 验证回滚结果
kubectl get pods
ccc@cccs-MBP ~ % kubectl get pods

NAME                                   READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-5478c6c664-nxc5h   1/1     Running   0          80m
kubernetes-bootcamp-5478c6c664-vk6gn   1/1     Running   0          80m
kubectl describe pods

ccc@cccs-MBP ~ % kubectl get pods

NAME                                   READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-5478c6c664-nxc5h   1/1     Running   0          80m
kubernetes-bootcamp-5478c6c664-vk6gn   1/1     Running   0          80m
ccc@cccs-MBP ~ % kubectl describe pods

Name:             kubernetes-bootcamp-5478c6c664-nxc5h
Namespace:        default
Priority:         0
Service Account:  default
Node:             docker-desktop/192.168.65.4
Start Time:       Fri, 30 Jan 2026 12:36:46 +0800
Labels:           app=kubernetes-bootcamp
                  pod-template-hash=5478c6c664
Annotations:      <none>
Status:           Running
IP:               10.1.0.21
IPs:
  IP:           10.1.0.21
Controlled By:  ReplicaSet/kubernetes-bootcamp-5478c6c664
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://4989f6c40f95afecad9c3001fefd8c24d665920112e21dbf864eace95a31ac67
    Image:          docker.io/jocatalin/kubernetes-bootcamp:v2
    Image ID:       docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 30 Jan 2026 12:36:47 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6mvpg (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-6mvpg:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
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  80m   default-scheduler  Successfully assigned default/kubernetes-bootcamp-5478c6c664-nxc5h to docker-desktop
  Normal  Pulled     80m   kubelet            Container image "docker.io/jocatalin/kubernetes-bootcamp:v2" already present on machine
  Normal  Created    80m   kubelet            Created container kubernetes-bootcamp
  Normal  Started    80m   kubelet            Started container kubernetes-bootcamp


Name:             kubernetes-bootcamp-5478c6c664-vk6gn
Namespace:        default
Priority:         0
Service Account:  default
Node:             docker-desktop/192.168.65.4
Start Time:       Fri, 30 Jan 2026 12:36:47 +0800
Labels:           app=kubernetes-bootcamp
                  pod-template-hash=5478c6c664
Annotations:      <none>
Status:           Running
IP:               10.1.0.22
IPs:
  IP:           10.1.0.22
Controlled By:  ReplicaSet/kubernetes-bootcamp-5478c6c664
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://1f00674f52fec884f55e53641d430aae76d7bbdb01d9a8b8903c40aeb5afce3c
    Image:          docker.io/jocatalin/kubernetes-bootcamp:v2
    Image ID:       docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Fri, 30 Jan 2026 12:36:48 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-l9rz7 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-l9rz7:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
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  80m   default-scheduler  Successfully assigned default/kubernetes-bootcamp-5478c6c664-vk6gn to docker-desktop
  Normal  Pulled     80m   kubelet            Container image "docker.io/jocatalin/kubernetes-bootcamp:v2" already present on machine
  Normal  Created    80m   kubelet            Created container kubernetes-bootcamp
  Normal  Started    80m   kubelet            Started container kubernetes-bootcamp
ccc@cccs-MBP ~ %

最佳实践和注意事项

  1. 多实例部署:滚动更新要求应用至少运行多个实例,这是实现零停机更新的前提

  2. 健康检查:确保配置了适当的就绪探针,以便Kubernetes准确判断Pod何时准备好接收流量

  3. 更新策略配置:

    • maxUnavailable:更新期间允许的最大不可用Pod数量(默认1)

    • maxSurge:更新期间允许创建的最大新Pod数量(默认1)

  4. 渐进式验证:更新后逐步验证应用功能,而不是一次性完全切换流量

  5. 监控和告警:在更新期间密切监控应用性能指标和错误率

总结

滚动更新是Kubernetes部署策略的核心组成部分,它使开发团队能够频繁、可靠地交付新版本,同时保证应用的高可用性。掌握滚动更新及其回滚机制,对于在生产环境中管理Kubernetes应用至关重要。

更多推荐