Expose an application with an existing SLB instance

Updated at:
Copy as MD

Attach an existing CLB or NLB to a LoadBalancer Service to keep a fixed IP or share across Services.

From outside the cluster, use <SLB-IP>:<Service port> or the SLB domain name. From within the cluster, use <Service name>:<Service port>.

Prerequisites

Ensure the following:

Important

If you plan to reuse an existing SLB instance, do not select Install Ingresses during cluster creation. Specify the instance manually afterward. See Use an existing CLB instance and Use an existing NLB instance.

Usage notes

Listener management depends on the cloud-controller-manager version, and some SLB instances cannot be reused.

Listener behavior by cloud-controller-manager version

Listener configuration depends on the cloud-controller-manager version:

  • v1.9.3.59-ge3bc999-aliyun and later: cloud-controller-manager does not configure listeners by default. Add service.beta.kubernetes.io/alibaba-cloud-loadbalancer-force-override-listeners: "true" to let ACK manage listeners, or configure them manually.

  • Earlier than v1.9.3.59-ge3bc999-aliyun: cloud-controller-manager creates and manages backend server groups, associating all listeners—including manually configured ones—with those groups.

Check the cloud-controller-manager version

  • Console: In the ACK console, go to your cluster's Operations > Add-ons page. On the Core Components tab, check the Cloud Controller Manager version.

  • kubectl (ACK dedicated clusters only):

    kubectl get pod -n kube-system -o yaml | grep image: | grep cloud-con | uniq

Usage limits

Deploy an application

Deploy a stateless NGINX application as the LoadBalancer Service backend.

Use the ACK console

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

  2. On the Clusters page, click your cluster name. In the left pane, choose Workloads > Deployments.

  3. On the Deployments page, click Create from Image and configure the Deployment:

    1. On the Basic Information page, set Name to my-nginx and click Next.

    2. On the Container page, configure the image and port:

      Parameter

      Value

      Image Name

      Click Select images. On the Artifact Center tab, search for nginx and select openanolis/nginx. Click Select Image Tag, set a tag, and click OK.

      Port

      Name: nginx, Container Port: 80

    3. On the Advanced page, keep the defaults and click Create.

Use kubectl

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

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-nginx        # Deployment name
      labels:
        app: nginx
    spec:
      replicas: 2           # Number of pod replicas
      selector:
        matchLabels:
          app: nginx        # Must match the selector in the Service
      template:
        metadata:
          labels:
            app: nginx
        spec:
        #  nodeSelector:
        #    env: test-team
          containers:
          - name: nginx
            image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6
            ports:
            - containerPort: 80
  2. Deploy the application:

    kubectl apply -f my-nginx.yaml
  3. Verify the Deployment is ready:

    kubectl get deployment my-nginx

    Expected output:

    NAME       READY   UP-TO-DATE   AVAILABLE   AGE
    my-nginx   2/2     2            2           50s

Create a LoadBalancer Service

Point the Service to your existing SLB instance to route external traffic to the NGINX pods.

Use the ACK console

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

  2. On the Clusters page, click your cluster name. In the left pane, choose Network > Services.

  3. On the Services page, click Create. In the Create Service dialog box, configure these parameters:

    Parameter

    Description

    Example

    Name

    Name of the Service

    my-nginx-svc

    Service Type

    Select SLB, set SLB Type to CLB, and select Use Existing Resource. Choose a CLB instance. For instances without listeners, enable Overwrite Existing Listeners.

    SLB > CLB > Use Existing Resource

    External Traffic Policy

    Local routes traffic only to pods on the current node. Cluster routes traffic to pods across all nodes.

    Local

    Backend

    The application to associate with the Service

    Name: app, Value: nginx

    Port Mapping

    Maps Service port to container port

    Service port: 80, Container port: 80, Protocol: TCP

    Annotations

    Add annotations to configure the SLB instance. See Use annotations to configure CLB instances and Use annotations to configure NLB instances.

    N/A

  4. Click OK. Click the Service name to open details, then click the External IP (for example, 39.106.XX.XX:80) to verify access.

    image

Use kubectl

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

    Field

    Description

    metadata.annotations

    alibaba-cloud-loadbalancer-id: the existing SLB instance ID. alibaba-cloud-loadbalancer-force-override-listeners: 'true': lets ACK manage listeners. Omit to keep existing listener configuration.

    spec.selector

    Selects pods to receive traffic. Must match the matchLabels in the Deployment (app: nginx).

    spec.ports.port

    The port exposed on the cluster IP. Access within the cluster via clusterIP:port.

    spec.ports.targetPort

    The backend pod port. kube-proxy forwards traffic from port to targetPort.

    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        service.beta.kubernetes.io/alibaba-cloud-loadbalancer-id: ${YOUR_LB_ID}                      # Replace with your SLB instance ID
        service.beta.kubernetes.io/alibaba-cloud-loadbalancer-force-override-listeners: 'true'       # Set to true for a new instance with no listeners configured
      labels:
        app: nginx
      name: my-nginx-svc
      namespace: default
    spec:
      ports:
      - port: 80
        protocol: TCP
        targetPort: 80
      selector:
        app: nginx            # Must match the labels on the backend pods
      type: LoadBalancer

    Key fields:

  2. Apply the Service:

    kubectl apply -f my-nginx-svc.yaml
  3. Confirm the Service has an external IP:

    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:30471/TCP   5m
  4. Access the application:

    curl <YOUR-EXTERNAL-IP>    # Replace with the external IP from the previous step

    Expected output:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    ...
    </html>

Manage Services

Use the ACK console

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

  2. On the Clusters page, click your cluster name. In the left pane, choose Network > Services.

  3. On the Services page, click a Service name for details, or click Update or Delete in the Actions column.

Use kubectl

Update a Service

kubectl edit service my-nginx-svc

Or edit my-nginx-svc.yaml and reapply:

kubectl apply -f my-nginx-svc.yaml

View a Service

kubectl get service my-nginx-svc

Expected output:

NAME           TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE
my-nginx-svc   LoadBalancer   172.21.XX.XX   192.168.XX.XX    80:31599/TCP   5m

Delete a Service

kubectl delete service my-nginx-svc

CLB configuration reference

Configure these parameters when you create or reuse a CLB instance in the ACK console.

Create a new CLB instance

Parameter

Description

Name

Name of the CLB instance

Access Method

Public Access or Internal Access

Billing Method

Pay-by-specification or Pay-as-you-go (Pay-by-CU). See CLB billing.

IP Version

IPv4 or IPv6

Scheduling Algorithm

Round Robin (RR) (default): distributes requests in sequence. Weighted Round Robin (WRR): higher-weight servers receive more requests.

Access Control

Enable or disable listener-level access control. See Access control.

Health Check

TCP or HTTP. See CLB health checks.

Others

Configure with annotations. See Use annotations to configure CLB instances.

Use an existing CLB instance

Select the instance and optionally enable Overwrite Existing Listeners. See Use an existing CLB instance and forcefully overwrite the listeners of the CLB instance.

Important

Review usage limits before reusing a CLB. See Usage notes in "Considerations for configuring a LoadBalancer Service."

Advanced settings

Parameter

Description

Scheduling Algorithm

Round Robin (RR) (default) or Weighted Round Robin (WRR)

Access Control

Enable or disable listener access control

Health Check

TCP or HTTP. See CLB health checks.

Others

Configure with annotations. See Use annotations to configure CLB instances.

NLB configuration reference

Configure these parameters when you create or reuse an NLB instance in the ACK console.

Create a new NLB instance

Parameter

Description

Name

Name of the NLB instance

Access Method

Public Access or Internal Access

Billing Method

Pay-as-you-go (Pay-by-CU). See NLB billing.

IP Version

IPv4 or Dual-stack

Scheduling Algorithm

Round-Robin: distributes in sequence. Weighted Round-Robin (default): higher-weight servers receive more requests. Source IP Hashing: same source IP routes to same server. Four-Element Hashing: routes by source IP, destination IP, source port, and destination port. QUIC ID Hashing: same QUIC ID routes to same server. Weighted Least Connections: routes by weight and connection count.

Health Check

TCP (default): SYN probe. Configure Health Check Response Timeout, Health Check Interval, Healthy Threshold, and Unhealthy Threshold. HTTP: HEAD/GET probe. Configure Domain Name (Backend Server Internal IP or Custom Domain Name), Path, and Health Check Status Codes (http_2xx, http_3xx, http_4xx, http_5xx; default: http_2xx).

VPC

The cluster's VPC region and VPC ID

Vswitch

Select a vSwitch in a supported zone, or create one

Others

Configure with annotations. See Use annotations to configure NLB instances.

Use an existing NLB instance

Select the instance and optionally enable Overwrite Existing Listeners. See Use an existing NLB instance.

Important

Review usage limits before reusing an NLB. See Usage notes in "Considerations for configuring a LoadBalancer Service."

Advanced settings

Parameter

Description

Scheduling Algorithm

Same options as the Create NLB instance table above

Health Check

Same options as the Create NLB instance table above

VPC

The cluster's VPC region and VPC ID

Vswitch

Select a vSwitch in a supported zone, or create one

Others

Configure with annotations. See Use annotations to configure NLB instances.

Next steps