Deploying a serverless application using Knative on ASM

Updated at:

Service Mesh (ASM) integrates with the Knative Serving capabilities of Service Mesh and Service Meshs to simplify deploying and managing Service Meshs in various Service Mesh environments. This improves development efficiency and enhances business resilience by enabling you to deploy and scale Service Meshs quickly and flexibly. This topic describes how to create a Knative Service by using Knative on ASM.

Prerequisites

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

    Important
    • When creating an ASM instance, select Allow data plane cluster KubeAPI to access Istio CR in the Resource configuration section. This option, selected by default, allows you to access Istio resources by using the Kubernetes API of clusters on the data plane.

    • The public API server must be enabled for the target ACK or ACK Serverless cluster to ensure quick access.

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

  • You have added an ingress gateway to the cluster. This example uses an ASM ingress gateway. For more information, see Create an ingress gateway.

    Note
    • For ASM instances of version 1.21.6.84 or later, you do not need to manually create an ingress gateway.

    • The ASM gateway supports traffic distribution for Knative Revisions, gRPC services, timeouts and retries, TLS certificates, and external authentication and authorization. For more information, see Overview of ASM gateways.

Step 1: Install Knative

Versions earlier than 1.18.2.104

  1. Deploy the Knative Serving component in your ACK or ACK Serverless cluster. For Gateway, select Kourier.

    • For more information about how to install Knative on ACK, see Deploy and manage Knative components.

    • For more information about how to install Knative on ACK Serverless, see Deploy Knative.

      Important

      After the component is installed, click the Components tab. In the Add-on Component section, uninstall the Kourier component.

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

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

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

Versions 1.18.2.104 to 1.21.6.83

  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, select the required Knative version and click Enable Knative on ASM.

  4. Deploy Knative in your ACK or ACK Serverless cluster. For Gateway, select ASM.

Versions 1.21.6.84 and later

Important

For ASM instances of version 1.21.6.84 or later, you do not need to enable Knative on ASM in the ASM console.

  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 Applications > Knative.

  3. On the Knative page, go to the Components tab and click Deploy Knative. Select ASM for the Gateway and click Deploy.

    After the deployment is successful, you can use Service Mesh (ASM) in Knative.

    Note

    After the deployment completes, ensure the ingress gateway is created and running properly before you deploy a Knative Service.

Step 2: Deploy a Knative Service

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

Method 1: ACK console

  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 Applications > Knative.

  3. On the Knative page, click the Services tab. In the upper-right corner, click Create Service. Configure the parameters and click Create.

    For more information about the parameters, see Quickly deploy a Knative Service.

    Parameter

    Description

    Example

    Namespace

    Select the namespace to which the service belongs.

    default

    Service Name

    Enter a custom name for the service.

    helloworld-go

    Image Name

    Click Select Image, select the desired image, and click OK. You can also specify an image from a private registry by using the format domainname/namespace/imagename:tag.

    registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go

    Image Version

    Click Select Image Version, select the target version, and click OK.

    73fbdd56

    Access Protocol

    HTTP and gRPC are supported.

    HTTP

    Container Port

    Set the container port to expose. The port number must be between 1 and 65535.

    8080

    Environment Variables

    Click Advanced, then click Add next to Environment Variables to configure them as key-value pairs.

    Type: Custom

    Variable Name: TARGET

    Value/ValueFrom: Knative

Method 2: YAML file

  1. Save the following content to a file named helloworld-go.yaml.

    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/knative-sample/helloworld-go:73fbdd56
              ports:
                - containerPort: 8080
              env:
                - name: TARGET
                  value: "Knative"
  2. Connect to the ACK cluster using kubectl. Run the following command to create the Knative Service.

    kubectl apply -f helloworld-go.yaml
  3. Run the following command to list the 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

Method 3: Template

  1. Log on to the ACK console. In the navigation pane on the left, choose Cluster.

  2. On the Clusters page, click the name of the target cluster. In the navigation pane on the left, choose Applications > Knative.

  3. On the Knative page, click the Services tab. Set Namespace to default. Click Create from Template, paste the following YAML sample into the editor, and click Create. This action creates a service named helloworld-go.

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: helloworld-go
    spec:
      template:
        spec:
          containers:
          - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56 # This is a sample image. You can replace it with your own image.
            env:
            - name: TARGET
              value: "Knative"

Step 3: Get the access gateway address

Method 1: ACK console

  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 Applications > Knative.

  3. Click the Services tab. In the service list, click the target service name to open its details page. In the Basic Information section, view and copy the Gateway address and the Default Domain.

Method 2: 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 target instance. In the navigation pane on the left, choose ASM Gateways > Ingress Gateway.

  3. On the Ingress Gateway page, view and copy the access gateway address from the Service address section.

Step 4: Access the service

After creating the Knative Service, access it by sending a request to the ingress gateway's IP address. You must specify the service's domain in the request's Host header.

  1. In the Gateway column on the Services page, get the gateway address for the helloworld-go service.

  2. Run the following command to access the helloworld-go service.

    curl -H "host: helloworld-go.default.example.com" http://39.XX.XX.XX # Replace the IP address with your actual gateway IP address.

    Expected output:

    Hello Knative!

    The output indicates that the service was accessed successfully.

Related topics

  • The default service domain for Knative is example.com. Knative on ASM allows you to use a custom domain as the default. For more information, see Use a custom domain name in Knative on ASM.

  • Because ASM gateways support HTTPS and dynamic certificate loading, you can use them to enable HTTPS access for your Knative Service. For more information, see Access a Knative Service over HTTPS by using an ASM gateway.

  • Knative on ASM provides traffic-based canary release capabilities. When you create a Knative Service, Knative automatically creates the first Revision for the service. Subsequently, whenever the service's configuration changes, Knative creates a new Revision. You can implement a canary release by modifying the percentage of traffic distributed to different Revisions. For more information, see Perform a traffic-based canary release for a service in Knative on ASM.

  • Knative Serving injects a queue-proxy container into each pod. This container reports the concurrency metrics of the application container to the autoscaler. The autoscaler then adjusts the number of pods in the Deployment based on the number of concurrent requests and the scaling algorithm to achieve automatic scaling. For more information, see Autoscale a service based on the number of concurrent requests.