kubernetes: Pods can be composed of multiple containers

2 min read | by Jordi Prats

One common misunderstanding with kubernetes is mistakenly assume "a pod" really means "a container".

A pod is the minimal unit we take into account in kubernetes but this does not mean that a pod is a container: A pod can be composed of several containers working together. We can easily see this on the READY column that are going to tell us for a given pod from how many containers it is composed:

$ kubectl  get pods
NAME                                     READY   STATUS      RESTARTS   AGE
ampa-7dcbfd689f-59ghw                    2/2     Running     0          5d1h

By trying to get it's logs can be even more obvious:

$ kubectl logs manualrun-xzqpg
error: a container name must be specified for pod manualrun-xzqpg, choose one of: [pet2cattle-sitemapgen pet2cattle-indexer]

Here kubectl logs doesn't know which logs we want to retrieve since this pod is composed of two different containers:

  • pet2cattle-sitemapgen
  • pet2cattle-indexer

To get the logs we just need to specify from which container we want to get it's logs using the -c flag:

$ kubectl logs manualrun-xzqpg -c pet2cattle-indexer

If the pod also have initContainers you can also use the -c flag to retrieve logs generated from one of them:

error: a container name must be specified for pod manualrun-jdzgs, choose one of: [pet2cattle-sitemapgen pet2cattle-indexer] or one of the init containers: [init-sitemapgen init-indexer]

Posted on 12/03/2021