Table of contents
In Kubernetes, a service is an abstraction that defines a logical set of pods and a policy by which to access them. It provides a stable network endpoint to access the pods, even as they are created, deleted, or scaled. Services enable communication and load balancing between different components or microservices within a Kubernetes cluster. The service groups the pods based on the selectors using labels.
In this example, We will see the two types of service:
ClusterIP.
NodePort.
ClusterIP:
This is the default service type. It exposes the service on an internal IP address, reachable only within the cluster. It allows communication between different services within the cluster.
In this image, the admin is internally using the cluster so he can access the clusterip service. The ClusterIP service type in Kubernetes provides a stable internal IP address to access a set of pods within a cluster.
Internal communication
Load balancing
Service discovery
Internal-only access
let's see how the Cluster IP works:
CLI commands to create a deployment and expose the service as ClusterIP.
Deployment
kubectl create deployment mydep1 --image docker.io/httpd --port 80 --replicas 5
This will create a deployment and have 5 pods in it
Service
kubectl expose deployment mydep1 --port 18080 --target-port 80
This we maps all the pods of deployment and create static IP for Internal use.
By default, the service is created as ClusterIP.
To get the svc to use the command "kubectl get svc".
To get the svc information in detail use the command
"kubectl describe svc (svc name)"
IP = static IP for accessing the pods
Endpoints = IPs of the pods that have been labelled as app=mydep1.
NodePort:
This type exposes the service on a static port on each node in the cluster. It creates a cluster-wide open port that routes to the service. It allows external access to the service using the cluster nodes' IP addresses.
The NodePort service type in Kubernetes allows external access to services by opening a static port on each node in the cluster. Here are the key aspects of NodePort:
External access
Port allocation
Node-level load balancing
Internal cluster communication
Firewall considerations
Command to expose service to the external world:
kubectl expose deployment mydep1 --port 28080 --target-port 80 --type NodePort
Described the service:
There is NodePort opened for the external world
NodePort= 31521, We can use the nodeip:Nodeport to excess it in the outer world.