Knative 基本功能深入剖析:Knative Serving 之服务路由管理
導讀:本文主要圍繞 Knative Service 域名展開,介紹了 Knative Service 的路由管理。文章首先介紹了如何修改默認主域名,緊接著深入一層介紹了如何添加自定義域名以及如何根據 path 關聯到不同的 Knative Service 。期望通過本文的介紹,能夠幫助您了解更多相關內容。
Knative 默認會為每一個 Service 生成一個域名,并且 Istio Gateway 要根據域名判斷當前的請求應該轉發給哪個 Knative Service。Knative 默認使用的主域名是 example.com,這個域名是不能作為線上服務的。
Knative Serving 的默認域名 example.com
首先需要部署一個 Knative Service,可以參考 Knative 初體驗:Serving Hello World。
如果你已經有了一個 Knative 集群,那么直接把下面的內容保存到 helloworld.yaml 文件中。然后執行一下 kubectl apply -f helloworld.yaml 即可把 hello 服務部署到 helloworld namespace 中。
--- apiVersion: v1 kind: Namespace metadata:name: helloworld--- apiVersion: serving.knative.dev/v1alpha1 kind: Service metadata:name: hellonamespace: helloworld spec:template:metadata:labels:app: helloannotations:autoscaling.knative.dev/target: "10"spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/simple-app:132e07c14c49env:- name: TARGETvalue: "World!"接下來看一下 Knative Service 自動生成的域名配置:
└─# kubectl -n helloworld get ksvc NAME URL LATESTCREATED LATESTREADY READY REASON hello http://hello.helloworld.example.com hello-wsnvc hello-wsnvc True現在使用 curl 指定 Host 就能訪問服務了。
- 首先獲取到 Istio Gateway IP;
- 然后訪問 hello 服務。
如果想要在瀏覽器中訪問 hello 服務需要先做 host 綁定,把域名 hello.helloworld.example.com 指向 47.95.191.136 才行。這種方式還不能對外提供服務。
配置自定義主域名
下面介紹一下如何把默認的 example.com 改成我們自己的域名,假設我們自己的域名是:serverless.kuberun.com,現在執行 kubectl edit cm config-domain --namespace knative-serving ,如下圖所示,添加 serverless.kuberun.com 到 ConfigMap 中,然后保存退出就完成了自定義主域名的配置。
再來看一下 Knative Service 的域名, 如下所示已經生效了。
└─# kubectl -n helloworld get ksvc NAME URL LATESTCREATED LATESTREADY READY REASON hello http://hello.helloworld.serverless.kuberun.com hello-wsnvc hello-wsnvc True泛域名解析
Knative Service 默認生成域名的規則是 servicename.namespace.use-domain 。所以不同的 namespace 會生成不同的子域名,每一個 Knative Service 也會生成一個唯一的子域名。為了保證所有的 Service 服務都能在公網上面訪問到,需要做一個泛域名解析。把 *.serverless.kuberun.com 解析到 Istio Gateway 47.95.191.136 上面去。如果你是在阿里云(萬網)上面購買的域名,你可以通過如下方式配置域名解析:
現在直接通過瀏覽器訪問 http://hello.helloworld.serverless.kuberun.com/ 就可以直接看到 helloworld 服務了:
自定義服務域名
剛才我們給 Knative 指定了一個主域名,使得 Service 基于主域名生成自己的唯一域名。但自動生成的域名不是很友好,比如剛才部署的 helloworld 的域名 hello.helloworld.serverless.kuberun.com 對于普通用戶來說意義不明顯、不好記憶。
如果能通過 hello.kuberun.com 訪問 hello world 服務那就完美了,接下來將會介紹實現方法:
- 先在萬網上面修改域名解析,把 hello.kuberun.com 的 A 記錄指向 Istio Gateway 47.95.191.136;
- hello.kuberun.com 解析到 Istio Gateway 以后 Istio Gateway 并不知道此時應該轉發到哪個服務,所以還需要配置 VirtualService 告知 Istio 如何轉發。
把下面的內容保存到 hello-ingress-route.yaml 文件:
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata:name: hello-ingress-routenamespace: knative-serving spec:gateways:- knative-ingress-gatewayhosts:- hello.helloworld.serverless.kuberun.com- hello.kuberun.comhttp:- match:- uri:prefix: "/"rewrite:authority: hello.helloworld.svc.cluster.localretries:attempts: 3perTryTimeout: 10m0sroute:- destination:host: istio-ingressgateway.istio-system.svc.cluster.localport:number: 80weight: 100timeout: 10m0swebsocketUpgrade: true現在打開 http://hello.kuberun.com/ 就能看到 helloworld 服務了:
基于路徑的服務轉發
真實線上服務的場景可能是一個路徑后端對應著一個應用,現在我們對剛才的 hello.kuberun.com 進行一下擴展。讓 /blog 開頭的路徑映射到 blog service,其他的路徑還是原樣打到 hello service 上面。
把下面的內容保存到 blog.yaml 文件,然后執行: kubectl apply -f blog.yaml 即可完成 blog 服務的部署。
--- apiVersion: v1 kind: Namespace metadata:name: blog--- apiVersion: serving.knative.dev/v1alpha1 kind: Service metadata:name: hello-blognamespace: blog spec:template:metadata:labels:app: helloannotations:autoscaling.knative.dev/target: "10"spec:containers:- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/simple-app:132e07c14c49env:- name: TARGETvalue: "Blog!"查看 blog 服務的默認域名:
└─# kubectl -n blog get ksvc NAME URL LATESTCREATED LATESTREADY READY REASON hello http://hello-blog.blog.serverless.kuberun.com hello-zbm7q hello-zbm7q True現在使用瀏覽器打開 http://hello-blog.blog.serverless.kuberun.com 就可以訪問剛剛部署的服務了:
這是默認域名,我們的需求是想要通過 http://hello.kuberun.com/blog 訪問, 所以還需要修改 Istio VirtualService 的配置。如下所示在 hello-ingress-route.yaml 增加 /blog 的配置:
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata:name: hello-ingress-routenamespace: knative-serving spec:gateways:- knative-ingress-gatewayhosts:- hello.helloworld.serverless.kuberun.com- hello.kuberun.comhttp:- match:- uri:prefix: "/blog"rewrite:authority: hello-blog.blog.svc.cluster.localretries:attempts: 3perTryTimeout: 10m0sroute:- destination:host: istio-ingressgateway.istio-system.svc.cluster.localport:number: 80weight: 100- match:- uri:prefix: "/"rewrite:authority: hello.helloworld.svc.cluster.localretries:attempts: 3perTryTimeout: 10m0sroute:- destination:host: istio-ingressgateway.istio-system.svc.cluster.localport:number: 80weight: 100timeout: 10m0swebsocketUpgrade: true現在就能在瀏覽器中打開 http://hello.kuberun.com/blog ,如下所示:
小結
本文主要圍繞 Knative Service 域名展開,介紹了 Knative Service 的路由管理。通過本文的介紹,您應該了解到如下內容:
- Knative Service 默認的主域名是 example.com, 所有 Knative Service 生成的獨立域名都是這個主域名的子域名;
- Knative Service 生成的域名規范;
- 如何配置 Knative Service 使用自定義的主域名,以及如何配置公網域名解析;
- 如何基于 Istio VirtualService 實現 Knative Service 的個性化 Ingress 配置,提供生產級別的服務路由。
歡迎加入 Knative 交流群
總結
以上是生活随笔為你收集整理的Knative 基本功能深入剖析:Knative Serving 之服务路由管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 正式开放 | 阿里云 10 亿级镜像服务
- 下一篇: 初探云原生应用管理之:聊聊 Tekton