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

12/28/2017

I am creating a release definition in VSTS where i need to fetch just the external ip of the service which is getting deployed using Shell Script. I am able to do it from command line but not from VSTS release definition. I have got the JSON stored in a variable which consists of the IP. I need to fetch the IP from this JSON.

Or let me know if their is any command for kubernetes which will return just the external ip of the deployed services and not any other information.

-- Vatan Bansal
azure-pipelines-release-pipeline
bash
json
kubernetes
shell

2 Answers

12/29/2017

Try this code:

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

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

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

Read a JSON Value in Bash

You can output the variable value to a JSON file by calling echo {variable} > test.json.

-- starian chen-MSFT
Source: StackOverflow

12/28/2017

Assuming you have a service of type LoadBalancer:

kubectl get svc <your_service> -ao jsonpath='{..ip}'
-- Sebastian
Source: StackOverflow