垃圾站 生活小记 基于域名的方式访问Istio服务网格中的多个应用程序的方法详解

基于域名的方式访问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资源提供应用程序的服务。

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

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程序的访问

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

2)bookinfo程序的访问

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

3)nginx程序的访问

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

上一篇
下一篇
联系我们

联系我们

返回顶部