Configure Services
In Kubernetes, a Service is a method for exposing a network application that is running as one or more Pods in your cluster.
TOC
Why Service is Needed
-
Pods have their own IPs, but:
-
Pod IPs are not stable (they change if the Pod is recreated).
-
Directly accessing Pods becomes unreliable.
-
-
Service solves this by providing:
-
A stable IP and DNS name.
-
Automatic load balancing to the matching Pods.
-
Example ClusterIP type Service:
- The available type values and their behaviors are
ClusterIP,NodePort,LoadBalancer,ExternalName - The set of Pods targeted by a Service is usually determined by a selector that you define.
Serviceport.- Bind
targetPortof the Service to the PodcontainerPort. In addition, you can referenceport.nameunder the pod container.
Headless Services
Sometimes you don't need load-balancing and a single Service IP. In this case, you can create what are termed headless Services:
Headless Services are useful when:
-
You want to discover individual Pod IPs, not just a single service IP.
-
You need direct connections to each Pod (e.g., for databases like Cassandra or StatefulSets).
-
You're using StatefulSets where each Pod must have a stable DNS name.
Creating a service by using the web console
-
Go to Container Platform.
-
In the left navigation bar, click Network > Services.
-
Click Create Service.
-
Refer to the following instructions to configure the relevant parameters.
-
Click Create.
Creating a service by using the CLI
Create a service based on an existing deployment resource my-app.
Example: Accessing an Application Within the Cluste
-
Apply this YAML:
-
Starting another Pod:
-
Accessing the
nginx-clusteripservice intest-podPod:
You should see a HTML response containing text like "Welcome to nginx!".
Example: Accessing an Application Outside the Cluste
-
Apply this YAML:
-
Checking Pods:
-
curl Service:
You should see a HTML response containing text like "Welcome to nginx!".
Of course, it is also possible to access the application from outside the cluster by creating a Service of type LoadBalancer.
Note: Please configure the LoadBalancer service beforehand.
-
Apply this YAML:
-
Get external ip address:
EXTERNAL-IPis the address you access from your browser.
You should see a HTML response containing text like "Welcome to nginx!".
If EXTERNAL-IP is pending, the Loadbalancer service is not currently deployed on the cluster.
Example: ExternalName type of Servce
-
Apply this YAML:
-
Try to resolve inside a Pod in the cluster:
then:
You'll see that it resolves to example.com.
LoadBalancer Type Service Annotations
AWS EKS Cluster
For detailed explanations of the EKS LoadBalancer Service annotations, please refer to the Annotation Usage Documentation .
Huawei Cloud CCE Cluster
For detailed explanations of the CCE LoadBalancer Service annotations, please refer to the Annotation Usage Documentation .
Azure AKS Cluster
For detailed explanations of the AKS LoadBalancer Service annotations, please refer to the Annotation Usage Documentation .
Google GKE Cluster
For detailed explanations of the GKE LoadBalancer Service annotations, please refer to the Annotation Usage Documentation .
Example: LoadBalancer with MetalLB BGP and Local Traffic Policy
This example demonstrates how to configure a LoadBalancer Service using MetalLB BGP mode with externalTrafficPolicy: Local to achieve active-active load balancing without extra network hops.
Benefits
- Active-active load balancing: Traffic is distributed across multiple nodes simultaneously
- No extra network hops: Direct routing to pods without intermediate node forwarding
- Better performance:
externalTrafficPolicy: Localpreserves source IP and reduces latency - High availability: BGP route announcements ensure traffic reaches healthy nodes
Prerequisites
Before configuring the LoadBalancer Service, ensure you have:
- MetalLB deployed: See Creating External IP Address Pool for installation
- BGP Peer configured: See Creating BGP Peers for BGP setup
- External IP address pool: Configure an IPAddressPool with BGPAdvertisement
Steps
Deploy your application with a LoadBalancer Service using externalTrafficPolicy: Local:
Key Configuration Points
externalTrafficPolicy: Local
The externalTrafficPolicy: Local setting provides several benefits:
- Source IP preservation: Client source IP is maintained, enabling proper logging and security policies
- Direct pod routing: Traffic goes directly to pods without node-level forwarding
LoadBalancer with BGP
When using MetalLB with BGP mode:
- Routes are advertised from nodes specified in the BGPAdvertisement nodeSelectors
- The BGP peer receives these announcements and can route traffic accordingly
- Node selector alignment between BGPPeer and BGPAdvertisement ensures consistent routing
Deployment Steps
-
Deploy the application:
-
Verify the LoadBalancer Service:
Expected output:
-
Test the service:
Verification
- Monitor service endpoints:
kubectl get endpoints nginx-loadbalancer-local - Check service status:
kubectl describe svc nginx-loadbalancer-local