Kubernetes Service
Kubernetes Service
A Kubernetes Service is an abstraction that defines a set of Pods and a policy by which to access them.
Since Pods in Kubernetes are ephemeral (i.e., they can be created and destroyed), services ensure that
applications can reliably access these Pods, even as they scale, restart, or change. Kubernetes Services offer
stable networking and load balancing for Pods, providing a consistent way to expose applications.
3. Components of a Service
4. Creating a Service
7. Best Practices
In Kubernetes, a Service is an abstraction layer over a set of Pods, typically providing access to them via a
stable IP address or DNS name. The Service ensures that the application is accessible, no matter how Pods
come and go, by providing reliable DNS and load balancing.
• Purpose: Services enable communication between Pods, between applications, and external users
or services.
• Load Balancing: Services can distribute traffic to multiple Pods, ensuring that requests are evenly
balanced and increasing the availability of the application.
There are several types of Services that cater to different use cases, such as ClusterIP, NodePort,
LoadBalancer, and ExternalName. Let’s dive into these types in the next section.
a. ClusterIP (Default)
The ClusterIP Service type is the default type and is used to expose a Service within the Kubernetes cluster.
It assigns a virtual IP (VIP) to the Service, which allows Pods to communicate with each other using the
service name, but it cannot be accessed from outside the cluster.
apiVersion: v1
kind: Service
metadata:
name: my-app-clusterip
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
b. NodePort
A NodePort Service type exposes the Service on each node’s IP at a static port (the NodePort). This allows
external traffic to access the Service by reaching any node in the cluster on that port. It’s often used for
testing or development environments.
• Use Case: Exposing services for testing or when you want a simple external access method without
a load balancer.
Example YAML for NodePort:
apiVersion: v1
kind: Service
metadata:
name: my-app-nodeport
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30007
type: NodePort
c. LoadBalancer
A LoadBalancer Service type is used in cloud environments that support external load balancers (like AWS,
GCP, or Azure). When you create a LoadBalancer service, Kubernetes automatically provisions an external
load balancer that routes traffic to the appropriate Pods in the cluster.
• Use Case: Exposing services to the internet with automatic load balancing, especially in cloud
environments.
Example YAML for LoadBalancer:
apiVersion: v1
kind: Service
metadata:
name: my-app-loadbalancer
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
d. ExternalName
An ExternalName Service type maps a Kubernetes Service to an external DNS name, allowing Kubernetes
to route traffic to an external service or API that is outside of the cluster.
• Use Case: When you need to reference an external service using a Kubernetes DNS name.
Example YAML for ExternalName:
apiVersion: v1
kind: Service
metadata:
name: external-db
spec:
type: ExternalName
externalName: db.example.com
3. Components of a Service
• Selector: The selector is used to associate the Service with Pods that have matching labels. This
ensures that only specific Pods are targeted by the Service.
Example:
selector:
app: my-app
• Ports: A Service has one or more ports that define how the Service communicates with Pods. These
include:
o port: The port the service exposes to the outside world (or within the cluster).
o targetPort: The port on the Pod that the service routes traffic to.
Example:
ports:
- protocol: TCP
port: 80
targetPort: 8080
• ClusterIP: This is the virtual IP address assigned to the Service. It is used for internal
communication.
4. Creating a Service
Creating a Kubernetes Service typically involves defining it in a YAML file and then applying it to the cluster
using kubectl.
Basic Steps:
1. Define the Service: Create a YAML file that defines the Service type, selector, and ports.
2. Apply the Configuration: Use kubectl apply to create the Service in the cluster.
Kubernetes provides automatic service discovery through DNS. When a Service is created, Kubernetes
assigns it a DNS name that can be used by other Pods in the cluster to access the Service.
For example, if you create a Service named my-app-service, other Pods can access it by using the DNS name
my-app-service.default.svc.cluster.local.
• DNS Resolution: The DNS service automatically resolves the Service’s IP address, and Kubernetes
ensures that traffic is routed correctly.
• Endpoints: The Service maintains a list of Endpoints, which are the IPs of the Pods selected by the
Service. These Endpoints can be viewed with the following command:
Service discovery also works with external services in case of ExternalName services.
• Internal Services: Most services are designed to be internal to the cluster, meaning they handle
traffic between Pods. This is done using the ClusterIP or NodePort service types.
• External Services: Services like LoadBalancer or ExternalName can expose services outside the
cluster. This is typically used for production deployments that need to be accessed by users or other
external systems.
• Headless Services: Sometimes, you might not want Kubernetes to provide load balancing. In these
cases, you can create a headless service by setting clusterIP: None. This is useful when you want
direct access to individual Pods (e.g., for stateful applications).
7. Best Practices
To ensure the reliable operation and scalability of Kubernetes Services, consider the following best
practices:
• Use Labels and Selectors: Always use clear, consistent labels to define selectors for Services. This
ensures that the correct Pods are selected and helps maintain clear relationships between Pods and
Services.
Example:
selector:
app: my-app
version: v1
• Define Resource Limits: Set resource requests and limits on your Pods to ensure that the Service
has enough resources to handle the expected load.
• Health Checks: Use liveness and readiness probes for Pods to ensure they are only included in the
Service’s pool of available Pods when they are healthy and ready to serve traffic.
• Security: Use network policies and RBAC to secure the traffic between services and manage who
can access each service.
• Use LoadBalancer Wisely: While the LoadBalancer service type is highly useful for exposing services
to the internet, it should be used in environments where the underlying cloud infrastructure
supports it (e.g., AWS, GCP).
Kubernetes Services provide a stable way to expose and access applications running inside a cluster.
Depending on your use case, you can choose from different types of Services. Each type has specific
characteristics, and understanding these differences is crucial for selecting the right service for your
application. Below is a comparison of the four main Kubernetes Service types: ClusterIP, NodePort,
LoadBalancer, and ExternalName.
Feature ClusterIP NodePort LoadBalancer ExternalName
<service-
DNS <service- <service- <service- name>.<namespace>.sv
name>.<namespace>.sv name>.<namespace>.sv name>.<namespace>.sv c.cluster.local resolves to
Name c.cluster.local c.cluster.local c.cluster.local
an external DNS name.
Production-grade
Internal Development or simple applications that require
Accessing third-party
Best Use microservices or testing, when access to high availability and
services or APIs from
Case communication the Service is needed access from outside the
within the cluster.
between Pods. from outside the cluster. cluster, typically in cloud
environments.
Conclusion
Kubernetes Services are an essential component of the platform, providing stable access to Pods, load
balancing, and network abstraction. They simplify the process of managing and exposing applications,
ensuring that they are accessible, resilient, and scalable. Understanding the different types of Services and
how to configure and use them properly will help you build and maintain robust Kubernetes-based
applications.