如何计算在 bash 中使用 1 个 liner cmd 在 1 个 yaml 文件中创建的容器数量?

how to count num of containers crreated in 1 yaml file with 1 liner cmd in bash?

有一个 k8s pod yaml 文件,它有 3 个容器,这是 yaml 文件的一部分:

Containers:
  apple:
    Container ID:  
    Image:         busybox
    Image ID:      
    Port:          <none>
    Host Port:     <none>
    Command:
      sleep
      4500
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-2ccr2 (ro)
  wine:
    Container ID:  
    Image:         busybox
    Image ID:      
    Port:          <none>
    Host Port:     <none>
    Command:
      sleep
      4500
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-2ccr2 (ro)
  scarlet:
    Container ID:  
    Image:         busybox
    Image ID:      
    Port:          <none>
    Host Port:     <none>
    Command:
      sleep
      4500
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-2ccr2 (ro)

我做了“kubectl describe pod mypod”

我还得数集装箱的数量。 我应该添加什么来从文件中获取容器的数量?像这样:

kubectl describe pod mypod | grep -i containers

但这只能找到关键字。

kubectl describe pod mypod | grep -i containers | wc -l

我只是想要一个漂亮的 eloquent 1 liner cmd 来显示容器的数量。

您可以使用 grep 来计算每个容器都必须拥有一次并且不能与其他密钥混淆的密钥的出现次数;例如 Container ID::

kubectl describe pod mypod | grep -c 'Container ID:'