Skip to Content
This project is a work in progress. If you have any questions or suggestions, feel free to contact me.
KubernetesNetworkingKubernetes Networking

Networking

Kubernetes is networking addresses four concerns

  • Containers within a pod use networking (internal network) to communicate via loopback
  • Cluster networking provides communication between different pods
  • The service resources let you expose an application running in pods to be reachable outside your cluster
  • You can also use services to publish services only for consumption inside your cluster

Communication between containers within the same pod

  • It usually happen through localhost within the containers.

image

k8s_example_10.yml
kind: Pod apiVersion: v1 metadata: name: testpod spec: containers: - name: c00 image: ubuntu command: ["/bin/bash", "-c", "while true; do echo Hello-from-ubuntu; sleep 5; done"] - name: c01 image: httpd ports: - containerPort: 80

Create pod

kubectl apply -f k8s_example_10.yml
Output
controlplane $ kubectl apply -f k8s_example_10.yml pod/example-10 created

Verify if the httpd service is running using ubuntu container

kubectl exec -it example-10 -c c00 -- /bin/bash

Here -c is to chose the specific container

Output {8}
controlplane $ kubectl exec -it example-10 -c c00 -- /bin/bash root@example-10:/# apt update && apt install curl -y . . . done. root@example-10:/# curl localhost:80 <html><body><h1>It works!</h1></body></html>

Communication between containers in different pod within same node

  • It usually happen through IP Address and the exposed port of container.
  • By default pods IP will not be accessible outside the node
  • For example - curl IP_ADDRESS:PORT

image

k8s_example_11_C1.yml
kind: Pod apiVersion: v1 metadata: name: example-11-c1 spec: containers: - name: c00 image: ubuntu command: ["/bin/bash", "-c", "while true; do echo Hello-from-ubuntu; sleep 5; done"]
k8s_example_11_C2.yml
kind: Pod apiVersion: v1 metadata: name: example-11-c2 spec: containers: - name: c01 image: httpd ports: - containerPort: 80

Create pod

kubectl apply -f k8s_example_11_C1.yml kubectl apply -f k8s_example_11_C2.yml
Output
controlplane $ kubectl apply -f k8s_example_11_C1.yml pod/example-11-c1 created controlplane $ kubectl apply -f k8s_example_11_C2.yml pod/example-11-c2 created

Retrieve the pods IP Address

kubectl get pods -o wide

Verify if the httpd service is running using ubuntu container

kubectl exec -it example-11 -- /bin/bash
Output
controlplane $ kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES example-11-c1 1/1 Running 0 39s 192.168.1.8 node01 <none> <none> example-11-c2 1/1 Running 0 25s 192.168.1.9 node01 <none> <none>
kubectl exec -it example-11-c1 -- /bin/bash

Here -c is to chose the specific container

Output
controlplane $ kubectl exec -it example-11-c1 -- /bin/bash root@example-10:/# apt update && apt install curl -y . . . done. root@example-10:/# curl 192.168.1.9:80 <html><body><h1>It works!</h1></body></html>

Important note

There is a possibility of pod crash so that the ip address will not be consistent and it will be changed dynamically from time to time in the case of pod failure. In order to make it accessible all the time via application without worrying about the change in pod IP,

We use an object called services to make the IP consistent.

Last updated on