【问题标题】:How to get the cluster IP of a kubernetes service如何获取 Kubernetes 服务的集群 IP
【发布时间】:2019-03-08 14:31:48
【问题描述】:

我创建了一个包装 nfs 的服务,我需要为其获取集群 IP,以便我可以使用它将其设置为持久卷。

我知道我可以使用以下方法来获得:

$ kubectl get svc nfs-server
NAME                  TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
nfs-server   ClusterIP   10.59.243.58   <none>        2049/TCP,20048/TCP,111/TCP   2m

我想知道的是,如何在 bash 脚本中提取该集群 IP?它是作为部署过程的一部分生成的,所以我不能只将它输入到我的持久卷清单中。

【问题讨论】:

    标签: kubernetes google-kubernetes-engine kubectl


    【解决方案1】:

    您可以像下面这样解析kubectl get svc 命令以获取部署的内部详细信息。

    export CLUSTER_IP=$(kubectl get services/nfs-server -o go-template='{{(index.spec.clusterIP)}}');echo CLUSTER_IP=$CLUSTER_IP
    

    或者,您可以尝试任何涉及cut and awk 的shell hack 组合。一个这样的例子是;

    kubectl describe svc/nfs-server | grep IP: | awk '{print $2;}'
    

    【讨论】:

    • export CLUSTER_IP 命令给了我error: error executing template "{{(index.spec.clusterIP)}}": template: output:1:3: executing "output" at &lt;index&gt;: wrong number of args for index: want at least 1 got 0。第二个命令虽然有效
    【解决方案2】:

    先前使用go-template 输出的答案失败。使用 jsonpath 对我有用:

    #!/bin/bash
    
    cluster_ip=$(kubectl get svc nfs-server -ojsonpath='{.spec.clusterIP}')
    echo $cluster_ip
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 2015-11-19
      • 2017-10-26
      相关资源
      最近更新 更多