简体   繁体   English

如何获取 kubernetes 服务的集群 IP

[英]How to get the cluster IP of a kubernetes service

I've created a service which wraps an nfs, and I need to get the cluster IP for it so that I can set it to persistent volumne using it.我创建了一个包装 nfs 的服务,我需要为它获取集群 IP,以便我可以使用它将它设置为持久卷。

I know I can use the following to get this:我知道我可以使用以下方法来获得它:

$ 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

What I want to know is, how do I extract that cluster IP in a bash script?我想知道的是,如何在 bash 脚本中提取该集群 IP? It's generated as part of a deployment process, so I can't just type it in to my persistent volume manifest.它是作为部署过程的一部分生成的,所以我不能只是将它输入到我的持久卷清单中。

You can parse the of kubectl get svc command something like below to get the inner details of the deployment.您可以像下面这样解析kubectl get svc命令以获取部署的内部细节。

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

Alternatively, you can try any combination of shell hacks involving cut and awk .或者,您可以尝试任何涉及cut and awk的 shell hack 组合。 One such example is;一个这样的例子是;

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

The previous answer using go-template output was failing.上一个使用go-template输出的答案失败了。 Using jsonpath worked for me:使用jsonpath对我jsonpath

#!/bin/bash

cluster_ip=$(kubectl get svc nfs-server -ojsonpath='{.spec.clusterIP}')
echo $cluster_ip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM