一、概述

Istio 提供一种简略的方法来为已布置的服务树立网络,该网络具有负载均衡、服务间认证、监控、网关等功用,而不需要对服务的代码做任何改动。这里首要讲Istio Gateway服务。

  • istio 适用于容器或虚拟机环境(特别是 k8s),兼容异构架构。
  • istio 运用 sidecar(边车形式)署理服务的网络,不需要对业务代码自身做任何的改动。
  • HTTP、gRPC、WebSocket 和 TCP 流量的主动负载均衡。
  • istio 经过丰厚的路由规矩、重试、故障转移和故障注入,能够对流量行为进行细粒度操控;支撑拜访操控、速率限制和配额。
  • istio 对出入集群进口和出口中所有流量的主动度量目标、日志记录和盯梢。
  • istio 支撑蓝绿发布和金丝雀发布(灰度发布)等。

Istio Gateway 描绘在网格边际运转的负载均衡器 接纳传入或传出的 HTTP/TCP 连接。规格 描绘应公开的一组端口,协议的类型 运用、负载均衡器的 SNI 装备等。

  • 运用网关为网格来办理入站和出站流量,能够让用户指定要进入或脱离网格的流量。
  • Gateway 用于为 HTTP / TCP 流量装备负载均衡器,并不管该负载均衡器将在哪里运转。网格中能够存在恣意数量的 Gateway,而且多个不同的 Gateway 完成能够共存。实际上,经过在装备中指定一组作业负载(Pod)标签,能够将 Gateway 装备绑定到特定的作业负载,然后答运用户经过编写简略的 Gateway Controller 来重用现成的网络设备
  • Gateway 只用于装备 L4-L6 功用(例如,对外公开的端口,TLS 装备),所有主流的 L7 署理均以统一的方法完成了这些功用。然后,经过在 Gateway 上绑定 VirtualService 的方法,能够运用规范的 Istio 规矩来操控进入 Gateway 的 HTTP 和 TCP 流量。

官方文档:istio.io/latest/zh/d… Istio Gateway 官方文档:preliminary.istio.io/latest/zh/d… GitHub地址:github.com/istio/istio

二、Istio 架构

在Kubernetes环境中,Ingress controller用于办理进入集群的流量。在Istio服务网格中 Istio Ingress Gateway承当相应的人物,它运用新的装备模型(Gateway 和 VirtualServices)完成流量办理的功用。经过下图做一个总的描绘。

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

  1. 用户向某端口发出恳求;
  2. 负载均衡器监听端口,并将恳求转发到集群中的某个节点上。Istio Ingress Gateway Service 会监听集群节点端口的恳求;
  3. Istio Ingress Gateway Service 将恳求交给Istio Ingress Gateway Pod 处理。IngressGateway Pod 经过 Gateway 和 VirtualService 装备规矩处理恳求。其间,Gateway 用来装备端口、协议和证书,VirtualService 用来装备一些路由信息(找到恳求对应处理的服务App Service);
  4. Istio Ingress Gateway Pod将恳求转给App Service;
  5. 最终的恳求会交给App Service 关联的App Deployment处理。

三、经过 istioctl 布置 Istio

1)装置istioctl 东西

wget https://github.com/istio/istio/releases/download/1.16.0/istio-1.16.0-linux-amd64.tar.gz
tar -xf istio-1.16.0-linux-amd64.tar.gz
ln -s /opt/istio/istioctl/istio-1.16.0/bin/istioctl /usr/local/bin/istioctl
istioctl version

2)经过istioctl装置istio

要想知道有哪几个内置的装备文件,能够运转以下指令:

istioctl profile list

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

装备文件 中心组件 阐明
default istio-ingressgateway、istiod 依据 IstioOperator API 的默许设置启动组件。可用于出产布置。
demo istio-egressgateway、istio-ingressgateway、istiod 旨在展示 Istio 的功用,启用了高档其他追寻和拜访日志(需要具有适度的资源),合适学习运用。
minimal istiod 与默许装备文件相同,但只装置了操控平面组件。
remote 装备 Multicluster Mesh 的 Remote Cluster。
empty 不布置任何东西。能够作为自界说装备的根本装备文件。
preview istio-ingressgateway、istiod 实验性。用于探索 Istio 的新功用。不确保稳定性、安全性和功用。

当你足够熟悉 Istio 后,你能够自界说装备文件。但在此之前,咱们还是先以 demo 来入门吧。

### 检查 demo 的装备信息
istioctl profile dump demo
### 开始装置
# 【方法一】经过--set传参
istioctl install --set profile=demo
# 【方法二】经过-f指定文件
cat >my-demo-config.yaml<<EOF
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
  profile: demo
EOF
istioctl install -f my-demo-config.yaml

3)检查

istioctl version
kubectl -n istio-system get deploy

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

四、Istio Gateway

在Kubernetes环境中,Ingress controller用于办理进入集群的流量。在Istio服务网格中 Istio Ingress Gateway承当相应的人物,它运用新的装备模型(GatewayVirtualServices)完成流量办理的功用。

  • 网关是一个运转在网格边际的负载均衡器,用于接纳传入或传出的HTTP/TCP连接。
  • 首要作业是接受外部恳求,把恳求转发到内部服务。网格边际的Ingress 流量,会经过对应的 Istio IngressGateway Controller 进入到集群内部。

官方文档:preliminary.istio.io/latest/zh/d…

【示例装备】

# cat gateway.yaml
apiVersion: networking.istio.io/v1beta1 
kind: Gateway
metadata:
  name: canary-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"	# *表明通配符,经过任何域名都能够拜访

在上面这个yaml里咱们装备了一个监听80端口的进口网关,它会将80端口的http流量导入到集群内对应的Virtual Service上。

五、Istio VirtualService 虚拟服务

VirtualService 是Istio流量治理的一个中心装备,能够说是Istio流量治理中最重要、最杂乱的。VirtualService在形式上表明一个虚拟服务,将满足条件的流量都转发到对应的服务后端,这个服务后端能够是一个服务,也能够是在DestinationRule中界说的服务的子集。

官方文档:preliminary.istio.io/latest/zh/d…

【示例装备】

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews-route
spec:
  hosts:
  - reviews.prod.svc.cluster.local
  http:
  - name: "reviews-v2-routes"
    match:
    - uri:
        prefix: "/wpcatalog"
    - uri:
        prefix: "/consumercatalog"
    rewrite:
      uri: "/newcatalog"
    route:
    - destination:
        host: reviews.prod.svc.cluster.local
        subset: v2
  - name: "reviews-v1-route"
    route:
    - destination:
        host: reviews.prod.svc.cluster.local
        subset: v1

字段阐明:

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

六、示例演示(bookinfo)

1)装置bookinfo运用

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作
在线书店-bookinfo:该运用由四个独自的微服务构成,这个运用模仿在线书店的一个分类,显现一本书的信息,页面上会显现一本书的描绘,书本的细节(ISBN、页数等),以及关于这本书的一些谈论。

Bookinfo运用分为四个独自的微服务

  • productpage这个微服务会调用details和reviews两个微服务,用来生成页面;
  • details这个微服务中包含了书本的信息;
  • reviews这个微服务中包含了书本相关的谈论,它还会调用ratings微服务;
  • ratings这个微服务中包含了由书本评价组成的评级信息。

reviews微服务有3个版本

  • v1版本不会调用ratings服务;
  • v2版本会调用ratings服务,并运用1到5个黑色星形图标来显现评分信息;
  • v3版本会调用ratings服务,并运用1到5个红色星形图标来显现评分信息。

1、创立指令空间

kubectl create ns bookinfo

2、增加label

由于Istio proxy的注入是基于label,因而咱们需要为demo namespace增加label,

kubectl label namespace bookinfo istio-injection=enabled
kubectl get ns --show-labels bookinfo

3、开始布置bookinfo

cd /opt/istio/istioctl/istio-1.16.0
kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml -n bookinfo
kubectl get pod -n bookinfo

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作
然后咱们可检查运用pod里的容器信息,能够看到已经被注入istio-proxy,

kubectl get pod productpage-v1-bf4b489d8-gt7gw -n bookinfo -o jsonpath='{.status.containerStatuses}' | jq

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

2)增加路由规矩

服务布置后,还需要增加路由规矩,将恳求路由到对应的服务

kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml -n bookinfo
kubectl get virtualservice -n bookinfo

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

3)拜访服务

1、经过NodePort拜访

获取host ip,也便是ingressgateway pod所在机器ip

kubectl get po -l istio=ingressgateway -n istio-system -o jsonpath='{.items[0].status.hostIP}'

获取port,也便是80端口映射的目的端口,即31082

kubectl -n istio-system get service istio-ingressgateway

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

web:http://192.168.182.111:32688/productpage

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

2、经过externalip拜访

由于咱们是本地测验,必定无法运用公网的LB,因而咱们能够直接将externalip修改为某个node的ip或许VIP,这是设置一个VIP(跟node节点同网段),这样就能经过80端口拜访

kubectl -n istio-system get service istio-ingressgateway
kubectl patch svc istio-ingressgateway --namespace istio-system --patch '{"spec": { "externalIPs": ["192.168.182.210"] }}'

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作
从上图可知,会把VIP帮到kube-ipvs0虚拟网卡上。接下来就能够经过VIP拜访web
【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

4)卸载bookinfo服务

cd /opt/istio/istioctl/istio-1.16.0
sh samples/bookinfo/platform/kube/cleanup.sh

5)卸载 istio

istioctl manifest generate --set profile=demo | kubectl delete -f -

七、Istio Gateway 示例演示

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

1)Helm 装置 Nginx,Apache

# 增加chart源
helm repo add bitnami https://charts.bitnami.com/bitnami
# 装置Nginx 
helm pull bitnami/nginx --version 13.2.1
helm install my-nginx-1 ./nginx-13.2.1.tgz
# 装置Apache
helm pull bitnami/apache --version 9.2.7
helm install my-apache-1 ./apache-9.2.7.tgz

2)http 测验

1、装备 Gateway

网关将是 运用于在带有标签的容器上运转的署理 app: my-grafana-gateway


cat >my-http-gw.yaml<<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: my-http-gw
spec:
  selector:
    istio: ingressgateway # use Istio default gateway implementation
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - my-http-gw.com
EOF

运用默许网关,istio: ingressgateway需要跟默许网关svc的labels字段对应。

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

2、装备 VirtualService

要为进入上面的 Gateway 的流量装备相应的路由,必须为同一个 host 界说一个 VirtualService,并运用装备中的 gateways 字段绑定到前面界说的 Gateway 上:

cat >my-http-vs.yaml<<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings-route
spec:
  hosts:
  - my-http-gw.com
  gateways:
  - my-http-gw # <---- bind to gateway
  http:
  - match:
    - uri:
        prefix: /nginx-1
    rewrite:
      uri: /
    route:
    - destination:
        host: my-nginx-1.default.svc.cluster.local  #<---- server name
        port:
          number: 80
  - match:
    - uri:
        prefix: /apache-1
    rewrite:
      uri: /
    route:
    - destination:
        host: my-apache-1.default.svc.cluster.local  #<---- server name
        port:
          number: 80
EOF

由于咱们是本地测验,必定无法运用公网的LB,因而咱们能够直接将externalip修改为某个node的ip或许同网段的VIP,且type: LoadBalancer,这样就能经过80端口拜访

kubectl -n istio-system get service istio-ingressgateway
# 192.168.182.210为VIP,无需主动创立,这个vip会主动绑定到kube-ipvs0虚拟网卡上
kubectl patch svc istio-ingressgateway --namespace istio-system --patch '{"spec": { "externalIPs": ["192.168.182.210"] }}'

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作
装备hosts

192.168.182.210 my-http-gw.com

3、测验验证

my-http-gw.com/nginx-1

【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作
my-http-gw.com/apache-1
【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作

3)https 测验

1、生成证书(有证书可忽略)

自签名证书来只答应 https 流量来确保 istio ingress gateway 的安全。

openssl req -x509 -nodes -newkey rsa:2048 -keyout my-http-gw.com.key -out my-http-gw.com.cert -subj "/CN=*.my-http-gw.com"
### 证书增加到 kubernetes secret
kubectl create -n istio-system secret tls istio-ingressgateway-certs --key my-http-gw.com.key --cert my-http-gw.com.cert
### 检查证书和私钥是否布置成功
kubectl exec -it -n istio-system \
  $(kubectl -n istio-system get pods \
    -l istio=ingressgateway \
    -o jsonpath='{.items[0].metadata.name}') \
  -- ls -l /etc/istio/ingressgateway-certs/

2、装备 Gateway 和 VirtualService

网关将是 运用于在带有标签的容器上运转的署理 app: my-grafana-gateway

cat >my-https.yaml<<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: my-https-gw
spec:
  selector:
    istio: ingressgateway # use Istio default gateway implementation
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - my-http-gw.com
    tls:
     httpsRedirect: true
  - port:
      number: 443
      name: https
      protocol: HTTPS
    hosts:
    - my-http-gw.com
    tls:
      mode: SIMPLE
      serverCertificate: /opt/istio/test/tls/my-http-gw.com.crt
      privateKey: /opt/istio/test/tls/my-http-gw.com.key
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: https-route
spec:
  hosts:
  - my-https-gw.com
  gateways:
  - my-https-gw # <---- bind to gateway
  http:
  - match:
    - uri:
        prefix: /nginx-1
    rewrite:
      uri: /
    route:
    - destination:
        host: my-nginx-1  #<---- server name
        port:
          number: 80
  - match:
    - uri:
        prefix: /apache-1
    rewrite:
      uri: /
    route:
    - destination:
        host: my-apache-1  #<---- server name
        port:
          number: 80
EOF

八、Ingress Controller 与 Istio Gateway 比较

K8S官方保护的Nginx Ingress Controller及 Istio Gateway 比较:

NGINX Ingress Controller Istio Gateway
依据HTTP Header挑选路由规矩 仅支撑单个Header,不支撑多个Header组合 支撑
Header规矩支撑正则表达式 支撑 支撑
服务之间设置权重拆分流量 支撑 支撑
Header和权重规矩组合运用 支撑 支撑
路由规矩检查 不支撑 支撑
路由规矩粒度 service service下的不同pod
支撑的协议 HTTP1.1/HTTP2/gRPC/TCP/Websockets HTTP1.1/HTTP2/gRPC/TCP/Websockets/MongoDB

这样一比较,就很明显看出,Istio Gateway比Ingress Controller强大,这里仅仅介绍了常用的负载转发功用,还有流量操控,安全操控等功用,如果仅仅简略的负载转发用istio就点大材小用了,如果公司需要更杂乱网络管控,能够挑选istio,所以一般在出产环境中能够运用Istio Gateway应对杂乱的网络环境。

Istio Gateway 介绍与 简略运用就先到这里了,有疑问的小伙伴欢迎给我留言,后续会继续更新【云原生+大数据】相关的文章,请小伙伴耐性等候~