According to best practices you should always try to have one container per Pod, and this is the most common Kubernetes use case.
However, a Pod can contain multiple containers and the primary reason for this is to support helper applications that assist a primary application.
Typical examples of helper applications are data pullers, data pushers and proxies.
This short note shows how to get Pod containers using a kubectl
command, and also how to get logs and log into the particular container running inside the Pod.
Cool Tip: Get Pods on specific Node in Kubernetes! Read more →
Kubectl: Get Pod Containers
Use one of the commands below to get the Pods and find out the name of the one which containers you want to list:
$ kubectl get pods
$ kubectl get pods --all-namespaces
$ kubectl get pods --namespace <namespaceName>
- sample output -
NAME READY STATUS RESTARTS AGE
runner-ctrl-71c8ff88-bc9pq 2/2 Running 0 5m5s
To get the Pod containers, execute the following command and search for the “Containers”:
$ kubectl describe pod <podName> - example - $ kubectl describe pod runner-ctrl-71c8ff88-bc9pq - sample output - ... Containers: runner: ← Container ID: ... Image: ... ... proxy: ← Container ID: ... Image: ... ... ...
To list the names only of the containers running inside the Pod, execute:
$ kubectl get pod <podName> -o jsonpath='{.spec.containers[*].name}' - example - $ kubectl get pod runner-ctrl-71c8ff88-bc9pq -o jsonpath='{.spec.containers[*].name}' - sample output - runner ← proxy ←
Cool Tip: Get Pod’s logs using the kubectl
command! Read more →
Get Pod Container Logs
If you know the names of the containers running inside the Pod, you can check the logs of the particular container:
$ kubectl logs pod/<podName> -c <containerName>
- example -
$ kubectl logs pod/runner-ctrl-71c8ff88-bc9pq -c runner
Log Into Pod Container
Also you can log into the particular container and start a shell sessions inside it using the kubectl exec
command:
$ kubectl exec -it <podName> -c <containerName> -- /bin/bash
$ kubectl exec -it <podName> -c <containerName> -- /bin/sh
- example -
$ kubectl exec -it runner-ctrl-71c8ff88-bc9pq -c runner -- /bin/sh