为什么 .spec.containers.ports.containerPort 返回

huangapple go评论73阅读模式
英文:

Troubleshooting kubectl get pods command: Why is .spec.containers.ports.containerPort returning <none>?

问题

我正在使用 kubectl get pods -o custom-columns=&quot;POD_IP&quot;:.status.podIPs 命令获取分配给 pod 的 IP 地址。

而基于相同的方法,我正在使用 kubectl get pods -o custom-columns=&quot;POD_PORT&quot;:.spec.containers.ports.containerPort 命令获取端口号,但是它为空白

英文:

I am getting the IP address assigned to the pod using kubectl get pods -o custom-columns=&quot;POD_IP&quot;:.status.podIPs command.

And based on same approach, I am using kubectl get pods -o custom-columns=&quot;POD_PORT&quot;:.spec.containers.ports.containerPort command to get the port number but it is coming as blank.

cloudshell:~$ kubectl get pods -o custom-columns=&quot;POD_IP&quot;:.status.podIPs
POD_IP
[map[ip:10.32.0.194]]

cloudshell:~$ kubectl get pods -o custom-columns=&quot;POD_PORT&quot;:.spec.containers.ports.containerPort
POD_PORT
&lt;none&gt;

cloudshell:~$ kubectl get pods -o custom-columns=&quot;POD_PORT&quot;:.spec.containers
POD_PORT
[map[image:nginx:1.10.1 imagePullPolicy:IfNotPresent name:servic1 ports:[map[containerPort:8080 protocol:TCP]] resources:map[limits:map[cpu:500m ephemeral-storage:1Gi memory:2Gi] requests:map[cpu:500m ephemeral-storage:1Gi memory:2Gi]] securityContext:map[capabilities:map[drop:[NET_RAW]]] terminationMessagePath:/dev/termination-log terminationMessagePolicy:File volumeMounts:[map[mountPath:/var/run/secrets/kubernetes.io/serviceaccount name:kube-api-access-mgk8k readOnly:true]]]]

cloudshell:~$ 

I have tried to use kubectl get pods -o custom-columns=&quot;Port Number of Pod&quot;:.spec.containers command and I can see that my mapping (.spec.containers.ports.containerPort) is correct but somehow it is still not working.

I am totally sure that .spec.containers.ports.containerPort mapping correct, and same command format is giving IP address, so not able to catch what is wrong.

Is anyone able to catch what is wrong here?

答案1

得分: 5

尝试:

kubectl get pods \
--output=custom-columns=\
"POD_PORT":.spec.containers[*].ports[*].containerPort

你也可以包含 .metadata.name 以增强清晰度:

kubectl get pods \
--output=custom-columns=\
"NAME":.metadata.name,\
"POD_PORT":.spec.containers[*].ports[*].containerPort

虽然它没有明确(!?)文档化,但我怀疑格式是 kubectl 的 JSONPath,在这里(似乎).spec.containers[].spec.containers[*] 之间存在微妙的区别,前者在属性未找到时停止,而后者包括一切。

因为 .spec 总是包含一个或多个 .containers,但每个 container 可能没有 .ports,你也可以:

kubectl get pods \
--output=custom-columns=\
"POD_PORT":.spec.containers[].ports[*].containerPort

这使用 containers[]ports[*] 产生相同效果。

注意容器 v1 核心中所解释的那样,请查看 "ports",容器暴露的端口不一定要通过 ports 指定,即此命令将返回文档化的端口,但此列表可能不包括容器暴露的端口(未经文档化的端口)。

英文:

Try:

kubectl get pods \
--output=custom-columns=\
&quot;POD_PORT&quot;:.spec.containers[*].ports[*].containerPort

You can include .metadata.name too to aid clarity:

kubectl get pods \
--output=custom-columns=\
&quot;NAME&quot;:.metadata.name,\
&quot;POD_PORT&quot;:.spec.containers[*].ports[*].containerPort

It's not clearly (!?) documented but I suspect the format is kubectl's JSONPath and there (appears to be) a subtle distinction between e.g. .spec.containers[] and .spec.containers[*] where the former stops when the property is not found and the latter includes everything.

Because .spec will always include one or more .containers, but each container may not have .ports, you can also:

kubectl get pods \
--output=custom-columns=\
&quot;POD_PORT&quot;:.spec.containers[].ports[*].containerPort

Which containers[] but ports[*] to the same effect.

NOTE as explained in Container v1 core see "ports", ports that are exposed by the container need not be specified through ports i.e. this command will return documented ports but this list may exclude ports that are exposed by the containers (and not documented).

huangapple
  • 本文由 发表于 2023年5月30日 01:29:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定