【问题标题】:How to make a k8s pod exits itself when the main container exits?主容器退出时如何让k8s pod自行退出?
【发布时间】:2020-01-25 01:58:58
【问题描述】:

我正在为一个 k8s pod 使用 sidecard 模式,其中有两个容器:主容器和 sidecar 容器。我想让 pod 状态仅取决于主容器(例如,如果主容器失败/完成,则 pod 应该处于相同状态)并丢弃边卡容器。

有没有优雅的方法来做到这一点?

【问题讨论】:

  • 你能把 sidecar 的 liveness probe 改为 no-op 吗?
  • 您能分享一下您的经验吗?最后 - 如何解决这个问题?

标签: kubernetes microservices kubernetes-pod


【解决方案1】:

不幸的是,restartPolicy 标志适用于 pod 中的所有容器,因此简单的解决方案实际上并不能奏效。你确定你的逻辑不应该在 initContainer 而不是 sidecar 中吗?如果它确实需要成为一个边车,让它在你的逻辑结束时永远休眠。

【讨论】:

    【解决方案2】:

    根据documentation

    Pod 正在运行并且有两个容器。容器 1 失败退出。

    记录失败事件。

    如果 restartPolicy 是:

    • 总是:重启容器; Pod 阶段保持正在运行
    • OnFailure:重启容器; Pod 阶段保持运行
    • 从不:不重启Container; Pod 阶段保持运行

    如果容器 1 没有运行,而容器 2 退出:

    记录失败事件。

    如果restartPolicy是:

    • 总是:重启容器; Pod 阶段保持运行
    • OnFailure:重启容器; Pod 阶段保持运行
    • 从不:Pod 阶段变为失败

    作为解决方法(此问题的部分解决方案)restartPolicy:Never - 您可以将 livenees 探测的结果从主容器应用到 sidecar 容器(使用 exec、http 或 tcp 探测)。

    在使用微服务时,这不是一个好的解决方案。

    示例:

    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        test: liveness
      name: liveness-exec
    spec:
      containers:
      - name: liveness1
        image: k8s.gcr.io/busybox
        args:
        - /bin/sh
        - -c
        - touch /test-pd/healthy; sleep 30; rm -rf /test-pd/healthy; sleep 30
        livenessProbe:
          exec:
            command:
            - cat
            - /test-pd/healthy
          initialDelaySeconds: 5
          periodSeconds: 5
        volumeMounts:
        - mountPath: /test-pd
          name: test-volume
      - name: liveness2
        image: k8s.gcr.io/busybox
        args:
        - /bin/sh
        - -c
        - sleep 120
        livenessProbe:
          exec:
            command:
            - cat
            - /test-pd2/healthy
          initialDelaySeconds: 5
          periodSeconds: 5
        volumeMounts:
        - mountPath: /test-pd2
          name: test-volume
      restartPolicy: Never
      volumes:
      - name: test-volume
        hostPath:
          # directory location on host
          path: /data
          type: Directory
    

    如果有帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-26
      • 2021-07-09
      • 1970-01-01
      • 2017-11-30
      • 2021-02-17
      • 2019-09-26
      • 2015-04-30
      • 2023-04-10
      相关资源
      最近更新 更多