The cloud-controller-manager (CCM) component automatically creates and manages a load balancer for a Service of type LoadBalancer. The load balancer can be a Classic Load Balancer (CLB) or a Network Load Balancer (NLB). This topic uses an Nginx application as an example to demonstrate exposing an application by using a LoadBalancer Service.
Notes
The CCM configures a load balancer only for a Service of
Type=LoadBalancer. No load balancer is configured for other Service types.The CCM uses a declarative API and automatically reconciles the load balancer configuration based on the Service definition. Any manual changes made in the Server Load Balancer (SLB) console might be overwritten.
ImportantDo not manually modify the configuration of a Kubernetes-managed load balancer in the Server Load Balancer (SLB) console. Your changes might be overwritten, making the Service inaccessible.
You cannot change the load balancer for an existing Service of type
LoadBalancer. To use a different load balancer, you must create a new Service.
If you change a Service's type from Type=LoadBalancer to Type!=LoadBalancer, the CCM deletes the configurations added to the load balancer, making the Service inaccessible via the load balancer.
Quotas
The CCM creates a load balancer instance for each Service of
Type=LoadBalancer. By default, you can have up to 60 instances. If you need more than 60 instances, log on to the Quota Center console and submit an application.The CCM creates a listener based on the ports defined in the Service. By default, you can add up to 50 listeners to a single load balancer instance. To add more listeners, log on to the Quota Center console and submit an application.
For more information about load balancer usage limits, see Limits.
To view your load balancer quotas, see Load Balancer Quota Management.
Step 1: Deploy a sample application
The following steps describe how to deploy the application by using kubectl.
Create a file named my-nginx.yaml with the following content.
apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1 kind: Deployment metadata: name: my-nginx # The name of the sample application. labels: app: nginx spec: replicas: 3 # The number of replicas. selector: matchLabels: app: nginx # The value must match the selector of the Service that is used to expose this application. template: metadata: labels: app: nginx spec: containers: - name: nginx image: registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest # Replace this with the actual image address in the format: <image_name:tags>. ports: - containerPort: 80 # This port must be exposed in the Service.Deploy the my-nginx application.
kubectl apply -f my-nginx.yamlVerify that the application is running.
kubectl get deployment my-nginxExample output:
NAME READY UP-TO-DATE AVAILABLE AGE my-nginx 3/3 3 3 50s
Step 2: Expose the application by using a LoadBalancer Service
You can use the console and kubectl to create a LoadBalancer Service and expose an application.
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, set the parameters for the Service.
Parameter
Description
Example
Name
Enter a name for the service.
my-nginx-svc
Service Type
Select a Service type. The following network modes are supported for different types of clients and access sources:
-
Select Server Load Balancer as the Server Load Balancer.
-
For CLB, select CLB. For Create Resource, select Create Resource.
-
If needed, adjust the settings in the Create CLB Instance section. Set Billing Method to Pay-by-specification.
External Traffic Policy
You can set External Traffic Policy only when Server Load Balancer is set to Server Load Balancer.
Local: Traffic is routed only to the Pods on the node that receives the traffic.
Cluster: Traffic can be forwarded to Pods on other nodes in the cluster.
Local
Backend
Select the backend application to bind to the service. If you do not associate a deployment, the system does not create Endpoints objects. For more information, see services-without-selectors.
Name: app
Value: nginx
Port Mapping
Add a service port (corresponds to the
portfield in the Service YAML file) and a container port (corresponds to thetargetPortfield in the Service YAML file). The container port must match the port exposed by the backend pod.80
Annotations
Add an Annotation to the Service to configure load balancer parameters. For more information, see Use Annotations to configure Classic Load Balancer (CLB).
ImportantDo not reuse the load balancer instance of the cluster's API Server. Otherwise, the cluster may become inaccessible.
In this example, the billing method of the Service is set to pay-by-bandwidth and the bandwidth is capped at 2 Mbit/s to control traffic. The annotations are as follows:
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-charge-type: paybybandwidthservice.beta.kubernetes.io/alibaba-cloud-loadbalancer-bandwidth: 2
Label
Add a label to identify the service.
None
-
kubectl
Create a file named my-nginx-svc.yaml with the following content.
Make sure that the value of the
selectorfield (app: nginxin this example) in the Service manifest matches the value of thematchLabelsfield in the backend Deployment. This associates the Service with the backend Deployment.apiVersion: v1 kind: Service metadata: labels: app: nginx name: my-nginx-svc namespace: default spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: LoadBalancerRun the following command to create the my-nginx-svc Service and expose the application.
kubectl apply -f my-nginx-svc.yamlRun the following command to verify that the LoadBalancer Service was 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 5mRun the following command to access the sample application.
curl <YOUR-External-IP> # Replace <YOUR-External-IP> with the EXTERNAL-IP address that you obtained.Expected 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>