我需要 shell 脚本来获取部署在 kubernetes 集群上的服务的 ip

I need shell script inorder to fetch the ip of services deployed on kubernetes cluster

我正在 VSTS 中创建发布定义,我只需要获取使用 Shell 脚本 部署的服务的外部 IP。我可以从命令行执行此操作,但不能从 VSTS 发布定义执行。我已经将 JSON 存储在一个由 IP 组成的变量中。我需要从这个 JSON.

获取 IP

或者让我知道它们是否是 kubernetes 的任何 命令,它将 return 只是已部署服务的外部 ip 而不是任何其他信息。

假设您有 LoadBalancer 类型的服务:

kubectl get svc <your_service> -ao jsonpath='{..ip}'

试试这个代码:

function readJson {  
  UNAMESTR=`uname`
  if [[ "$UNAMESTR" == 'Linux' ]]; then
    SED_EXTENDED='-r'
  elif [[ "$UNAMESTR" == 'Darwin' ]]; then
    SED_EXTENDED='-E'
  fi; 

  VALUE=`grep -m 1 "\"\""  | sed ${SED_EXTENDED} 's/^ *//;s/.*: *"//;s/",?//'`

  if [ ! "$VALUE" ]; then
    echo "Error: Cannot find \"\" in " >&2;
    exit 1;
  else
    echo $VALUE ;
  fi; 
}
NAME=`readJson package.json name` || exit 1;  
# $NAME is "sample-project"

Read a JSON Value in Bash

您可以通过调用 echo {variable} > test.json.

将变量值输出到 JSON 文件