Use a custom domain in Knative

更新时间:
复制 MD 格式

The default domain name format for a Knative Service is {route}.{namespace}.{default-example.com}, where {default-example.com} is the default domain suffix, which is typically example.com. ACS support custom domains for more flexible route configuration.

You can customize domains in two ways:

  • Customize the domain globally for all Knative Services by modifying the ConfigMap.

  • You can use the knative.aliyun.com/serving-ingress annotation to quickly configure a custom domain for a specific Knative Service.

Prerequisites

Configure a global custom domain

To apply a common domain suffix to all Knative Services, follow these steps.

  1. Run the following command to edit the config-domain ConfigMap object in the knative-serving namespace.

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

    Change the default domain name example.com in the configuration file to a custom domain and save the configuration. In this example, the custom domain is set to mydomain.

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: config-domain
      namespace: knative-serving
    data:
      mydomain.com: "" # Replace example.com with mydomain.com. In a real-world scenario, replace this with your own service domain.
  3. Run the following command to verify that the new domain is applied:

    # Replace helloworld-go 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

    This output confirms the custom domain is applied.

  4. Update the DNS records for your domain to point to the IP address of the Knative gateway. For more information, seeAdd a DNS record.

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

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

    Expected output:

    Hello Knative!

    This output confirms that the Knative Service is accessible via the custom domain.

Configure a per-service domain and path

To define a specific domain name and path for a Knative Service, you can use the knative.aliyun.com/serving-ingress  annotation to directly specify a custom domain name and path. For example, knative.aliyun.com/serving-ingress: cafe.mydomain.com/coffee specifies the cafe.mydomain.com domain name and the /coffee path.

Custom domain example

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: coffee-mydomain
  annotations:
    knative.aliyun.com/serving-ingress: cafe.mydomain.com
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8

Wildcard domain example

Separate multiple domains with a comma.
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: coffee-mydomain
  annotations:
    knative.aliyun.com/serving-ingress: "*.mydomain.com"
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8

Single path example

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: coffee-mydomain
  annotations:
    knative.aliyun.com/serving-ingress: cafe.mydomain.com/coffee
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8

Multiple paths example

Separate multiple paths with a comma.
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: coffee-mydomain
  annotations:
    knative.aliyun.com/serving-ingress: cafe.mydomain.com/coffee,cafe.mydomain.com/tea
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8

Rewrite a path

The ALB gateway supports path rewriting. You can rewrite the Path for a Knative Service by configuring the alb.ingress.kubernetes.io/rewrite-target:  annotation. The following is a code example.

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: coffee-mydomain
  annotations:
    knative.aliyun.com/serving-ingress: cafe.mydomain.com/api/coffee
    alb.ingress.kubernetes.io/rewrite-target: /coffee
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8

Configure a universal wildcard domain

To define a wildcard domain for a Knative Service, set the value of the Annotationknative.aliyun.com/serving-ingress  to /. For example, knative.aliyun.com/serving-ingress: / indicates a full wildcard domain. The following is an example.

Only ALB gateways are supported.

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: coffee-mydomain
  annotations:
    knative.aliyun.com/serving-ingress: "/"
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:160e4dc8