Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Service routing Management in Knative Serving

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Many novices are not very clear about how to understand the service routing management in Knative Serving. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Guide: the following mainly focuses on the Knative Service domain name and introduces the routing management of Knative Service. First, I introduced how to modify the default primary domain name, and then I went further to explain how to add a custom domain name and how to associate to a different Knative Service based on path.

Knative generates a domain name for each Service by default, and Istio Gateway determines which Knative Service the current request should be forwarded to based on the domain name. The primary domain name used by Knative by default is example.com, which cannot be used as an online service.

Default domain name example.com of Knative Serving

First of all, you need to deploy a Knative Service, which can be found in the Knative experience: Serving Hello World.

If you already have a Knative cluster, save the following directly to the helloworld.yaml file. Then execute kubectl apply-f helloworld.yaml to deploy the hello service to helloworld namespace.

-apiVersion: v1kind: Namespacemetadata: name: helloworld---apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: hello namespace: helloworldspec: template: metadata: labels: app: hello annotations: autoscaling.knative.dev/target: "10" spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/simple-app:132e07c14c49 env: -name: TARGET value: "World!"

Next, take a look at the domain name configuration automatically generated by Knative Service:

└─ # kubectl-n helloworld get ksvcNAME URL LATESTCREATED LATESTREADY READY REASONhello http://hello.helloworld.example.com hello-wsnvc hello-wsnvc True

Now you can access the service by specifying Host using curl.

First get the Istio Gateway IP

└─ # kubectl get svc istio-ingressgateway-- namespace istio-system-- output jsonpath= "{.status.loadBalancer.ingress [*] ['ip']}" 47.95.191.136

Then access the hello service.

└─ # curl-H "Host: hello.helloworld.example.com" http://47.95.191.136/Hello Worldlings!

If you want to access the hello service in the browser, you need to do host binding first, point the domain name hello.helloworld.example.com to 47.95.191.136. This way can not provide services to the outside world.

Configure a custom primary domain name

Here's how to change the default example.com to our own domain name. Suppose our own domain name is serverless.kuberun.com. Now execute kubectl edit cm config-domain-namespace knative-serving, as shown in the following figure, add serverless.kuberun.com to ConfigMap, and then save and exit to complete the configuration of the custom primary domain name.

Let's take a look at the domain name of Knative Service, which is already in effect as shown below.

└─ # kubectl-n helloworld get ksvcNAME URL LATESTCREATED LATESTREADY READY REASONhello http://hello.helloworld.serverless.kuberun.com hello-wsnvc hello-wsnvc True pan-domain name resolution

The default rule for Knative Service to generate a domain name is servicename.namespace.use-domain. So different namespace will generate different subdomain names, and each Knative Service will generate a unique subdomain name. In order to ensure that all Service services can be accessed on the public network, you need to do a pan-domain name resolution. Parse * .serverless.kuberun.com to Istio Gateway 47.95.191.136. If you purchased a domain name on Aliyun (Wanwang), you can configure domain name resolution in the following ways:

Now you can see the helloworld service directly by accessing http://hello.helloworld.serverless.kuberun.com/ directly through the browser:

Custom service domain name

Just now we assigned a primary domain name to Knative so that Service can generate its own unique domain name based on the primary domain name. However, the automatically generated domain names are not very friendly. For example, the domain name hello.helloworld.serverless.kuberun.com of helloworld just deployed has no obvious meaning for ordinary users and is difficult to remember.

It would be perfect if you could access the hello world service through hello.kuberun.com. Here's how to do it:

First modify the domain name resolution on Wanwang to point the A record of hello.kuberun.com to Istio Gateway 47.95.191.136

After hello.kuberun.com parses to Istio Gateway, Istio Gateway does not know which service to forward to at this time, so you also need to configure VirtualService to tell Istio how to forward it.

Save the following to the hello-ingress-route.yaml file:

ApiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: hello-ingress-route namespace: knative-servingspec: gateways:-knative-ingress-gateway hosts:-hello.helloworld.serverless.kuberun.com-hello.kuberun.com http:-match:-uri: prefix: "/" rewrite: authority: hello.helloworld.svc.cluster.local retries: attempts: 3 perTryTimeout: 10m0s route:-destination: host: istio-ingressgateway.istio -system.svc.cluster.local port: number: 80 weight: 100 timeout: 10m0s websocketUpgrade: true

Now open http://hello.kuberun.com/ and you can see the helloworld service:

Path-based service forwarding

The scenario of a real online service may be that the back end of a path corresponds to an application. Now let's extend the hello.kuberun.com just now. Let the path at the beginning of / blog be mapped to blog service, and the rest of the path will be typed on hello service as is.

Save the following to the blog.yaml file, and then execute: kubectl apply-f blog.yaml to complete the deployment of the blog service.

-apiVersion: v1kind: Namespacemetadata: name: blog---apiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: hello-blog namespace: blogspec: template: metadata: labels: app: hello annotations: autoscaling.knative.dev/target: "10" spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/simple-app:132e07c14c49 env: -name: TARGET value: "Blog!"

View the default domain name of the blog service:

└─ # kubectl-n blog get ksvcNAME URL LATESTCREATED LATESTREADY READY REASONhello http://hello-blog.blog.serverless.kuberun.com hello-zbm7q hello-zbm7q True

Now you can access the service you just deployed by opening http://hello-blog.blog.serverless.kuberun.com using a browser:

This is the default domain name, our requirement is to access through http://hello.kuberun.com/blog, so we also need to modify the configuration of Istio VirtualService. Add the configuration of / blog in hello-ingress-route.yaml as follows:

ApiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: hello-ingress-route namespace: knative-servingspec: gateways:-knative-ingress-gateway hosts:-hello.helloworld.serverless.kuberun.com-hello.kuberun.com http:-match:-uri: prefix: "/ blog" rewrite: authority: hello-blog.blog.svc.cluster.local retries: attempts: 3 perTryTimeout: 10m0s route:-destination: Host: istio-ingressgateway.istio-system.svc.cluster.local port: number: 80 weight: 100-match:-uri: prefix: "/" rewrite: authority: hello.helloworld.svc.cluster.local retries: attempts: 3 perTryTimeout: 10m0s route:-destination: host: istio-ingressgateway.istio-system.svc.cluster.local port: Number: 80 weight: 100 timeout: 10m0s websocketUpgrade: true

You can now open http://hello.kuberun.com/blog in your browser, as shown below:

From the above introduction, you should know the following:

The default primary domain name of Knative Service is example.com, and all independent domain names generated by Knative Service are subdomains of this primary domain name.

Domain name specification generated by Knative Service

How to configure Knative Service to use a custom primary domain name, and how to configure public network domain name resolution

How to realize the personalized Ingress configuration of Knative Service based on Istio VirtualService and provide production-level service routing.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report