All Products
Search
Document Center

Container Compute Service:Expose an application by using a service that automatically creates a load balancer

Last Updated:Sep 17, 2025

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.

  • Important

    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.

  • 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.

    Important

    Do 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.

Quota limits

Step 1: Deploy the sample application

You can deploy the application from the kubectl command line.

  1. 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.
  2. Deploy the my-nginx sample application.

    kubectl apply -f my-nginx.yaml
  3. Confirm that the sample application is in the Normal state.

    kubectl get deployment my-nginx

    Example 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

  1. Log on to the ACS console. In the left-side navigation pane, click Clusters.

  2. 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.

  3. On the Services page, click Create in the upper-left corner.

  4. In the Create Service dialog box, set the parameters for the Service.

  5. 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:

    ClusterIP

    This mode is mainly for internal communication within the cluster. Service discovery between service instances is supported only when the service type is set to the virtual ClusterIP. Using a Headless Service, you can interact with other service discovery mechanisms without relying on the default ClusterIP-based service discovery and load balancing provided by Kubernetes.

    LoadBalancer

    This mode exposes internal cluster applications to the internet by integrating with Alibaba Cloud Classic Load Balancer (CLB) and Network Load Balancer (NLB). This mode provides higher availability and performance than the NodePort mode.

    1. Select the Server Load Balancer service type.

    2. For Load balancer type, select Classic Load Balancer (CLB), and for Resource, select New Resource.

    3. 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 port field in the YAML file and the container port corresponds to the targetPort field 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.

    Important

    Do 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:paybybandwidth

    • service.beta.kubernetes.io/alibaba-cloud-loadbalancer-bandwidth:2

    Labels

    The label to be added to the Service, which identifies the Service.

    None

Use kubectl

  1. Create a file named my-nginx-svc.yaml with the following content for the Service.

    Make sure that the value of the selector field (app: nginx in this example) matches the value of the matchLabels field 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: LoadBalancer
  2. Run 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.yaml
  3. Run the following command to confirm that the LoadBalancer Service is created:

    kubectl get svc my-nginx-svc

    Expected 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
  4. Run 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.