Kubernetes - Mark Pod在容器完成时完成

时间:2017-12-16 17:57:17

标签: kubernetes

假设我有一个包含2个容器的PodAppDatabase。我想运行一个Pod来执行App中的命令,然后终止。

我已经设置了App容器来运行该命令,然后它会成功运行并终止,这很棒。但现在我的Database容器仍在运行,因此Pod未标记为complete

如何在Pod容器完成后将App标记为完整?

1 个答案:

答案 0 :(得分:0)

您可以调用Kubernetes API服务器来完成此操作。请考虑以下示例:

---
apiVersion: v1
kind: Pod
metadata:
  name: multi-container-completion
spec:
  containers:
    - name: long-running-process
      image: fbgrecojr/office-hours:so-47848488
      command: ["sleep", "1000"]
    - name: short-running-process
      image: fbgrecojr/office-hours:so-47848488
      command: ["sleep", "1"]
      lifecycle:
        preStop:
          exec:
            command: ["/pre-stop.sh"]

<强> pre-stop.sh

#!/bin/bash
curl \
  -X DELETE \
  -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
  --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt \
  https://kubernetes.default.svc.cluster.local/api/v1/namespaces/$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace)/pods/$HOSTNAME

Dockerfile fbgrecojr/office-hours:so-47848488

FROM centos:latest
COPY pre-stop.sh /
RUN chmod +x /pre-stop.sh

注意:我无法正确测试,因为preStop挂钩似乎不能用于我的本地Minikube设置。如果此问题未本地化,我可以跟踪相应的问题here