Skip to content

Instantly share code, notes, and snippets.

@skipperkongen
Last active July 18, 2019 19:58
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 skipperkongen/24799a5b60b6fb3d47281a891180d3e7 to your computer and use it in GitHub Desktop.
Save skipperkongen/24799a5b60b6fb3d47281a891180d3e7 to your computer and use it in GitHub Desktop.
Get public IP address of nodes in a Kubernetes service

Get the public IP addresses of nodes in a Kubernetes cluster:

kubectl get nodes -o wide

Get the public IP address, in an unnecessarily round-about way, of the node that a particular service is running on:

# Create an nginx deployment and service
kubectl create deployment web --image=nginx
kubectl expose deploy/web --type=NodePort --port=80

# Get pod ID
POD_ID=$(kubectl get pods -l app=web -o json | jq -r '.items[0].metadata.name')

# Get public IP of pod (works because nginx uses ubuntu)
kubectl exec -it $POD_ID -- apt-get update
kubectl exec -it $POD_ID -- apt-get install -y curl 
PUBLIC_IP=$(kubectl exec -it $POD_ID -- curl ifconfig.me)

# Get nodePort
PUBLIC_PORT=$(kubectl get svc web -o json | jq -r '.spec.ports[0].nodePort')

echo "Public IP and port found... $PUBLIC_IP:$PUBLIC_PORT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment