<p>Let's start from the beginning.</p>

<p>First, I'll answer the original question, the one in the title.</p>

<h2>When to put all containers in a pod and when to deploy them individually?</h2>

<p>This depends on what your app does. For instance (this is an actual example from the company I work for), for a web application, you may have a pod which runs three containers:</p>

<ul>
<li>app: the actual app with its code</li>
<li>nginx: serves all the requests</li>
<li>heka: a log service which proxies the logs to wherever you want them</li>
</ul>

<p>The reason for grouping this three containers within a single pod is simple: a pod assures you the containers run in the same Kubernetes node, this is to have the least possible latency with requests being interchanged along the three containers.</p>

<p>That being said, this is only an example, you could have different and multiple reasons for grouping containers in a pod, but I think this is the most common.</p>

<p>With that being clarified, I follow to the question that you present in the body of your post.</p>

<h2>How do you access a single container within a pod?</h2>

<p>This is simply using kubectl, I use the command like this:</p>

<p><code>kubectl exec -it &lt;pod-name&gt; -c=&lt;container-name&gt; -- /bin/bash</code></p>

<p>Suppose we want to access the <code>nginx</code> container inside our <code>my-app</code> pod; we would type this:</p>

<p><code>kubectl exec -it my-app -c=nginx -- /bin/bash</code></p>

<p>If you don't specify the container with the <code>-c</code> flag, it defaults to the first container that is in the Kubernetes <code>yaml</code> (I think). So when you say it was impossible to access a container, you actually <strong><em>did</em></strong> access a container (the default one), because a pod cannot be accessed, it is merely a collection of containers and its configurations.</p>

<p>Hope I've been helpful.</p>

CC BY-SA 3.0
2017-09-09T22:09:02.693Z
2017-09-10T20:31:40.000Z
2017-09-10T20:31:40.000Z
klaus
<p>You can still exec containers when multiple are running in a pod, you specify the container to execute in using the "-c" parameter.</p>

CC BY-SA 3.0
2017-08-22T10:51:01.910Z
2017-08-22T10:51:01.910Z
Trondh
<p>One of the reasons for asking this question is that it seems to be impossible to access a container using kubectl exec when these are deployed in a pod.</p>

CC BY-SA 3.0
2017-08-22T07:50:39.787Z
You never run directly a container in k8s, your run pod made of one or more containers. when using kubectl exec you can exec on the first container of the pod or a specific one by specifying it on the command line. I can't really tell if this answer the question or not btw
Tensibai
<kubernetes>
When to put all containers in a pod and when to deploy them individually?
030
# When to put all containers in a pod and when to deploy them individually?

(Asked by: username_0 on 2017-08-21)

One of the reasons for asking this question is that it seems to be impossible to access a container using kubectl exec when these are deployed in a pod.

**Tags:** kubernetes

> **Comments:**
> **username_1**: You never run directly a container in k8s, your run pod made of one or more containers. when using kubectl exec you can exec on the first container of the pod or a specific one by specifying it on the command line. I can't really tell if this answer the question or not btw  

---

## 2 answers

### Answer by username_2 on 2017-09-09. Score: 2 | Accepted answer

Let's start from the beginning.

First, I'll answer the original question, the one in the title.

## When to put all containers in a pod and when to deploy them individually?

This depends on what your app does. For instance (this is an actual example from the company I work for), for a web application, you may have a pod which runs three containers:

* app: the actual app with its code
* nginx: serves all the requests
* heka: a log service which proxies the logs to wherever you want them

The reason for grouping this three containers within a single pod is simple: a pod assures you the containers run in the same Kubernetes node, this is to have the least possible latency with requests being interchanged along the three containers.

That being said, this is only an example, you could have different and multiple reasons for grouping containers in a pod, but I think this is the most common.

With that being clarified, I follow to the question that you present in the body of your post.

## How do you access a single container within a pod?

This is simply using kubectl, I use the command like this:

`kubectl exec -it  -c= -- /bin/bash`

Suppose we want to access the `nginx` container inside our `my-app` pod; we would type this:

`kubectl exec -it my-app -c=nginx -- /bin/bash`

If you don't specify the container with the `-c` flag, it defaults to the first container that is in the Kubernetes `yaml` (I think). So when you say it was impossible to access a container, you actually ***did*** access a container (the default one), because a pod cannot be accessed, it is merely a collection of containers and its configurations.

Hope I've been helpful.


---

### Answer by username_3 on 2017-08-22. Score: 1

You can still exec containers when multiple are running in a pod, you specify the container to execute in using the "-c" parameter.


---

