Container Communication within Kubernetes Pods: Shared IP and Ports

Photo by Growtika on Unsplash

Container Communication within Kubernetes Pods: Shared IP and Ports

In Kubernetes, containers that are part of the same Pod share the same network namespace, which means they share the same IP address and port space. This design allows containers within the same Pod to communicate with each other using localhost because they are effectively on the same "machine" in terms of networking. However, this also means that containers within the same Pod must coordinate port usage to avoid conflicts. Pods are considered to be a single logical host from a network perspective, and this setup facilitates tight coupling of containers that need to work closely together.

The containers in a Pod can also communicate with each other using standard inter-process communications (IPC) like SystemV semaphores or POSIX shared memory. Containers in different Pods have distinct IP addresses and can not communicate by OS-level IPC without special configuration. Containers that want to interact with a container running in a different Pod can use IP networking to communicate.

Workshop: Container communication within the same Pod

This exercise involves creating a Pod that runs two containers: one container will serve a simple web application, and the other will access the web application using localhost, since both containers share the same network namespace.



Step 1: Create a Simple Web Server Container

First, you'll need a simple web server. For this exercise, we'll use a basic Python Flask application.

  1. Create the Flask App - Save the following Python code as

     from flask import Flask
     app = Flask(__name__)
     def hello():
         return "Hello from Flask!"
     if __name__ == "__main__":'', port=80)
  2. Create a Dockerfile - Save the following Dockerfile in the same directory as

     FROM python:3.8-slim
     WORKDIR /app
     COPY /app
     RUN pip install flask
     CMD ["python", ""]
  3. Build and Push the Docker Image - Run the following commands to build the Docker image and push it to a Docker registry (e.g., Docker Hub). Replace yourusername/flask-app with your actual Docker Hub username and repository.

     docker build -t yourusername/flask-app .
     docker push yourusername/flask-app

Step 2: Create a Pod with Two Containers

  1. Define the Pod Manifest - Save the following YAML as two-container-pod.yaml. This manifest defines a Pod with two containers: the Flask app you just created and a busybox container to test the network communication.

     apiVersion: v1
     kind: Pod
       name: two-container-pod
       - name: flask-container
         image: yourusername/flask-app
         - containerPort: 80
       - name: busybox-container
         image: busybox
         command: ['sh', '-c', 'echo Waiting for a minute && sleep 60']

    Replace yourusername/flask-app with the name of your Docker image.

  2. Apply the Manifest - Use kubectl to create the Pod:

     kubectl apply -f two-container-pod.yaml

Step 3: Test Container Communication

  1. Exec into the Busybox Container - Once the Pod is running, exec into the busybox container:

     kubectl exec -it two-container-pod -c busybox-container -- sh
  2. Test Network Communication - Inside the busybox container, use wget to test accessing the Flask application running in the flask-container:

     wget -qO- http://localhost

    This command should output Hello from Flask!, demonstrating that containers within the same Pod can communicate using localhost.


This exercise demonstrates the concept of shared networking within a Kubernetes Pod. Containers within the same Pod can communicate over localhost because they share the same network namespace. Remember to clean up the resources by deleting the Pod:

kubectl delete pod two-container-pod

This simple exercise is a foundation for understanding more complex inter-container communication patterns in Kubernetes.


  1. Pod networking

  2. Install and Set Up kubectl on Linux


  4. Kubernetes network stack fundamentals: How containers inside a pod communicate