In Kubernetes, a Service is a method for exposing a network application that is running as one or more Pods in your cluster.
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.
ClusterIP
, NodePort
, LoadBalancer
, ExternalName
Service
port.targetPort
of the Service to the Pod containerPort
. In addition, you can reference port.name
under the pod container.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.
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.
Parameter | Description |
---|---|
Virtual IP Address | If enabled, a ClusterIP will be allocated for this Service, which can be used for service discovery within the cluster. If disabled, a Headless Service will be created, which is usually used by StatefulSet. |
Type |
|
Target Component |
|
Port | Used to configure the port mapping for this Service. In the following example, other podss within the cluster can call this Service via the virtual IP (if enabled) and TCP port 80; the access requests will be forwarded to the externally exposed TCP port 6379 or redis of the target component's pods.
|
Session Affinity | Session affinity based on the source IP address (ClientIP). If enabled, all access requests from the same IP address will be kept on the same server during load balancing, ensuring that requests from the same client are forwarded to the same server for processing. |
Click Create.
Create a service based on an existing deployment resource my-app
.
nginx-clusterip
service in test-pod
Pod:You should see a HTML response containing text like "Welcome to nginx!".
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.
EXTERNAL-IP
is 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.
then:
You'll see that it resolves to example.com
.
For detailed explanations of the EKS LoadBalancer Service annotations, please refer to the Annotation Usage Documentation .
Key | Value | Description |
---|---|---|
service.beta.kubernetes.io/aws-load-balancer-type | external: Use the official AWS LoadBalancer Controller. | Specifies the controller for the LoadBalancer type. Note: Please contact the platform administrator in advance to deploy the AWS LoadBalancer Controller. |
service.beta.kubernetes.io/aws-load-balancer-nlb-target-type |
| Specifies how traffic reaches the pods. |
service.beta.kubernetes.io/aws-load-balancer-scheme |
| Specifies whether to use a private network or a public network. |
service.beta.kubernetes.io/aws-load-balancer-ip-address-type |
| Specifies the supported IP address stack. |
For detailed explanations of the CCE LoadBalancer Service annotations, please refer to the Annotation Usage Documentation .
Key | Value | Description |
---|---|---|
kubernetes.io/elb.id | Fill in the ID of the cloud load balancer, must use an existing cloud load balancer. | |
kubernetes.io/elb.autocreate | Example: {"type":"public","bandwidth_name":"cce-bandwidth-1551163379627","bandwidth_chargemode":"bandwidth","bandwidth_size":5,"bandwidth_sharetype":"PER","eip_type":"5_bgp","available_zone":["cn-north-4b"],"l4_flavor_name":"L4_flavor.elb.s1.small"} Note: Please read the Filling Instructions first and adjust the example parameters as needed. | New cloud load balancer to be created. |
kubernetes.io/elb.subnet-id | The ID of the subnet where the cluster is located. When the Kubernetes version is 1.11.7-r0 or lower, this field must be filled when creating a new cloud load balancer. | |
kubernetes.io/elb.class |
| Specifies the type of the new cloud load balancer to be created, please refer to Differences Between Exclusive and Shared Elastic Load Balancing. |
kubernetes.io/elb.enterpriseID | Specifies the enterprise project to which the newly created cloud load balancer belongs. |
For detailed explanations of the AKS LoadBalancer Service annotations, please refer to the Annotation Usage Documentation .
Key | Value | Description |
---|---|---|
service.beta.kubernetes.io/azure-load-balancer-internal |
| Specifies whether to use a private network or a public network. |
For detailed explanations of the GKE LoadBalancer Service annotations, please refer to the Annotation Usage Documentation .
Key | Value | Description |
---|---|---|
networking.gke.io/load-balancer-type | Internal | Specifies the use of a private network. |
loud.google.com/l4-rbs | enabled | Defaults to public. If this parameter is configured, traffic will route directly to the pods. |