The default domain name for a Knative service is in the format {route}.{namespace}.{default-example.com}. The {default-example.com} part is the default suffix, which is typically `example.com`. To use a custom domain name for more flexible routing, you can follow the steps in this topic. ACK Knative lets you change the global custom domain name for all Knative services using a ConfigMap. You can also use a DomainMapping to set a custom domain name for a single Knative service.
Prerequisites
Knative is deployed in your cluster. For more information, see Deploy and manage Knative.
You have registered a domain name. For more information, see Alibaba Cloud DNS.
You have obtained the kubeconfig file of your cluster and used kubectl to connect to the cluster.
Configure a global custom domain name
To use the same domain name suffix for all Knative services in ACK Knative without setting a domain name for each service individually, you can follow these steps.
Run the following command to edit the ConfigMap object named
config-domainin theknative-servingnamespace.kubectl edit cm config-domain --namespace knative-servingModify the configuration file.
Change the default domain name from
example.comto your custom domain name and save the file. This example sets the custom domain name tomydomain.apiVersion: v1 kind: ConfigMap metadata: name: config-domain namespace: knative-serving data: mydomain.com: "" # Replace example.com with mydomain.com. Use your actual service domain name.Run the following command to check whether the domain name has taken effect.
# 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.comThe output indicates that the custom domain name has taken effect.
Add a DNS record to map the custom domain name to the IP address of the Knative gateway.
Run the following command to access the Knative service using the custom domain name.
curl http://helloworld-go.default.mydomain.comExpected output:
Hello Knative!The output indicates that the custom domain name is configured and the Knative service has successfully responded to the request.
Configure a custom domain name for a single service
To define a specific domain name for a Knative service, you can follow these steps.
Console
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, find the cluster you want and click its name. In the left navigation pane, choose .
On the Knative page, click the Services tab and then click the name of the target service.
On the service details page, click Access Control in the upper-right corner to configure a custom domain name for the service.
kubectl
To define a specific domain name for a Service and use an Ingress controller for flexible routing, you can use a DomainMapping.
Run the following command to create the
helloworld.knative.top.yamlfile.vi helloworld.knative.top.yamlAdd the following YAML content to the file. Then, save and close the file.
apiVersion: serving.knative.dev/v1beta1 kind: DomainMapping metadata: name: helloworld.knative.top.mydomain.com # Set the service domain name. Use your actual service domain name. namespace: default # Set the namespace to the one where the service resides. spec: ref: name: helloworld-go # The name of the target service. kind: Service apiVersion: serving.knative.dev/v1Run the following command to apply the resources defined in
helloworld.knative.top.yamlto the Kubernetes cluster.kubectl apply -f helloworld.knative.top.yamlExpected output:
domainmapping.serving.knative.dev/helloworld.knative.top createdRun the following command to verify the DomainMapping.
kubectl get domainmapping helloworld.knative.topExpected output:
NAME URL READY REASON helloworld.knative.top http://helloworld.knative.top TrueThe output indicates that the custom domain name has taken effect.
Add a DNS record to map the custom domain name to the IP address of the Knative gateway.
Run the following command to access the Knative service using the custom domain name.
curl http://helloworld.knative.top.mydomain.comExpected output:
Hello Knative!The output indicates that the custom domain name is configured and the Knative service has successfully responded to the request.
Configure a custom path for a single service
To define a specific path for a Knative service, you can use the knative.aliyun.com/serving-ingress annotation to specify the 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.
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:160e4dc8Multiple path example:
Use commas (,) to separate multiple paths.
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:160e4dc8Rewrite the path
The Application Load Balancer (ALB) gateway supports path rewriting. To rewrite the path for a Knative service, you can configure the alb.ingress.kubernetes.io/rewrite-target: annotation. The following code shows an 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:160e4dc8Set a wildcard domain name
To define a wildcard domain name for a Knative service, you can set the value of the knative.aliyun.com/serving-ingress annotation to /. For example, knative.aliyun.com/serving-ingress: / specifies a wildcard domain name. The following code shows 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:160e4dc8References
To configure an HTTPS certificate for a custom domain name, see Configure an HTTPS certificate.
To deploy a gRPC service in Knative and improve network efficiency, see Deploy a gRPC service in Knative.