Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gabrielfalcao/1859c235769bc4c30dff347117ff3911 to your computer and use it in GitHub Desktop.
Save gabrielfalcao/1859c235769bc4c30dff347117ff3911 to your computer and use it in GitHub Desktop.
shell script to forward all the kubernetes service ports to localhost while automatically handling port numbers < 1024 (e.g.: 443 to 8443)
#!/usr/bin/env bash
for json in $(kubectl get svc -o json | jq -c '.items[] | select(.kind == "Service") | {name:.metadata.name, port:.spec.ports[0].port}'); do
service=$(echo "${json}" | jq .name | tr -d '"')
remote_port=$(echo "${json}" | jq .port | tr -d '"')
local_port=$((remote_port))
if [ $((local_port)) -lt 1024 ]; then
local_port=$((local_port + 8000))
fi
echo kubectl port-forward "service/${service}" "${local_port}:${remote_port}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment