天天看點

Istio - crds - Vistrual Service

> 虛拟服務(Vistrual Service)是 Istio 重要的資源對象之一,作用是将流量路由到網格中的服務。支援基于權重、http header條件等優先級的路由,比Kuberentes service對于流量的管控更加的豐富,顆粒度更加精細。

#### 有了 Kubernetes Service,為什麼還需要 Istio Vistrual Service

簡單來說,基于 Kubernetes Service,隻可以實作簡單的流量負載均衡,如果想實作基于HTTP Header,負載百分比等等複雜的流量控制就無從下手了,Istio Vistrual Service在原本 Kubernetes Service 的功能之上,提供了更加豐富的路由控制。

#### 通過例子來了解

有兩個Deployment(nginx 及 httpd),通過Service關聯到一起,通過通路Service隻能做到簡單的負載均衡,通過實驗發現 nginx 和 httpd 流量各自在 50% 左右。

Deployment & Service

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: nginx
        server: web
    spec:
      containers:
        - image: 'nginx:latest'
          name: nginx-deployment
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: httpd
  name: httpd-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpd
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: httpd
        server: web
    spec:
      containers:
        - image: 'httpd:latest' 
          name: httpd-deployment 
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
  name: httpd-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: httpd
  type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    server: web
  type: ClusterIP      
Istio - crds - Vistrual Service

如果想實作更加細顆粒度的流量管控,通過引入Istio Vistrual Service 非常簡單的就實作複雜的流量管理。

VirtualService 根據 Destination 進行排程,并且設定相關的負載百分比實作精準的控制。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-vs
spec:
  hosts:
  - web-service
  http:
  - route:
    - destination:
        host: nginx-service
      weight: 80
    - destination:
   host: httpd-service
      weight: 20      
Istio - crds - Vistrual Service

通過用戶端測試以上的實驗,請留意用戶端也必須經過 Istio 注入,因為隻有用戶端被 Istio 注入才可以接收到來自 Pilot 有關 Virtual Service 和 Destination Rule 的配置資訊,才可以保證流量接管生效。

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: client-deployment
  name: client-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: client-deployment
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: client-deployment
    spec:
      containers:
        - image: 'busybox:latest'
          name: client-deployment
          command: [ "/bin/sh", "-c", "sleep 3600"]      

wget -q -O - web-service

#### Vistrual Service 除了權重之外,還有條件比對

很多場景下,需要針對不同的使用者已提供個性化的服務等(提前内測新版本),例如針對地理位置、是否為VIP等等,那就需要對 httpd 流量進行識别比對。

####

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-vs
spec:
  hosts:
  - web-service
  http:
  - match:
    - headers:
        end-user:
          exact: carryyip
      uri:
        prefix: "/health"
      ignoreUriCase: true
    route:
      - destination:
          host: httpd-service
  - route:
    - destination:
        host: nginx-service      

基于 HttpMatchRequest,路由規則從上到下進行優先級排序,在生産環境中建議使用一個無條件的規則作為最後規則,確定流量始終會比對到最少一條規則,防止意外情況的方式。

路由規則從 match 關鍵字開始比對,可以使用精确 exact 和 字首 prefix 或者 正規表達式進行不同場景下的比對。

繼續閱讀