Mastering Kubernetes Services: The Key to Network Application Exposure
Kubernetes Services exist to simplify the exposure of network applications running in your cluster. They provide a stable endpoint for accessing Pods, which can be dynamic and ephemeral. Without Services, you’d need to manage direct Pod IPs, which can change frequently, leading to a fragile setup. Services abstract this complexity, allowing you to focus on your application rather than the underlying infrastructure.
The Service API defines a logical set of endpoints, typically Pods, and includes policies for accessing them. Each Service has a selector that matches Pods based on labels, ensuring that traffic is routed correctly. Key configuration parameters include port, which specifies the Service’s listening port, and targetPort, which directs traffic to the appropriate port on the Pod. The default protocol is TCP, making it suitable for most applications. Here’s a basic example:
1apiVersion: v1
2kind: Service
3metadata:
4 name: my-service
5spec:
6 selector:
7 app.kubernetes.io/name: MyApp
8 ports:
9 - protocol: TCP
10 port: 80
11 targetPort: 9376In production, you need to be aware of certain gotchas. Ensure that endpoint IPs are not loopback or link-local addresses, as these will cause issues. Also, avoid using cluster IPs of other Services as endpoints, since kube-proxy doesn’t support virtual IPs as destinations. These nuances can trip you up if not handled correctly. Remember that Kubernetes v1.34 introduced some changes, so always check your version compatibility when implementing Services.
Key takeaways
- →Understand the role of Services in exposing Pods over the network.
- →Configure `targetPort` correctly to ensure traffic reaches the right container port.
- →Avoid using loopback or link-local IPs for endpoints to prevent connectivity issues.
- →Use the selector feature to dynamically manage Pods as they scale up or down.
Why it matters
In production, Services are critical for maintaining stable access to your applications, especially as Pods are created and destroyed. They help ensure that your services remain available and resilient to changes in the underlying infrastructure.
Code examples
apiVersion:v1kind:Servicemetadata:name:my-servicespec:selector:app.kubernetes.io/name:MyAppports:-protocol:TCPport:80targetPort:9376apiVersion:v1kind:Servicemetadata:name:nginx-servicespec:selector:app.kubernetes.io/name:proxyports:-name:name-of-service-portprotocol:TCPport:80targetPort:http-web-svc---apiVersion:v1kind:Podmetadata:name:nginxlabels:app.kubernetes.io/name:proxyspec:containers:-name:nginximage:nginx:stableports:-containerPort:80name:http-web-svcapiVersion:discovery.k8s.io/v1kind:EndpointSlicemetadata:name:my-service-1# by convention, use the name of the Service# as a prefix for the name of the EndpointSlicelabels:# You should set the "kubernetes.io/service-name" label.# Set its value to match the name of the Servicekubernetes.io/service-name:my-serviceaddressType:IPv4ports:-name:http# should match with the name of the service port defined aboveappProtocol:httpprotocol:TCPport:9376endpoints:-addresses:-"10.4.5.6"-addresses:-"10.1.2.3"When NOT to use this
The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.
Want the complete reference?
Read official docsUnified observability — logs, uptime monitoring, and on-call in one place. Used by 50,000+ engineering teams to ship faster and sleep better.
Try Better Stack free →Zero-Downtime Migration: From Ingress NGINX to Envoy Gateway
Migrating from Ingress NGINX to Envoy Gateway without downtime is crucial for maintaining service continuity. By leveraging weighted DNS records, you can run both systems simultaneously and control traffic flow seamlessly. This article breaks down the practical steps to achieve this migration effectively.
Mastering Ingress Request Tracing for Multi-Tenant SaaS on Kubernetes
In a multi-tenant SaaS environment, understanding request flows is crucial for maintaining performance and reliability. By implementing end-to-end ingress request tracing, you can track customer requests through your services using Trace IDs and Span IDs.
Building a Cloud Native Platform: Kairos, k0rdent, and bindy in Action
Creating a cloud native platform from scratch can be daunting. With Kairos, you get an immutable Linux distribution that boots from OCI images, ensuring consistency. Dive into how k0rdent and bindy enhance your Kubernetes management and DNS operations.
Get the daily digest
One email. 5 articles. Every morning.
No spam. Unsubscribe anytime.