【问题标题】:Kubectl wait command for init containers初始化容器的 Kubectl 等待命令
【发布时间】:2020-05-14 03:22:20
【问题描述】:

我正在寻找用于初始化容器的 Kubectl 等待命令。基本上我需要等到 init 容器的 pod 初始化,然后再继续我的脚本中的下一步。

我可以看到 pod 的等待选项,但不是特定于 init 容器。

任何线索如何实现这一点

请建议任何其他在脚本中等待的方法

【问题讨论】:

    标签: kubernetes containers


    【解决方案1】:

    您可以在 init 容器或多个 init 容器中运行多个命令来达到目的。


    • 多个命令
    command: ["/bin/sh","-c"]
    args: ["command one; command two && command three"]
    

    参考:https://stackoverflow.com/a/33888424/3514300


    * 多个初始化容器

    apiVersion: v1
    kind: Pod
    metadata:
      name: myapp-pod
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp-container
        image: busybox:1.28
        command: ['sh', '-c', 'echo The app is running! && sleep 3600']
      initContainers:
      - name: init-myservice
        image: busybox:1.28
        command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
      - name: init-mydb
        image: busybox:1.28
        command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
    

    参考:https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#init-containers-in-use

    【讨论】:

    • 感谢您的建议...如果 command1 失败,那么我应该立即使用 Kubectl logs -f podname -c initcontainer 显示日志...我如何实现这一点...请注意我无法执行手动..我需要自动化这个
    • 最简单的解决方案是始终显示日志,而不仅仅是在初始化容器失败时显示日志
    • 如果您只想在 init 容器失败时显示日志,您可能需要围绕 init pod kubernetes.io/docs/tasks/debug-application-cluster/… 的状态编写一个包装脚本
    • 是的,我可以借助脚本显示日志,但是这里的挑战我不知道我的 init 容器何时成功或失败。这就是我正在为 Init 容器寻找等待命令的主要原因
    • 描述pod时可以获取init容器的状态,参考:kubernetes.io/docs/tasks/debug-application-cluster/…
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2023-04-01
    • 1970-01-01
    • 2012-12-08
    • 2016-07-27
    • 2019-03-27
    • 1970-01-01
    • 2020-06-29
    相关资源
    最近更新 更多