首页 > 解决方案 > 如何解析 kubectl describe 输出并获取所需的字段值

问题描述

我正在尝试使用 kubectl describe 命令从特定 pod 获取 Nodeport。我从这个问题中了解到 -o 选项不适用于描述,因此我正在尝试以下方式,但我没有得到所需的值,有人可以纠正我。

kubectl -n core describe svc/pg-debug
Name:                     pg-debug
Namespace:                core
Labels:                   <none>
Annotations:              <none>
Selector:                 app=postgresql-default
Type:                     NodePort
IP:                       172.17.17.19
Port:                     <unset>  5432/TCP
TargetPort:               5432/TCP
NodePort:                 <unset>  24918/TCP
Endpoints:                172.16.90.10:5432
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

以下是我试图获取值的命令"24918"

kubectl -n core describe svc/pg-debug | grep NodePort |awk -F:  '/nodePort/{gsub(/ /,"",$2)}'

标签: kuberneteskubectl

解决方案


您可以使用以下命令从服务中获取 nodePort

kubectl get svc pg-debug -n core -o jsonpath='{.spec.ports[].nodePort}'

参考:


推荐阅读