Understanding Kubernetes Services: A Guide with YAML Examples

Parth SharmaParth Sharma
4 min read

In Kubernetes, a Service is like a friendly middleman that makes it easy for other applications or users to communicate with your application, no matter where it is running inside the cluster.

Here’s a simple analogy: Imagine you have a food delivery service with multiple chefs (your application’s pods) working in a kitchen. Customers don’t need to know which chef is cooking their food; they just place their order through the service counter (the Kubernetes Service), and the counter makes sure their request gets to the right chef. Even if one chef takes a break (a pod goes down), the service counter keeps working by redirecting the request to another available chef.

What Is a Kubernetes Service?

A Kubernetes Service acts as an abstraction layer that defines a logical set of Pods and a policy by which to access them. It enables communication between different components of your application, seamlessly distributing traffic to the appropriate Pods, even as they scale up or down.

Key Features of Kubernetes Services

  1. Load Balancing: Automatically distributes incoming network traffic across multiple Pods.

  2. Service Discovery: Allows applications to discover and communicate with each other using a stable IP address and DNS name.

  3. Decoupling: Helps decouple different parts of your application, making it more flexible and resilient.

Types of Kubernetes Services

Kubernetes supports several types of Services, including:

  • ClusterIP: The default type, exposing the Service on a cluster-internal IP. This Service is only accessible within the cluster.

  • NodePort: Exposes the Service on each Node’s IP at a static port. It allows external traffic to access the Service.

  • LoadBalancer: Creates an external load balancer in the cloud (if supported) and assigns a fixed, external IP to the Service.

  • ExternalName: Maps the Service to the contents of the externalName field (e.g., some.external.service.com).

Creating a Kubernetes Service: A YAML Example

Below, we’ll show how to define a ClusterIP Service using a YAML file. This Service will route traffic to a set of Pods running a simple web application.

Step 1: Defining a Deployment

First, let’s create a Deployment to manage our Pods. This example uses a simple Nginx application.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Step 2: Creating the Service

Now, let’s define the corresponding Service.

apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  selector:
    app: web
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: ClusterIP

Explanation of the Service YAML

  • apiVersion: Specifies the API version of the Service (v1).

  • kind: Identifies the Kubernetes resource type; here it's a Service.

  • metadata: Holds the service metadata, such as its name.

  • spec: Defines the specifications of the Service:

    • selector: This specifies how to find the Pods that will receive traffic. In this case, it matches Pods with the label app: web.

    • ports: Defines the list of ports that the Service will expose. Here, we forward traffic from port 80 on the Service to port 80 on the Pods (targetPort).

    • type: Denotes the type of Service. We’re using the default ClusterIP, which only allows internal access.

Deploying the Configuration

To deploy both the Deployment and Service, save the YAML definitions in a file (e.g., deployment.yaml) and use the following command:

kubectl apply -f deployment.yaml

After deployment, you can verify that the Service and Pods are running with:

kubectl get deployments
kubectl get services

𝑹𝒆𝒂𝒍-𝒘𝒐𝒓𝒍𝒅 𝑬𝒙𝒂𝒎𝒑𝒍𝒆 𝒊𝒏 𝑲𝒖𝒃𝒆𝒓𝒏𝒆𝒕𝒆𝒔:

Let’s say you’re running a backend API for an e-commerce website, and you have multiple pods handling incoming API requests. Instead of making users figure out which pod to contact, you set up a Kubernetes Service. The Service:

\> Provides a consistent API endpoint (api.example.com or a ClusterIP).

\> Routes incoming requests to any of the healthy pods in your backend.

Even if one pod crashes or new pods are added (e.g., during scaling), the Service ensures uninterrupted customer experience. All the complexity is hidden behind the Service’s simplicity.

This helps you manage dynamic and resilient communication within your Kubernetes cluster.

Conclusion

Kubernetes Services are crucial for managing the communication between your Pods, ensuring that your application remains accessible and resilient. By using a Service, you can easily route traffic, balance loads, and decouple your application components.

This example has shown how to create a simple Service in Kubernetes using YAML. As your application grows, you'll want to explore other Service types and configurations to best meet your needs.

"I believe this article will be beneficial, allowing you to uncover fresh insights and gain enriching knowledge."

Happy Learning🙂

PARTH SHARMA

0
Subscribe to my newsletter

Read articles from Parth Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Parth Sharma
Parth Sharma