基于域名的方式访问Istio服务网格中的多个应用程序的方法详解


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/v1alpha3kind: Gatewaymetadata:  name: httpbin-gatewayspec:  selector:    istio: ingressgateway  servers:  - port:      number: 80      name: http      protocol: HTTP    hosts:    - "httpbin.jiangxl.com"#在hosts中绑定程序的域名---apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:  name: httpbinspec:  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.yamlgateway.networking.istio.io/httpbin-gateway createdvirtualservice.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/v1alpha3kind: Gatewaymetadata:  name: bookinfo-gatewayspec:  selector:    istio: ingressgateway # use istio default controller  servers:  - port:      number: 80      name: http      protocol: HTTP    hosts:    - "bookinfo.jiangxl.com"---apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:  name: bookinfospec:  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 bookinfogateway.networking.istio.io/bookinfo-gateway configuredvirtualservice.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/v1alpha3kind: Gatewaymetadata:  name: nginx-gateway  namespace: istio-projectspec:  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/v1alpha3kind: VirtualServicemetadata:  name: nginx-vs  namespace: istio-projectspec:  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 nginx2.配置Nginx反向代理Istio的IngressGateway[root@lb~]# vim /etc/nginx/conf.d/istio-ingressgateway.confserver {        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程序的访问

到此这篇关于基于域名的方式访问Istio服务网格中的多个应用程序的文章就介绍到这了,更多相关Istio服务网格内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!