Created
May 18, 2026 12:38
-
-
Save nijave/48fb7ba53b49e301d5f57aaa4a461db3 to your computer and use it in GitHub Desktop.
"ssh" into a Kubernetes node with a privileged pod and chroot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| kssh () { | |
| pod_name=nick-ssh | |
| kubectl delete pod $pod_name | |
| k8s_node=$1 | |
| cat <<EOF | kubectl create -f - | |
| apiVersion: v1 | |
| kind: Pod | |
| metadata: | |
| name: "${pod_name}" | |
| spec: | |
| restartPolicy: Never | |
| hostNetwork: true | |
| hostPID: true | |
| containers: | |
| - name: ubuntu | |
| image: ubuntu:22.04 | |
| command: [sleep] | |
| args: [infinity] | |
| securityContext: | |
| privileged: true | |
| volumeMounts: | |
| - name: host | |
| mountPath: /host | |
| volumes: | |
| - name: host | |
| hostPath: | |
| path: / | |
| type: Directory | |
| nodeName: "${k8s_node}" | |
| EOF | |
| kubectl wait --for=condition=Ready=true "pod/${pod_name}" | |
| kubectl exec -i --tty "${pod_name}" -- chroot /host /bin/bash | |
| kubectl delete pod --wait=false $pod_name | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment