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.
-
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– TensibaiAug 22, 2017 at 7:50
2 Answers
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 <pod-name> -c=<container-name> -- /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.
You can still exec containers when multiple are running in a pod, you specify the container to execute in using the "-c" parameter.