Configuring a Range of Listening Ports for LoadBalancer Services
When creating a LoadBalancer Service, you can specify a port range for the ELB listener. This allows the listener to receive requests on ports within the specified range and forward them to the target backend servers.
This function requires ELB. Before using this function, check whether ELB supports full-port listening and forwarding for layer-4 protocols in the current region.
Prerequisites
- A Kubernetes cluster is available and the cluster version meets the following requirements:
- v1.23: v1.23.18-r0 or later
- v1.25: v1.25.13-r0 or later
- v1.27: v1.27.10-r0 or later
- v1.28: v1.28.8-r0 or later
- v1.29: v1.29.4-r0 or later
- v1.30: v1.30.1-r0 or later
- To create a cluster using commands, ensure kubectl is used. For details, see Accessing a Cluster Using kubectl.
Precautions
- Listening port ranges can be configured only when you use a dedicated load balancer with TCP, UDP, or TLS.
- ELB listeners do not support direct port modification. When a Service's port range changes, CCE deletes the original listener and creates a new one. This causes a brief service interruption (typically a few seconds) during listener rebuild. Evaluate the impact before changing Service ports and proceed with caution.
Creating a LoadBalancer Service and Configuring a Range of Listening Ports
Use one of the following methods.
- Log in to the CCE console and click the cluster name to access the cluster console.
- In the navigation pane, choose Services and Ingresses. In the upper right corner, click Create Service.
In this example, only mandatory parameters for configuring SNI are listed. Retain the default settings for other parameters. For details, see Using the CCE Console.
- Configure basic parameters.
Parameter
Description
Example
Service Type
Select LoadBalancer.
None
Service Name
Enter a name, which can be the same as the workload name.
nginx
Namespace
Select the namespace that the workload belongs to.
default
Selector
Add the key and value of a pod label. The Service will be associated with the workload pods based on the label and direct traffic to the pods with this label.
You can also click Reference Workload Label to use the label of an existing workload. In the dialog box displayed, select a workload and click OK.
app:nginx

- Configure load balancer parameters.
Parameter
Description
Example
Load Balancer
Select a load balance type and how the load balancer will be created.- Use existing: Only the load balancers in the same VPC as the cluster can be selected. If no load balancer is available, click Create Load Balancer to create one on the ELB console.
- Auto create: The load balancer will be created in the VPC that the cluster belongs to. For details, see Table 1.
An existing Dedicated load balancer of the Network (TCP/UDP/TLS) & Application (HTTP/HTTPS) type

- Configure access parameters.
Parameter
Description
Example
Service Affinity
Whether to route external traffic to a local node or a cluster-wide endpoint. For details, see Service Affinity (externalTrafficPolicy).- Cluster-level: The IP addresses and ports of all nodes in a cluster can access the workload associated with the Service. However, accessing the Service may result in performance deterioration due to route redirection, and the client's source IP address may not be obtainable.
- Node-level: Only the IP address and port of the node where the workload is located can access the workload associated with the Service. Accessing the Service will not result in a performance decrease due to route redirection, and the client's source IP address can be obtained.
Cluster-level
Port
- Protocol: the protocol used by the Service.
- Container Port: the port that the workload listens on. For example, Nginx uses port 80 by default.
- Service Port: the port used by the Service.
- Listen on a port: The port ranges from 1 to 65535.
- Listen on ports: ELB allows you to create listeners that listen on ports within specified ranges. Each listener can support up to 10 non-overlapping port ranges.
To configure port ranges for load balancer listeners, ensure the following conditions are met:
- The cluster version must be v1.23.18-r0, v1.25.13-r0, v1.27.10-r0, v1.28.8-r0, v1.29.4-r0, v1.30.1-r0, or later.
- A dedicated load balancer must be used with TCP/UDP selected.
- This function requires ELB. Before using this function, check whether ELB supports full-port listening and forwarding for layer-4 protocols in the current region.
- Frontend Protocol: Set the protocol of the load balancer listener for establishing connections with clients. When a dedicated load balancer is selected, HTTP/HTTPS can be configured only when Application (HTTP/HTTPS) is selected.
NOTE:When a LoadBalancer Service is created, a random node port number (NodePort) is automatically generated.
- Protocol: TCP
- Container Port: 80
- Service Port: 80-88
- Frontend Protocol: TCP

- Click Create.
- Use kubectl to access the cluster. For details, see Accessing a Cluster Using kubectl.
- Create a YAML file named service-test.yaml. The file name can be customized.
vi service-test.yaml
An example YAML file of a Service associated with an existing load balancer is as follows:apiVersion: v1 kind: Service metadata: name: service-test labels: app: test version: v1 namespace: default annotations: kubernetes.io/elb.class: performance # A dedicated load balancer is required. kubernetes.io/elb.id: <your_elb_id> # Replace it with the ID of your existing load balancer. kubernetes.io/elb.port-ranges: '{"cce-service-0":["100,200", "300,400"], "cce-service-1":["500,600", "700,800"]}' # Configure ranges of listening ports. spec: selector: app: test version: v1 externalTrafficPolicy: Cluster ports: - name: cce-service-0 targetPort: 80 # Replace it with your container port. nodePort: 0 port: 100 # If a port range is configured for listening, this parameter becomes invalid. However, it still needs to be assigned a unique value. By default, it is set to the starting port number of the range. protocol: TCP - name: cce-service-1 targetPort: 81 # Replace it with your container port. nodePort: 0 port: 500 # If a port range is configured for listening, this parameter becomes invalid. However, it still needs to be assigned a unique value. protocol: TCP type: LoadBalancer loadBalancerIP: <your_elb_ip> # Replace it with the private IP address of your existing load balancer.Table 1 Parameters for listening to ports within a range Parameter
Mandatory
Type
Description
kubernetes.io/elb.port-ranges
Yes
String
If a dedicated load balancer is used and the TCP, UDP, or TLS protocol is selected, you can create a listener that listens to ports within a certain range from 1 to 65535. You can add a maximum of 10 port ranges that do not overlap for each listener.
The parameter value is in the following format, where ports_name and port must be unique:
'{"<ports_name_1>":["<port_1>,<port_2>","<port_3>,<port_4>"], "<ports_name_2>":["<port_5>,<port_6>","<port_7>,<port_8>"]}'For example, the port names are cce-service-0 and cce-service-1, and the listener listens to ports 100–200 and 300–400, and 500–600 and 700–800, respectively.
'{"cce-service-0":["100,200", "300,400"], "cce-service-1":["500,600", "700,800"]}' - Create the Service.
kubectl create -f service-test.yaml
If information similar to the following is displayed, the Service has been created:
service/service-test created
Verifying Configuration
- Log in to the CCE console and click the cluster name to access the cluster console.
- In the navigation pane, choose Services and Ingresses. Locate the row that contains the created Service and click the load balancer name to go to the ELB console.
- On the Listeners tab page, check the port number of the new listener.
