All Products
Search
Document Center

Container Compute Service:Getting started with ACS using kubectl

Last Updated:Dec 10, 2024

Alibaba Cloud Container Compute Service (ACS) provides high-performance containerized computing services. You can use ACS to run containerized applications in the cloud in a convenient and efficient manner. This topic describes how to use kubectl to deploy, expose, and monitor a containerized application in an ACS cluster.

Background information

  • This topic demonstrates how to use an image to deploy an application named acs-kube in an ACS cluster. This application provides an online magic cube game. After you perform the steps in this topic, an ACS cluster is created and deployed with an application that provides a magic cube game.

  • The container image used to deploy the application is built based on an open source project. The image address is registry.cn-hangzhou.aliyuncs.com/acr-toolkit/ack-cube.

  • kubectl is a command-line tool that Kubernetes provides for you to connect to and manage Kubernetes clusters. For more information about kubectl, see kubectl.

  • Cloud Shell is a web-based command-line tool provided by Alibaba Cloud. You can use kubectl in Cloud Shell in the ACS console to manage ACS clusters. Installation and configuration are not required.

Procedure

image

Activate and grant permissions to ACS

If this is the first time you use ACS, you must activate ACS and grant ACS the permissions to access cloud resources.

  1. Log on to the ACS console and click Activate.

  2. Go to the ACS activation page and follow the on-screen instructions to activate ACS.

  3. Return to the ACS console and refresh the page. Click Authorize Now.

  4. Go to the ACS authorization page and follow the on-screen instructions to grant permissions to ACS.

    After you complete the preceding operations, refresh the ACS console. Then, you can get started with ACS.

Step 2: Create an ACS cluster.

This step shows how to configure cluster parameters when you create an ACS cluster.

  1. In the upper-left corner of the Clusters page, click Create Cluster.

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

  3. On the Create Cluster page, set the parameters described in the following table.

    Use default settings for parameters that are not listed in the table.

    Parameter

    Description

    Example

    Cluster Name

    Enter a name for the cluster.

    ACS-Demo

    Region

    Select a region to deploy the cluster.

    China (Beijing)

    VPC

    ACS clusters can be deployed only in virtual private clouds (VPCs). You must specify a VPC in the same region as the cluster.

    Click Create VPC to create a VPC named vpc-acs-demo in the China (Beijing) region. For more information, see Create and manage a VPC.

    vpc-acs-demo

    vSwitch

    Select vSwitches for nodes in the cluster to communicate with each other.

    Click Create vSwitch and create a vSwitch named vswitch-ack-demo in the vpc-ack-demo VPC. Then, select vswitch-ack-demo in the vSwitch list. For more information, see Create and manage a vSwitch.

    vswitch-acs-demo

    API Server Access Settings

    Specify whether to expose the Kubernetes API server of the cluster to the Internet. If you want to manage the cluster over the Internet, you must expose the Kubernetes API server with an elastic IP address (EIP).

    Select Expose API Server with EIP.

    Service Discovery

    Specify whether to enable service discovery for the cluster. To enable service discovery, select CoreDNS.

    Select CoreDNS.

  4. Click Confirm Order, read and select Terms of Service, and then click Create Cluster.

    Note

    It requires approximately 10 minutes to create a cluster. After the cluster is created, you can view the cluster on the Clusters page.

Step 3: Connect to the cluster

This step shows how to connect to the ACS cluster by using a kubectl client or Cloud Shell. For more information, see Obtain the kubeconfig file of a cluster and how to use kubectl to connect to the cluster and Use kubectl on Cloud Shell to manage ACS clusters.

Connect to the cluster by using a kubectl client

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

  2. On the Clusters page, click the name of the cluster you created, which is ACS-Demo in this example.

  3. On the Cluster Information page, click the Connection Information tab. Click Copy on the Public Access tab to copy the credentials used to access the cluster over the Internet.

  4. Paste the credential to the config file in the $HOME/.kube directory, save the file, and then exit.

    Note

    If the .kube folder and the config file do not exist in the $HOME/ directory, you must manually create the folder and file.

  5. Run the following kubectl command to verify the connectivity of the cluster.

    Run the following command to query the namespaces of the cluster:

    kubectl get namespace

    Expected output:

    NAME              STATUS   AGE
    arms-prom         Active   4h39m
    default           Active   4h39m
    kube-node-lease   Active   4h39m
    kube-public       Active   4h39m
    kube-system       Active   4h39m

Connect to the cluster by using Cloud Shell

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

  2. On the Clusters page, find the cluster you created and choose More > Open Cloud Shell in the Actions column.

    It requires a few seconds to start Cloud Shell. After Cloud Shell is started, you can run kubectl commands on the Cloud Shell interface to manage the cluster and applications deployed in the cluster.

Step 4: Deploy and expose an application

This step shows how to use kubectl to deploy a stateless application by creating a Deployment and then use a LoadBalancer Service to expose the application. For more information about how to expose an application, see Use an automatically created SLB instance to expose an application.

  1. Create a file named acs-cube.yaml and copy the following content to the file.

    Show the content of acs-cube.yaml

    apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
    kind: Deployment
    metadata:
      name: acs-cube # The application name. 
      labels:
        app: acs-cube
    spec:
      replicas: 2 # The number of replicated pods. 
      selector:
        matchLabels:
          app: acs-cube  # The value must be the same as the selector of the Service that is used to expose the application. 
      template:
        metadata:
          labels:
            app: acs-cube
        spec:
          containers:
          - name: acs-cube
            image: registry.cn-hangzhou.aliyuncs.com/acr-toolkit/ack-cube:1.0 # Replace with the address of the image that you want to use in the <image_name:tags> format. 
            ports:
            - containerPort: 80 # The container port that you want to expose. 
            resources:
              limits: # Set resource limits. 
                cpu: '1'
                memory: 1Gi
              requests: # Set resource requests. 
                cpu: 500m
                memory: 512Mi        
  2. Run the following command to deploy the acs-cube application:

    kubectl apply -f acs-cube.yaml
  3. Run the following command to query the status of the application:

    kubectl get deployment acs-cube

    Expected output:

    NAME       READY   UP-TO-DATE   AVAILABLE   AGE
    acs-cube   2/2     2            2           96s
  4. Create a file named acs-cube-svc.yaml and copy the following content to the file.

    Set selector to the value of matchLabels in the ack-cube.yaml file. In this example, the value is set to app: ack-cube. This adds the application to the backend of the Service.

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: acs-cube
      name: acs-cube-svc
      namespace: default
    spec:
      ports:
      - port: 80
        protocol: TCP
        targetPort: 80
      selector:
        app: acs-cube # You must specify the value of the matchLabels parameter in the YAML file used to create the Deployment. 
      type: LoadBalancer
  5. Run the following command to create a Service named ack-cube-svc and use the Service to expose the application.

    ACS automatically creates an Internet-facing Server Load Balancer (SLB) instance and associates the SLB instance with the Service.

    kubectl apply -f acs-cube-svc.yaml
  6. Run the following command to check whether the LoadBalancer Service is created.

    The application that you created is exposed by using the IP address in the EXTERNAL-IP column in the output.

    kubectl get svc acs-cube-svc

    Expected output:

    NAME           TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
    acs-cube-svc   LoadBalancer   172.16.72.161   47.94.xx.xx   80:31547/TCP   32s

Step 5: Test the application

Enter the IP address (EXTERNAL-IP) in the address bar of your browser and press Enter to start the magic cube game.

References

  • To enable auto scaling for application pods, you can configure the Horizontal Pod Autoscaler (HPA), Cron Horizontal Pod Autoscaler (CronHPA), and Vertical Pod Autoscaler (VPA). For more information, see Auto scaling overview.

  • In addition to exposing applications through Services, you can use Ingresses to enable application traffic routing at Layer 7. For more information, see Get started with ALB Ingresses.

  • If you want to view monitoring information about applications, go to the cluster details page and choose Operations > Prometheus Monitoring in the left-side navigation pane. On the Prometheus Monitoring page, you can view monitoring information such as CPU utilization, memory utilization, and network I/O. For more information, see Use Managed Service for Prometheus to monitor ACS clusters.