Help Center/ Cloud Container Engine/ User Guide (Paris Regions)/ Network/ Service/ LoadBalancer/ Using Annotations to Configure Load Balancing
Updated on 2024-01-26 GMT+08:00

Using Annotations to Configure Load Balancing

You can add annotations to a YAML file to use some CCE advanced functions. This section describes the available annotations when a LoadBalancer service is created.

Interconnection with ELB

Table 1 Annotations for interconnecting with ELB

Parameter

Type

Description

Supported Cluster Version

kubernetes.io/elb.class

String

Select a proper load balancer type.

  • union: shared load balancer
  • performance: dedicated load balancer, which can be used only in clusters of v1.17 and later.

v1.9 or later

kubernetes.io/elb.id

String

Mandatory when an existing load balancer is to be associated.

ID of a load balancer.

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.

v1.9 or later

kubernetes.io/elb.autocreate

Table 9

Mandatory when load balancers are automatically created.

Example:

  • If a public network load balancer will be automatically created, set this parameter to the following value:

    {"type":"public","bandwidth_name":"cce-bandwidth-1551163379627","bandwidth_chargemode":"bandwidth","bandwidth_size":5,"bandwidth_sharetype":"PER","eip_type":"5_bgp","name":"james"}

  • If a private network load balancer will be automatically created, set this parameter to the following value:

    {"type":"inner","name":"A-location-d-test"}

v1.9 or later

kubernetes.io/elb.enterpriseID

String

Optional when load balancers are automatically created.

Clusters of v1.15 and later versions support this field. In clusters earlier than v1.15, load balancers are created in the default project by default.

This parameter indicates the ID of the enterprise project in which the ELB load balancer will be created.

If this parameter is not specified or is set to 0, resources will be bound to the default enterprise project.

How to obtain:

Log in to the EPS console. In the navigation pane, choose Project Management. In the enterprise project list, click the name of the enterprise project to be added. On the enterprise project details page, copy the project ID.

v1.15 or later

kubernetes.io/elb.subnet-id

String

Optional when load balancers are automatically created.

ID of the subnet where the cluster is located. The value can contain 1 to 100 characters.

  • Mandatory when a cluster of v1.11.7-r0 or earlier is to be automatically created.
  • Optional for clusters later than v1.11.7-r0.

Mandatory for versions earlier than v1.11.7-r0

Discarded in versions later than v1.11.7-r0

kubernetes.io/elb.lb-algorithm

String

Specifies the load balancing algorithm of the backend server group. The default value is ROUND_ROBIN.

Options:

  • ROUND_ROBIN: weighted round robin algorithm
  • LEAST_CONNECTIONS: weighted least connections algorithm
  • SOURCE_IP: source IP hash algorithm
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.

v1.9 or later

The following shows how to use the preceding annotations:

  • Associating an existing load balancer. For details, see Using kubectl to Create a Service (Using an Existing Load Balancer).
    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: performance                        # Load balancer type
        kubernetes.io/elb.lb-algorithm: ROUND_ROBIN                   # Load balancer algorithm
    spec:
      selector: 
         app: nginx
      ports: 
      - name: service0 
        port: 80
        protocol: TCP 
        targetPort: 80
      type: LoadBalancer
  • Automatically creating a load balancer. For details, see Using kubectl to Create a Service (Automatically Creating a Load Balancer).
    Shared load balancer:
    apiVersion: v1 
    kind: Service 
    metadata: 
      annotations:   
        kubernetes.io/elb.class: union
        kubernetes.io/elb.autocreate: '{
          "type": "public",
          "bandwidth_name": "cce-bandwidth-1551163379627",
          "bandwidth_chargemode": "bandwidth",
          "bandwidth_size": 5,
          "bandwidth_sharetype": "PER",
          "eip_type": "5_bgp"
        }'
        kubernetes.io/elb.enterpriseID: '0'               # ID of the enterprise project to which the load balancer belongs
        kubernetes.io/elb.lb-algorithm: ROUND_ROBIN     # Load balancer algorithm
      labels: 
        app: nginx 
      name: nginx 
    spec: 
      ports: 
      - name: service0 
        port: 80
        protocol: TCP 
        targetPort: 80
      selector: 
        app: nginx 
      type: LoadBalancer
    Dedicated load balancer:
    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": "bandwidth",
          "bandwidth_size": 5,
          "bandwidth_sharetype": "PER",
          "eip_type": "5_bgp",
          "available_zone": [
             ""
          ],
          "l4_flavor_name": "L4_flavor.elb.s1.small"
        }'
        kubernetes.io/elb.enterpriseID: '0'               # ID of the enterprise project to which the load balancer belongs
        kubernetes.io/elb.lb-algorithm: ROUND_ROBIN     # Load balancer algorithm
    spec:
      selector:
        app: nginx
      ports:
      - name: cce-service-0
        targetPort: 80
        nodePort: 0
        port: 80
        protocol: TCP
      type: LoadBalancer

Sticky Session

Table 2 Annotations for sticky session

Parameter

Type

Description

Supported Cluster Version

kubernetes.io/elb.session-affinity-mode

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.

  • Disabling sticky session: Do not configure this parameter.
  • Enabling sticky session: Set this parameter to SOURCE_IP, indicating that the sticky session is based on the source IP address.
NOTE:

When kubernetes.io/elb.lb-algorithm is set to SOURCE_IP (source IP address algorithm), sticky session cannot be enabled.

v1.9 or later

kubernetes.io/elb.session-affinity-option

Table 12

Sticky session timeout.

v1.9 or later

The following shows how to use the preceding annotations:
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.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)
spec:
  selector: 
     app: nginx
  ports: 
  - name: service0 
    port: 80
    protocol: TCP 
    targetPort: 80
  type: LoadBalancer

Health Check

Table 3 Annotations for health check

Parameter

Type

Description

Supported Cluster Version

kubernetes.io/elb.health-check-flag

String

Whether to enable the ELB health check.

  • Enabling health check: Leave blank this parameter or set it to on.
  • Disabling health check: Set this parameter to off.

If this parameter is enabled, the kubernetes.io/elb.health-check-option field must also be specified at the same time.

v1.9 or later

kubernetes.io/elb.health-check-option

Table 10

ELB health check configuration items.

v1.9 or later

kubernetes.io/elb.health-check-options

Table 11

ELB health check configuration item. Each Service port can be configured separately, and you can configure only some ports.

NOTE:

kubernetes.io/elb.health-check-option and kubernetes.io/elb.health-check-options cannot be configured at the same time.

v1.19.16-r5 or later

v1.21.8-r0 or later

v1.23.6-r0 or later

v1.25.2-r0 or later

  • The following shows how to use kubernetes.io/elb.health-check-option:
    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.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
        protocol: TCP 
        targetPort: 80
      type: LoadBalancer
  • For details about how to use kubernetes.io/elb.health-check-options, see Configuring Health Check for Multiple Ports.

HTTP Protocol

Table 4 Annotations for using HTTP protocols

Parameter

Type

Description

Supported Cluster Version

kubernetes.io/elb.protocol-port

String

Layer-7 forwarding configuration port used by the Service.

v1.19.16 or later

kubernetes.io/elb.cert-id

String

HTTP certificate used by the Service for Layer-7 forwarding.

v1.19.16 or later

For details about the application scenarios, see Service Using HTTP.

Dynamic Adjustment of the Weight of the Backend ECS

Table 5 Annotations for dynamically adjusting the weight of the backend ECS

Parameter

Type

Description

Supported Cluster Version

kubernetes.io/elb.adaptive-weight

String

Dynamically adjusts the weight of the load balancer backend ECS based on pods. The requests received by each pod are more balanced.

  • true: enabled
  • false: disabled

This parameter applies only to clusters of v1.21 or later and is invalid in passthrough networking.

v1.21 or later

The following shows how to use the preceding annotations:
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.adaptive-weight: 'true'                   # Enable dynamic adjustment of the weight of the backend ECS.
spec:
  selector: 
     app: nginx
  ports: 
  - name: service0 
    port: 80
    protocol: TCP 
    targetPort: 80
  type: LoadBalancer

Pass-through Capability

Table 6 Annotations for pass-through capability

Parameter

Type

Description

Supported Cluster Version

kubernetes.io/elb.pass-through

String

Whether the access requests from within the cluster to the Service pass through the ELB load balancer.

v1.19 or later

For details about the application scenarios, see Enabling Passthrough Networking for LoadBalancer Services.

Host Network

Table 7 Annotations for host network

Parameter

Type

Description

Supported Cluster Version

kubernetes.io/hws-hostNetwork

String

If the pod uses hostNetwork, the ELB forwards the request to the host network after this annotation is used.

Options:

  • true: enabled
  • false (default): disabled

v1.9 or later

The following shows how to use the preceding annotations:
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/hws-hostNetwork: 'true'                     # The load balancer forwards the request to the host network.
spec:
  selector: 
     app: nginx
  ports: 
  - name: service0 
    port: 80
    protocol: TCP 
    targetPort: 80
  type: LoadBalancer

Timeout

Table 8 Annotation for configuring timeout

Parameter

Type

Description

Supported Cluster Version

kubernetes.io/elb.keepalive_timeout

Integer

Timeout for client connections. If there are no requests reaching the load balancer after the timeout duration elapses, the load balancer will disconnect the connection with the client and establish a new connection when there is a new request.

Value:

  • For TCP listeners, the value ranges from 10 to 4000 (in seconds). The default value is 300.
  • For HTTP, HTTPS, and TERMINATED_HTTPS listeners, the value ranges from 10 to 4000 (in seconds). The default value is 60.
  • For UDP listeners, this parameter does not take effect.

v1.19.16-r30, v1.21.10-r10, v1.23.8-r10, v1.25.3-r10, and later

For details about the application scenarios, see Configuring Timeout for a LoadBalancer Service.

Data Structure

Table 9 Data structure of the elb.autocreate field

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.

  • public: public network load balancer
  • inner: private network 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.

  • bandwidth: billed by bandwidth
  • traffic: billed by traffic

Default: bandwidth

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.
  • The minimum increment is 1 Mbit/s if the allowed bandwidth does not exceed 300 Mbit/s.
  • The minimum increment is 50 Mbit/s if the allowed bandwidth ranges from 300 Mbit/s to 1000 Mbit/s.
  • The minimum increment is 500 Mbit/s if the allowed bandwidth exceeds 1000 Mbit/s.

bandwidth_sharetype

Yes for public network load balancers

String

Bandwidth sharing mode.

  • PER: dedicated bandwidth

eip_type

Yes for public network load balancers

String

EIP type.

  • 5_bgp: dynamic BGP

The specific type varies with regions. For details, see the EIP console.

available_zone

Yes

Array of strings

AZ where the load balancer is located.

This parameter is available only for dedicated load balancers.

l4_flavor_name

Yes

String

Flavor name of the layer-4 load balancer.

This parameter is available only for dedicated load balancers.

l7_flavor_name

No

String

Flavor name of the layer-7 load balancer.

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. Therefore, you are not advised to 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"
 ]
Table 10 Data structure description of the elb.health-check-option field

Parameter

Mandatory

Type

Description

delay

No

String

Initial waiting time (in seconds) for starting the health check.

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: /

The value can contain 1 to 10,000 characters.

Table 11 Data structure description of the elb.health-check-options field

Parameter

Mandatory

Type

Description

target_service_port

Yes

String

Port for health check specified by spec.ports. The value consists of the protocol and port number, for example, TCP:80.

monitor_port

No

String

Re-specified port for health check. If this parameter is not specified, the service port is used by default.

NOTE:

Ensure that the port is in the listening state on the node where the pod is located. Otherwise, the health check result will be affected.

delay

No

String

Initial waiting time (in seconds) for starting the health check.

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.

Default value: protocol of the associated Service

Value options: TCP, UDP, or HTTP

path

No

String

Health check URL. This parameter needs to be configured when the protocol is HTTP.

Default value: /

The value can contain 1 to 10,000 characters.

Table 12 Data structure of the elb.session-affinity-option field

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