基于域名的方式訪問(wèn)Istio服務(wù)網(wǎng)格中的多個(gè)應(yīng)用程序的方法詳解
1.為什么要使用域名訪問(wèn)部署在Istio中的程序
我們?cè)贗stio中部署的程序一定不止有一個(gè),前面我們已經(jīng)在Istio中部署了Httpbin、Bookinfo、Nginx這三個(gè)應(yīng)用程序,但是我們使用節(jié)點(diǎn)IP加NodePort端口的方式永遠(yuǎn)只是請(qǐng)求到了一個(gè)應(yīng)用程序,就好比我們已經(jīng)實(shí)現(xiàn)了Nginx的基于端口的訪問(wèn)模式,不過(guò)每個(gè)應(yīng)用程序都是用的是80端口,才導(dǎo)致只訪問(wèn)到了一個(gè)應(yīng)用程序,在實(shí)際生產(chǎn)中,Istio中一定會(huì)部署很多個(gè)應(yīng)用程序,我們需要實(shí)現(xiàn)基于域名來(lái)訪問(wèn)不同的應(yīng)用程序。
應(yīng)用部署在Istio之后,將程序?qū)ν獍l(fā)布,會(huì)創(chuàng)建Gateway以及VirtualService資源,我們只需要在這兩個(gè)資源中聲明程序使用的域名,就可以接受來(lái)自LB的請(qǐng)求轉(zhuǎn)發(fā),LB的請(qǐng)求中會(huì)攜帶主機(jī)頭,從而轉(zhuǎn)發(fā)到對(duì)應(yīng)的應(yīng)用程序。
當(dāng)然也可以不額外占用服務(wù)器去搭建LB產(chǎn)品,我們可以在K8S集群中搭建一個(gè)Nginx服務(wù),由K8S中的Nginx服務(wù)接收80端口流量請(qǐng)求轉(zhuǎn)發(fā)至IngressGateway,為什么不使用Ingress呢,Ingress需要為每一個(gè)網(wǎng)站創(chuàng)建資源編排文件,如果域名很多的情況下,配置比較繁瑣。
如下圖所示:用戶請(qǐng)求bookinfo的項(xiàng)目,在瀏覽器中輸入bookinfo.jiangxl.com域名,由DNS解析到LB負(fù)載均衡器,LB負(fù)載均衡器會(huì)將請(qǐng)求轉(zhuǎn)發(fā)到IngressGateway中,IngressGateway根據(jù)請(qǐng)求頭中的域名,將請(qǐng)求轉(zhuǎn)發(fā)到對(duì)應(yīng)的Gateway中,然后在將請(qǐng)求轉(zhuǎn)發(fā)到應(yīng)用程序的Service資源,最后由應(yīng)用程序的Pod資源提供應(yīng)用程序的服務(wù)。

2.通過(guò)域名的方式訪問(wèn)Istio網(wǎng)格中的應(yīng)用程序
2.1.配置Gateway和VirtualService資源
配置每個(gè)應(yīng)用程序的Gateway以及VirtualService資源,為應(yīng)用程序綁定使用的域名,綁定后只有這個(gè)域名的流量請(qǐng)求才會(huì)被轉(zhuǎn)發(fā)到這個(gè)Gateway以及VirtualService資源上。
2.1.1.修改httpbin程序的Gateway和VirtualService資源
1)配置Gateway以及VirtualService資源綁定域名
[root@k8s-master istio-1.8.2]# vim samples/httpbin/httpbin-gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: httpbin-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "httpbin.jiangxl.com" #在hosts中綁定程序的域名
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- "httpbin.jiangxl.com" #同樣在hosts中管理程序的域名
gateways:
- httpbin-gateway
http:
- route:
- destination:
host: httpbin
port:
number: 80002)更新httpbin程序的GW和VS資源
[root@k8s-master istio-1.8.2]# kubectl apply -f samples/httpbin/httpbin-gateway.yaml gateway.networking.istio.io/httpbin-gateway created virtualservice.networking.istio.io/httpbin created
2.1.2.修改bookinfo程序的Gateway和VirtualService資源
1)配置Gateway以及VirtualService資源綁定域名
[root@k8s-master istio-1.8.2]# vim samples/bookinfo/networking/bookinfo-gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: bookinfo-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "bookinfo.jiangxl.com"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: bookinfo
spec:
hosts:
- "bookinfo.jiangxl.com"
gateways:
- bookinfo-gateway
http:
- match:
- uri:
exact: /productpage
- uri:
prefix: /static
- uri:
exact: /login
- uri:
exact: /logout
- uri:
prefix: /api/v1/products
route:
- destination:
host: productpage
port:
number: 9080
2)更新bookinfo程序的GW和VS資源
[root@k8s-master istio-1.8.2]# kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml -n bookinfo gateway.networking.istio.io/bookinfo-gateway configured virtualservice.networking.istio.io/bookinfo configured
2.1.3.修改nginx程序的Gateway和VirtualService資源
1)配置Gateway以及VirtualService資源綁定域名
[root@k8s-master istio-1.8.2]# vim samples/myproject/nginx-gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: nginx-gateway
namespace: istio-project
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "nginx.jiangxl.com"
[root@k8s-master istio-1.8.2]# vim samples/myproject/nginx-virtualservice.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: nginx-vs
namespace: istio-project
spec:
hosts:
- "nginx.jiangxl.com"
gateways:
- nginx-gateway
http:
- route:
- destination:
host: nginx-svc
subset: v1
weight: 100
mirror:
host: nginx-svc
subset: v2
mirror_percent: 100
2)創(chuàng)建nginx程序的GW和VS資源
[root@k8s-master istio-1.8.2]# kubectl apply -f samples/myproject/nginx-gateway.yaml gateway.networking.istio.io/nginx-gateway configured [root@k8s-master istio-1.8.2]# kubectl apply -f samples/myproject/nginx-virtualservice.yaml virtualservice.networking.istio.io/nginx-vs configured
2.2.配置LB代理Istio的IngressGateway
LB負(fù)載均衡我們采用Nginx來(lái)實(shí)現(xiàn),由Nginx去反向代理IngressGateway的NodePort端口來(lái)實(shí)現(xiàn)基于域名去訪問(wèn)Istio中的程序。
1.安裝Nginx
[root@lb~]# yum -y install nginx
2.配置Nginx反向代理Istio的IngressGateway
[root@lb~]# vim /etc/nginx/conf.d/istio-ingressgateway.conf
server {
listen 80;
server_name _;
location / {
proxy_http_version 1.1; #開啟http的1.1版本協(xié)議,istio是1.1版本,nginx默認(rèn)1.0版本
proxy_set_header Host $host; #代理轉(zhuǎn)發(fā)時(shí)攜帶請(qǐng)求的主機(jī)頭
proxy_pass http://192.168.20.10:31105; #代理到istio的IngressGateway
}
}
3.啟動(dòng)Nginx
[root@lb~]# systemctl restart nginx3.基于域名來(lái)訪問(wèn)Istio中的各個(gè)程序
測(cè)試之前先將域名解析寫入本地hosts文件。
192.168.20.13 httpbin.jiangxl.com bookinfo.jiangxl.com nginx.jiangxl.com
1)httpbin程序的訪問(wèn)

2)bookinfo程序的訪問(wèn)

3)nginx程序的訪問(wèn)

到此這篇關(guān)于基于域名的方式訪問(wèn)Istio服務(wù)網(wǎng)格中的多個(gè)應(yīng)用程序的文章就介紹到這了,更多相關(guān)Istio服務(wù)網(wǎng)格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
hexo博客開啟https的SSL?證書實(shí)現(xiàn)過(guò)程
這篇文章主要為大家介紹了hexo?博客開啟https的SSL證書實(shí)現(xiàn)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
openlayers?模仿高德箭頭導(dǎo)航路線圖的代碼詳解
這篇文章主要介紹了openlayers?模仿高德箭頭導(dǎo)航路線圖的示例代碼,主要包括原始數(shù)據(jù)、起點(diǎn)/終點(diǎn)尋找、起點(diǎn)和終點(diǎn)樣式函數(shù),結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
i++循環(huán)與i-–循環(huán)的執(zhí)行效率(遞增與遞減效率)
i++循環(huán)與i-–循環(huán)的執(zhí)行效率(遞增與遞減效率),需要的朋友可以參考下。2011-01-01
301重定向代碼合集(iis,asp,php,asp.net,apache)
腳本之家將SEO工作中所需要的301轉(zhuǎn)向代碼進(jìn)行了整理,收藏并分享,以備查閱。2011-02-02
關(guān)于Sourcetree啟動(dòng)問(wèn)題(完美解決)
文章講述了SourceTree閃退的問(wèn)題及其解決方法,源因?yàn)槲搓P(guān)閉SourceTree而關(guān)機(jī)或系統(tǒng)更新導(dǎo)致緩存信息不匹配,解決方法是刪除緩存文件或特定緩存目錄中的[Composition.cache]文件2024-11-11
代碼著色之SyntaxHighlighter項(xiàng)目(最流行的代碼高亮)
dp.SyntaxHighlighter。它可以在網(wǎng)頁(yè)中對(duì)各種程序源代碼語(yǔ)法進(jìn)行加亮顯示。支持當(dāng)前流 行的各種編程語(yǔ)言:C#、CSS、C++、Delphi、Java、JavaScript、PHP、Python、Ruby、SQL、Visual Basic、XML / HTML等2014-04-04

