Expose an application by using a LoadBalancer Service

更新时间:
复制 MD 格式

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.

  • Important

    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.

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

    Important

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

Quotas

Step 1: Deploy a sample application

The following steps describe how to deploy the application by using kubectl.

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

    kubectl apply -f my-nginx.yaml
  3. Verify that the application is running.

    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 by using a LoadBalancer Service

You can use the console and kubectl to create a LoadBalancer Service and expose an application.

Console

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

  2. On the Clusters page, click the name of the target cluster. In the left navigation pane, 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

    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:

    Cluster IP

    This type is primarily used for communication within the cluster. Service discovery between instances is supported only for ClusterIP Services. By 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 type integrates with Alibaba Cloud Classic Load Balancer (CLB) and Network Load Balancer (NLB) to expose applications in the cluster. This mode provides higher availability and performance than the NodePort mode.

    1. Select Server Load Balancer as the Server Load Balancer.

    2. For CLB, select CLB. For Create Resource, select Create Resource.

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

    Important

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

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

    Label

    Add a label to identify the service.

    None

kubectl

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

    Make sure that the value of the selector field (app: nginx in this example) in the Service manifest matches the value of the matchLabels field 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: LoadBalancer
  2. Run the following command to create the my-nginx-svc Service and expose the application.

    kubectl apply -f my-nginx-svc.yaml
  3. Run the following command to verify that the LoadBalancer Service was 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 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>