Multi-Container Pods and Container Communication in Kubernetes
Multi-Container Pods and Container Communication in Kubernetes
Multi-Container Pods and Container Communication in Kubernetes
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 1/20
3/27/2020 Multi-container pods and container communication in Kubernetes
And that makes sense, because in many respects, a Pod aces like a single
server. For example, each container can access the other containers in the
pod as different ports on localhost.
While it would seem simpler to just deploy a single container directly, there
are good reasons to add a layer of abstraction represented by the Pod. A
container is an existing entity, which refers to a speci c thing. That speci c
thing might be a Docker container, but it might also be a rkt container, or a
VM managed by Virtlet. Each of these has different requirements.
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 2/20
3/27/2020 Multi-container pods and container communication in Kubernetes
Containers in a Pod run on a “logical host”; they use the same network
namespace (in other words, the same IP address and port space), and the
same IPC namespace. They can also use shared volumes. These properties
make it possible for these containers to ef ciently communicate, ensuring
data locality. Also, Pods enable you to manage several tightly coupled
application containers as a single unit.
• Sidecar containers “help” the main container. Some examples include log
or data change watchers, monitoring adapters, and so on. A log watcher, for
example, can be built once by a different team and reused across different
applications. Another example of a sidecar container is a le or data loader
that generates data for the main container.
• Proxies, bridges, and adapters connect the main container with the
external world. For example, Apache HTTP server or nginx can serve static
les. It can also act as a reverse proxy to a web application in the main
container to log and limit HTTP requests. Another example is a helper
container that re-routes requests from the main container to the external
world. This makes it possible for the main container to connect to localhost
to access, for example, an external database, but without any service
discovery.
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 3/20
3/27/2020 Multi-container pods and container communication in Kubernetes
A standard use case for a multi-container Pod with a shared Volume is when
one container writes logs or other les to the shared directory, and the other
container reads from the shared directory. For example, we can create a Pod
like so:
LIVE WEBINAR
SIGN UP
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 4/20
3/27/2020 Multi-container pods and container communication in Kubernetes
apiVersion: v1
kind: Pod
metadata:
name: mc1
spec:
volumes:
- name: html
emptyDir: {}
containers:
- name: 1st
image: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
- name: 2nd
image: debian
volumeMounts:
- name: html
mountPath: /html
command: ["/bin/sh", "-c"]
args:
- while true; do
date >> /html/index.html;
sleep 1;
done
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 5/20
3/27/2020 Multi-container pods and container communication in Kubernetes
You can check that the pod is working either by exposing the nginx port and
accessing it using your browser, or by checking the shared directory directly
in the containers:
Containers in a Pod share the same IPC namespace, which means they can
also communicate with each other using standard inter-process
communications such as SystemV semaphores or POSIX shared memory.
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 6/20
3/27/2020 Multi-container pods and container communication in Kubernetes
message queue for reading and reads messages until it receives the exit
message. We also set the restart policy to ‘Never’, so the Pod stops after
termination of both containers.
apiVersion: v1
kind: Pod
metadata:
name: mc2
spec:
containers:
- name: producer
image: allingeek/ch6_ipc
command: ["./ipc", "-producer"]
- name: consumer
image: allingeek/ch6_ipc
command: ["./ipc", "-consumer"]
restartPolicy: Never
To check this out, create the pod using kubectl create and watch the Pod
status:
Now you can check logs for each container and verify that the 2nd container
received all messages from the 1st container, including the exit message:
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 7/20
3/27/2020 Multi-container pods and container communication in Kubernetes
There is one major problem with this Pod, however, and it has to do with how
containers start up.
Currently, all containers in a Pod are being started in parallel and there is no
way to de ne that one container must be started after other container. For
example, in the IPC example, there is a chance that the second container
might nish starting before the rst one has started and created the
message queue. In this case, the second container will fail, because it expects
that the message queue already exists.
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 8/20
3/27/2020 Multi-container pods and container communication in Kubernetes
Some efforts to provide some measure of control over how containers start,
such as Kubernetes Init Containers, which start rst (and sequentially), are
under development, but in a cloud native environment, it’s always better to
plan for failures outside of your immediate control. For example, one way to
to x this issue would be to change the application to wait for the message
queue to be created.
Containers in a Pod are accessible via “localhost”; they use the same network
namespace. Also, for containers, the observable host name is a Pod’s name.
Because containers share the same IP address and port space, you should
use different ports in containers for incoming connections. In other words,
applications in a Pod must coordinate their usage of ports.
LIVE WEBINAR
SIGN UP
Step 1. Create a Con gMap with the nginx con guration le. Incoming HTTP
requests to port 80 will be forwarded to port 5000 on localhost:
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 9/20
3/27/2020 Multi-container pods and container communication in Kubernetes
apiVersion: v1
kind: ConfigMap
metadata:
name: mc3-nginx-conf
data:
nginx.conf: |-
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream webapp {
server 127.0.0.1:5000;
}
server {
listen 80;
location / {
proxy_pass http://webapp;
proxy_redirect off;
}
}
}
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 10/20
3/27/2020 Multi-container pods and container communication in Kubernetes
Step 2. Create a multi-container Pod with the simple web app and nginx in
separate containers. Note that for the Pod, we de ne only nginx port 80. Port
5000 will not be accessible outside of the Pod.
apiVersion: v1
kind: Pod
metadata:
name: mc3
labels:
app: mc3
spec:
containers:
- name: webapp
image: training/webapp
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
volumeMounts:
- name: nginx-proxy-config
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
- name: nginx-proxy-config
configMap:
name: mc3-nginx-conf
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 11/20
3/27/2020 Multi-container pods and container communication in Kubernetes
Now you can use your browser (or curl) to navigate to your node’s port to
access the web application through reverse proxy, as in:
http://myhost:31418
This request will then be forwarded to port 5000 of the webapp container.
While this example shows how to use a single container to access other
containers in the pod, it’s quite common for several containers in a Pod to
listen on different ports — all of which need to be exposed. To make this
happen, you can either create a single service with multiple exposed ports, or
you can create a single service for every poirt you’re trying to expose.
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 12/20
3/27/2020 Multi-container pods and container communication in Kubernetes
That’s just the beginning, of course. Interested in seeing what else you can do
and getting more hands-on with Kubernetes? Check out our new advanced
Kubernetes and Docker Bootcamp II (KD200) – the rst course that prepares
for the new Certi ed Kubernetes Administrator (CKA) exam. Hope to see you
there!
WEBINAR RECORDING
WATCH NOW
4 3 2 1 : /
Featured Posts
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 13/20
3/27/2020 Multi-container pods and container communication in Kubernetes
Ryan says:
REPLY
Dee says:
REPLY
multi container pod must be used only for tightly coupled containers.
not scenario like mentioned by you ( web/app server combo). that will
affect replicas performance.
because app containers must run in a separate deployment hence pod.
the same way web container also must run in a separate deployment.
so you can scale like
kubectl edit deploy/app –replicas=N
kubectl autoscale deploy/app –min=1 –max=10
don’t go multi container pod for web/app combo
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 14/20
3/27/2020 Multi-container pods and container communication in Kubernetes
REPLY
—- Nick
REPLY
Paul K says:
Any ideas?
Thanks
REPLY
My guess is that htis is the second time you’ve run this, and the
service stille xists from the rst time. Try
REPLY
Paul K says:
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 15/20
3/27/2020 Multi-container pods and container communication in Kubernetes
REPLY
My pleasure!
REPLY
Nilesh says:
Let’s say you have an application with multiple microservices using mysql
database. For now lets just talk about only one microservice A.
What I will need – one container(having mysql 5.7) and another container
(for microservice A).
Two scenarios:
REPLY
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 16/20
3/27/2020 Multi-container pods and container communication in Kubernetes
Mike says:
Hey, I’m no expert and still learning but Persistent Volumes seem to be
the solution for stateful resources like Databases.
REPLY
Agreed!
REPLY
Eva says:
REPLY
Hi,
What host i should provide instead of myhost ,i tried with localhost and
private ip it didn’t work for inter container networking.
Please help.
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 17/20
3/27/2020 Multi-container pods and container communication in Kubernetes
Regards,
Gaurav
REPLY
yadyreve says:
REPLY
I can vouch for this article because I personally wrote it more than
two years ago. That doesn’t mean that Mr. Linchpiner stole it
directly from us, however; our blogs are syndicated on many sites
over the web, and especially back then it was often done without
credit. It’s entirely possible he took his information from one of
those places, entirely unintentionally.
REPLY
LEAVE A REPLY
Your email address will not be published. Required elds are marked *
Comment
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 18/20
3/27/2020 Multi-container pods and container communication in Kubernetes
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I
comment.
Nu sunt robot
reCAPTCHA
Con dențialitate - Termeni
Post Comment
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 19/20
3/27/2020 Multi-container pods and container communication in Kubernetes
Build Overview
Operate Instructor-led
Transfer Ondemand
MCP Support Certi cation
K8s Support Private
RESOURCES ABOUT
Customers Contact
Partners Company
Blog Locations
TCO Calculator Careers
Webinars Meet the Team
Events Board of Directors
Brochures Press Center
Videos
Mirantis Inc. 900 E Hamilton Avenue, Suite 650, Campbell, CA 95008 +1-650-963-9828
© 2005 - 2020 Mirantis, Inc. All rights reserved. "Mirantis" and "FUEL" are registered trademarks of Mirantis, Inc. All other
trademarks are the property of their respective owners.
https://www.mirantis.com/blog/multi-container-pods-and-container-communication-in-kubernetes/ 20/20