Post

Kubernetes - Services

Delve into Kubernetes Services

Kubernetes - Services

Introduction

Kubernetes Services are a critical component of the Kubernetes ecosystem, enabling reliable communication between different components of a cluster. In this blog, we will explore what Kubernetes Services are, the different types available, and their purposes, including load balancing, service discovery, and headless configurations.

What Are Kubernetes Services?

  • In Kubernetes, a Service is an abstraction that defines a logical set of pods and a policy to access them.
  • Since pods are ephemeral and can be replaced at any time, Services provide a stable way to interact with the underlying pods, ensuring consistent communication within the cluster or with external clients.
  • Key Features of Kubernetes Services:
    • Abstract communication between components.
    • Provide stable IP addresses and DNS names.
    • Enable load balancing across multiple pods.
    • Facilitate service discovery.

Types of Kubernetes Services

Kubernetes Service Types

  • Kubernetes offers four main types of Services, each designed for a specific use case:
  1. ClusterIP (default):
    • Exposes the Service within the cluster.
    • Pods within the cluster can communicate with this Service using its internal IP.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
     apiVersion: v1
     kind: Service
     metadata:  
       name: my-internal-service
     spec:
       selector:    
         app: my-app
       type: ClusterIP
       ports:  
       - name: http
         port: 80
         targetPort: 80
         protocol: TCP
    
  2. NodePort:
    • Exposes the Service on each node’s IP at a static port.
    • Allows external traffic to access the Service through :.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
     apiVersion: v1
     kind: Service
     metadata:  
       name: my-nodeport-service
     spec:
       selector:    
         app: my-app
       type: NodePort
       ports:  
       - name: http
         port: 80
         targetPort: 80
         nodePort: 30036
         protocol: TCP
    
  3. LoadBalancer:
    • Integrates with cloud providers to provision an external load balancer.
    • Routes external traffic to the pods within the Service.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
     apiVersion: v1
     kind: Service
     metadata:
     name: my-loadbalancer-service
     spec:
     selector:
         app: my-app
     type: LoadBalancer
     ports:
         - protocol: TCP
         port: 80
         targetPort: 9376
    
  4. ExternalName:
    • Maps a Service to an external DNS name.
    • No proxying of traffic is performed; it resolves to the external name specified.
    1
    2
    3
    4
    5
    6
    7
    
     apiVersion: v1
     kind: Service
     metadata:
     name: my-externalname-service
     spec:
     type: ExternalName
     externalName: my.database.example.com
    

LoadBalancer Service

  • A LoadBalancer Service is used to expose a Service to external clients through a cloud provider’s load balancing infrastructure.
  • It is particularly useful for applications that need to be accessed publicly, such as APIs or front-end applications.
  • Key Benefits of LoadBalancer Services:
    • Automatically provisions an external load balancer.
    • Simplifies routing by abstracting complex configurations.
    • Balances traffic across multiple pods to ensure reliability and scalability.
  • Example YAML for a LoadBalancer Service:
1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: Service
metadata:
  name: my-loadbalancer-service
spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

Headless Service

  • A Headless Service is a Service without a cluster IP, used for direct pod-to-pod communication or service discovery in StatefulSets.
  • To create a headless Service, you set the clusterIP field to None.
  • Example YAML for a Headless Service:
1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: Service
metadata:
  name: my-headless-service
spec:
  clusterIP: None
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  • Use Cases
    • Stateful applications requiring direct access to individual pod instances.
    • Service discovery in combination with DNS.

Services Summary

ServiceWhat It DoesKey FeaturesExampleUse Case
ClusterIPExposes application only within the cluster.Default service type; internal communication.Frontend app communicating with a backend API.Microservices communication within the cluster.
NodePortExposes application on a static port of each node.External access via NodeIP:NodePort.Accessing a web app on http://<NodeIP>:30007.Local testing or prototyping without load balancer setup.
LoadBalancerExposes application to the internet via a cloud provider’s load balancer.Simplifies external access; cloud-native.Hosting an e-commerce app accessible worldwide.Public-facing applications like websites or APIs.
ExternalNameMaps a Kubernetes service to an external DNS name.Acts as a DNS alias for external services.Connecting a Kubernetes app to an external database (e.g., AWS RDS).Integrating external resources into Kubernetes workloads.
Headless ServiceExposes individual Pods directly without a ClusterIP.Direct Pod-to-Pod communication.Running a distributed database like Cassandra with direct peer addressing.Stateful applications requiring direct Pod communication.

How Services Can Be Used for Service Discovery

  • Kubernetes Services facilitate service discovery by providing a stable DNS name that resolves to the Service’s IP or, in the case of headless Services, directly to the pod IPs.
  • How It Works:
    • DNS Integration: Kubernetes automatically creates DNS records for Services.
    • ClusterIP Services: Resolve to the Service’s IP.
    • Headless Services: Resolve to the individual pod IPs.
  • Example DNS queries:
    • my-service.default.svc.cluster.local resolves to the ClusterIP or pod IPs.
  • This ensures that applications can dynamically discover and communicate with other components, even if the underlying pods change.

How Endpoints Relate to Kubernetes Services

  • Endpoints are objects that represent the IP addresses and ports of the pods associated with a Service.
  • When a Service selects pods based on its label selector, Kubernetes automatically updates the corresponding Endpoint resource.
  • Key Points:
    • Endpoints map Services to the actual pods.
    • For headless Services, Endpoints list all pod IPs directly.
  • You can view Endpoints using:

    1
    
    kubectl get endpoints
    
  • Example Endpoint resource:
1
2
3
4
5
6
7
8
9
10
apiVersion: v1
kind: Endpoints
metadata:
  name: my-service
subsets:
  - addresses:
      - ip: 192.168.1.1
      - ip: 192.168.1.2
    ports:
      - port: 8080

Conclusion

Kubernetes Services are a cornerstone of application communication and discovery within a cluster. Whether you’re exposing applications to the outside world with LoadBalancer Services, enabling pod-to-pod communication with headless Services, or leveraging service discovery through DNS, Kubernetes Services offer a versatile and robust solution. By understanding the relationship between Services and Endpoints, you can build scalable and resilient architectures in your Kubernetes environment.

This post is licensed under CC BY 4.0 by the author.