问题描述
我们正在使用istio作为服务网格来保护集群。我们有几个通过入口网关公开的Web应用程序,如下所示:ingress-gateway-id:80 / app1 /,ingress-gateway-id:80 / app2 /和ingress-gateway-id:80 / app3/。
我们有一个网关,用于在端口80上路由入口网关的流量。
对于每个应用程序,我们创建一个虚拟服务,将流量从(例如)入口网关ID:80 / app1 / app1-api-uri /路由到app1-service / app1-api-uri / >
我们当前面临的主要问题是某些应用程序仅通过/(例如)app2-service /工作,这迫使我们允许/通过虚拟服务,并限制入口网关仅允许一个应用程序通过入口网关(没有在标头中指定主机,因为我们所有的应用程序都是Web应用程序,因此在我们的用例中可以通过浏览器访问)。
我的问题是如何允许多个应用程序通过我的入口网关(例如在同一端口80上)访问/通过我的入口网关,而无需处理来自客户端(在我们的情况下是浏览器)的主机头设置?
解决方法
如果您不想将域用作虚拟服务主机,我会说这里唯一的选择是
- 在虚拟服务中使用重写。
- 使用自定义标题
有一个有关从istio文档进行重写的示例。
HTTPRewrite
HTTPRewrite可用于在将请求转发到目标之前重写HTTP请求的特定部分。重写原语只能与HTTPRouteDestination一起使用。以下示例演示了在进行实际的API调用之前,如何将api调用(/ ratings)的URL前缀重写为Ratings服务。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: ratings-route
spec:
hosts:
- ratings.prod.svc.cluster.local
http:
- match:
- uri:
prefix: /ratings
rewrite:
uri: /v1/bookRatings
route:
- destination:
host: ratings.prod.svc.cluster.local
subset: v1
有两个nginx部署的示例,两个部署都在/
上进行。
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"]
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx2
spec:
selector:
matchLabels:
run: nginx2
replicas: 1
template:
metadata:
labels:
run: nginx2
app: frontend
spec:
containers:
- name: nginx2
image: nginx
ports:
- containerPort: 80
lifecycle:
postStart:
exec:
command: ["/bin/sh","echo Hello nginx2 > /usr/share/nginx/html/index.html"]
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: frontend
spec:
ports:
- port: 80
protocol: TCP
selector:
app: frontend
---
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
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: nginxvirt
spec:
gateways:
- comp-ingress-gateway
hosts:
- '*'
http:
- name: match
match:
- uri:
prefix: /a
rewrite:
uri: /
route:
- destination:
host: nginx.default.svc.cluster.local
subset: v1
port:
number: 80
- name: default
match:
- uri:
prefix: /b
rewrite:
uri: /
route:
- destination:
host: nginx.default.svc.cluster.local
subset: v2
port:
number: 80
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: nginxdest
spec:
host: nginx.default.svc.cluster.local
subsets:
- name: v1
labels:
run: nginx1
- name: v2
labels:
run: nginx2
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
还有卷曲测试。
curl -v xx.xxx.xxx.x/a
HTTP/1.1 200 OK
Hello nginx1
curl -v xx.xxx.xxx.x/b
HTTP/1.1 200 OK
Hello nginx2
istio文档中有一个有关自定义标头的example。