A service is a kubernetes object that has a fixed IP address and port. It is the link to a group of pods that offer the same service (web server for example).
Programs like web clients connect to the service and get their requests forwarded to the right pod.
The service server handle the networking and the communication between the different components of kubernetes as well as the communication with the outside world.
The nodePort service allows programs from outside the cluster to talk to the application inside the Pod through a port on the node (30033) in our example. The service then forwards the packets to a port (target port) on the Pod (300) where the applications is listening to requests.
When we create a nodePort service, kubernetes reserves the same port number on all the nodes
Pod Network : it is an internal network that makes it possible for Pods to communicate on any mode. Kubernetes allocates an internal IP address to each node.
Three port types:
Port on the node (Nodeport) :30033, we use it to access the application in the Pod form the outside.
Port on the service : The service server (in red in the above diagram) has an IP address (cluster IP of the service) -10.3.11.5 and a port 82 in our example, it could be considered as a virtual server inside the node.
Port on the Pod (Target port) : 300 and 301.
Some types of services are mentioned below:.
Nodeport services (the above diagram): exposes pods to external requests. Cluster IP services: accessible only from within the kubernetes cluster. Load-balancer services: accessible from outside the kubernetes cluster.
We are only talking about the NodePort services here.
We use a YAML file to create a service like below:
We get the selector information from the creation YAML file of thePod, the selector tells the service which Pod to forward the request to
The selector also tells which Pods are endpoints of that service.
Target port lets the service know which port on the Pod to send the request to in case the Pod more than one container running inside with more than one port open.
When we create a nodePort service, the cluster IP service that will receive the requests is automatically created
Node port are between : 30000 and 32767.
Commands to create a service:
We use the below command to create a service:
To display the services, we use:
Now we can directly connect to the application inside the Pod using the node IP 10.1.11.4and port 30033.