A workload can be accessed from public networks through a load balancer, which is more secure and reliable than EIP.
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.
You can set the Service when creating a workload on the CCE console. An Nginx workload is used as an example.
ELB Configuration
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 subnet CIDR block.
You can create public network or private network load balancers.
You can set the Service after creating a workload. This has no impact on the workload status and takes effect immediately. The procedure is as follows:
The parameters are the same as those in Adding a Service When Creating a Workload.
You can set the access type 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: annotations: kubernetes.io/elb.id: 3c7caa5a-a641-4bff-801a-feace27424b6 # Load balancer ID. Replace it with the actual value. kubernetes.io/elb.class: performance # Load balancer type name: nginx spec: ports: - name: service0 port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: LoadBalancer
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
kubernetes.io/elb.class |
No |
String |
Select a proper load balancer type as required. The value can be:
Default value: union |
kubernetes.io/elb.session-affinity-mode |
No |
String |
Listeners ensure session stickiness based on IP addresses. Requests from the same IP address will be forwarded to the same backend server.
|
kubernetes.io/elb.session-affinity-option |
No |
Table 2 Object |
This parameter specifies the sticky session timeout. |
kubernetes.io/elb.id |
Yes |
String |
This parameter indicates the ID of a load balancer. The value can contain 1 to 100 characters. Mandatory when an existing load balancer is to be associated. Obtaining the load balancer ID: 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. |
kubernetes.io/elb.subnet-id |
- |
String |
This parameter indicates the ID of the subnet where the cluster is located. The value can contain 1 to 100 characters.
|
kubernetes.io/elb.lb-algorithm |
No |
String |
This parameter indicates the load balancing algorithm of the backend server group. The default value is ROUND_ROBIN. Options:
When the value is SOURCE_IP, the weights of backend servers in the server group are invalid. |
kubernetes.io/elb.health-check-flag |
No |
String |
Whether to enable the ELB health check. Enabled by default.
|
kubernetes.io/elb.health-check-option |
No |
Table 3 Object |
ELB health check configuration items. |
port |
Yes |
Integer |
Access port that is registered on the load balancer and mapped to the cluster-internal IP address. |
targetPort |
Yes |
String |
Container port set on the CCE console. |
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 |
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_CONNECT, or HTTP |
path |
No |
String |
Health check URL. This parameter needs to be configured when the protocol is HTTP. Default value: / The value contains 1 to 10,000 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-c1xwh 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 successfully, 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 add a 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: 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", "eip_type": "5_bgp", "name": "james" }' labels: app: nginx name: nginx spec: ports: - name: service0 port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: LoadBalancer
Example of a Service using a dedicated, public network 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": "traffic", "bandwidth_size": 5, "bandwidth_sharetype": "PER", "eip_type": "5_bgp", "available_zone": [ "eu-de-01" ], "l4_flavor_name": "L4_flavor.elb.s1.small" }' spec: selector: app: nginx ports: - name: cce-service-0 targetPort: 80 nodePort: 0 port: 80 protocol: TCP type: LoadBalancer
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
kubernetes.io/elb.class |
No |
String |
Select a proper load balancer type as required. The value can be:
Default value: union |
kubernetes.io/elb.subnet-id |
- |
String |
This parameter indicates the ID of the subnet where the cluster is located. The value can contain 1 to 100 characters.
|
kubernetes.io/elb.enterpriseID |
No |
String |
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 management console and choose Enterprise > Project Management on the top menu bar. In the list displayed, click the name of the target enterprise project, and copy the ID on the enterprise project details page. |
kubernetes.io/elb.session-affinity-option |
No |
Table 2 Object |
Sticky session timeout. |
kubernetes.io/elb.autocreate |
Yes |
elb.autocreate object |
Whether to automatically create a load balancer associated with the Service. Example: |
kubernetes.io/elb.lb-algorithm |
No |
String |
This parameter indicates the load balancing algorithm of the backend server group. The default value is ROUND_ROBIN. Options:
When the value is SOURCE_IP, the weights of backend servers in the server group are invalid. |
kubernetes.io/elb.health-check-flag |
No |
String |
Whether to enable the ELB health check. Disabled by default.
|
kubernetes.io/elb.health-check-option |
No |
Table 3 Object |
ELB health check configuration items. |
kubernetes.io/elb.session-affinity-mode |
No |
String |
Listeners ensure session stickiness based on IP addresses. Requests from the same IP address will be forwarded to the same backend server.
|
kubernetes.io/elb.session-affinity-option |
No |
Table 2 Object |
Sticky session timeout. |
kubernetes.io/hws-hostNetwork |
No |
String |
This parameter indicates whether the workload Services use the host network. Setting this parameter to true will enable the load balancer to forward requests to the host network. The host network is not used by default. The value can be true or false. |
externalTrafficPolicy |
No |
String |
If sticky session is enabled, add this parameter so that requests are transferred to a fixed node. If a LoadBalancer Service with this parameter set to Local is created, a client can access the target backend only if the client is installed on the same node as the backend. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
name |
No |
String |
Name of the automatically created load balancer. Value range: a string of 1 to 64 characters, including lowercase letters, digits, and underscores (_). The value must start with a lowercase letter and end with a lowercase letter or digit. Default name: cce-lb+service.UID |
type |
No |
String |
Network type of the load balancer.
Default value: inner |
bandwidth_name |
Yes for public network load balancers |
String |
Bandwidth name. The default value is cce-bandwidth-******. Value range: a string of 1 to 64 characters, including lowercase letters, digits, and underscores (_). The value must start with a lowercase letter and end with a lowercase letter or digit. |
bandwidth_chargemode |
No |
String |
Bandwidth billing mode.
|
bandwidth_size |
Yes for public network load balancers |
Integer |
Bandwidth size. The default value is 1 to 2000 Mbit/s. Set this parameter based on the bandwidth range allowed in your region. |
bandwidth_sharetype |
Yes for public network load balancers |
String |
Bandwidth sharing mode.
|
eip_type |
Yes for public network load balancers |
String |
EIP type, which may vary depending on sites. For details, see the type parameter specified when creating an EIP.
|
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. |
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" ] |
kubectl create -f nginx-deployment.yaml
If information similar to the following is displayed, the workload is being created.
deployment "nginx" created
kubectl get po
If information similar to the following is displayed, the workload is running.
NAME READY STATUS RESTARTS AGE nginx-2601814895-c1xwh 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 successfully, 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.
If the service affinity of a LoadBalancer Service is set to the node level, that is, the value of externalTrafficPolicy is Local, the ELB address may fail to be accessed from the cluster (specifically, nodes or containers).
This is because when the LoadBalancer Service is created, kube-proxy adds the ELB access address (external-ip) to iptables or IPVS. When the ELB address is accessed from the cluster, the ELB load balancer is not used. Instead, kube-proxy directly forwards the access request. The case depends on which container network model and service forwarding mode you use.
The following methods can be used to solve this problem:
apiVersion: v1
kind: Service
metadata:
annotations:
kubernetes.io/elb.class: union
kubernetes.io/elb.autocreate: '{"type":"public","bandwidth_name":"cce-bandwidth","bandwidth_chargemode":"traffic","bandwidth_size":5,"bandwidth_sharetype":"PER","eip_type":"5_bgp","name":"james"}'
labels:
app: nginx
name: nginx
spec:
externalTrafficPolicy: Cluster
ports:
- name: service0
port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
type: LoadBalancer