Setting up a custom domain

By default, Knative Serving routes use example.com as the default domain. The fully qualified domain name for a route by default is {route}.{namespace}.{default-domain}.

To change the {default-domain} value there are a few steps involved:

Edit using kubectl

  1. Edit the domain configuration config-map to replace example.com with your own domain, for example mydomain.com:

    1. kubectl edit configmap config-domain -n knative-serving

    This command opens your default text editor and allows you to edit the ConfigMap.

    1. apiVersion: v1
    2. data:
    3. _example: |
    4. ################################
    5. # #
    6. # EXAMPLE CONFIGURATION #
    7. # #
    8. ################################
    9. # ...
    10. example.com: |
    11. kind: ConfigMap
  2. Edit the file to replace example.com with the domain you want to use, then remove the _example key and save your changes. In this example, mydomain.com is configured as the domain for all routes:

    1. apiVersion: v1
    2. data:
    3. mydomain.com: ""
    4. kind: ConfigMap
    5. [...]

Apply from a file

You can also apply an updated domain configuration:

  1. Replace the example.org and example.com values with the new domain you want to use and run the command:

    1. kubectl apply -f - <<EOF
    2. apiVersion: v1
    3. kind: ConfigMap
    4. metadata:
    5. name: config-domain
    6. namespace: knative-serving
    7. data:
    8. # These are example settings of domain.
    9. # example.org will be used for routes having app=prod.
    10. example.org: |
    11. selector:
    12. app: prod
    13. # Default value for domain, for routes that does not have app=prod labels.
    14. # Although it will match all routes, it is the least-specific rule so it
    15. # will only be used if no other domain matches.
    16. example.com: ""
    17. EOF

Deploy an application

If you have an existing deployment, Knative reconciles the change made to the ConfigMap, and automatically updates the host name for all of the deployed Services and Routes.

Deploy an app (for example, helloworld-go), to your cluster as normal. You can retrieve the URL in Knative Route “helloworld-go” with the following command:

  1. kubectl get route helloworld-go --output jsonpath="{.status.url}"

You should see the full customized domain: helloworld-go.default.mydomain.com.

And you can check the IP address of your Knative gateway by running:

  1. export INGRESSGATEWAY=istio-ingressgateway
  2. if kubectl get configmap config-istio -n knative-serving &> /dev/null; then
  3. export INGRESSGATEWAY=istio-ingressgateway
  4. fi
  5. kubectl get svc $INGRESSGATEWAY --namespace istio-system --output jsonpath="{.status.loadBalancer.ingress[*]['ip']}"

Local DNS setup

You can map the domain to the IP address of your Knative gateway in your local machine with:

  1. INGRESSGATEWAY=istio-ingressgateway
  2. export GATEWAY_IP=`kubectl get svc $INGRESSGATEWAY --namespace istio-system --output jsonpath="{.status.loadBalancer.ingress[*]['ip']}"`
  3. # helloworld-go is the generated Knative Route of "helloworld-go" sample.
  4. # You need to replace it with your own Route in your project.
  5. export DOMAIN_NAME=`kubectl get route helloworld-go --output jsonpath="{.status.url}" | cut -d'/' -f 3`
  6. # Add the record of Gateway IP and domain name into file "/etc/hosts"
  7. echo -e "$GATEWAY_IP\t$DOMAIN_NAME" | sudo tee -a /etc/hosts

You can now access your domain from the browser in your machine and do some quick checks.

Publish your Domain

Follow these steps to make your domain publicly accessible:

Set static IP for Knative Gateway

You might want to set a static IP for your Knative gateway, so that the gateway IP does not change each time your cluster is restarted.

Update your DNS records

To publish your domain, you need to update your DNS provider to point to the IP address for your service ingress.

  • Create a wildcard record for the namespace and custom domain to the ingress IP Address, which would enable hostnames for multiple services in the same namespace to work without creating additional DNS entries.
  1. *.default.mydomain.com 59 IN A 35.237.28.44
  • Create an A record to point from the fully qualified domain name to the IP address of your Knative gateway. This step needs to be done for each Knative Service or Route created.
  1. helloworld-go.default.mydomain.com 59 IN A 35.237.28.44

If you are using Google Cloud DNS, you can find step-by-step instructions in the Cloud DNS quickstart.

Once the domain update has propagated, you can access your app using the fully qualified domain name of the deployed route, for example http://helloworld-go.default.mydomain.com