首页 > 解决方案 > k8s yaml文件中标签策略的不同值有什么区别

问题描述

我用两个 yaml 文件进行测试,只有标签策略不同

第一:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    test.k8s: test
  name: test
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        test.k8s: test
    spec:
      containers:
      - name: test
        image: alpine3.6
        imagePullPolicy: IfNotPresent
...

第二:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    test.k8s: test
  name: test
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        test.k8s: test
    spec:
      containers:
      - name: test
        image: alpine3.6
        imagePullPolicy: IfNotPresent
...

然后我使用 kubectl patch 和 kubectl replace 命令更新部署。似乎只有新的 pod 开始时间不同。当新的 pod 启动失败并丢失图像时,旧的 pod 将在这两种情况下最终终止。

有人知道吗?

非常感谢~

标签: kubernetes

解决方案


基本上,.spec.strategy标签指定集群引擎如何用新 Pod 替换旧 Pod。

在您的情况下,.spec.strategy.type==Recreate标签告诉集群引擎在创建新 Pod 之前终止(杀死)所有现有 Pod。

至于第二个例子,.spec.strategy.type==RollingUpdate标签描述了在没有临时中断的情况下更新服务的方法,因为它涉及每次更新一个 pod 以避免服务不可用。

从您的示例中,有两个参数定义了 RollingUpdate 策略:

.spec.strategy.rollingUpdate.maxUnavailable- 表示更新过程中不可用的 Pod 的最大数量。

.spec.strategy.rollingUpdate.maxSurge- 指定在所需 Pod 数量上可以创建的最大 Pod 数量。

您可以考虑在 中使用几个附加参数RollingUpdate,有关更多信息,请参阅文档

通过使用kubectl replace命令,您可以重新创建策略并重建对象,但不能更新。


推荐阅读