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

 更新时间:2022年07月15日 10:43:49   作者:Jiangxl~  
这篇文章主要介绍了基于域名的方式访问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/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程序的访问

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

相关文章

  • hexo博客开启https的SSL 证书实现过程

    hexo博客开启https的SSL 证书实现过程

    这篇文章主要为大家介绍了hexo 博客开启https的SSL证书实现过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • git pull每次都要输入用户名和密码的解决办法

    git pull每次都要输入用户名和密码的解决办法

    本文主要介绍了git pull每次都要输入用户名和密码的解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Git基本常用命令

    Git基本常用命令

    本文主要介绍了Git基本常用命令。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • idea2020.1 常用设置图文详解

    idea2020.1 常用设置图文详解

    这篇文章主要介绍了idea2020.1 常用设置,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • 2020最新版vscode格式化代码的详细教程

    2020最新版vscode格式化代码的详细教程

    这篇文章主要介绍了2020最新版vscode格式化代码的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • openlayers 模仿高德箭头导航路线图的代码详解

    openlayers 模仿高德箭头导航路线图的代码详解

    这篇文章主要介绍了openlayers 模仿高德箭头导航路线图的示例代码,主要包括原始数据、起点/终点寻找、起点和终点样式函数,结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • i++循环与i-–循环的执行效率(递增与递减效率)

    i++循环与i-–循环的执行效率(递增与递减效率)

    i++循环与i-–循环的执行效率(递增与递减效率),需要的朋友可以参考下。
    2011-01-01
  • 301重定向代码合集(iis,asp,php,asp.net,apache)

    301重定向代码合集(iis,asp,php,asp.net,apache)

    脚本之家将SEO工作中所需要的301转向代码进行了整理,收藏并分享,以备查阅。
    2011-02-02
  • 关于Sourcetree启动问题(完美解决)

    关于Sourcetree启动问题(完美解决)

    文章讲述了SourceTree闪退的问题及其解决方法,源因为未关闭SourceTree而关机或系统更新导致缓存信息不匹配,解决方法是删除缓存文件或特定缓存目录中的[Composition.cache]文件
    2024-11-11
  • 代码着色之SyntaxHighlighter项目(最流行的代码高亮)

    代码着色之SyntaxHighlighter项目(最流行的代码高亮)

    dp.SyntaxHighlighter。它可以在网页中对各种程序源代码语法进行加亮显示。支持当前流 行的各种编程语言:C#、CSS、C++、Delphi、Java、JavaScript、PHP、Python、Ruby、SQL、Visual Basic、XML / HTML等
    2014-04-04

最新评论