Kubernetes networking addresses four concerns
Container within a pod use networking to communicate via loopback.
Cluster networking provides communicate between different pods.
The service resources lets you expose on application running in pods to be reachable from outside 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 conatiner.
Now try to establish communication b/w two different pods within same machine.
Pod to pod communication on same worker node happen through pod IP.
By default, Pods IP will not be accessible outside the node.
Create a two yaml file by pod1.yml and pod2.yml and also start minikube and check status should running
kind: Pod
apiVersion: v1
metadata:
name: testpod1
spec:
containers:
- name: c01
image: nginx
ports:
- containerPort: 80
kind: Pod
apiVersion: v1
metadata:
name: testpod2
spec:
containers:
- name: c01
image: httpd
ports:
- containerPort: 80
the apply the code
kubectl apply -f pod1.yml
kubectl apply -f pod2.yml
kubectl get pods
kubectl get pods -o wide
curl 'pod IP':80
curl 'pod1 IP':80
example:- curl 13.32.43.43:80
this command helps in establishing the communication between pods.
Problem in this method
As we know that due to some reason like traffic, network failer etc pod will get destroy and new pod will created in place of old pod. But problem is that, the new pod have different IP address due to this connect get failed beacause we map the ip with old pod ip address.
For solving this problem , Kubernetes service come in Picture.
Kubernetes Services
Some important points regarding Kubernetes services
when using RC, pods are terminated and created during scaling or replication operation.
When using deployment, while uploading the image version the pods are terminated and new pods take the place of other pods.
Pods are very dynamic ie. they come and go on the K8s cluster and on any of the available nodes and it would be difficult to access the pods as the pods ip changes once its recreated.
Services object is an logical bridge betweek pods and end users, which provides virtual IP (VIP).
Service above clients to reliably connect to container running in pod using VIP.
The VIP is not an actual IP connected to network interface but its purpose is purely towards traffic to one or more pods.
Kube proxy is the one which keeps the mapping between the VIP and the pod upto date , which queries the API server to learn about new services in the cluster.
Although each POD has a unique IP address , through IP's are not exposed outside the cluster.
services help to expose the VIP mapped to the pods and allow application to receive traffic.
Labels are used to select which are the pods to be put under a service.
Creating a service will create an endpoint to access the pods/application in it.
Types of Services:
ClusterIP: Provides a stable internal IP within the cluster for accessing pods.
NodePort: Exposes a port on each node, forwarding traffic to a Service.
LoadBalancer: Exposes a Service externally through a cloud load balancer (if supported by your cloud provider).
ExternalName: Maps a Service to a DNS name without providing any proxy functionality.
By default service can run only between ports 30000-32767.
The set od pods targeted by service is usually determined by a selector.
Conclusion
we learn about basic concept of kubenetes networking and how to connect pods and also learn about kubernetes service theory & its importance.
In next part we will perform lab.
Happy learning!..