共計 5111 個字符,預(yù)計需要花費 13 分鐘才能閱讀完成。
Knative Serving 中的服務(wù)路由管理該如何理解,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面丸趣 TV 小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
導(dǎo)讀:下面主要圍繞 Knative Service 域名展開,介紹了 Knative Service 的路由管理。首先介紹了如何修改默認(rèn)主域名,緊接著深入一層介紹了如何添加自定義域名以及如何根據(jù) path 關(guān)聯(lián)到不同的 Knative Service。
Knative 默認(rèn)會為每一個 Service 生成一個域名,并且 Istio Gateway 要根據(jù)域名判斷當(dāng)前的請求應(yīng)該轉(zhuǎn)發(fā)給哪個 Knative Service。Knative 默認(rèn)使用的主域名是 example.com,這個域名是不能作為線上服務(wù)的。
Knative Serving 的默認(rèn)域名 example.com
首先需要部署一個 Knative Service,可以參考 Knative 初體驗:Serving Hello World。
如果你已經(jīng)有了一個 Knative 集群,那么直接把下面的內(nèi)容保存到 helloworld.yaml 文件中。然后執(zhí)行一下 kubectl apply -f helloworld.yaml 即可把 hello 服務(wù)部署到 helloworld namespace 中。
---
apiVersion: v1
kind: Namespace
metadata:
name: helloworld
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: hello
namespace: helloworld
spec:
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!
接下來看一下 Knative Service 自動生成的域名配置:
└─# kubectl -n helloworld get ksvc
NAME URL LATESTCREATED LATESTREADY READY REASON
hello http://hello.helloworld.example.com hello-wsnvc hello-wsnvc True
現(xiàn)在使用 curl 指定 Host 就能訪問服務(wù)了。
首先獲取到 Istio Gateway IP;
└─# kubectl get svc istio-ingressgateway --namespace istio-system --output jsonpath= {.status.loadBalancer.ingress[*][ip]}
47.95.191.136
然后訪問 hello 服務(wù)。
└─# curl -H Host: hello.helloworld.example.com http://47.95.191.136/
Hello World!!
如果想要在瀏覽器中訪問 hello 服務(wù)需要先做 host 綁定,把域名 hello.helloworld.example.com 指向 47.95.191.136 才行。這種方式還不能對外提供服務(wù)。
配置自定義主域名
下面介紹一下如何把默認(rèn)的 example.com 改成我們自己的域名,假設(shè)我們自己的域名是:serverless.kuberun.com,現(xiàn)在執(zhí)行 kubectl edit cm config-domain –namespace knative-serving ,如下圖所示,添加 serverless.kuberun.com 到 ConfigMap 中,然后保存退出就完成了自定義主域名的配置。
再來看一下 Knative Service 的域名, 如下所示已經(jīng)生效了。
└─# 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 默認(rèn)生成域名的規(guī)則是 servicename.namespace.use-domain。所以不同的 namespace 會生成不同的子域名,每一個 Knative Service 也會生成一個唯一的子域名。為了保證所有的 Service 服務(wù)都能在公網(wǎng)上面訪問到,需要做一個泛域名解析。把 *.serverless.kuberun.com 解析到 Istio Gateway 47.95.191.136 上面去。如果你是在阿里云 (萬網(wǎng)) 上面購買的域名,你可以通過如下方式配置域名解析:
現(xiàn)在直接通過瀏覽器訪問 http://hello.helloworld.serverless.kuberun.com/ 就可以直接看到 helloworld 服務(wù)了:
自定義服務(wù)域名
剛才我們給 Knative 指定了一個主域名,使得 Service 基于主域名生成自己的唯一域名。但自動生成的域名不是很友好,比如剛才部署的 helloworld 的域名 hello.helloworld.serverless.kuberun.com 對于普通用戶來說意義不明顯、不好記憶。
如果能通過 hello.kuberun.com 訪問 hello world 服務(wù)那就完美了,接下來將會介紹實現(xiàn)方法:
先在萬網(wǎng)上面修改域名解析,把 hello.kuberun.com 的 A 記錄指向 Istio Gateway 47.95.191.136;
hello.kuberun.com 解析到 Istio Gateway 以后 Istio Gateway 并不知道此時應(yīng)該轉(zhuǎn)發(fā)到哪個服務(wù),所以還需要配置 VirtualService 告知 Istio 如何轉(zhuǎn)發(fā)。
把下面的內(nèi)容保存到 hello-ingress-route.yaml 文件:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: hello-ingress-route
namespace: knative-serving
spec:
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
現(xiàn)在打開 http://hello.kuberun.com/ 就能看到 helloworld 服務(wù)了:
基于路徑的服務(wù)轉(zhuǎn)發(fā)
真實線上服務(wù)的場景可能是一個路徑后端對應(yīng)著一個應(yīng)用,現(xiàn)在我們對剛才的 hello.kuberun.com 進行一下擴展。讓 /blog 開頭的路徑映射到 blog service,其他的路徑還是原樣打到 hello service 上面。
把下面的內(nèi)容保存到 blog.yaml 文件,然后執(zhí)行:kubectl apply -f blog.yaml 即可完成 blog 服務(wù)的部署。
---
apiVersion: v1
kind: Namespace
metadata:
name: blog
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: hello-blog
namespace: blog
spec:
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!
查看 blog 服務(wù)的默認(rèn)域名:
└─# kubectl -n blog get ksvc
NAME URL LATESTCREATED LATESTREADY READY REASON
hello http://hello-blog.blog.serverless.kuberun.com hello-zbm7q hello-zbm7q True
現(xiàn)在使用瀏覽器打開 http://hello-blog.blog.serverless.kuberun.com 就可以訪問剛剛部署的服務(wù)了:
這是默認(rèn)域名,我們的需求是想要通過 http://hello.kuberun.com/blog 訪問, 所以還需要修改 Istio VirtualService 的配置。如下所示在 hello-ingress-route.yaml 增加 /blog 的配置:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: hello-ingress-route
namespace: knative-serving
spec:
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
現(xiàn)在就能在瀏覽器中打開 http://hello.kuberun.com/blog ,如下所示:
通過上文的介紹,您應(yīng)該了解到如下內(nèi)容:
Knative Service 默認(rèn)的主域名是 example.com, 所有 Knative Service 生成的獨立域名都是這個主域名的子域名;
Knative Service 生成的域名規(guī)范;
如何配置 Knative Service 使用自定義的主域名,以及如何配置公網(wǎng)域名解析;
如何基于 Istio VirtualService 實現(xiàn) Knative Service 的個性化 Ingress 配置,提供生產(chǎn)級別的服務(wù)路由。
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注丸趣 TV 行業(yè)資訊頻道,感謝您對丸趣 TV 的支持。