Kubernetes Networking Visualized

Tushar Rajpoot
2 min readSep 16, 2022

--

Kubernetes Networking addresses four concerns:

  1. Containers within a pod use networking to communicate via loopback.
  2. Cluster Networking provides communication between different pods.
  3. The service resources let you expose an application running in pods to be reachable from outside of your cluster.
  4. You can also use services to publish services only for consumption inside your cluster.

Container to Container communication on the same pod

  • happens through localhost within the containers.

Try Yourself?

  • Create a manifest file for creating a pod with 2 containers.
kind: Pod
apiVersion: v1
metadata:
name: testpod
spec:
containers:
- name: c00
image: ubuntu
command: ["/bin/bash", "-c", "while true; do echo Hello; sleep 5; done"]
- name: c01
image: httpd
ports:
- containerPort: 80
  • Apply manifest file and create a pod with 2 containers.
kubectl apply -f pod.yml
  • Exec inside one container c00
kubectl get pods
kubectl exec testpod -it -c c00 -- /bin/bash
  • Curl localhost:80 for communicating to container 2 from container 1
apt update
apt install curl
curl localhost:80

Communication between two different Pods within the same machine(Node)

  • Pod to Pod communication on same worker node through Pod IP.
  • By Default Pod’s IP will not be accessible outside the node.

Try Yourself?

  • Create 2 Pods on same node.
kind: Pod
apiVersion: v1
metadata:
name: testpod1
spec:
containers:
- name: c00
image: nginx
ports:
- containerPort: 80
  • manifest 2
kind: Pod
apiVersion: v1
metadata:
name: testpod2
spec:
containers:
- name: c03
image: httpd
ports:
- containerPort: 80
  • Apply manifest files
kubectl apply -f pod2.yml
kubectl apply -f pod3.yml
  • Inside node, run commands to get requests on pods IP addresses
curl <POD_ID>:80

--

--

Tushar Rajpoot
Tushar Rajpoot

Written by Tushar Rajpoot

DevOps Engineer | Backend Developer

Responses (1)