k8s 检查等待 pod 是否存在

k8s check wait for pod existence

在 yaml(带 helm)中声明的 K8s 作业文件中,我需要作业将 运行 只有 pod 数据库是否存在并准备就绪。

为了做好准备,它工作正常,因为我添加了以下内容:

initContainers
    - name: wait-mysql-ready
      image: "bitnami//kubectl:latest"
      imagePullPolicy: IfNotPresent
      command:
        - kubectl
      args:
        - wait
        - pod/mysql-pod
        - --for=condition=ready
        - --timeout=120s

它工作正常,但我需要作业 运行 一次(没有重复,并且需要很长时间才能结束)。

工作没有像往常一样运行一次,它的名字,当运行宁kubectl get pods<jobname-hash>

如果它没有 运行 一次,在最后一次尝试时它将成功(因为 pod 尚未创建)。其他尝试可能会失败。

所以我在主要规范中添加了以下几行:

spec:
  parallelism: 1 
  completions: 1
  backoffLimit: 0

在初始化容器部分(在上一个之前),我添加了:

initContainers
  - name: wait-mysql-exist-pod
    image: "bitnami/kubectl:latest"
    imagePullPolicy: IfNotPresent
    command:
      - /bin/sh
    args:
      - -c
      - "while !(kubectl get pod mysql-pod); do echo 'Waiting for mysql pod to be existed...'; sleep 5; done"

(对于 ! - 对于多行字符串,我找不到其他好的语法。我很乐意知道如何做)。

还需要另一个作业等待当前作业

如何创建一个 运行 一次的作业,并在检查就绪状态之前检查 pod 存在的作业?

您可以使用this script作为init-container等待其他资源。