Services & Microservices
A Service in Kubernetes is a resource that creates a stable, fixed access point (like an IP or DNS name) for other applications or users to reach a group of pods. Even though the individual pod IPs might change as pods are created or destroyed, the Service ensures that traffic is consistently routed to the right pods, handling load balancing and internal/external communication.
Microservice is a term that describes a design pattern or architecture where an application is broken down into small, independently deployable services, each responsible for a specific part of the application’s functionality.
There are 4 main Service types in K8S;
ExternalName
NodePort
NodePort is a Kubernetes Service type that exposes an application on a specific port (within the range 30000-32767) on each node's IP, allowing external traffic to access the service. It also includes a ClusterIP for internal traffic within the cluster while enabling external access through the node's IP and the specified nodePort.
In short, NodePort is used to map a port on the pod to a port on the node, making it accessible via the node IP over the specified port externally.
targetPort - This is the port on the pod which the application is listening on.
port - The port that the Service listens on within the cluster (for internal traffic)
nodePort - The external port on the node where traffic enters the cluster (for external traffic). nodePort has to be within the port range: 30000-32767
In other words, a NodePort service exposes an application (comprised of pods/containers) both to the internal cluster network, and the outside world. The NodePort groups together pods into a service, and then handles the routing of traffic to those pods.
Example;
I have the following pod definition that I'd like to include in my new service:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: webapp
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Rather than copy the pod definition into the service definition, we'll instead make use of the labels assigned to the pod, as below:
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
type: NodePort
ports:
- targetPort: 80 # Defines the port on which to direct traffic to the pod
port: 80 # The port that the Service listens on within the cluster (for internal traffic)
nodePort: 30008 # The external port on the node where traffic enters the cluster (for external traffic)
selector: #Selector to SELECT which labels will be associated with the service
app: webapp #label from pod definition - to associate that pod with this service
ClusterIP (default service type)
A ClusterIP Service in Kubernetes provides internal-only access to the service within the cluster by assigning a stable internal IP (ClusterIP). It allows other pods and services in the cluster to communicate with the service but is not accessible from outside the cluster.
As the name suggest, the ClusterIP service is assigned an internal IP for the cluster, which other services/pods/objects can then use to interact with the service and its associated pods.
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
type: ClusterIP
ports:
- targetPort: 80 # Defines the port on which to direct traffic to the pod
port: 80 # The port that the Service listens on within the cluster (for internal traffic)
selector: #Selector to SELECT which labels will be associated with the service
app: webapp #label from pod definition - to associate that pod with this service
Load Balancer
A LoadBalancer Service in Kubernetes automatically provisions an external load balancer from the underlying cloud provider (like AWS, GCP, or Azure) to expose the service to external clients. It distributes incoming traffic to the pods and provides a single external IP for accessing the service from outside the cluster.
The LoadBalancer service type will only work with Cloud Platforms that support this feature - since the control plane has to instruct the Cloud Platform to configure an external (to the cluster) LoadBalancer.
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
type: LoadBalancer
ports:
- targetPort: 80 # Defines the port on which to direct traffic to the pod
port: 80 # The port that the Service listens on within the cluster (for internal traffic)
nodePort: 30008 # The external port on the node where traffic enters the cluster (for external traffic)
selector: #Selector to SELECT which labels will be associated with the service
app: webapp #label from pod definition - to associate that pod with this service
Create service
kubectl create -f service-definition.yml
View service
kubectl get services
No Comments