LoadBalancer Services can access workloads from the public network through ELB, which is more reliable than EIP-based access. The LoadBalancer access address is in the format of IP address of public network load balancer:Access port, for example, 10.117.117.117:80.
In this access mode, requests are transmitted through an ELB load balancer to a node and then forwarded to the destination pod through the Service.
When CCE Turbo clusters and dedicated load balancers are used, passthrough networking is supported to reduce service latency and ensure zero performance loss.
External access requests are directly forwarded from a load balancer to pods. Internal access requests can be forwarded to a pod through a Service.
A load balancer can be dedicated or shared.
How to Create |
Configuration |
---|---|
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 |
|
You can click in the Set ELB area and configure load balancer parameters in the Set ELB dialog box.
Parameter |
Description |
---|---|
Protocol |
When the protocol of Port is set to TCP, the TCP and HTTP are supported. When the protocol of Port is set to UDP, the UDP is supported.
|
Port |
By default, the service port (NodePort or container port of the Service) is used for health check. You can also specify another port for health check. After the port is specified, a service port named cce-healthz will be added for the Service.
|
Check Period (s) |
Specifies the maximum interval between health checks. The value ranges from 1 to 50. |
Timeout (s) |
Specifies the maximum timeout duration for each health check. The value ranges from 1 to 50. |
Max. Retries |
Specifies the maximum number of health check retries. The value ranges from 1 to 10. |
When a LoadBalancer Service is created, a random node port number (NodePort) is automatically generated.
You can set the Service when creating a workload using kubectl. This section uses an Nginx workload as an example to describe how to add a LoadBalancer Service using kubectl.
The file names are user-defined. nginx-deployment.yaml and nginx-elb-svc.yaml are merely example file names.
vi nginx-deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - image: nginx name: nginx imagePullSecrets: - name: default-secret
vi nginx-elb-svc.yaml
Before enabling sticky session, ensure that the following conditions are met:
apiVersion: v1 kind: Service metadata: name: nginx annotations: kubernetes.io/elb.id: <your_elb_id> # ELB ID. Replace it with the actual value. kubernetes.io/elb.class: union # Load balancer type kubernetes.io/elb.lb-algorithm: ROUND_ROBIN # Load balancer algorithm kubernetes.io/elb.session-affinity-mode: SOURCE_IP # The sticky session type is source IP address. kubernetes.io/elb.session-affinity-option: '{"persistence_timeout": "30"}' # Stickiness duration (min) kubernetes.io/elb.health-check-flag: 'on' # Enable the ELB health check function. kubernetes.io/elb.health-check-option: '{ "protocol":"TCP", "delay":"5", "timeout":"10", "max_retries":"3" }' spec: selector: app: nginx ports: - name: service0 port: 80 # Port for accessing the Service, which is also the listener port on the load balancer. protocol: TCP targetPort: 80 # Port used by a Service to access the target container. This port is closely related to the applications running in a container. nodePort: 31128 # Port number of the node. If this parameter is not specified, a random port number ranging from 30000 to 32767 is generated. type: LoadBalancer
The preceding example uses annotations to implement some advanced functions of load balancing, such as sticky session and health check. For details, see Table 3.
For more annotations and examples related to advanced functions, see Using Annotations to Balance Load.
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
kubernetes.io/elb.id |
Yes |
String |
ID of an enhanced load balancer. Mandatory when an existing load balancer is to be associated. How to obtain: On the management console, click Service List, and choose Networking > Elastic Load Balance. Click the name of the target load balancer. On the Summary tab page, find and copy the ID. NOTE:
The system preferentially connects to the load balancer based on the kubernetes.io/elb.id field. If this field is not specified, the spec.loadBalancerIP field is used (optional and available only in 1.23 and earlier versions). Do not use the spec.loadBalancerIP field to connect to the load balancer. This field will be discarded by Kubernetes. For details, see Deprecation. |
kubernetes.io/elb.class |
Yes |
String |
Select a proper load balancer type. The value can be:
NOTE:
If a LoadBalancer Service accesses an existing dedicated load balancer, the dedicated load balancer must support TCP/UDP networking. |
kubernetes.io/elb.lb-algorithm |
No |
String |
Specifies the load balancing algorithm of the backend server group. The default value is ROUND_ROBIN. Options:
NOTE:
If this parameter is set to SOURCE_IP, the weight setting (weight field) of backend servers bound to the backend server group is invalid, and sticky session cannot be enabled. |
kubernetes.io/elb.session-affinity-mode |
No |
String |
Source IP address-based sticky session is supported. That is, access requests from the same IP address are forwarded to the same backend server.
NOTE:
When kubernetes.io/elb.lb-algorithm is set to SOURCE_IP (source IP hash), sticky session cannot be enabled. |
kubernetes.io/elb.session-affinity-option |
No |
Table 4 object |
Sticky session timeout. |
kubernetes.io/elb.health-check-flag |
No |
String |
Whether to enable the ELB health check.
If this parameter is enabled, the kubernetes.io/elb.health-check-option field must also be specified at the same time. |
kubernetes.io/elb.health-check-option |
No |
Table 5 object |
ELB health check configuration items. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
persistence_timeout |
Yes |
String |
Sticky session timeout, in minutes. This parameter is valid only when elb.session-affinity-mode is set to SOURCE_IP. Value range: 1 to 60. Default value: 60 |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
delay |
No |
String |
Health check interval (s) Value range: 1 to 50. Default value: 5 |
timeout |
No |
String |
Health check timeout, in seconds. Value range: 1 to 50. Default value: 10 |
max_retries |
No |
String |
Maximum number of health check retries. Value range: 1 to 10. Default value: 3 |
protocol |
No |
String |
Health check protocol. Value options: TCP or HTTP |
path |
No |
String |
Health check URL. This parameter needs to be configured when the protocol is HTTP. Default value: / Value range: 1-80 characters |
kubectl create -f nginx-deployment.yaml
If information similar to the following is displayed, the workload has been created.
deployment/nginx created
kubectl get pod
If information similar to the following is displayed, the workload is running.
NAME READY STATUS RESTARTS AGE nginx-2601814895-c1xhw 1/1 Running 0 6s
kubectl create -f nginx-elb-svc.yaml
If information similar to the following is displayed, the Service has been created.
service/nginx created
kubectl get svc
If information similar to the following is displayed, the access type has been set, and the workload is accessible.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.247.0.1 <none> 443/TCP 3d nginx LoadBalancer 10.247.130.196 10.78.42.242 80:31540/TCP 51s
The Nginx is accessible.
You can set the Service when creating a workload using kubectl. This section uses an Nginx workload as an example to describe how to add a LoadBalancer Service using kubectl.
The file names are user-defined. nginx-deployment.yaml and nginx-elb-svc.yaml are merely example file names.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - image: nginx name: nginx imagePullSecrets: - name: default-secret
vi nginx-elb-svc.yaml
Before enabling sticky session, ensure that the following conditions are met:
apiVersion: v1 kind: Service metadata: annotations: kubernetes.io/elb.class: union kubernetes.io/elb.autocreate: '{ "type": "public", "bandwidth_name": "cce-bandwidth-1551163379627", "bandwidth_chargemode": "traffic", "bandwidth_size": 5, "bandwidth_sharetype": "PER", "vip_subnet_cidr_id": "*****", "vip_address": "**.**.**.**", "eip_type": "5_bgp" }' kubernetes.io/elb.lb-algorithm: ROUND_ROBIN # Load balancer algorithm kubernetes.io/elb.session-affinity-mode: SOURCE_IP # The sticky session type is source IP address. kubernetes.io/elb.session-affinity-option: '{"persistence_timeout": "30"}' # Stickiness duration (min) kubernetes.io/elb.health-check-flag: 'on' # Enable the ELB health check function. kubernetes.io/elb.health-check-option: '{ "protocol":"TCP", "delay":"5", "timeout":"10", "max_retries":"3" }' labels: app: nginx name: nginx spec: ports: - name: service0 port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: LoadBalancer
apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx namespace: default annotations: kubernetes.io/elb.class: performance kubernetes.io/elb.autocreate: '{ "type": "public", "bandwidth_name": "cce-bandwidth-1626694478577", "bandwidth_chargemode": "traffic", "bandwidth_size": 5, "bandwidth_sharetype": "PER", "eip_type": "5_bgp", "vip_subnet_cidr_id": "*****", "vip_address": "**.**.**.**", "elb_virsubnet_ids": ["*****"], "ipv6_vip_virsubnet_id": "*****", "available_zone": [ "" ], "l4_flavor_name": "L4_flavor.elb.s1.small" }' kubernetes.io/elb.lb-algorithm: ROUND_ROBIN # Load balancer algorithm kubernetes.io/elb.session-affinity-mode: SOURCE_IP # The sticky session type is source IP address. kubernetes.io/elb.session-affinity-option: '{"persistence_timeout": "30"}' # Stickiness duration (min) kubernetes.io/elb.health-check-flag: 'on' # Enable the ELB health check function. kubernetes.io/elb.health-check-option: '{ "protocol":"TCP", "delay":"5", "timeout":"10", "max_retries":"3" }' spec: selector: app: nginx ports: - name: cce-service-0 targetPort: 80 nodePort: 0 port: 80 protocol: TCP type: LoadBalancer
The preceding example uses annotations to implement some advanced functions of load balancing, such as sticky session and health check. For details, see Table 6.
For more annotations and examples related to advanced functions, see Using Annotations to Balance Load.
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
kubernetes.io/elb.class |
Yes |
String |
Select a proper load balancer type. The value can be:
|
kubernetes.io/elb.autocreate |
Yes |
elb.autocreate object |
Whether to automatically create a load balancer associated with the Service. Example
|
kubernetes.io/elb.subnet-id |
None |
String |
ID of the subnet where the cluster is located. The value can contain 1 to 100 characters.
|
kubernetes.io/elb.lb-algorithm |
No |
String |
Specifies the load balancing algorithm of the backend server group. The default value is ROUND_ROBIN. Options:
NOTE:
If this parameter is set to SOURCE_IP, the weight setting (weight field) of backend servers bound to the backend server group is invalid, and sticky session cannot be enabled. |
kubernetes.io/elb.session-affinity-mode |
No |
String |
Source IP address-based sticky session is supported. That is, access requests from the same IP address are forwarded to the same backend server.
NOTE:
When kubernetes.io/elb.lb-algorithm is set to SOURCE_IP (source IP hash), sticky session cannot be enabled. |
kubernetes.io/elb.session-affinity-option |
No |
Table 4 object |
Sticky session timeout. |
kubernetes.io/elb.health-check-flag |
No |
String |
Whether to enable the ELB health check.
If this parameter is enabled, the kubernetes.io/elb.health-check-option field must also be specified at the same time. |
kubernetes.io/elb.health-check-option |
No |
Table 5 object |
ELB health check configuration items. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
name |
No |
String |
Name of the automatically created load balancer. The value can contain 1 to 64 characters. Only letters, digits, underscores (_), hyphens (-), and periods (.) are allowed. Default: cce-lb+service.UID |
type |
No |
String |
Network type of the load balancer.
Default: inner |
bandwidth_name |
Yes for public network load balancers |
String |
Bandwidth name. The default value is cce-bandwidth-******. The value can contain 1 to 64 characters. Only letters, digits, underscores (_), hyphens (-), and periods (.) are allowed. |
bandwidth_chargemode |
No |
String |
Bandwidth mode.
Default: traffic |
bandwidth_size |
Yes for public network load balancers |
Integer |
Bandwidth size. The default value is 1 to 2000 Mbit/s. Configure this parameter based on the bandwidth range allowed in your region. The minimum increment for bandwidth adjustment varies depending on the bandwidth range.
|
bandwidth_sharetype |
Yes for public network load balancers |
String |
Bandwidth sharing mode.
|
eip_type |
Yes for public network load balancers |
String |
EIP type.
The specific type varies with regions. For details, see the EIP console. |
vip_subnet_cidr_id |
No |
String |
Subnet where a load balancer is located. The subnet must belong to the VPC where the cluster resides. If this parameter is not specified, the ELB load balancer and the cluster are in the same subnet. This field can be specified only for clusters of v1.21 or later. |
vip_address |
No |
String |
Private IP address of the load balancer. Only IPv4 addresses are supported. The IP address must be in the ELB CIDR block. If this parameter is not specified, an IP address will be automatically assigned from the ELB CIDR block. This parameter is available only in clusters of v1.23.11-r0, v1.25.6-r0, v1.27.3-r0, or later versions. |
available_zone |
Yes |
Array of strings |
AZ where the load balancer is located. You can obtain all supported AZs by getting the AZ list. This parameter is available only for dedicated load balancers. |
l4_flavor_name |
Yes |
String |
Flavor name of the layer-4 load balancer. You can obtain all supported types by getting the flavor list. This parameter is available only for dedicated load balancers. |
l7_flavor_name |
No |
String |
Flavor name of the layer-7 load balancer. You can obtain all supported types by getting the flavor list. This parameter is available only for dedicated load balancers. The value of this parameter must be the same as that of l4_flavor_name, that is, both are elastic specifications or fixed specifications. |
elb_virsubnet_ids |
No |
Array of strings |
Subnet where the backend server of the load balancer is located. If this parameter is left blank, the default cluster subnet is used. Load balancers occupy different number of subnet IP addresses based on their specifications. Do not use the subnet CIDR blocks of other resources (such as clusters and nodes) as the load balancer CIDR block. This parameter is available only for dedicated load balancers. Example: "elb_virsubnet_ids": [ "14567f27-8ae4-42b8-ae47-9f847a4690dd" ] |
ipv6_vip_virsubnet_id |
No |
String |
Specifies the ID of the IPv6 subnet where the load balancer resides. IPv6 must be enabled for the corresponding subnet. This parameter is mandatory only when the dual-stack clusters are used. This parameter is available only for dedicated load balancers. |
kubectl create -f nginx-deployment.yaml
If information similar to the following is displayed, the workload is being created.
deployment/nginx created
kubectl get pod
If information similar to the following is displayed, the workload is running.
NAME READY STATUS RESTARTS AGE nginx-2601814895-c1xhw 1/1 Running 0 6s
kubectl create -f nginx-elb-svc.yaml
If information similar to the following is displayed, the Service has been created.
service/nginx created
kubectl get svc
If information similar to the following is displayed, the access type has been set, and the workload is accessible.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.247.0.1 <none> 443/TCP 3d nginx LoadBalancer 10.247.130.196 10.78.42.242 80:31540/TCP 51s
The Nginx is accessible.