使用 containous/whoami 作为 deploy ,这个镜像可以方便的看到 headers
Hostname: whoami-f449bb88d-9lldn
IP: 127.0.0.1
IP: ::1
IP: 10.42.0.139
IP: fe80::389a:bcff:fe2b:d62d
RemoteAddr: 192.168.194.183:43655
GET / HTTP/1.1
Host: 192.168.194.183:30087
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Upgrade-Insecure-Requests: 1
DestinationRule
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: who-desrule
namespace: who
spec:
host: whoamisvc.who.svc.cluster.local
subsets:
- name: v1
labels:
version: v1
Gateway
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: who-gateway
namespace: who
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*.com"
VirtualService
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: who-default
namespace: who
spec:
hosts:
- "who.lizhe.com"
gateways:
- who-gateway
http:
- route:
- destination:
host: whoamisvc.who.svc.cluster.local
subset: v1
在没开始加header之前我们看一下
然后我们修改 VirtualService来添加一个header
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: who-default
namespace: who
spec:
hosts:
- "who.lizhe.com"
gateways:
- who-gateway
http:
- headers:
request:
add:
lizhetoken: test123456789
route:
- destination:
host: whoamisvc.who.svc.cluster.local
subset: v1
不过如果你在请求中提前设置了 header,那么add动作将 不会覆盖 你设置的内容
curl who.lizhe.com -H lizhetoken:9999999
此时需要将 add改为 set
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: who-default
namespace: who
spec:
hosts:
- "who.lizhe.com"
gateways:
- who-gateway
http:
- headers:
request:
set:
lizhetoken: test123456789
route:
- destination:
host: whoamisvc.who.svc.cluster.local
subset: v1
而且 如果 header key 不存在, set 也会帮你添加