All Products
Search
Document Center

Alibaba Cloud Service Mesh:Use Knative on ASM to deploy a serverless application

Last Updated:Aug 29, 2023

Service Mesh (ASM) integrates the capabilities of the Knative Serving component that is deployed in a Container Service for Kubernetes (ACK) cluster or Serverless Kubernetes (ASK) cluster. This helps you manage serverless applications. This topic describes how to use Knative on ASM to create a Knative Service.

Prerequisites

  • An ASM instance of version 1.16 or later is created and an ACK or ACK Serverless cluster is added to the instance. For more information, see Create an ASM instance and Add a cluster to an ASM instance.

    Important
    • When you create an ASM instance, make sure that Allow data plane cluster KubeAPI to access Istio CR is selected in the Resource configuration section. This check box is selected by default. This indicates that you can access Istio resources by using the Kubernetes API of clusters on the data plane.

    • The destination ACK or ACK Serverless cluster has enabled public access to the API server for quick access.

    • The ASM instance and the ACK or ACK Serverless cluster must be in the same virtual private cloud (VPC) in the same region, and use the same vSwitch in the same VPC.

  • The Knative Serving component is deployed in the ACK or ACK Serverless cluster.

    • For more information about how to deploy Knative in an ACK cluster, see Deploy Knative.

    • For more information about how to deploy Knative in an ACK Serverless cluster, see Install Knative.

    Note

    On the Clusters page of the ACK console, click the name of the desired cluster and choose Applications > Knative in the left-side navigation pane. If you select Kourier as the gateway when you deploy Knative, uninstall Kourier after you deploy Knative by performing the following operations: Navigate to the Components tab. In the Add-on Component section, find the Kourier component and click Uninstall to uninstall it.

  • An ingress gateway is added to the cluster. The ASM instance uses an ASM ingress gateway as the gateway of the cluster. For more information, see Create an ingress gateway.

    Note

    The ASM gateway allows you to split traffic for different Knative Revisions, access Google Remote Procedure Call (gRPC) services, configure timeout and retry settings, mount Transport Layer Security (TLS) certificates, and perform external authentication and authorization. For more information, see Overview of ASM gateways.

Step 1: Enable Knative on ASM

  1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Ecosystem > Knative on ASM.

  3. On the Knative on ASM page, click Enable Knative on ASM.

Step 2: Deploy a Knative Service

Knative on ASM allows you to deploy a Knative Service in the ACK console or by using a YAML configuration file. You can select a deployment method based on your business requirements.

Deploy a Knative Service in the ACK console

  1. Log on to the ACK console and click Clusters in the left-side navigation pane.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Applications > Knative in the left-side navigation pane.

  3. On the Services tab, click Create Service in the upper-right corner, set the required parameters, and then click Create.

    Parameter

    Description

    Namespace

    Select the namespace to which the Service belongs.

    Service Name

    Enter a name for the Service.

    Image Name

    To select an image, click Select Image. In the Select Image dialog box, select an image and click OK. You can also specify a private registry. The registry address must be in the domainname/namespace/imagename:tag format. In this example, registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go is used.

    Image Version

    To select an image version, click Select Image Version. In this example, 73fbdd56 is used.

    Access Protocol

    HTTP and gRPC are supported.

    Container Port

    Set the container port that you want to expose. The port number must be in the range of 1 to 65535.

    Advanced

    Parameter

    Description

    Internal Access Only

    If you select Internal Access Only, the Knative Service cannot be accessed over the Internet.

    Maximum Concurrent Requests

    Set the maximum number of concurrent requests supported by the container. The default value is 0. This indicates that the number of concurrent requests is unlimited.

    Minimum Pods

    Set the minimum number of pods that must be kept running when no request is received. If you set this parameter to 0, the number of running pods is reduced to zero when no request is received.

    Maximum Pods

    Set the maximum number of pods that are allowed to run.

    Resource Limit

    Set the maximum amount of CPU, memory, and GPU resources that can be allocated to the Knative Service. This prevents the Service from occupying an excessive amount of resources.

    Lifecycle

    You can set values for Command and Parameter. Take note of the following limits:

    • If Command and Parameter are not set, the default values of Command and Parameter in the image configurations are used.

    • If only Parameter is set, the default value of Command in the image configurations and the specified value of Parameter are used.

    • If Command and Parameter are set, the default configurations of the image are overwritten.

    Environment Variables

    Set environment variables in key-value pairs.

    Volume

    You can mount local storage volumes and persistent volume claims (PVCs) to the container.

    • Add Local Storage: You can select EmptyDir, HostPath, ConfigMap, or Secret from the PV Type drop-down list. Then, set Mount Source and Container Path to mount the volume to the container. For more information, see Volumes.

    • Add PVC: Select Cloud Storage.

Deploy a Knative Service by using a YAML configuration file

  1. Save the following content as the hello.yaml file:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: helloworld-go
      annotations:
        knative.k8s.alibabacloud/tls: "false"
    spec:
      template:
        spec:
          containers:
            - image: registry.cn-hangzhou.aliyuncs.com/acs/helloworld-go:160e4dc8
              ports:
                - containerPort: 8080
              env:
                - name: TARGET
                  value: "Knative"
  2. Use kubectl to connect to the ACK cluster. Run the following command to create a Knative Service. Wait until the Knative Service is created.

    kubectl apply -f hello.yaml
  3. Run the following command to view the list of Knative Services:

    kubectl get ksvc

    Expected output:

    NAME            URL                                        LATESTCREATED         LATESTREADY           READY   REASON
    helloworld-go   http://helloworld-go.default.example.com   helloworld-go-00001   helloworld-go-00001   True

Step 3: Query the gateway address

Query the gateway address in the ACK console

  1. Log on to the ACK console and click Clusters in the left-side navigation pane.

  2. On the Clusters page, click the name of the cluster that you want to manage and choose Applications > Knative in the left-side navigation pane.

  3. Click the Services tab. In the list of Services, click the name of the Knative Service for which you want to query the gateway address to view the details of the Knative Service. In the Basic Information section, you can view and obtain information about Gateway and Default Domain.

Query the gateway address in the ASM console

  1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  2. On the Mesh Management page, click the name of the desired ASM instance. In the left-side navigation pane, choose ASM Gateways > Ingress Gateway.

  3. In the Service address section on the Ingress Gateway page, view and obtain the gateway address.

Step 4: Access the Knative Service

After the Knative Service is deployed, you can point its domain name to the IP address of a gateway to associate the Service with the gateway. This allows you to access the Knative Service by using its URL.

  1. Add the following information to the hosts file to point the domain name of the Service to the IP address of the gateway that you obtained in Step 3: Query the gateway address.

    The following sample code shows the content that is to be added to the hosts file.

    Note

    Replace xx.xx.xxx.xx with the gateway address that you actually access.

    xx.xx.xxx.xx helloworld-go.default.example.com
  2. After you add the preceding information to the hosts file, you can directly access the Knative Service by using its domain name.

    • Access the Knative Service by running the following command

      curl http://helloworld-go.default.example.com
      
      # Expected output:
      Hello Knative!
    • Access the Knative Service by visiting the following website

      Enter http://helloworld-go.default.example.com in a browser to directly access the Service.

      Expected output:HelloKnative

Related operations