Skip to content

Instantly share code, notes, and snippets.

@elgalu
Forked from snstanton/debug.sh
Created August 18, 2021 19:14
Show Gist options
  • Save elgalu/94cccf2e7ab876f9c56487bbbea3d7e5 to your computer and use it in GitHub Desktop.
Save elgalu/94cccf2e7ab876f9c56487bbbea3d7e5 to your computer and use it in GitHub Desktop.
Remote debugging k8s with pycharm
job_name=debug-`date +%Y%m%d%H%M%S`
kubectl create job ${job_name} --from cj/myveeva-v118273-app-maint
kubectl label pod -l job-name=${job_name} run=debug
kubectl wait --for condition=Ready pod -l job-name=${job_name}
pod_name=`kubectl get pods -l job-name=${job_name} -o jsonpath='{.items[0].metadata.name}'`
echo "installing tools"
kubectl exec ${pod_name} -- sh -c "apk -q update; apk -q add bash curl jq openssh rsync vim unzip postgresql-client; pip install -q ipython"
tar cf - -s '/profile.sh/.bashrc/' profile.sh | kubectl exec -i ${pod_name} -- tar xf - -C /root
kubectl exec ${pod_name} -- env > ~/.env
if [[ -d "~/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/211.7628.24/PyCharm 2021.1 EAP.app/Contents/plugins/python/helpers" ]]
then
echo "Copying pycharm helpers"
tar cpf - -j -C "/Users/scotts/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/211.7628.24/PyCharm 2021.1 EAP.app/Contents/plugins/python/helpers" . | kubectl exec -i ${pod_name} -- sh -c "mkdir -p /root/.helpers
tar xf - -j -C /root/.helpers"
fi
echo "copying ssh key"
tar cf - -C ~ -s /id_rsa.pub/authorized_keys/ .ssh/id_rsa.pub | kubectl exec -i job/${job_name} -- tar xf - -C /root
tar cf - sshd_config | kubectl exec -i job/${job_name} -- tar xf - -C /etc/ssh
# Set up port forwarding
kill -INT `lsof -t -i tcp:2222` 2>/dev/null
kubectl port-forward job/${job_name} 2222:22 & PFPID=$!
function finish_ssh {
kill ${PFPID}
}
trap finish_ssh EXIT INT
echo "starting sshd"
kubectl exec -i job/${job_name} -- bash -c "chown root /root/.ssh/authorized_keys && chmod 0600 /root/.ssh/authorized_keys && echo 'root:*' | chpasswd --encrypted && ssh-keygen -q -f /etc/ssh/ssh_host_rsa_key -N '' && /usr/sbin/sshd"
kubectl exec -ti job/${job_name} -- bash
kubectl delete job ${job_name}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment