istio部署demoapp应用 (七)修改标头

Headers Operation

  • Headers为Istio提供了操作HTTP Header的途径,用于操作HTTP 请求报文中Request或者Response标头

    • headers字段支持request和response两个内嵌字段

      • request:操纵发送给Destination的请求报文中的标头

      • response:操纵发送给客户端的响应报文中的标头

    • 以上两个对应的类型都是 HeaderOperations 类型,都支持使用set、add、remove字段操纵指定的标头

      • set:使用map上的Key和Value覆盖Request或者Response中对应的Header

      • add:追加map上的Key和Value到原有 Header

      • remove:删除在列表中指定的Header

virtualservice-demoapp.yaml

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: demoapp
spec:
  hosts:
  - demoapp
  http:
  - name: canary
    match:
    - headers:
        x-canary: 
          exact: "true"
    route:
    - destination:
        host: demoapp
        subset: v11
      headers:
        request:
          set:
            User-Agent: Chrome
        response:
          add:
            x-canary: "true"
  - name: default
    headers:
      response:
        add:
          X-Envoy: test
    route:
    - destination:
        host: demoapp
        subset: v10

更新demoapp vs

# kubectl apply -f virtualservice-demoapp.yaml virtualservice.networking.istio.io/demoapp configured

访问demoapp

访问v10子集

root@client # curl   demoapp:8080
iKubernetes demoapp v1.0 !! ClientIP: 127.0.0.6, ServerName: demoappv10-78b6586d58-kdv4m, ServerIP: 172.20.154.195!
root@client # curl  demoapp:8080/user-agent
User-Agent: curl/7.67.0
root@client # curl -I  demoapp:8080
HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
content-length: 116
server: envoy
date: Wed, 26 Oct 2022 08:51:25 GMT
x-envoy-upstream-service-time: 2
x-envoy: test

访问v11子集

root@client # curl  -H "x-canary: true" demoapp:8080
iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-78bf898c74-hckjf, ServerIP: 172.20.89.189!
root@client # curl  -H "x-canary: true" demoapp:8080/user-agent
User-Agent: Chrome
root@client # curl -I -H "x-canary: true" demoapp:8080
HTTP/1.1 200 OK
content-type: text/html; charset=utf-8
content-length: 116
server: envoy
date: Wed, 26 Oct 2022 08:52:32 GMT
x-envoy-upstream-service-time: 114
x-canary: true

参考文档

Headers:https://istio.io/latest/zh/docs/reference/config/networking/virtual-service/#Headers

Headers.HeaderOperations:https://istio.io/latest/zh/docs/reference/config/networking/virtual-service/#Headers-HeaderOperations

posted @ 2022-10-26 16:55  小吉猫  阅读(144)  评论(0编辑  收藏  举报