【问题标题】:Kubernetes. Finding initContainers in pods in a particular namespaceKubernetes。在特定命名空间的 pod 中查找 initContainers
【发布时间】:2021-09-06 08:13:22
【问题描述】:

我最近加入了 Kubernetes 的一个新项目。最后一个团队似乎没有很好地管理部署,并且一些任务是通过运行在 init 容器中的单个 pod 来管理的。

因此,例如,我们在其中有命名空间“test”和 pod - 其中一些是手动运行的,除了其他团队成员的部署并包含initContainers。我需要找到一个包含 init 容器的特定 pod - 获取他的名字、清单等。

Kubernetes 文档中的备忘单提出了一个获取容器 ID 的解决方案:

kubectl get pods -n test -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3

它给了我一个InitContainer的ID。

当我尝试获取它的名称和相应的 pod 名称时,我尝试使用此代码 sn-p:

kubectl get pod -n test -o jsonpath='{range .items[?(@.status.containerStatuses[].containerID=="docker://686bc30be6e870023dcf611f7a7808516e041c892a236e565ba2bd3e0569ff7a")]}{.metadata.name}{end}'

到目前为止,它什么也没给我。

有没有更优雅、更简单的方法在特定命名空间中使用initcontainers 获取 pod 名称?

我也试过这个解决方案:

kubectl get pod -n test -o custom-columns='POD_NAME:metadata.name,INIT_NAME:spec.InitContainers[*].name'

但它什么也不返回。

我现在使用的解决方案是在 bash 中使用“for”循环解析 yaml 输出,但这对我来说听起来不太好。

有什么建议吗?

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    您需要通过initContainerStatuses 改进您的请求,以便仅查找 Init 容器的必要信息:

    kubectl get pod -n <namespace> -o jsonpath='{range .items[?(@.status.initContainerStatuses[].containerID=="docker://<container_id>")]}{.metadata.name}{end}'
    

    例如:

    kubectl get pod -n kube-system -o jsonpath='{range .items[?(@.status.initContainerStatuses[].containerID=="docker://e235d512c3a5472c8f7de6e73c724317639c9132c07193
    cb9")]}{.metadata.name}{end}'
    weave-net-s26tf
    

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 2017-12-11
      • 2020-11-06
      • 1970-01-01
      • 2020-12-11
      • 2021-09-12
      相关资源
      最近更新 更多