Kubernetes: Services

Claire Lee
5 min readMar 5, 2023

--

In Kubernetes, Service acts as an intermediary between a set of pods and other applications or end-users, allowing them to communicate with each other. Kubernetes offers three types of services: NodePort, ClusterIP, and LoadBalancer. The NodePort service allows external access to a pod, the ClusterIP service provides access within the cluster, and the LoadBalancer service exposes the service externally using a cloud provider’s load balancer.

Kubernetes: Services

Services(Front-of-house staff)

In Kubernetes, the Service object is an abstraction layer that provides a stable IP address and DNS name for a set of Pods. This ensures that requests are routed to the correct destination and load balancing is maintained. The Service object acts as a mediator between clients and the Pods that make up an application.

In a restaurant, the front-of-house staff who manage the flow of customers to the various tables, ensuring that each customer is served by the appropriate waiter or waitress.

Three Types of Services

The three types of Services in Kubernetes can also be compared to the front-of-house staff who manages different sections in a restaurant.

NodePort(Street-side cafe)

NodePort

This type of service exposes the service on each node’s IP address and a static port. It creates a network route from the cluster nodes to the service, allowing external traffic to reach the pod through the node’s IP address and the specified port.

When creating a NodePort service in Kubernetes, three types of ports are used:

  1. NodePort: This port is opened on each node in the cluster and is typically a high port number between 30000–32767. It is used to route traffic from outside the cluster to the service.
  2. Port: This port is used to access the service from within the cluster. It is the port number that is exposed by the service and is typically the same as the port number used by the application.
  3. TargetPort: This port number is the one that the application is listening on inside the pod. It is the port that the service routes traffic to once it is received on the NodePort or Port.

When a client sends a request to the NodePort service, the request first reaches the NodePort opened on each node in the cluster. The node then routes the request to the Port exposed by the service. The Service then forwards the request to the TargetPort, which is the port number that the application is listening on inside the pod. The pod then processes the request and sends the response back to the client through the same NodePort, Port, and targetPort.

NodePort is like a street-side cafe where customers can walk in off the street and dine.

ClusterIP(Main dining room)

ClusterIP

ClusterIP service creates a virtual IP address that is used internally by the cluster to communicate with the pods. The service is only accessible from within the cluster, and not from outside the cluster. It allows internal traffic to reach the pod through the specified port.

When a request is sent to the ClusterIP service, the request is forwarded to one of the pods that is selected by the service. The selection is based on the routing policy that is defined for the service. Once the request reaches the pod, it is processed and a response is sent back through the same IP address, Port, and TargetPort.

ClusterIP is like the main dining room in a restaurant where customers are seated at tables and are served by a waiter.

LoadBalancer(Private dining room)

LoadBalancer

LoadBalancer service creates an external load balancer in the cloud infrastructure, which routes traffic to the Kubernetes service. It can be used to load-balance traffic to multiple pods and route traffic to the appropriate pod based on labels.

LoadBalancer is like a private dining room where customers have their own space and dedicated staff.

Short Name: svc

$ kubectl api-resources
NAME SHORTNAMES APIVERSION NAMESPACED KIND
services svc v1 true Service

Service with YAML

Service with YAML

service.yaml

apiVersion: v1
kind: Service
metadata:
name: <service_name>
spec:
type: <service_type>
ports:
##################################
# based on the type of service #
##################################
selector:
<key1>: <value1>
<key2>: <value2>
:
<keyN>: <valueN>

The Service uses selector to select the target Pods based on their labels.

NodePort

  type: NodePort
ports:
- targetPort: <port_on_pod>
port: <port_on_service>
nodePort: <port_on_node>

ClusterIP

  type: ClusterIP
ports:
- targetPort: <port_on_pod>
port: <port_on_service>

LoadBalancer

  type: LoadBalancer
ports:
- targetPort: <port_on_pod>
port: <port_on_service>

Commands

Service commands
  1. Creates a new service based on the YAML file
$ kubectl create -f <service>.yaml

2. Create a ClusterIP service

$ kubectl create service clusterip <service-name> --tcp=<port>:<targetPort>

3. Create a NodePort service

$ kubectl create service nodeport <service-name> --tcp=<port>:<targetPort>
--node-port=<nodePort>

4. Create a LoadBalancer service

$ kubectl create service loadbalancer <service-name> --tcp=<port>:<targetPort>

5. Create a service for a pod

$ kubectl expose pod <pod-name> --name=<service-name> \
--port=<port> --target-port=<targetPort> --type=<service-type>

6. Create a service for a deployment

$ kubectl expose deployment <deployment-name> --name=<service-name> \
--port=<port> --target-port=<targetPort> --type=<service-type>

7. Retrieve a list of services that have been created

$ kubectl get services

8. Retrieve detailed information about a specific service

$ kubectl describe services <service-name>

9. Retrieve the endpoint(s) associated with a specific service

$ kubectl get endpoints <service-name>

These are my personal notes for CKA exam preparation on Kubernetes. Please feel free to correct me if you notice any errors. 😊

Related Story

Reference:

--

--