【问题标题】:Kubernetes API: list pods with a labelKubernetes API:列出带有标签的 pod
【发布时间】:2020-11-19 23:54:27
【问题描述】:

我的命名空间很少部署。其中一个部署具有特定标签 (my-label=yes)。我想获取所有带有这个标签的 pod。

kubectl 是这样处理的:

kdev get pods -l my-label=yes

它正在工作。

现在我想用 Kubernetes API 来做这件事。这是我得到的最近点:

curl https://kubernetes.default.svc/api/v1/namespaces/XXX/pods --silent --header "Authorization: Bearer $TOKEN" --insecure

此命令获取命名空间中的所有 pod。我想将结果过滤到具有此请求标签的所有 pod。怎么做?

更广泛的问题:这是否可以将 kubectl 命令“翻译”为 REST API 调用?

【问题讨论】:

    标签: kubernetes kubectl


    【解决方案1】:

    找到了。

    curl https://kubernetes.default.svc/api/v1/namespaces/XXX/pods?labelSelector=my-label%3Dyes --silent --header "Authorization: Bearer $TOKEN" --insecure
    

    【讨论】:

      【解决方案2】:

      这是否可以将 kubectl 命令“翻译”成 REST API 调用?

      当您使用 kubectl 执行任何命令时,它会在内部转换为带有 json 有效负载的 REST 调用,然后再将其发送到 Kubernetes API 服务器。一个简单的检查方法是运行命令,增加详细程度

      kubectl get pods -n kube-system -l=tier=control-plane --v=8
      I0730 15:21:01.907211    5320 loader.go:375] Config loaded from file:  /Users/arghyasadhu/.kube/config
      I0730 15:21:01.912119    5320 round_trippers.go:420] GET https://xx.xx.xxx.xxx:6443/api/v1/namespaces/kube-system/pods?labelSelector=tier%3Dcontrol-plane&limit=500
      I0730 15:21:01.912135    5320 round_trippers.go:427] Request Headers:
      I0730 15:21:01.912139    5320 round_trippers.go:431]     Accept: application/json;as=Table;v=v1;g=meta.k8s.io,application/json;as=Table;v=v1beta1;g=meta.k8s.io,application/json
      I0730 15:21:01.912143    5320 round_trippers.go:431]     User-Agent: kubectl/v1.18.0 (darwin/amd64) kubernetes/9e99141
      I0730 15:21:02.071778    5320 round_trippers.go:446] Response Status: 200 OK in 159 milliseconds
      I0730 15:21:02.071842    5320 round_trippers.go:449] Response Headers:
      I0730 15:21:02.071858    5320 round_trippers.go:452]     Cache-Control: no-cache, private
      I0730 15:21:02.071865    5320 round_trippers.go:452]     Content-Type: application/json
      I0730 15:21:02.071870    5320 round_trippers.go:452]     Date: Thu, 30 Jul 2020 09:51:02 GMT
      I0730 15:21:02.114281    5320 request.go:1068] Response Body: {"kind":"Table","apiVersion":"meta.k8s.io/v1","metadata":{"selfLink":"/api/v1/namespaces/kube-system/pods","resourceVersion":"1150005"},"columnDefinitions":[{"name":"Name","type":"string","format":"name","description":"Name must be unique within a namespace. Is required when creating resources, although some resources may allow a client to request the generation of an appropriate name automatically. Name is primarily intended for creation idempotence and configuration definition. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names","priority":0},{"name":"Ready","type":"string","format":"","description":"The aggregate readiness state of this pod for accepting traffic.","priority":0},{"name":"Status","type":"string","format":"","description":"The aggregate status of the containers in this pod.","priority":0},{"name":"Restarts","type":"integer","format":"","description":"The number of times the containers in this pod have been restarted.","priority":0},{"name":"Age","type":"strin [truncated 16503 chars]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-11
        • 2022-09-27
        • 2019-02-22
        • 2022-01-02
        • 1970-01-01
        • 1970-01-01
        • 2020-10-16
        • 2020-05-22
        相关资源
        最近更新 更多