我用 3 个简单的 nginx pod 复制了您的问题,问题是我无法仅使用 1 个标头和 3 个 uri。
所以我换了一种方式,每个匹配的虚拟服务都有自己的标头和 uri。
查看以下示例。
我们有 3 个 pod -> 3 个服务 -> virtual service -> gateway -> ingressgateway
部署 1
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx1
spec:
selector:
matchLabels:
run: nginx1
replicas: 1
template:
metadata:
labels:
run: nginx1
app: frontend
spec:
containers:
- name: nginx1
image: nginx
ports:
- containerPort: 80
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello nginx1 > /usr/share/nginx/html/index.html"]
部署 2
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx2
spec:
selector:
matchLabels:
run: nginx2
replicas: 1
template:
metadata:
labels:
run: nginx2
app: frontend2
spec:
containers:
- name: nginx2
image: nginx
ports:
- containerPort: 80
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello nginx2 > /usr/share/nginx/html/index.html"]
部署 3
piVersion: apps/v1
kind: Deployment
metadata:
name: nginx3
spec:
selector:
matchLabels:
run: nginx3
replicas: 1
template:
metadata:
labels:
run: nginx3
app: frontend3
spec:
containers:
- name: nginx3
image: nginx
ports:
- containerPort: 80
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello nginx3 > /usr/share/nginx/html/index.html"]
服务 1
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: frontend
spec:
ports:
- name: http
port: 80
protocol: TCP
selector:
app: frontend
服务 2
apiVersion: v1
kind: Service
metadata:
name: nginx2
labels:
app: frontend2
spec:
ports:
- port: 80
protocol: TCP
selector:
app: frontend2
服务 3
apiVersion: v1
kind: Service
metadata:
name: nginx3
labels:
app: frontend3
spec:
ports:
- port: 80
protocol: TCP
selector:
app: frontend3
虚拟服务
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: nginxvirt
spec:
gateways:
- mesh # traffic inside cluster
- comp-ingress-gateway # traffic from outside cluster
hosts:
- nginx.default.svc.cluster.local
- nginx.com # outside cluster
- nginx3.default.svc.cluster.local
- nginx2.default.svc.cluster.local
http:
- name: a
match:
- uri:
prefix: /wagholi
headers:
location:
exact: pune
rewrite:
uri: /
route:
- destination:
host: nginx.default.svc.cluster.local
port:
number: 80
- name: b
match:
- uri:
prefix: /yerwada
headers:
location:
exact: pune
rewrite:
uri: /
route:
- destination:
host: nginx2.default.svc.cluster.local
port:
number: 80
- name: c
match:
- uri:
prefix: /hadasapar
headers:
location:
exact: pune
rewrite:
uri: /
route:
- destination:
host: nginx3.default.svc.cluster.local
port:
number: 80
网关
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: comp-ingress-gateway
spec:
selector:
istio: ingressgateway
servers:
- hosts:
- '*'
port:
name: http
number: 80
protocol: HTTP
- hosts:
- '*'
port:
name: https
number: 443
protocol: HTTPS
tls:
mode: SIMPLE
privateKey: /etc/istio/ingressgateway-certs/tls.key
serverCertificate: /etc/istio/ingressgateway-certs/tls.crt
一些用于测试内部流量的 ubuntu pod
apiVersion: v1
kind: Pod
metadata:
name: ubu1
spec:
containers:
- name: ubu1
image: ubuntu
command: ["/bin/sh"]
args: ["-c", "apt-get update && apt-get install curl -y && sleep 3000"]
结果:
内部:
root@ubu1:/# curl -H "location: pune" nginx/wagholi
Hello nginx1
root@ubu1:/# curl -H "location: pune" nginx/hadasapar
Hello nginx3
root@ubu1:/# curl -H "location: pune" nginx/yerwada
Hello nginx2
外面:
curl -H "location: pune" -H "host: nginx.com" ingress_gateway_ip/hadasapar
Hello nginx3
curl -H "location: pune" -H "host: nginx.com" ingress_gateway_ip/wagholi
Hello nginx1
curl -H "location: pune" -H "host: nginx.com" ingress_gateway_ip/yerwada
Hello nginx2
编辑
如何找到 ingress_gateway_ip?
你可以使用
kubectl get svc istio-ingressgateway -n istio-system
它是istio-ingressgateway EXTERNAL-IP
如果设置了 EXTERNAL-IP 值,则您的环境具有可用于入口网关的外部负载平衡器。如果 EXTERNAL-IP 值为(或永久),则您的环境不为入口网关提供外部负载均衡器。在这种情况下,您可以使用服务的node port 访问网关。
我可以为每个服务做不同的 Ingress,然后映射到 VirtualService。
我不确定,但我认为这是可能的。检查以下链接
希望对你有帮助。如果您还有其他问题,请告诉我。