OpsCart Hub

Kubernetes Services: A Comprehensive Guide

Table of Contents

  1. What is a Kubernetes Service?
  2. Types of Kubernetes Services
  3. How Traffic is Routed in Kubernetes Services
  4. Use Cases for Each Service Type
  5. Azure AKS Example: Using LoadBalancer
  6. How Requests are Routed: NodePort Example
  7. Key Takeaways
  8. Further Reading

Kubernetes Services are a fundamental building block for exposing applications running in a Kubernetes cluster. They provide a stable endpoint (IP address and port) for accessing your applications, regardless of where the pods are running. In this guide, we’ll explore the different types of Kubernetes Services, their use cases, and how traffic is routed in Azure AKS.


What is a Kubernetes Service?

A Kubernetes Service is an abstraction that defines a logical set of pods and a policy to access them. It enables:

  • Service Discovery: Automatically assigns a DNS name to the service.
  • Load Balancing: Distributes traffic across multiple pods.
  • Stable Networking: Provides a consistent IP address and port, even as pods are created or destroyed.

Types of Kubernetes Services

Kubernetes supports four main types of Services:

1. ClusterIP

  • Description: The default Service type. It exposes the Service on a cluster-internal IP address.
  • Use Case: Used for internal communication between pods within the cluster.
  • Example:
    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
    spec:
      type: ClusterIP
      ports:
        - port: 80
          targetPort: 8080
      selector:
        app: my-app
    

2. NodePort

  • Description: Exposes the Service on a static port on each node’s IP address. Automatically includes ClusterIP functionality.
  • Use Case: Used for external access to the cluster when a LoadBalancer is not available.
  • Example:
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 30007
  selector:
    app: my-app

3. LoadBalancer

  • Description: Exposes the Service externally using a cloud provider’s load balancer. Automatically includes NodePort and ClusterIP functionality.
  • Use Case: Used for production workloads that require external access.
  • Example:
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: my-app

4. ExternalName

  • Description: Maps the Service to an external DNS name (e.g., a database hosted outside the cluster).
  • Use Case: Used for integrating with external services.
  • Example:
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: ExternalName
  externalName: my.database.example.com

How Traffic is Routed in Kubernetes Services

ClusterIP

  • Traffic is routed internally within the cluster.
  • Pods can access the Service using its ClusterIP and port.

NodePort

  • Traffic from outside the cluster is routed to a specific port on each node.
  • The node forwards the traffic to the Service, which then routes it to the appropriate pod.

LoadBalancer

  • The cloud provider’s load balancer routes traffic to the nodes.
  • The nodes forward the traffic to the Service, which then routes it to the appropriate pod.

Use Cases for Each Service Type

ClusterIP

  • Internal microservices communication.
  • Backend services that don’t need external access.

NodePort

  • Development and testing environments.
  • Situations where a LoadBalancer is not available.

LoadBalancer

  • Production workloads that require external access.
  • Public-facing applications (e.g., web servers).

ExternalName

  • Integrating with external databases or APIs.
  • Migrating from on-premise to cloud-native services.

Azure AKS Example: Using LoadBalancer

In Azure AKS, a LoadBalancer Service automatically provisions an Azure Load Balancer to route traffic to your pods.

Example YAML

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: my-app

How It Works

  • Azure provisions a public IP address and associates it with the Load Balancer.
  • The Load Balancer routes traffic to the nodes in the AKS cluster.
  • The nodes forward the traffic to the Service, which routes it to the appropriate pod.

How Requests are Routed: NodePort Example

Let’s break down how a request is routed when using a NodePort Service:

  • External Request: A user sends a request to : (e.g., 203.0.113.1:30007).
  • Node Routing: The node receives the request and forwards it to the Service.
  • Service Routing: The Service routes the request to one of the pods based on the selector.
  • Pod Response: The pod processes the request and sends a response back to the user.

Key Takeaways

  • ClusterIP: Use for internal communication.
  • NodePort: Use for external access when a LoadBalancer is not available.
  • LoadBalancer: Use for production workloads with external access.
  • ExternalName: Use for integrating with external services.

Further Reading

Powered by 2025 OpsCart Hub. Theme by TechDoc.