我设法在根路径中运行一个grpc服务。但是我试图通过在虚拟服务中添加自定义路径路由来添加更多的grpc服务,但这是行不通的。任何帮助将不胜感激。
这是网关:
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: my-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
我将此虚拟服务路由到仅一个grpc服务,并且工作正常
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-virtual-svc
spec:
hosts:
- "*"
gateways:
- my-gateway
http:
- name: "my-grpc-1"
match:
- uri:
prefix: "/"
route:
- destination:
port:
number: 9090
host: my-grpc-1-svc
但我想尝试以下类似方法,但是不能正常工作
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-virtual-svc
spec:
hosts:
- "*"
gateways:
- my-gateway
http:
- name: "my-grpc-1"
match:
- uri:
prefix: "/my-grpc-1"
route:
- destination:
port:
number: 9090
host: my-grpc-1-svc
- name: "my-grpc-2"
match:
- uri:
prefix: "/my-grpc-2"
route:
- destination:
port:
number: 9090
host: my-grpc-2-svc
最佳答案
那可能不起作用,因为您的应用程序监听/
并使用第一个虚拟服务(有效),istio将请求发送到/
,而第二个虚拟服务却没有发生。
答案是将rewrite添加到您的第二个虚拟服务中。
HTTPRewrite can be used to rewrite specific parts of a HTTP request before forwarding the request to the destination. Rewrite primitive can be used only with HTTPRouteDestination. The following example demonstrates how to rewrite the URL prefix for api call (/ratings) to ratings service before making the actual API call.
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
所以看起来像这样apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-virtual-svc
spec:
hosts:
- "*"
gateways:
- my-gateway
http:
- name: "my-grpc-1"
match:
- uri:
prefix: "/my-grpc-1"
rewrite:
uri: "/"
route:
- destination:
port:
number: 9090
host: my-grpc-1-svc
- name: "my-grpc-2"
match:
- uri:
prefix: "/my-grpc-2"
rewrite:
uri: "/"
route:
- destination:
port:
number: 9090
host: my-grpc-2-svc
有关它的相关文档是here。我认为您可以尝试使用此github issue comment中提到的gRPC服务的FQN来完成此操作
另一个想法是像提到的here一样使用nginx。
关于kubernetes - 如何基于Istio(Kubernetes)中的路径路由多个gRPC服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62459006/