With every new technology comes a learning curve and Kubernetes is no exception.
1. Container to Container Communications
The smallest object we can deploy in Kubernetes is the pod, however within each pod you may want to run multiple containers. A common usecase for this is a helper where a secondary container helps a primary container with tasks such as pushing and pulling data.
Container to container communication within a K8s pod uses either the shared file system or the localhost network interface.
We can test this by using the K8s provided example, two-container-pod, and modifying it slightly.
https://k8s.io/examples/pods/two-container-pod.yaml
When we deploy this pod we can see two containers, “nginx-container” and “debian-container“. I’ve created two separate options to test, one with a shared volume, and one without a shared volume but using localhost instead.
Shared Volume Communication
When we use the shared volume, Kubernetes will create a volume in the pod which will be mapped to both containers. In the “nginx-container”, files from the shared volume will map to the “/usr/share/nginx/html” directory, while in the “debian-container” files will map to the “/pod-data” directory. When we update the “index.html” file from the Debian container, this change will also be reflected in our Nginx container, thereby providing a mechanism for our helper (Debian) to push and pull data to and from Nginx.
Localhost Communication
In the second scenario shared volume has been removed from the pod and a message has been written in the “index.html” file which only resides in the Nginx container. As previously mentioned, the other method for multiple containers to communicate within a pod is through the localhost interface and the port number to which they’re listening.
In this example Nginx is listening on port 80, therefore when we run the “curl https://localhost” command from the Debian container we can see that the “index.html“ page is served back to us from Nginx.
Here’s the “nginx-container” showing the contents of the “index.html” file.
Confirmation that we’re receiving the file when we Curl from the “debian-container”
0 comments:
Post a Comment