Kubernetes API:列出带有标签的 Pod

No1*_*ver 7 kubernetes kubectl

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

这就是它的完成方式kubectl

kdev get pods -l my-label=yes
Run Code Online (Sandbox Code Playgroud)

它正在工作。

现在我想用 Kubernetes API 来实现。这是我得到的最接近的点:

curl https://kubernetes.default.svc/api/v1/namespaces/XXX/pods --silent --header "Authorization: Bearer $TOKEN" --insecure
Run Code Online (Sandbox Code Playgroud)

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

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

Arg*_*dhu 16

是否可以将 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]
Run Code Online (Sandbox Code Playgroud)


No1*_*ver 8

找到了。

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