A Service exposed by Alibaba Cloud Server Load Balancer (SLB) can be accessed from outside the cluster by using the SLB domain name or <IP:Service Port>, and from inside the cluster by using <Service Name:Service Port>. This article uses an Nginx application as an example to describe how to expose an application in an ACS cluster by using a Service that uses an existing load balancer.
Prerequisites
You must have an SLB instance that you created in the SLB console. The instance must be in the same region as the ACS cluster. This topic uses an internet-facing Classic Load Balancer (CLB) instance as an example. If you do not have a CLB instance, see Create and manage CLB instances.
Considerations
SLB reuse limits
-
The SLB instance that you want to reuse must meet the following requirements:
-
You can reuse only SLB instances that you create manually in the SLB console. You cannot reuse instances automatically created by the cloud controller manager (CCM) or used by the cluster API server.
-
To reuse an intranet SLB instance in an ACS cluster, the SLB instance and the ACS cluster must be in the same virtual private cloud (VPC).
-
The address type of the SLB instance must match the access type of the Service:
-
If the Service provides Public Access (specified by the annotation
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-type: "internet"), the Address Type of the SLB instance must be Public Network. -
If the Service provides internal access (specified by the annotation
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-type: "intranet"), the Address Type of the SLB instance must be Internal Network.
-
-
Multiple Services cannot use the same listener port on the same SLB instance.
-
When you reuse an SLB instance across clusters, make sure that the combination of the namespace and Service name is unique in each cluster.
-
-
The CCM configures an SLB instance only for a
type: LoadBalancerService.ImportantIf you change a
type: LoadBalancerService to another type, the CCM deletes the configurations that were added to the SLB instance. As a result, the Service becomes inaccessible through the SLB instance. -
The CCM uses a declarative API and automatically reconciles the SLB configuration based on the Service configuration under specific conditions. If the
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-force-override-listeners: "true"annotation is set, any configurations that you manually modify in the SLB console may be overwritten.ImportantDo not manually modify any configurations of an SLB instance managed by ACS in the SLB console. Otherwise, the configurations may be lost and the Service may become inaccessible.
-
You cannot change the SLB instance for an existing LoadBalancer service. If you want to change the SLB instance, you must create a new Service.
SLB quota limits
CCM creates an SLB instance for each Service of
Type=LoadBalancer. By default, you can have up to 60 CLB instances and 60 NLB instances. To increase this quota, log on to the Quota Center console and submit an application.CCM attaches ECS instances or Elastic Network Interfaces (ENIs) to the backend server group of the SLB instance based on the Service configuration. It also creates a separate server group for each different targetPort in the Service. Note the following quota limits:
Number of backend servers: By default, you can attach up to 200 backend servers to a CLB instance and up to 400 ECS, ENI, or IP-based backend servers to an NLB instance. The required quota is calculated as follows: Number of backend servers × Number of targetPorts. To increase the quota, log on to the Quota Center console and submit an application in advance.
Number of times an instance can be attached to server groups: By default, the same ECS instance or ENI can be attached to a maximum of 50 different backend server groups of a CLB instance and 200 different backend server groups of an NLB instance. To attach it to more server groups, log on to the Quota Center console and submit an application.
Extra quota usage during rolling updates: During a rolling update, new pods are created before old pods are destroyed. This consumes extra quota. The actual consumption may exceed your expectations. Reserve enough quota in advance.
CCM creates listeners based on the ports defined in the Service. By default, you can add up to 50 listeners to a CLB or NLB instance. To add more listeners, log on to the Quota Center console and submit an application.
For more information about SLB limits, see CLB limits and NLB limits.
To query SLB quotas, see Server Load Balancer quota management.
Step 1: Deploy a sample application
The following steps show how to deploy the application by using kubectl.
-
Create a file named my-nginx.yaml with the following content.
-
Run the following command to deploy the my-nginx sample application.
kubectl apply -f my-nginx.yaml -
Run the following command to verify that the application is running.
kubectl get deployment my-nginxExpected output:
NAME READY UP-TO-DATE AVAILABLE AGE my-nginx 3/3 3 3 50s
Step 2: Expose the application with a Service
You can create a LoadBalancer service to expose the application by using either the console or kubectl.
Console
-
Log on to the ACS console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Network > Services.
-
On the Services page, click Create in the upper-left corner.
-
In the Create Service dialog box, configure the parameters for the Service.
Parameter
Description
Example
Name
Enter a name for the Service.
my-nginx-svc
Service Type
Select the type of the Service. This determines how the Service is accessed.
Create Service:
-
Service Type: LoadBalancer
-
SLB Type: Classic Load Balancer (CLB)
-
Select Resource: Use Existing Resource
-
Overwrite Existing Listeners: Selected
Backend
If you do not associate a workload, an Endpoints object is not created, but you can bind one manually. For more information, see Services without selectors.
Click + Reference Workload Label and select my-nginx:
-
Name: app
-
Value: nginx
Port Mapping
Add a service port (the
portfield in the Service YAML file) and a container port (thetargetPortfield in the Service YAML file). The container port must match the port exposed by the backend Pod.-
Service Port: 80
-
Container Port: 80
-
Protocol: TCP
Annotations
Add an annotation to the Service to configure parameters for the load balancer. For more information, see Use annotations to configure Classic Load Balancer (CLB) instances.
This example sets the billing method to pay-by-bandwidth and the bandwidth limit to 2 Mbit/s to control Service traffic.
Set the billing method:
-
Name:
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-charge-type -
Value: paybybandwidth
Set the bandwidth:
-
Name:
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-bandwidth -
Value: 2
Label
Add a label to identify the Service.
None
-
-
Click Create. On the Services page, you can view the newly created Service.
-
Access the sample application by using the IP address in the External IP column, such as 39.106.XX.XX:80.
After you access the application, the Welcome to nginx! page is displayed. This indicates that the Nginx service is deployed and runs as expected.
kubectl
-
Create a file named my-nginx-svc.yaml with the following content.
-
Update the value of the
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-idannotation. Replace ${YOUR_LB_ID} with the ID of the SLB instance that you created in the SLB console. -
By default, reusing an existing SLB instance does not create or overwrite its listeners. If required, set the
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-force-override-listenersannotation totrue. In this example, the SLB instance is newly created and requires a listener, so the value is set totrue. For more information, see Use annotations to configure Classic Load Balancer (CLB) instances. -
Set the value of
selectorto the value ofmatchLabelsin themy-nginx.yamlfile, which isapp: nginx, to associate the Service with the backend application.
apiVersion: v1 kind: Service metadata: annotations: service.beta.kubernetes.io/alibaba-cloud-loadbalancer-id: ${YOUR_LB_ID} service.beta.kubernetes.io/alicloud-loadbalancer-force-override-listeners: 'true' labels: app: nginx name: my-nginx-svc namespace: default spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: LoadBalancer -
-
Run the following command to create the my-nginx-svc Service and expose the application.
kubectl apply -f my-nginx-svc.yaml -
Run the following command to verify that the LoadBalancer service is created.
kubectl get svc my-nginx-svcExpected output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-nginx-svc LoadBalancer 172.21.5.82 39.106.XX.XX 80/TCP 5m -
Run the curl <YOUR-External-IP> command to access the application. Replace the <YOUR-External-IP> placeholder with the
EXTERNAL-IPfrom the previous step.curl 39.106.XX.XXExpected output:
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>