VirtualService的关键字段
- hosts: 必选字段,用于指定流量请求的目标,可以是一个DNS名称或IP地址;DNS名称可以使用通配符,也可以使用短格式的service名称;
- gateways:指定应用流量规则的Gateway资源,在VirtualService上使用gateways字段遵循如下规则:
- 仅用于网格内部东西流量,省略gateway字段;
- 仅用于引入外部网格的流量,为gateway字段赋予合适的值即可;
客户端源自网格外部
;Gateway负责接入这部分流量,VirtualService负责将这部分流量完成在网格内路由
; - 同时用于网格内和网格外的流量时,需要将gateways字段的列表值其中一项指定为
mesh
;客户端即可源自网络外部,也可以是网格内部的流量;
- http:配置http协议的流量管理机制的有序列表,其配置框架称为HTTPRoute;
- tls: 用于处理TLS和https流量的有序列表,其配置框架称为TLSRoute;
- tcp:管理四层的TCP流量的有序列表,其配置框架称为TCPRoute;
5.1 HTTP路由配置(HTTPRoute)
-
virtualservice.http
配置如何处理http流量- 服务的端口协议是HTTP,HTTP2,GRPC,即在服务的端口名中包含http-,http2-,grpc-等;
- Gateway的端口协议是HTTP,HTTP2,GRPC,或者Gateway终结了TLS;
- ServiceEntry的端口协议是HTTP,HTTP2,GRPC;
- 路由机制及服务韧性等相关配置,即满足了HTTPMatchRequest条件的流量,做如下处理:
- 路由到指定目标 (HTTPRouteDestination)
- 执行重定向(HTTPRedict)
- URL重写(HTTPRewrite)
- 请求重试(HTTPRetry)
- 故障注入(HTTPFaultInjection)
- 跨站引用资源
- …
5.2 redirect
-
部署前端proxy服务
apiVersion: apps/v1 kind: Deployment metadata: name: proxy spec: progressDeadlineSeconds: 600 replicas: 1 selector: matchLabels: app: proxy template: metadata: labels: app: proxy spec: containers: - env: - name: PROXYURL value: http://demoappv10:8080 image: ikubernetes/proxy:v0.1.1 imagePullPolicy: IfNotPresent name: proxy ports: - containerPort: 8080 name: web protocol: TCP resources: limits: cpu: 50m --- apiVersion: v1 kind: Service metadata: name: proxy spec: ports: - name: http-80 ## service要想让virtualservice识别出http,就以http-* port: 80 protocol: TCP targetPort: 8080 selector: app: proxy
-
部署后端backend服务
--- apiVersion: apps/v1 kind: Deployment metadata: labels: app: backend version: v3.6 name: backendv36 spec: progressDeadlineSeconds: 600 replicas: 2 selector: matchLabels: app: backend version: v3.6 template: metadata: creationTimestamp: null labels: app: backend version: v3.6 spec: containers: - image: ikubernetes/gowebserver:v0.1.0 imagePullPolicy: IfNotPresent name: gowebserver env: - name: "SERVICE_NAME" value: "backend" - name: "SERVICE_PORT" value: "8082" - name: "SERVICE_VERSION" value: "v3.6" ports: - containerPort: 8082 name: web protocol: TCP resources: limits: cpu: 50m --- apiVersion: v1 kind: Service metadata: name: backend spec: ports: - name: http-web port: 8082 protocol: TCP targetPort: 8082 selector: app: backend version: v3.6
-
配置redirect的virtualservice
apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: proxy spec: hosts: - proxy http: - name: redict match: - uri: prefix: "/backend" redirect: uri: / # 覆盖match中的urri authority: backend # 覆盖请求的url的主机 port: 8082 #覆盖请求的url的端口 - name: default route: - destination: host: proxy
-
请求
http://proxy/backend
看是否重定向