简体   繁体   English

Kubectl 命令列出 Kubernetes 中部署的 pod

[英]Kubectl command to list pods of a deployment in Kubernetes

Is there a way to use kubectl to list only the pods belonging to a deployment?有没有办法使用kubectl仅列出属于部署的 pod? Currently, I do this to get pods:目前,我这样做是为了获取 pod:

kubectl get pods| grep hello

But it seems an overkill to get ALL the pods when I am interested to know only the pods for a given deployment.但是,当我有兴趣只知道给定部署的 pod 时,获取所有 pod 似乎有点过头了。 I use the output of this command to see the status of all pods, and then possibly exec into one of them.我使用此命令的 output 来查看所有 Pod 的状态,然后可能执行到其中一个。

I also tried kc get -o wide deployments hellodeployment , but it does not print the Pod names.我还尝试kc get -o wide deployments hellodeployment ,但它不打印 Pod 名称。

There's a label in the pod for the selector in the deployment.部署中的选择器在 pod 中有一个标签 That's how a deployment manages its pods.这就是部署管理其 pod 的方式。 For example for the label or selector app=http-svc you can do something like that this and avoid using grep and listing all the pods (this becomes useful as your number of pods becomes very large):例如,对于标签或选择器app=http-svc您可以执行类似的操作并避免使用grep并列出所有 pod(当您的 pod 数量变得非常大时,这会变得很有用):

$ kubectl get pods -l=app=http-svc

or或者

$ kubectl get pods --selector=app=http-svc

K8s components are linked to each other by labels and selectors. K8s 组件通过标签和选择器相互链接。 There are just no built-in attributes of My-List-of-ReplicaSets or My-List-Of-Pods for a deployment.部署没有 My-List-of-ReplicaSets 或 My-List-Of-Pods 的内置属性。 You can't get them from kubectl describe or kubectl get您无法从kubectl describekubectl get获取它们

As @Rico suggested above, you have to use label filters.正如上面@Rico 建议的那样,您必须使用标签过滤器。 But you can't simply use the labels that you specify in the deployment metafile because deployment will generate a random hash and use it as an additional label.但是您不能简单地使用您在部署元文件中指定的标签,因为部署将生成一个随机散列并将其用作附加标签。

For example, I have a deployment and a standalone pod that share the same label app=http-svc .例如,我有一个部署和一个独立的 pod,它们共享相同的标签app=http-svc While the first two are managed by the deployment, the 3rd one is not and shouldn't be in the result.虽然前两个由部署管理,但第三个不是也不应该出现在结果中。


ma.chi@~/k8s/deployments % kubectl get pods --show-labels
NAME                   READY   STATUS    RESTARTS   AGE   LABELS
http-9c89b5578-6cqbp   1/1     Running   0          7s    app=http-svc,pod-template-hash=574561134
http-9c89b5578-vwqbx   1/1     Running   0          7s    app=http-svc,pod-template-hash=574561134
nginx-standalone       1/1     Running   0          7s    app=http-svc
ma.chi@~/k8s/deployments %

The source file is源文件是

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: http-svc
  name: http
spec:
  replicas: 2
  selector:
    matchLabels:
      app: http-svc
  strategy: {}
  template:
    metadata:
      labels:
        app: http-svc
    spec:
      containers:
      - image: nginx:1.9.1
        name: nginx1

---

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: http-svc
  name: nginx-standalone
spec:
  containers:
  - image: nginx:1.9.1
    name: nginx1-standalone

To exact spot the containers created and managed by your deployment, you can use the script below(which is ugly, but this is the best I can do)要准确定位由您的部署创建和管理的容器,您可以使用下面的脚本(这很丑陋,但这是我能做的最好的)

DEPLOY_NAME=http
RS_NAME=`kubectl describe deployment $DEPLOY_NAME|grep "^NewReplicaSet"|awk '{print $2}'`; echo $RS_NAME

POD_HASH_LABEL=`kubectl get rs $RS_NAME -o jsonpath="{.metadata.labels.pod-template-hash}"` ; echo $POD_HASH_LABEL

POD_NAMES=`kubectl get pods -l pod-template-hash=$POD_HASH_LABEL --show-labels | tail -n +2 | awk '{print $1}'`; echo $POD_NAMES

Here's a tidier shell alias (based on this code by @kekaoyunfuwu) that only lists the pods of a deployment (no interim results are shown):这是一个更简洁的 shell 别名(基于@kekaoyunfuwu 的此代码),它仅列出了部署的 pod(未显示中间结果):

k_list_pods_in_deployment() {
  test $# -eq 0 && {
    echo "Missing deployment name" && kubectl get deployments
    return 1
  }
  local deployment="$1"; shift
  local replicaSet="$(kubectl describe deployment $deployment \
    | grep '^NewReplicaSet' \
    | awk '{print $2}'
  )"

  local podHashLabel="$(kubectl get rs $replicaSet \
    -o jsonpath='{.metadata.labels.pod-template-hash}'
  )"

  kubectl get pods -l pod-template-hash=$podHashLabel --show-labels \
    | tail -n +2 | awk '{print $1}'

}
alias k.list-pods-in-deployment=k_list_pods_in_deployment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM