The cloud-controller-manager (CCM) component automatically creates and manages a load balancer for a Service of the LoadBalancer type. 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 show you how to expose an application using a Service that automatically creates a load balancer.
Notes
The CCM configures a load balancer only for Services of type
Type=LoadBalancer. The CCM does not configure load balancers for other Service types.The CCM uses a declarative API. It automatically refreshes the load balancer configuration based on the Service configuration. As a result, any configurations that you modify in the Server Load Balancer console risk being overwritten.
ImportantDo not manually modify the configuration of a load balancer that is managed by Kubernetes in the Server Load Balancer console. Your changes may be overwritten, which can cause the Service to become inaccessible.
You cannot change the load balancer for an existing Service of the LoadBalancer type. 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 load balancer configuration. This makes the Service inaccessible through the load balancer.
Quota limits
The CCM creates load balancers for Services of type
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 listeners based on the ports defined in the Service. By default, you can add up to 50 listeners to a load balancer instance. To add more listeners, log on to the Quota Center console and submit an application.
For more information about load balancer limits, see Limits.
To query your load balancer quotas, see Load balancer quota management.
Step 1: Deploy the sample application
You can deploy the application from the kubectl command line.
Create a file named my-nginx.yaml that contains the following YAML content for the sample application.
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 exposes 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. The format is <image_name:tags>. ports: - containerPort: 80 # This port must be exposed in the Service.Deploy the my-nginx sample application.
kubectl apply -f my-nginx.yamlConfirm that the sample application is in the Normal state.
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 using a service that automatically creates a load balancer
You can create a Service of the LoadBalancer type using the console or kubectl to expose the application.
Use the console
Log on to the ACS console. In the left-side navigation pane, click Clusters.
On the Clusters page, find the cluster that you want to manage and click its ID. In the left-side navigation pane of the cluster details page, 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
Service Name
The name of the Service.
my-nginx-svc
Service Type
Select a Service type. The Service network supports the following modes for different types of clients and access sources:
Select the Server Load Balancer service type.
For Load balancer type, select Classic Load Balancer (CLB), and for Resource, select New Resource.
In the Create CLB Resource dropdown list, adjust the configuration items as needed and set Billing Method to Pay-by-specification (PayBySpec).
External Traffic Policy
You can set the External Traffic Policy only when the service type is Node Port or Server Load Balancer.
Local: routs traffic only to the pods of the current node.
Cluster: Forwards traffic to pods on other nodes in the cluster.
Local
Service Association
The backend application that you want to associate with the Service. If you do not select a backend application, no Endpoint objects are created. For more information, see Services-without-selectors.
Name: app
Value: nginx
Port Mapping
The Service port and container port. The Service port corresponds to the
portfield in the YAML file and the container port corresponds to thetargetPortfield in the YAML file. The container port must be the same as the port that is exposed in the backend pod.80
Annotations
Add an annotation to the service to configure the load balancer parameters. For more information, see Configure a CLB instance using annotations.
ImportantDo not reuse the SLB instance of the cluster's API Server. Otherwise, cluster access may become abnormal.
In this example, the billing method for the service is set to pay-by-bandwidth and the bandwidth peak is set to 2 Mbit/s to control service traffic. The annotations are as follows:
service.beta.kubernetes.io/alibaba-cloud-loadbalancer-charge-type:paybybandwidthservice.beta.kubernetes.io/alibaba-cloud-loadbalancer-bandwidth:2
Labels
The label to be added to the Service, which identifies the Service.
None
Use kubectl
Create a file named my-nginx-svc.yaml with the following content for the Service.
Make sure that the value of the
selectorfield (app: nginxin this example) matches the value of thematchLabelsfield in the backend application's deployment. This associates the Service with the backend application.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 a Service named my-nginx-svc and use the Service to expose the application:
kubectl apply -f my-nginx-svc.yamlRun the following command to confirm 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 5mRun the following command to access the application:
curl <YOUR-External-IP> # Replace <YOUR-External-IP> with the external IP address obtained in the previous step.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>Thank you for using nginx.