How Do I Use kubectl to Set the Workload Access Type to LoadBalancer (ELB)?
This section uses the Nginx workload as an example to describe how to set the workload access type to LoadBalancer (ELB).
Prerequisites
- A shared load balancer has been created. For details, see Creating a Load Balancer.
- You have connected an Elastic Cloud Server (ECS) to the cluster by running the kubectl command. For details, see Connecting to a CCE Cluster Using kubectl or web-terminal.
Procedure
- Log in to the ECS on which the kubectl has been configured. For details, see Logging In to a Linux ECS.
- Create and edit the nginx-deployment.yaml and nginx-elb-svc.yaml files.
The file names are user-defined. nginx-deployment.yaml and nginx-elb-svc.yaml are merely example file names.
vi nginx-deployment.yaml
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx spec: replicas: 1 selector: matchLabels: app: nginx strategy: type: RollingUpdate template: metadata: labels: app: nginx spec: containers: - image: nginx imagePullPolicy: Always name: nginx imagePullSecrets: - name: default-secretvi nginx-elb-svc.yaml
Before enabling sticky session, ensure that the following conditions are met:
- The workload protocol is TCP.
- Anti-affinity has been configured between pods of the workload. That is, all pods of the workload are deployed on different nodes. For details, see Workload-Node Anti-Affinity.
- Automatically creating a load balancer
apiVersion: v1 kind: Service metadata: annotations: kubernetes.io/elb.class: union kubernetes.io/session-affinity-mode: SOURCE_IP kubernetes.io/elb.subnet-id: 5083f225-9bf8-48fa-9c8b-67bd9693c4c0 kubernetes.io/elb.enterpriseID: debb7ae2-6d2f-4e6c-a0aa-1ccafd92b8eb kubernetes.io/elb.autocreate: '{"type":"public","bandwidth_name":"cce-bandwidth-1551163379627","bandwidth_chargemode":"bandwidth","bandwidth_size":5,"bandwidth_sharetype":"PER","eip_type":"5_bgp","name":"james"}' labels: app: nginx name: nginx spec: externalTrafficPolicy: Local ports: - name: service0 port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: LoadBalancer - Using an existing load balancer
apiVersion: v1 kind: Service metadata: annotations: kubernetes.io/elb.class: union kubernetes.io/session-affinity-mode: SOURCE_IP kubernetes.io/elb.id: 3c7caa5a-a641-4bff-801a-feace27424b6 kubernetes.io/elb.subnet-id: 5083f225-9bf8-48fa-9c8b-67bd9693c4c0 labels: app: nginx name: nginx spec: loadBalancerIP: 10.78.42.242 externalTrafficPolicy: Local ports: - name: service0 port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: LoadBalancer
Table 1 Key parameters Parameter
Type
Description
kubernetes.io/elb.class
String
Mandatory and must be set to union if a shared load balancer is in use.
kubernetes.io/session-affinity-mode
String
Optional. If sticky session is enabled, add this parameter.
The value SOURCE_IP indicates that listeners ensure sticky session based on source IP addresses.
kubernetes.io/elb.id
String
Optional. This parameter is mandatory if an existing load balancer is used.
ID of a shared load balancer.
kubernetes.io/elb.subnet-id
String
Optional. This parameter is mandatory only if a load balancer will be automatically created. For clusters of v1.11.7-r0 or later, this parameter can be left blank.
kubernetes.io/elb.enterpriseID
String
Optional. This parameter is mandatory if a public/private network load balancer will be automatically created.
This parameter indicates the name of the ELB enterprise project in which the load balancer will be created.
kubernetes.io/elb.autocreate
elb.autocreate object
Optional. This parameter is mandatory if a public network load balancer will be automatically created. The system will create a load balancer and an EIP. This parameter is also mandatory if a private network load balancer will be automatically created. The system will create a load balancer.
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:
loadBalancerIP
String
Private IP address of a private network load balancer or public IP address of a public network load balancer.
externalTrafficPolicy
String
Optional. If sticky session is enabled, add this parameter so requests are transferred to a fixed node. If a LoadBalancer Service with this parameter set to Local is created for a workload, the workload can be accessed only when the client is installed on the same node as the server.
port
Integer
Access port that is registered on the load balancer and mapped to the cluster-internal IP address.
targetPort
String
Container port on the CCE console.
Table 2 Data structure of the elb.autocreate field Parameter
Type
Description
name
String
Name of the load balancer that is automatically created.
The value is a string of 1 to 64 characters that consist of letters, digits, underscores (_), and hyphens (-).
type
String
Network type of the load balancer.
- public: public network load balancer.
- inner: private network load balancer.
bandwidth_name
String
Bandwidth name. The default value is cce-bandwidth-******.
The value is a string of 1 to 64 characters that consist of letters, digits, underscores (_), hyphens (-), and periods (.).
bandwidth_chargemode
String
Bandwidth billing mode.
- bandwidth: billed by bandwidth.
- traffic: billed by traffic.
bandwidth_size
Integer
Bandwidth size. Set this parameter based on the bandwidth range supported by the region. For details, see the description of the size field in Table 4 in Assigning an EIP.
bandwidth_sharetype
String
Bandwidth sharing mode.
- PER: dedicated bandwidth.
eip_type
String
EIP type. For details, see the description of the type field in Table 3 in Assigning an EIP.
- Create a workload.
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 etcd-0 0/1 ImagePullBackOff 0 1h icagent-m9dkt 0/0 Running 0 3d nginx-2601814895-c1xhw 1/1 Running 0 6s
- Create a Service.
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 etcd-svc ClusterIP None <none> 3120/TCP 1h kubernetes ClusterIP 10.247.0.1 <none> 443/TCP 3d nginx LoadBalancer 10.247.130.196 10.78.42.242 80:31540/TCP 51s
- In the address box of your browser, enter 10.78.42.242:31540 and press Enter. 10.78.42.242 indicates the IP address of the load balancer, and 31540 indicates the access port displayed on the CCE console.
Nginx is accessible.
Figure 1 Accessing Nginx through a load balancer
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.