Shell into Kubernetes Pod
Syntax:
1kubectl exec --stdin --tty <pod_name> -- /bin/bash
Example, with the following pods
1me@example:~$ kubectl get pod
2NAME READY STATUS RESTARTS AGE
3cert-manager-cainjector-68c88cc858-c2rhx 1/1 Running 0 109m
4cert-manager-d76d644df-pgqn9 1/1 Running 0 109m
5cert-manager-webhook-6dd68c6fbc-rzcln 1/1 Running 0 109m
6my-first-vanilla-drupal-6f6dd66b94-xldj7 1/1 Running 0 108m
7my-first-vanilla-drupal-mariadb-0 1/1 Running 0 108m
8traffic-cop-nginx-ingress-controller-5d685cfd59-pjfdg 1/1 Running 0 144m
9traffic-cop-nginx-ingress-default-backend-55f6bcb48-brg6b 1/1 Running 0 144m
Run this to go to the drupal pod my-first-vanilla-drupal-6f6dd66b94-xldj7
1kubectl exec --stdin --tty my-first-vanilla-drupal-6f6dd66b94-xldj7 -- /bin/bash
It's just like any docker container or linux machine when you're in:
1me@example:~$ kubectl exec --stdin --tty my-first-vanilla-drupal-6f6dd66b94-xldj7 -- /bin/bash
2I have no name!@my-first-vanilla-drupal-6f6dd66b94-xldj7:/$ ls
3bin bitnami boot dev etc home lib lib64 media mnt opt post-init.d post-init.sh proc root run sbin srv sys tmp usr var
4I have no name!@my-first-vanilla-drupal-6f6dd66b94-xldj7:/$ ps aux
5USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
61001 1 0.0 2.0 298024 41036 ? Ss 18:19 0:00 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
71001 189 0.0 1.6 300912 34116 ? S 18:20 0:02 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
81001 190 0.0 2.6 302344 53684 ? S 18:20 0:05 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
91001 191 0.0 1.7 300912 35488 ? S 18:20 0:02 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
101001 192 0.0 2.2 300932 45772 ? S 18:20 0:02 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
111001 193 0.0 1.6 300896 33880 ? S 18:20 0:02 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
121001 194 0.0 1.6 300912 34220 ? S 18:20 0:02 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
131001 195 0.0 1.7 300896 35356 ? S 18:24 0:02 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
141001 196 0.0 1.6 300808 34028 ? S 18:25 0:02 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
151001 197 0.0 2.3 301072 47032 ? S 18:25 0:02 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
161001 198 0.0 2.5 301280 51712 ? S 18:25 0:03 /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -D FOREGROUND
171001 199 0.0 0.1 4004 3324 pts/0 Ss 20:10 0:00 /bin/bash
181001 205 0.0 0.1 7644 2716 pts/0 R+ 20:10 0:00 ps aux
19I have no name!@my-first-vanilla-drupal-6f6dd66b94-xldj7:/$
For those without shell, you would see an error like:
1me@example:~$ kubectl exec --stdin --tty cert-manager-cainjector-68c88cc858-c2rhx -- /bin/bash
2OCI runtime exec failed: exec failed: container_linux.go:349: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown
3command terminated with exit code 126
More details in https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/