A solution to retrieve all containers running in a pod is to run kubectl get pods POD_NAME_HERE -o jsonpath={.spec.containers[*].name}
, however this command line does not provide the init containers.
Is there a way to cleanly retrieve all containers running in a pod, including init containers?
[edit] as svenwltr noted, on Kubernete 1.6.0 or higher, it is possible to retrieve the init container with kubectl get pods POD_NAME_HERE -o jsonpath={.spec.initContainers[*].name}
and all containers can be retrieved with kubectl get pod POD_NAME_HERE -o jsonpath="{.spec['containers','initContainers'][*].name}"
. However, this is not a valid workaround for lower versions of Kubernetes where .spec.initContainers
isn't implemented yet.
Another way to do this is to use kubectl describe pod <POD_NAME_HERE>
. This will print the Init Containers in a separate section from the regular Containers of your pod.
Example:
$ kubectl describe pod myapp-pod
Name: myapp-pod
Namespace: default
[...]
Init Containers:
init-myservice:
Container ID: docker://87c2601c9f18b4e9d63f995e03d83338e1321e016e76798243257d3e682256c2
Image: busybox
Image ID: docker-pullable://busybox@sha256:3841678eb5ddd5b62fec74d7f530fe633ee7cf11d4e3827c0b74e2e4c2a2466f
Port: <none>
Command:
sh
-c
until nslookup myservice; do echo waiting for myservice; sleep 2; done;
State: Running
Started: Thu, 02 Nov 2017 10:54:01 -0400
Ready: False
Restart Count: 0
Requests:
cpu: 100m
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-38lv5 (ro)
init-mydb:
Container ID:
Image: busybox
Image ID:
Port: <none>
Command:
sh
-c
until nslookup mydb; do echo waiting for mydb; sleep 2; done;
State: Waiting
Reason: PodInitializing
Ready: False
Restart Count: 0
Requests:
cpu: 100m
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-38lv5 (ro)
Containers:
myapp-container:
Container ID:
Image: busybox
Image ID:
Port: <none>
Command:
sh
-c
echo The app is running! && sleep 3600
State: Waiting
Reason: PodInitializing
Ready: False
Restart Count: 0
Requests:
cpu: 100m
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-38lv5 (ro)
[...]
The init containers are stored in spec.initContainers
:
kubectl get pods POD_NAME_HERE -o jsonpath={.spec.initContainers[*].name}
You can display both with a bit of JSONPath magic:
kubectl get pod POD_NAME_HERE -o jsonpath="{.spec['containers','initContainers'][*].name}"
Before Kubernetes 1.6 the init containers were stored in .metadata.annotations."pod.beta.kubernetes.io/init-containers"
. So it should be possible to get them via:
kubectl get pods POD_NAME_HERE -o jsonpath='{metadata.annotations."pod.beta.kubernetes.io/init-containers".[*].name}'
Unfortunately I cannot test this, because I don't have a cluster with this version. Also joining containers and init containers into a single command looks a bit harder this way.