By default, Knative Service domain names use the format '{route}.{namespace}.{default-example.com}', where '{default-example.com}' is the default suffix, typically example.com. This topic shows you how to use a custom domain name to enable more flexible routing. ACS Knative lets you set a global custom domain for all Knative Services by using a ConfigMap, or map a unique domain to a single Knative Service by using a DomainMapping.
Prerequisites
-
You have deployed Knative in the ACS cluster.
-
You have registered a domain name. For more information, see Alibaba Cloud Domains.
Configure a global custom domain
To use the same domain suffix for all Knative Services in ACS Knative, follow these steps.
Run the following command to edit the
config-domainConfigMap in theknative-servingnamespace:kubectl edit cm config-domain --namespace knative-servingModify the configuration file.
Replace the default domain
example.comwith your custom domain name and save the file. This example usesmydomain.comas the custom domain name.apiVersion: v1 kind: ConfigMap metadata: name: config-domain namespace: knative-serving data: mydomain.com: "" # This example uses mydomain.com. In production, replace this with your own domain name.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.comThe output indicates that the custom domain has taken effect.
Add a DNS record that maps your custom domain to the IP address of the Knative gateway.
Run the following command to access the Knative Service by using the custom domain name:
curl http://helloworld-go.default.mydomain.comExpected output:
Hello Knative!This output indicates that the custom domain is configured correctly and the Knative Service is responding to requests.
Configure a custom domain for a single service
To set a specific domain name for a Knative Service, use one of the following methods.
Console
-
Log on to the ACS console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose .
-
On the Knative page, click the Services tab, and then find and click the name of your service.
-
On the service details page, click Access Control in the upper-right corner and configure a custom domain name for the service as prompted.
Kubectl
Use a DomainMapping to assign a specific domain to a service and enable flexible routing with an Ingress controller.
-
Run the following command to create a file named
helloworld.knative.top.yaml:vi helloworld.knative.top.yaml -
Add the following YAML content to the file, and then save and close the editor:
apiVersion: serving.knative.dev/v1beta1 kind: DomainMapping metadata: name: helloworld.knative.top.mydomain.com # Set the domain name. In production, replace this with your domain name. namespace: default # Set the namespace. This must be the same namespace as your service. spec: ref: name: helloworld-go # The name of the target service. kind: Service apiVersion: serving.knative.dev/v1 -
Run the following command to apply the
helloworld.knative.top.yamlfile:kubectl apply -f helloworld.knative.top.yamlExpected output:
domainmapping.serving.knative.dev/helloworld.knative.top created -
Run 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 has taken effect.
-
Add a DNS record that maps your custom domain to the IP address of the Knative gateway. For more information, see Add a DNS record.
-
Run the following command to access the Knative Service by using the custom domain name:
curl http://helloworld.knative.top.mydomain.comThis output indicates that the custom domain is configured correctly and the Knative Service is responding to requests.
Next steps
-
To serve traffic over HTTPS, configure a TLS certificate for your custom domain name. For more information, see Configure a certificate to access Knative Services over HTTPS.
-
You can deploy a gRPC service in Knative to improve network efficiency. For more information, see Deploy a gRPC service in Knative.