Services and Networking in Kubernetes

Kubernetes fundamentals — Part (3.a)

Teva Veluppillai
6 min readJan 20, 2024

Introduction

Kubernetes has revolutionized the way organizations deploy, scale, and manage containerized applications. At the heart of this orchestration platform lies an intricate web of services and networking components that ensure seamless communication and connectivity among containers. In this article, we will delve into the world of Kubernetes services and networking, exploring the key concepts, types of services, and how they contribute to the overall efficiency of containerized applications.

Tutorial Flow:

We will discuss about service and then dive into ingress controller.

Services in K8s

What is Kubernetes Service?

In Kubernetes, a service is an abstraction that defines a logical set of pods and a policy by which to access them. Services enable communication and discovery between different parts of an application, allowing for scalable and robust architectures.

What are the main types of services in Kubernetes?

  1. NodePort Service: Exposes the service on each node’s IP at a static port. Enables external access to the service by accessing any node on that port.
  2. ClusterIP Service: The default service type. Exposes the service on a cluster-internal IP. Pods within the cluster can reach the service using this IP.
  3. LoadBalancer Service: Exposes the service externally using a cloud provider’s load balancer. Suitable for applications requiring external access with load balancing.

What is a NodePort Service and give an example?

NodePort is a type of service that exposes a service on a specific port on each node in the cluster.

This allows external access to the service from outside the cluster.

NodePort is commonly used to expose a service to the external world, for example, to access a web application running inside the cluster.

Key points about NodePort services:

  1. External Access: NodePort makes a service accessible externally by mapping a port on each node to a port on the service.
  2. ClusterIP: NodePort services also have a ClusterIP, which means they can be accessed internally within the cluster using the ClusterIP.
  3. Port Mapping: When you create a NodePort service, Kubernetes automatically assigns a port from a predefined range (default is 30000–32767) on each node to the service. This port is the NodePort.
  4. Load Balancing: While NodePort doesn’t provide sophisticated load balancing, it allows external access to the service by reaching any node in the cluster.
NodePort Architecture
NodePort Service Implementation. demoNoteport.yaml

Let’s break down the components of this example:

  • metadata: Specifies the name of the service.
  • spec.selector: Defines the selector that determines which pods the service will target. In this case, it targets pods with the label app: my-app.
  • spec.ports: Specifies the ports configuration. In this example, the service listens on port 80 and forwards traffic to the pods on port 8080.
  • spec.type: NodePort: Indicates that this is a NodePort service.
  • When you apply this YAML configuration using kubectl apply -f demoNodeport.yaml, Kubernetes will create a NodePort service named teva-nodeport-service.

Single Node with Multiple Pods

Mutiple Nodes with Pods

Mutiple Nodes with single pods

curl http://192.169.1.4:30008 or curl http://192.169.1.6:30008 or curl http://192.169.1.9:30008

What is a ClusterIP Service and give an example?

ClusterIP service is a type of service that exposes an internal IP address and makes it accessible only within the cluster.

This means that the service is not accessible from outside the cluster.

ClusterIP services are often used for communication between different components or microservices within the cluster.

Key points about ClusterIP services:

  1. Internal IP Address: ClusterIP services are assigned an internal IP address that is only reachable within the Kubernetes cluster.
  2. Pod Selector: They are associated with a set of pods based on a selector. The service forwards traffic to the pods that match the specified label selector.
  3. Load Balancing: ClusterIP services provide basic load balancing across the pods associated with them. Incoming traffic is distributed among the selected pods.
  4. Communication Within the Cluster: These services are suitable for communication between different parts of an application or microservices running within the same Kubernetes cluster.
Cluster IP

Example Sample ClusterIP implementation.

This YAML defines a ClusterIP service named “backend-service” that selects pods labeled with “app: backend.”

The service listens on port 8080 and forwards traffic to the pods on their port 8080.

Frontend Pod and ClusterIP Service

Breaking down the components of this example:

  • metadata: Specifies the name of the service.
  • spec.selector: Defines the selector that determines which pods the service will target. In this case, it targets pods with the label app: frontend.
  • spec.ports: Specifies the ports configuration. In this example, the service listens on port 80 and forwards traffic to the pods on port 8080.
  • spec.type: ClusterIP: Indicates that this is a ClusterIP service.
  • When you apply this YAML configuration using kubectl apply -f <filename>.yaml, Kubernetes will create a ClusterIP service named example-clusterip-service.
  • The service will be accessible within the cluster using its ClusterIP address.
  • Other pods within the same Kubernetes cluster can communicate with this service using its ClusterIP and port (example-frontend-service:80).

What is a LoadBalancerService and give an example?

In Kubernetes (K8s), a LoadBalancer is a type of service that exposes your service to the external world and automatically provisions an external load balancer.

This load balancer distributes incoming network traffic across multiple nodes to ensure high availability and reliability of your application.

Key points about LoadBalancer services:

  1. External Access: LoadBalancer services make a service accessible externally by provisioning a cloud provider’s load balancer. The external load balancer typically has a public IP address and can distribute traffic to the nodes running your application.
  2. Automatic Provisioning: When you create a LoadBalancer service, Kubernetes communicates with the cloud provider to automatically provision a load balancer. The specifics of this process depend on the cloud provider.
  3. NodePort and ClusterIP: LoadBalancer services also have a NodePort and ClusterIP. The NodePort allows access to the service from outside the cluster, and the ClusterIP allows internal access within the cluster.
  4. Automatic Scaling: The external load balancer can scale horizontally to distribute traffic across multiple nodes, providing scalability and fault tolerance.
Save this deployment as backend-deployment.yaml

In this example, we define a simple deployment (backend-deployment) that creates three replicas of a backend pod. The pod is labeled with app: backend for selector matching.

Save this service as backend-service.yaml

The backend-service is a LoadBalancer service associated with the backend pods. It exposes port 80 and forwards traffic to the pods on port 8080.

Now, apply both YAML files to create the deployment and service:

Accessing the Service: Once the LoadBalancer service is provisioned, you can access your service externally using the external IP address.

The IP address may take some time to be assigned, depending on your cloud provider.

Look for the EXTERNAL-IP field, and once it's assigned, you can access your service:

Ingress Networking

--

--

Teva Veluppillai

Tech enthusiast, bucket list conqueror, and avid learner exploring the intersection of tech and philosophy. Join me on this journey of continuous growth! 🚀✨