Kubernetes Networking addresses four concerns:
- Containers within a pod use networking 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 from outside of your cluster.
- You can also use services to publish services only for consumption inside your cluster.
Container to Container communication on same pod
- happens through localhost within the 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
kubectl apply -f pod.yml
kubectl get pods
kubectl exec testpod -it -c c00 -- /bin/bash
- inside the container
apt update
apt install curl
curl localhost:80
Communication between two different Pods within same machine(Node)
- Pod to Pod communication on same worker node through Pod IP.
- By Default Pod's IP will not be acccessible outside the node.
- Create 2 pods
kind: Pod
apiVersion: v1
metadata:
name: testpod1
spec:
containers:
- name: c00
image: nginx
command: ["/bin/bash","-c","while true; do echo Hello; sleep 5; done"]
ports:
- containerPort: 80
kind: Pod
apiVersion: v1
metadata:
name: testpod2
spec:
containers:
- name: c03
image: httpd
ports:
- containerPort: 80
kubectl apply -f pod2.yml
kubectl apply -f pod3.yml
- Check pods are running, and pods wide description
kubectl get pods
kubectl get pods -o wide
- Inside node, run commands to get request on pods IP addresses
curl <POD_ID>:80
ย