Skip to content

Instantly share code, notes, and snippets.

@fionn
Created January 3, 2022 05:13
Show Gist options
  • Save fionn/487c06fb068bcfb84d876490c53a0b43 to your computer and use it in GitHub Desktop.
Save fionn/487c06fb068bcfb84d876490c53a0b43 to your computer and use it in GitHub Desktop.
Get a shell on a k8s pod
#!/usr/bin/env bash
# Usage: k8shell namespace/pod.
set -euo pipefail
mapfile -d "/" -t ns_pod <<< "$1"
if [ "${#ns_pod[@]}" == 2 ]; then
ns="${ns_pod[0]}"
pod="${ns_pod[1]%$'\n'}"
elif [ "${#ns_pod[@]}" == 1 ]; then
ns="default"
pod="${ns_pod[0]%$'\n'}"
else
echo "Pod required" 2>&1
exit 1
fi
kubectl exec -n "$ns" -it "$pod" -- /bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment