开发公司Istio三之VirtualService、Gateway、DestinationRule配置使用

开发公司前面介绍了Istio开发公司如何完成流量劫持,开发公司此篇博客将介绍Istio开发公司中如何配置VirtualService、Gateway、DestinationRule开发公司来完成流量管理。开发公司还是先从理论开始,先看看VirtualService、Gateway、DestinationRule的概念。
VirtualService:在Istio开发公司服务网格中定义路由规则
DestinationRule:在VirtualService开发公司路由生效后,开发公司配置应用与请求的策略集
ServiceEntry:开发公司通常用于在Istio开发公司服务网格之外启用对服务的请求
Gateway:为HTTP/TCP开发公司流量配置负载均衡器,开发公司最常见的是在网格的边缘的操作,开发公司以启用应用程序的入口流量。Istio中gateway分为IngressGateway和EgressGateway,开发公司分别管理进来的流量和开发公司出去的流量。开发公司下面是对上面3开发公司个对象配置中的各个字开发公司段含义解析。

VirtualService字段名称字段说明
spec.hosts开发公司定义路由规则关联一组的 hosts,开发公司可以是带有通配符的 DNS 名称或者 IP 地址(IP 开发公司地址仅能应用于来源流开发公司量为边缘代理网关)。开发公司该字段能应用于 HTTP 和 TCP 流量。在 Kubernetes 环境中,可以使用 service 开发公司的名称作为缩写,Istio 会按照 VirtualService所在 namespace 补齐缩写,例如在 default namespace 的 VirtualService 包含 host 缩写 reviews 开发公司会被补齐为 reviews.default.svc.cluster.local。开发公司为避免误配置,推荐填写 host 全称
spec.gateways开发公司定义应用路由规则的来源流量,可以是一个或多个网关,或网格内部的 sidecar,指定方式为 <gateway namespace>/<gateway name>,保留字段 mesh 表示网格内部所有的 sidecar,当该参数缺省时,会默认填写 mesh,即该路由规则的来源流量为网格内部所有 sidecar
spec.http定义一组有序的(优先匹配靠前的路由规则)应用于 HTTP 流量的路由规则,HTTP 路由规则会应用于网格内部的 service 端口命名为 http-, http2-, grpc- 开头的流量以及来自 gateway 的协议为 HTTP, HTTP2, GRPC, TLS-Terminated-HTTPS 的流量
spec.http.match定义路由的匹配规则列表,单个匹配规则项内所有条件是且关系,列表中多个匹配规则之间为或关系
spec.http.route定义路由转发目的地列表,一条 HTTP 路由可以是重定向或转发(默认),转发的目的地可以是一个或多个服务(服务版本)。同时也可以配置权重、header 操作等行为
spec.http.redirect定义路由重定向,一条 HTTP 路由可以是重定向或转发(默认),如规则中指定了 passthrough 选项,route、redirect 均会被忽略。可将 HTTP 301 重定向到另外的 URL 或 Authority
spec.http.rewrite定义重写 HTTP URL 或 Authority headers,不能与重定向同时配置,重写操作会在转发前执行
spec.http.timeout定义 HTTP 请求的超时时间
spec.http.retries定义 HTTP 请求的重试策略
spec.http.fault定义 HTTP 流量的故障注入策略,开启时超时和重试策略不会开启
spec.http.mirror定义将 HTTP 流量复制到另一个指定的目的端,被复制的流量按照“best effort”原则,sidecar/网关不会等待复制流量的响应结果就会从源目的端返回响应。镜像流量的目的服务端会产生监控指标。
spec.http.mirrorPercent定义流量镜像的复制百分比,缺省时复制100%的流量。最大值为100
spec.http.corsPolicy定义 CORS 策略(跨域资源共享,Cross-Origin Resource Sharing,CORS),更多关于 CORS 的介绍请参见 CORS,关于 Istio CORS 策略配置语法请参见 CorsPolicy
spec.http.headers定义 header 操作规则,包括 request 和 response header 的更新,增加,移除操作
spec.tcp定义一组有序的(优先匹配靠前的路由规则)应用于 TCP 流量的路由规则,该路由规则会应用于任何非 HTTP 和 TLS 的端口
spec.tcp.match定义 TCP 流量路由的匹配规则列表,单个匹配规则项内所有条件是且关系,列表中多个匹配规则之间为或关系
spec.tcp.route定义 TCP 连接转发的目的端
spec.tls定义一组有序的(优先匹配靠前的路由规则)应用于未终止的 TLS 或 HTTPS 流量的路由规则,该路由规则会应用于网格内部的 service 端口命名为 https-,tls- 开头的流量,来自 gateway 的端口协议为 HTTPS, TLS 的未终止加密流量,Service Entry 使用 HTTPS, TLS 协议的端口。当 https-, tls- 端口未关联 VirtualService 规则时将会被视为 TCP 流量
spec.tls.match定义 TLS 流量路由的匹配规则列表,单个匹配规则项内所有条件是且关系,列表中多个匹配规则之间为或关系
spec.tls.route定义连接转发的目的端

DestinationRule字段名称字段含义
spec.host关联 DestinationRule 配置的服务名称,可以是自动发现的服务(例如 Kubernetes service name),或通过 ServiceEntry 声明的 hosts。如填写的服务名无法在上述源中找到,则该 DestinationRule 中定义的规则无效
spec.subsets定义服务的版本(subsets),版本可通过标签键值对匹配匹配服务中的endpoints。可以在 subsets 级覆盖流量策略配置
spec.trafficPolicy定义流量策略,包括负载均衡、连接池、健康检查、TLS 策略
spec.trafficPolicy.loadBalancer配置负载均衡算法,可配置:简单负载均衡算法(round robin, least conn, random...),一致性哈希(会话保持,支持按 header name,cookie,IP,query parameter 哈希),地域感知负载均衡算法
spec.trafficPolicy.connectionPool配置与上游服务的连接量,可设置 TCP/HTTP 的连接池
spec.trafficPolicy.outlierDetection配置从负载均衡池中驱逐不健康的 hosts
spec.trafficPolicy.tls连接上游服务的 client 端 TLS 相关配置,与 PeerAuthentication 策略(server 端 TLS 模式配置)配合使用
spec.trafficPolicy.portLevelSettings配置端口级别的流量策略,端口级别的流量策略会覆盖服务 / subsets 级别的流量策略配置
GateWay字段名称字段含义
metadata.nameGateway 名称
metadata.namespaceGateway 命名空间
spec.selectorGateway 使用填写的标签键值对匹配配置下发的边缘代理网关实例
spec.servers.port.number端口
spec.servers.port.protocol通信协议,支持:HTTP, HTTPS, GRPC, HTTP2, MONGO, TCP, TLS,请注意同一网关同一端口的协议配置需要保持一致。
spec.servers.port.name端口名称
spec.severs.hosts域名,支持通配符 *
spec.servers.tls.httpsRedirect值为 true 时,边缘代理网关会对所有 http 请求返回 301 重定向,要求客户端发起 https 请求
spec.servers.tls.mode配置当前端口的 TLS 安全认证模式,如需要开启当前端口的安全认证则需要填写。支持:PASSTHROUGH, SIMPLE, MUTUAL, AUTO_PASSTHROUGH, ISTIO_MUTUAL
spec.servers.tls.credentialName配置发现 TLS 证书密钥的 secret 的名称
spec.servers.tls.serverCertificate设置端口的 TLS 证书密钥通过 file mount 形式(不推荐,推荐采用填写 credentialName 字段加载证书私钥)挂载时需要填写的证书路径字段,Istio 默认使用网关所在命名空间下 istio-ingressgateway-certs secret 加载证书至路径 /etc/istio/ingressgateway-certs
spec.servers.tls.privateKey设置端口的 TLS 证书密钥通过 file mount 形式(不推荐,推荐采用填写 credentialName 字段加载证书私钥)挂载时需要填写的私钥路径字段,Istio 默认使用网关所在命名空间下 istio-ingressgateway-certs secret 加载私钥至路径 /etc/istio/ingressgateway-certs
spec.servers.tls.caCertificates设置端口的 TLS 证书密钥通过 file mount 形式(不推荐,推荐采用填写 credentialName 字段加载证书私钥)挂载时需要填写的跟证书路径字段,Istio 默认使用网关所在命名空间下 istio-ingressgateway-ca-certs 加载根证书至路径 /etc/istio/ingressgateway-ca-certs,双向认证时需要配置根证书

接着通过实际例子演示如果通过配置VirtualService等完成流量管理。

案例一:部署后端服务simple,配置istio的VirtualService和Gateway,gateway网关添加在istio-ingress pod上,gateway网关expose出80端口。VirtualService将simple网关、80端口发送的,且请求的hosts是“simple.cncamp.io”的请求路由到后端务"simple.simple.svc.cluster.local"上。之前在CoreDNS章节介绍过,这个域名地址实际就是部署的后端simple服务的service地址。

  1. apiVersion: networking.istio.io/v1beta1
  2. kind: VirtualService
  3. metadata:
  4. name: simple
  5. spec:
  6. gateways:
  7. - simple
  8. hosts:
  9. - simple.cncamp.io
  10. http:
  11. - match:
  12. - port: 80
  13. route:
  14. - destination:
  15. host: simple.simple.svc.cluster.local
  16. port:
  17. number: 80
  18. ---
  19. apiVersion: networking.istio.io/v1beta1
  20. kind: Gateway
  21. metadata:
  22. name: simple
  23. spec:
  24. selector:
  25. istio: ingressgateway
  26. servers:
  27. - hosts:
  28. - simple.cncamp.io
  29. port:
  30. name: http-simple
  31. number: 80
  32. protocol: HTTP
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: simple
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app: simple
  10. template:
  11. metadata:
  12. annotations:
  13. prometheus.io/scrape: "true"
  14. prometheus.io/port: "80"
  15. labels:
  16. app: simple
  17. spec:
  18. containers:
  19. - name: simple
  20. imagePullPolicy: Always
  21. image: cncamp/httpserver:v1.0-metrics
  22. ports:
  23. - containerPort: 80
  24. ---
  25. apiVersion: v1
  26. kind: Service
  27. metadata:
  28. name: simple
  29. spec:
  30. ports:
  31. - name: http
  32. port: 80
  33. protocol: TCP
  34. targetPort: 80
  35. selector:
  36. app: simple

上面的simple 服务的部署配置文件,通过配置文件创建相关对象。

  1. kubectl create ns simple
  2. kubectl create -f simple.yaml -n simple
  3. kubectl create -f istio-specs.yaml -n simple

查看istio-ingress的service的IP地址,通过istio-ingress的IP地址发送请求。

 可以看到请求返回了响应,说明整个路由转发成功发送到了后端服务。

案例二:再部署一个服务nginx,配置VirtualService根据不同URL转发到不同的后端服务的路有规则,VirtulaService中还设置了rewrite字段。Gateway与上面相同,还是expose出80端口即可。

  1. apiVersion: networking.istio.io/v1beta1
  2. kind: VirtualService
  3. metadata:
  4. name: simple
  5. spec:
  6. gateways:
  7. - simple
  8. hosts:
  9. - simple.cncamp.io
  10. http:
  11. - match:
  12. - uri:
  13. exact: "/simple/hello"
  14. rewrite:
  15. uri: "/hello"
  16. route:
  17. - destination:
  18. host: simple.simple.svc.cluster.local
  19. port:
  20. number: 80
  21. - match:
  22. - uri:
  23. prefix: "/nginx"
  24. rewrite:
  25. uri: "/"
  26. route:
  27. - destination:
  28. host: nginx.simple.svc.cluster.local
  29. port:
  30. number: 80

 nginx应用部署的文件内容如下所示

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app: nginx
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx
  17. image: nginx
  18. ---
  19. apiVersion: v1
  20. kind: Service
  21. metadata:
  22. name: nginx
  23. spec:
  24. ports:
  25. - name: http
  26. port: 80
  27. protocol: TCP
  28. targetPort: 80
  29. selector:
  30. app: nginx

执行下面的命令启动pod以及访问应用。

  1. kubectl apply -f nginx.yaml -n simple
  2. kubectl apply -f istio-specs.yaml -n simple
  3. curl -H "Host: simple.cncamp.io" $INGRESS_IP/simple/hello
  4. curl -H "Host: simple.cncamp.io" $INGRESS_IP/nginx

可以看到nginx和simple服务都访问成功。

案例三:上面都是http方式访问,如果要通过https访问,配置tls即可,Gateway中expose 443端口,并配置了tls,tls是一个secret,后面会通过命令生成这个secret。

  1. apiVersion: networking.istio.io/v1beta1
  2. kind: VirtualService
  3. metadata:
  4. name: httpsserver
  5. spec:
  6. gateways:
  7. - httpsserver
  8. hosts:
  9. - httpsserver.cncamp.io
  10. http:
  11. - match:
  12. - port: 443
  13. route:
  14. - destination:
  15. host: httpserver.securesvc.svc.cluster.local
  16. port:
  17. number: 80
  18. ---
  19. apiVersion: networking.istio.io/v1beta1
  20. kind: Gateway
  21. metadata:
  22. name: httpsserver
  23. spec:
  24. selector:
  25. istio: ingressgateway
  26. servers:
  27. - hosts:
  28. - httpsserver.cncamp.io
  29. port:
  30. name: https-default
  31. number: 443
  32. protocol: HTTPS
  33. tls:
  34. mode: SIMPLE
  35. credentialName: cncamp-credential

后端服务httpserver的部署yaml文件内容如下所示。

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: httpserver
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app: httpserver
  10. template:
  11. metadata:
  12. annotations:
  13. prometheus.io/scrape: "true"
  14. prometheus.io/port: "80"
  15. labels:
  16. app: httpserver
  17. spec:
  18. containers:
  19. - name: httpserver
  20. imagePullPolicy: Always
  21. image: cncamp/httpserver:v1.0-metrics
  22. ports:
  23. - containerPort: 80
  24. ---
  25. apiVersion: v1
  26. kind: Service
  27. metadata:
  28. name: httpserver
  29. spec:
  30. ports:
  31. - name: http
  32. port: 80
  33. protocol: TCP
  34. targetPort: 80
  35. selector:
  36. app: httpserver
  1. openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=cncamp Inc./CN=*.cncamp.io' -keyout cncamp.io.key -out cncamp.io.crt
  2. kubectl create -n istio-system secret tls cncamp-credential --key=cncamp.io.key --cert=cncamp.io.crt
  3. kubectl apply -f istio-specs.yaml -n securesvc

通过openssl命令生成key和cert,通过kubectl create secret tls命令生成secret对象,通过https访问访问服务。

curl --resolve httpsserver.cncamp.io:443:$INGRESS_IP https://httpsserver.cncamp.io/healthz -v -k

可以看到访问成功,因为是https方式访问,故可以看到有TLS握手的过程。

 案例四:模拟金丝雀部署,将应用的两个版本同时部署上,配置路由到V1和V2的规则。 如果请求的header中包含jesse,则路有到V2版本,否则路有到V1版本。Destination中定义V1和V2.

  1. apiVersion: networking.istio.io/v1beta1
  2. kind: VirtualService
  3. metadata:
  4. name: canary
  5. spec:
  6. hosts:
  7. - canary
  8. http:
  9. - match:
  10. - headers:
  11. user:
  12. exact: jesse
  13. route:
  14. - destination:
  15. host: canary
  16. subset: v2
  17. - route:
  18. - destination:
  19. host: canary
  20. subset: v1
  21. ---
  22. apiVersion: networking.istio.io/v1alpha3
  23. kind: DestinationRule
  24. metadata:
  25. name: canary
  26. spec:
  27. host: canary
  28. trafficPolicy:
  29. loadBalancer:
  30. simple: RANDOM
  31. subsets:
  32. - name: v1
  33. labels:
  34. version: v1
  35. - name: v2
  36. labels:
  37. version: v2
  38. trafficPolicy:
  39. loadBalancer:
  40. simple: ROUND_ROBIN

模拟的后端服务V1和V2版本,区分V1和V2应用主要是通过lables version进行区分的。

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: canary
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app: canary
  10. template:
  11. metadata:
  12. annotations:
  13. prometheus.io/scrape: "true"
  14. prometheus.io/port: "80"
  15. labels:
  16. app: canary
  17. version: v1
  18. spec:
  19. containers:
  20. - name: canary
  21. imagePullPolicy: Always
  22. image: cncamp/httpserver:v1.0-metrics
  23. ports:
  24. - containerPort: 80
  25. ---
  26. apiVersion: apps/v1
  27. kind: Deployment
  28. metadata:
  29. name: canary-v2
  30. spec:
  31. replicas: 1
  32. selector:
  33. matchLabels:
  34. app: canary
  35. template:
  36. metadata:
  37. annotations:
  38. prometheus.io/scrape: "true"
  39. prometheus.io/port: "80"
  40. labels:
  41. app: canary
  42. version: v2
  43. spec:
  44. containers:
  45. - name: canary
  46. imagePullPolicy: Always
  47. image: cncamp/httpserver:v2.0-metrics
  48. ports:
  49. - containerPort: 80
  50. ---
  51. apiVersion: v1
  52. kind: Service
  53. metadata:
  54. name: canary
  55. spec:
  56. ports:
  57. - name: http
  58. port: 80
  59. protocol: TCP
  60. targetPort: 80
  61. selector:
  62. app: canary

启动后端服务和生产VirtualService和DestinationRule。

  1. kubectl apply -f canary.yaml -n canary
  2. kubectl apply -f istio-specs.yaml -n canary

为了访问canary,这里再启动一个能执行linux命令的pod,toolbox。

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: toolbox
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app: toolbox
  10. template:
  11. metadata:
  12. labels:
  13. app: toolbox
  14. access: "true"
  15. spec:
  16. containers:
  17. - name: toolbox
  18. image: centos
  19. command:
  20. - tail
  21. - -f
  22. - /dev/null

登录到toolbox中,通过curl命令(curl canary/hello -H "user: jesse")访问后端服务,如果header中有jesse字段值,那么就会访问到V2版本的服务,否则会访问V1版本的服务。执行结果如下所示

以上就是通过例子演示Istio中VirtualService、Gateway、DestinationRule的配置,从而完成流量管理。

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发