All Products
Search
Document Center

Container Service for Kubernetes:Specify a custom domain name for a Knative Service

Last Updated:Sep 07, 2023

The default domain name of a Knative Service is in the {route}.{namespace}.{default-example.com} format. {default-example.com} is the default suffix. In most cases, the suffix is example.com. You can change the default domain name based on your business requirement. This topic describes how to specify a global domain name for all Knative Services and how to specify a custom domain name for each Knative Service.

Prerequisites

Step 1: Create a Knative Service

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

  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 of the Knative page, set Namespace to default, click Create from Template, select a predefined sample template to create a Knative Service, and then click Create.

    In this topic, a Knative Service named helloworld-go is created.

    image.png

Step 2: Specify a custom domain name

  • Specify a global domain name: You can specify a global domain name if you do not want to specify a domain name for each Knative Service. After you complete the configuration, all Knative Services can use the specified global domain name.

  • Use DomainMappings to specify a domain name for each Knative Service: You can choose this method if you want to use the Ingress controller to customize routing configurations for different Knative Services.

Method 1: Specify a global domain name

  1. Run the following command to modify the config-domain ConfigMap in the knative-serving namespace:

    kubectl edit cm config-domain --namespace knative-serving
  2. Modify the ConfigMap.

    Change the default domain name example.com in the ConfigMap to your custom domain name and save the ConfigMap. In this example, a custom domain name mydomain is used.

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: config-domain
      namespace: knative-serving
    data:
      mydomain.com: "" # Change example.com to mydomain.com. Replace the default domain name with the actual domain name that you want to use.
  3. Run the following command to verify the domain name:

    # Replace helloworld-go in the command with the name of your Knative Service. 
    kubectl get route helloworld-go --output jsonpath="{.status.url}" | awk -F/ '{print $3}'

    Expected output:

    helloworld-go.default.mydomain.com

    The output indicates that the custom domain name is in effect.

  4. Add the IP address of the Knative gateway to the resolution settings of the custom domain name in the Alibaba Cloud DNS console.

  5. Run the following command to access the Knative Service through the custom domain name:

    curl http://helloworld-go.default.mydomain.com

    Expected output:

    Hello Knative!

    The output indicates that the Knative Service successfully responds to the request.

Method 2: Use DomainMappings to specify a custom domain name for each Knative Service

  1. Run the following command to create a file named helloworld.knative.top.yaml:

    vi helloworld.knative.top.yaml
  2. Open the vi editor, add the following YAML content, save the content, and then exit.

    apiVersion: serving.knative.dev/v1alpha1
    kind: DomainMapping
    metadata:
      name: helloworld.knative.top.mydomain.com # The domain name of the Knative Service. Replace it with the domain name that you want to use. 
      namespace: default # The namespace to which the Knative Service belongs. 
    spec:
      ref:
        name: helloworld-go # The name of the Knative Service.
        kind: Service
        apiVersion: serving.knative.dev/v1
  3. Run the following command to deploy resources defined in the helloworld.knative.top.yaml file to your cluster:

    kubectl apply -f helloworld.knative.top.yaml

    Expected output:

    domainmapping.serving.knative.dev/helloworld.knative.top created
  4. Run the following command to verify the DomainMapping:

    kubectl get domainmapping helloworld.knative.top

    Expected output:

    NAME                          URL                                      READY   REASON
    helloworld.knative.top       http://helloworld.knative.top            True

    The output indicates that the custom domain name is in effect.

  5. Add the IP address of the Knative gateway to the resolution settings of the custom domain name in the Alibaba Cloud DNS console.

  6. Run the following command to access the Knative Service through the custom domain name:

    curl http://helloworld.knative.top.mydomain.com

    Expected output:

    Hello Knative!

    The output indicates that the Knative Service successfully responds to the request.