Skip to content

Instantly share code, notes, and snippets.

@anxolin
Last active December 6, 2019 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anxolin/3a8f83de7acf96e7c8d0c3dd94c25838 to your computer and use it in GitHub Desktop.
Save anxolin/3a8f83de7acf96e7c8d0c3dd94c25838 to your computer and use it in GitHub Desktop.
Get Kubernetes exposed service IP/Port and run bussy box
#!/usr/bin/env sh
SERVICE=${1:-web}
echo "Getting service IP and PORT for Service '$SERVICE'..."
# Get IP / Port
SERVICE_IP=$(kubectl get service $SERVICE -o go-template='{{.spec.clusterIP}}')
SERVICE_PORT=$(kubectl get service $SERVICE -o go-template='{{(index .spec.ports 0).port}}')
if [[ ! -z $SERVICE_IP && ! -z $SERVICE_PORT ]]; then
echo "Service '$SERVICE' is exposed in $SERVICE_IP:$SERVICE_PORT"
# Run bussy box
echo "Running bussy box..."
kubectl run busybox \
--generator=run-pod/v1 \
--image=busybox \
--restart=Never \
--tty \
-i \
--env "SERVICE_IP=$SERVICE_IP" \
--env "SERVICE_PORT=$SERVICE_PORT"
exit 0
else
echo "No IP/Port was assigned to '$SERVICE'. Check services:"
echo "\tkubectl get services"
echo "\tkubectl get service $SERVICE"
echo "\tkubectl describe service $SERVICE"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment