網站首頁 編程語言 正文
1.為什么要使用域名訪問部署在Istio中的程序
我們在Istio中部署的程序一定不止有一個,前面我們已經在Istio中部署了Httpbin、Bookinfo、Nginx這三個應用程序,但是我們使用節點IP加NodePort端口的方式永遠只是請求到了一個應用程序,就好比我們已經實現了Nginx的基于端口的訪問模式,不過每個應用程序都是用的是80端口,才導致只訪問到了一個應用程序,在實際生產中,Istio中一定會部署很多個應用程序,我們需要實現基于域名來訪問不同的應用程序。
應用部署在Istio之后,將程序對外發布,會創建Gateway以及VirtualService資源,我們只需要在這兩個資源中聲明程序使用的域名,就可以接受來自LB的請求轉發,LB的請求中會攜帶主機頭,從而轉發到對應的應用程序。
當然也可以不額外占用服務器去搭建LB產品,我們可以在K8S集群中搭建一個Nginx服務,由K8S中的Nginx服務接收80端口流量請求轉發至IngressGateway,為什么不使用Ingress呢,Ingress需要為每一個網站創建資源編排文件,如果域名很多的情況下,配置比較繁瑣。
如下圖所示:用戶請求bookinfo的項目,在瀏覽器中輸入bookinfo.jiangxl.com域名,由DNS解析到LB負載均衡器,LB負載均衡器會將請求轉發到IngressGateway中,IngressGateway根據請求頭中的域名,將請求轉發到對應的Gateway中,然后在將請求轉發到應用程序的Service資源,最后由應用程序的Pod資源提供應用程序的服務。
2.通過域名的方式訪問Istio網格中的應用程序
2.1.配置Gateway和VirtualService資源
配置每個應用程序的Gateway以及VirtualService資源,為應用程序綁定使用的域名,綁定后只有這個域名的流量請求才會被轉發到這個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: 8000
2)更新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)創建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負載均衡我們采用Nginx來實現,由Nginx去反向代理IngressGateway的NodePort端口來實現基于域名去訪問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版本協議,istio是1.1版本,nginx默認1.0版本
proxy_set_header Host $host; #代理轉發時攜帶請求的主機頭
proxy_pass http://192.168.20.10:31105; #代理到istio的IngressGateway
}
}
3.啟動Nginx
[root@lb~]# systemctl restart nginx
3.基于域名來訪問Istio中的各個程序
測試之前先將域名解析寫入本地hosts文件。
192.168.20.13 httpbin.jiangxl.com bookinfo.jiangxl.com nginx.jiangxl.com
1)httpbin程序的訪問
2)bookinfo程序的訪問
3)nginx程序的訪問
原文鏈接:https://blog.csdn.net/weixin_44953658/article/details/125758935
相關推薦
- 2022-07-22 在windows上配置git支持多賬號
- 2022-05-08 ASP.NET?MVC使用母版頁視圖_實用技巧
- 2022-05-31 Pandas保存csv數據的三種方式詳解_python
- 2022-04-30 DataGridView實現點擊列頭升序和降序排序_C#教程
- 2023-08-13 element表單組件的trigger表單驗證邏輯規則
- 2023-07-04 spring boot security之前后端分離配置
- 2022-06-16 golang?gorm錯誤處理事務以及日志用法示例_Golang
- 2023-10-15 el-input有時候添加不了有時候刪不了
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支