Deploy a workload and implement load balancing in an ACK Auto Mode cluster

更新时间:
复制 MD 格式

This topic shows you how to deploy a workload in an ACK Auto Mode cluster and expose it to the internet using an ALB Ingress. After completing these steps, you can access the application with a specified domain name for efficient external traffic management and load balancing.

This topic covers the following operations:

  1. Create a namespace for the sample application.

  2. Deploy a sample Nginx application and create a Service to expose the application to the cluster network.

  3. Create an ALB Ingress to expose the application to the internet. You need to manually create an AlbConfig to manage the ALB instance and an IngressClass to associate with the AlbConfig.

  4. Verify that the deployment is successful and access the application from a browser. You can release the resources if they are no longer needed.

After you complete these operations, you will have:

  • A running sample Nginx application with two replicas.

  • A stable public entry point for the application provided by an ALB Ingress and a Service.

  • Reduced operational overhead because ACK in Auto Mode automatically scales resources and manages the node lifecycle based on the application load.

Prerequisites

Step 1: Create a namespace

Create a namespace to isolate the resources for this tutorial.

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

  2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Namespaces and Quotas.

  3. Click Create. In the dialog box that appears, enter a custom namespace name (this topic uses my-nginx-namespace as an example), configure other settings as prompted on the page, and then click OK.

Step 2: Deploy the Nginx application and Service

In this section, you deploy a sample Nginx Deployment named my-nginx and a Service named my-nginx-svc to expose the application.

  • Deployment: 2 replicas. The image uses a sample Nginx application. Port 80 is exposed to receive HTTP traffic.

  • Service: The Service type is ClusterIP. Service port 80 maps to pod container port 80. The Service uses a label selector to match pods.

This section provides only a sample procedure. For detailed procedures to create workloads and Services, see Create a workload and Service management.
  1. Create a file named my-nginx.yaml with the following content to deploy the Deployment and Service.

    apiVersion: apps/v1 
    kind: Deployment
    metadata:
      name: my-nginx    # The name of the sample application.
      namespace: my-nginx-namespace  # Replace with the name of the namespace that you created.
      labels:
        app: nginx
    spec:
      replicas: 2       # The number of replicas.
      selector:
        matchLabels:
          app: nginx     # The value must match the selector in the Service to expose this application.
      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         # This port must be exposed in the Service.
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: nginx
      name: my-nginx-svc
      namespace: my-nginx-namespace  # Replace with the name of the namespace that you created.
    spec:
      ports:
      - port: 80
        protocol: TCP
        targetPort: 80
      selector:
        app: nginx
      type: ClusterIP   # If you use Flannel, you must use NodePort.
  2. Deploy the Deployment and the Service.

    kubectl apply -f my-nginx.yaml
  3. Check the status of the Deployment and Service.

    • Check the status of the Deployment.

      kubectl get deployment my-nginx -n my-nginx-namespace

      Expected output:

      NAME       READY   UP-TO-DATE   AVAILABLE   AGE
      my-nginx   2/2     2            2           4m36s
    • Check the status of the Service.

      kubectl get svc my-nginx-svc -n my-nginx-namespace

      Expected output:

      NAME           TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE
      my-nginx-svc   ClusterIP   192.XX.XX.164   <none>        80/TCP    42s

Step 3: Create an ALB Ingress and related resources

To manage external traffic that accesses applications in the cluster by using an ALB Ingress, you must prepare the following resources.

Before you create an ALB Ingress, we recommend that you learn about its principles and requirements in ALB Ingress management.
  • ALB Ingress Controller: a component that manages Ingress resources. After you enable Auto Mode for a cluster, the ALB Ingress Controller is installed by default.

  • AlbConfig: a CustomResourceDefinition (CRD) created by the ALB Ingress Controller to declare the configuration of an ALB instance. Each AlbConfig corresponds to one ALB instance. An ALB instance is the entry point for user requests and forwards requests to backend Services.

  • IngressClass: When you create an ALB Ingress, you can specify this IngressClass to reference the corresponding AlbConfig. This allows you to apply specific application routing configurations and load balancing policies.

Create an AlbConfig

  1. Create a file named alb.yaml with the following content to create an AlbConfig.

    • This AlbConfig sets the addressType to Internet. This means that the ALB instance is assigned a public IP address, its DNS domain name resolves to the public IP address, and the instance is accessible from the internet.

    • For vSwitchId, enter the IDs of two vSwitches from two different zones. The vSwitches must be in the same VPC as the cluster and in zones supported by ALB.

      In the VPC console, you can obtain the vSwitch ID on the vSwitch page. If you need to create a vSwitch, see Create and manage vSwitches.
    apiVersion: alibabacloud.com/v1
    kind: AlbConfig
    metadata:
      name: alb
    spec:
      config:
        name: alb
        addressType: Internet  # The address type of the load balancer. An Internet-facing ALB instance is accessible over the internet.
        zoneMappings:               
        - vSwitchId: vsw-uf6ccg2a9g71hx8go****  # Replace with the ID of your vSwitch.
        - vSwitchId: vsw-uf6nun9tql5t8nh15****  # Replace with the ID of your vSwitch.
      listeners:
        - port: 80
          protocol: HTTP
  2. Create the AlbConfig.

    kubectl apply -f alb.yaml
  3. View the AlbConfig resource.

    kubectl get AlbConfig alb

    Expected output:

    NAME   ALBID        DNSNAME                                  PORT&PROTOCOL   CERTID   AGE
    alb    alb-******   alb-******.<regionID>.alb.aliyuncs.com                            60s
    Note

    The PORT&PROTOCOL and CERTID fields are populated only after you create an HTTPS listener and configure a certificate for it. It is normal for these fields to be empty.

Create an IngressClass

  1. Create a file named ingress_class.yaml with the following content to create an IngressClass.

    apiVersion: networking.k8s.io/v1
    kind: IngressClass
    metadata:
      name: alb
    spec:
      controller: ingress.k8s.alibabacloud/alb
      parameters:
        apiGroup: alibabacloud.com
        kind: AlbConfig
        name: alb # The name of the AlbConfig associated with the IngressClass.
  2. Create the IngressClass.

    kubectl apply -f ingress_class.yaml

Create an ALB Ingress

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

  2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Network > Ingresses.

  3. On the Ingresses page, select the namespace that you created, and then click Create Ingress. In the Create Ingress dialog box, configure the Ingress and follow the on-screen instructions to create the Ingress.

    The following table describes only the key parameters. For more information about the parameters and related operations, such as configuring DNS resolution, see Create an ALB Ingress.

    Parameter

    Description

    Gateway Type

    Select ALB Ingress.

    Name

    Enter a custom name for the Ingress, such as my-albingress.

    IngressClass

    Select the IngressClass that you created to reference the corresponding AlbConfig.

    Rules

    • Name: Select the destination Service, which is the Service that you created.

    • Port: Select the port that you want to expose for the Service, which is 80 in this example.

    Keep the default values for other parameters.

    After the Ingress is created, return to the Ingresses page. After about a minute, find and copy the DNS name from the Endpoint column.

Step 4: Access the application

Paste the DNS name into your browser to access the Nginx application.

The browser displays the default Nginx welcome page with the title Welcome to nginx on Anolis OS! This indicates that the web server is running as expected and the ALB Ingress configuration is working correctly.

(Optional) Step 5: Clean up resources

The resources created in this topic include a Deployment, a Service, an AlbConfig, an IngressClass, and an ALB Ingress. Run the following commands to clean up the resources.

Replace the sample names in the following commands with your actual resource names.
kubectl delete deployment my-nginx -n my-nginx-namespace
kubectl delete service my-nginx-svc -n my-nginx-namespace
kubectl delete ingress my-albingress -n my-nginx-namespace
kubectl delete albconfig alb
kubectl delete ingressclass alb

Related documents