How to Execute Commands on a Running Container in Kubernetes

This blog post will show how to execute shell commands on a running container hosted on a Kubernetes cluster.

In my case, I am using Azure AKS cluster. However, this process will work on any Kubernetes system.

Kubectl Exec

In the below examples, I am running the Pd -l command on my MariaDB container.

kubectl exec -t  deploycontainers-mariadb-0 -- ls -l

If bash is not available on the container, you can attach to a running container active process using the below attach command.

kubectl attach -t deploycontainers-mariadb-0