【云原生】Kubernetes(k8s)Istio Gateway 介绍与实战操作
文章目录
一、概述
二、Istio 架构
三、通过 istioctl 部署 Istio
1)安装istioctl 工具
2)通过istioctl安装istio
3)检查
四、Istio Gateway
五、Istio VirtualService 虚拟服务
六、示例演示(bookinfo)
1)安装bookinfo应用
1、创建命令空间
2、添加label
3、开始部署bookinfo
2)添加路由规则
3)访问服务
1、通过NodePort访问
2、通过externalip访问
4)卸载bookinfo服务
5)卸载 istio
七、Istio Gateway 示例演示
1)Helm 安装 Nginx,Apache
2)http 测试
1、配置 Gateway
2、配置 VirtualService
3、测试验证
3)https 测试
1、生成证书(有证书可忽略)
2、配置 Gateway 和 VirtualService
八、Ingress Controller 与 Istio Gateway 比较
一、概述
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 流量。
官方文档:https://istio.io/latest/zh/docs/
Istio Gateway 官方文档:https://preliminary.istio.io/latest/zh/docs/reference/config/networking/gateway/
GitHub地址:https://github.com/istio/istio
二、Istio 架构
在Kubernetes环境中,Ingress controller用于管理进入集群的流量。在Istio服务网格中 Istio Ingress Gateway承担相应的角色,它使用新的配置模型(Gateway 和 VirtualServices)完成流量管理的功能。通过下图做一个总的描述。
用户向某端口发出请求;
负载均衡器监听端口,并将请求转发到集群中的某个节点上。Istio Ingress Gateway Service 会监听集群节点端口的请求;
Istio Ingress Gateway Service 将请求交给Istio Ingress Gateway Pod 处理。IngressGateway Pod 通过 Gateway 和 VirtualService 配置规则处理请求。其中,Gateway 用来配置端口、协议和证书,VirtualService 用来配置一些路由信息(找到请求对应处理的服务App Service);
Istio Ingress Gateway Pod将请求转给App Service;
最终的请求会交给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
1
2
3
4
2)通过istioctl安装istio
要想知道有哪几个内置的配置文件,可以运行以下命令:
istioctl profile list
1
配置文件 核心组件 说明
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3)检查
istioctl version
kubectl -n istio-system get deploy
1
2
四、Istio Gateway
在Kubernetes环境中,Ingress controller用于管理进入集群的流量。在Istio服务网格中 Istio Ingress Gateway承担相应的角色,它使用新的配置模型(Gateway 和 VirtualServices)完成流量管理的功能。
网关是一个运行在网格边缘的负载均衡器,用于接收传入或传出的HTTP/TCP连接。
主要工作是接受外部请求,把请求转发到内部服务。网格边缘的Ingress 流量,会通过对应的 Istio IngressGateway Controller 进入到集群内部。
官方文档:https://preliminary.istio.io/latest/zh/docs/reference/config/networking/gateway/
【示例配置】
# 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:
- "*" # *表示通配符,通过任何域名都可以访问
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
在上面这个yaml里我们配置了一个监听80端口的入口网关,它会将80端口的http流量导入到集群内对应的Virtual Service上。
五、Istio VirtualService 虚拟服务
VirtualService 是Istio流量治理的一个核心配置,可以说是Istio流量治理中最重要、最复杂的。VirtualService在形式上表示一个虚拟服务,将满足条件的流量都转发到对应的服务后端,这个服务后端可以是一个服务,也可以是在DestinationRule中定义的服务的子集。
官方文档:https://preliminary.istio.io/latest/zh/docs/reference/config/networking/virtual-service/
【示例配置】
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
字段说明:
六、示例演示(bookinfo)
1)安装bookinfo应用
在线书店-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
1
2、添加label
因为Istio proxy的注入是基于label,因此我们需要为demo namespace添加label,
kubectl label namespace bookinfo istio-injection=enabled
kubectl get ns --show-labels bookinfo
1
2
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
1
2
3
然后我们可查看应用pod里的容器信息,可以看到已经被注入istio-proxy,
kubectl get pod productpage-v1-bf4b489d8-gt7gw -n bookinfo -o jsonpath='{.status.containerStatuses}' | jq
1
2)添加路由规则
服务部署后,还需要添加路由规则,将请求路由到对应的服务
kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml -n bookinfo
kubectl get virtualservice -n bookinfo
1
2
3)访问服务
1、通过NodePort访问
获取host ip,也就是ingressgateway pod所在机器ip
kubectl get po -l istio=ingressgateway -n istio-system -o jsonpath='{.items[0].status.hostIP}'
1
获取port,也就是80端口映射的目的端口,即31082
kubectl -n istio-system get service istio-ingressgateway
1
web:http://192.168.182.111:32688/productpage
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"] }}'
1
2
3
从上图可知,会把VIP帮到kube-ipvs0虚拟网卡上。接下来就可以通过VIP访问web
4)卸载bookinfo服务
cd /opt/istio/istioctl/istio-1.16.0
sh samples/bookinfo/platform/kube/cleanup.sh
1
2
5)卸载 istio
istioctl manifest generate --set profile=demo | kubectl delete -f -
1
七、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
1
2
3
4
5
6
7
8
9
10
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
使用默认网关,istio: ingressgateway需要跟默认网关svc的labels字段对应。
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
因为我们是本地测试,肯定没法使用公网的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"] }}'
1
2
3
4
配置hosts
192.168.182.210 my-http-gw.com
1
3、测试验证
http://my-http-gw.com/nginx-1
http://my-http-gw.com/apache-1
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/
1
2
3
4
5
6
7
8
9
10
11
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
八、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 介绍与 简单使用就先到这里了,有疑问的小伙伴欢迎给我留言,后续会持续更新【云原生+大数据】相关的文章,请小伙伴耐心等待~
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_35745940/article/details/127951515

浙公网安备 33010602011771号