Kubernetes Istio可从浏览器访问,但不能从curl访问
所以我已经成功地部署了istio,至少我认为是这样,一切似乎都很好。我已经在Istio中部署了我的API,我可以通过浏览器访问它。我甚至可以使用postman测试我的API,但是当我试图通过curl访问我的API时,它说Kubernetes Istio可从浏览器访问,但不能从curl访问,kubernetes,kubernetes-ingress,istio,Kubernetes,Kubernetes Ingress,Istio,所以我已经成功地部署了istio,至少我认为是这样,一切似乎都很好。我已经在Istio中部署了我的API,我可以通过浏览器访问它。我甚至可以使用postman测试我的API,但是当我试图通过curl访问我的API时,它说无法解析远程名称:“API.localhost”。这是第一个危险信号,但我忽略了它。现在我正试图从我的webapp访问我的API,但Chrome的响应是net:ERR\u失败了 似乎我的服务只提供给主人,也就是我,其他什么都没有。我在互联网上似乎找不到解决这个问题的方法,所以我希
无法解析远程名称:“API.localhost”
。这是第一个危险信号,但我忽略了它。现在我正试图从我的webapp访问我的API,但Chrome的响应是net:ERR\u失败了
似乎我的服务只提供给主人,也就是我,其他什么都没有。我在互联网上似乎找不到解决这个问题的方法,所以我希望有人已经过期并且知道一个修复方法
谢谢
编辑:更多信息 我的基础设施都是本地的,Docker for Desktop with Kubernetes。我使用的Istio版本是1.5.0 网关:
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: api-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http-api
protocol: HTTP
hosts:
- "api.localhost"
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: pb-api
spec:
gateways:
- api-gateway
hosts:
- "*"
http:
- match:
- uri:
prefix: /
rewrite:
uri: /
route:
- destination:
host: pb-api
port:
number: 3001
虚拟服务:
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: api-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http-api
protocol: HTTP
hosts:
- "api.localhost"
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: pb-api
spec:
gateways:
- api-gateway
hosts:
- "*"
http:
- match:
- uri:
prefix: /
rewrite:
uri: /
route:
- destination:
host: pb-api
port:
number: 3001
当我尝试做
curl时http://api.localhost/user/me
我希望是401
,但我得到的是无法解析远程名称:“api.localhost”
,如上所述。该错误与我关闭Docker for desktop并重试时的错误相同。通过postman和浏览器,它可以正常工作,但是curl和我的react Web应用程序无法达到它。正如我在评论中提到的,curl应该是这样的
curl-v-H“host:api.localhost”istio入口网关外部ip/
您可以使用检查istio入口网关外部ip
kubectl get svc istio-ingressgateway -n istio-system
正如@sjaakvbrant所提到的
外部IP是localhost,所以我尝试了这个命令curl-v-H“host:api.localhost”localhost/user/me,它给了我401
UbuntuMinikube示例 此外,如果您希望curl api.localhost本身,那么您必须在本地配置您的主机,我不确定这在您的情况下如何工作,因为您的外部IP是localhost 但如果您愿意,您可以使用一个负载平衡器,这样您的istio入口网关将获得一个可以在etc/hosts中配置的IP Yamls
piVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: demo
spec:
selector:
matchLabels:
app: demo
replicas: 1
template:
metadata:
labels:
app: demo
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello nginx1 > /usr/share/nginx/html/index.html"]
---
apiVersion: v1
kind: Service
metadata:
name: demo
namespace: demo
labels:
app: demo
spec:
ports:
- name: http-demo
port: 80
protocol: TCP
selector:
app: demo
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: demo-gw
namespace: demo
spec:
selector:
istio: ingressgateway
servers:
- port:
name: http
number: 80
protocol: HTTP
hosts:
- "example.com"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: demo-vs
namespace: demo
spec:
gateways:
- demo-gw
hosts:
- "example.com"
http:
- match:
- uri:
prefix: /
rewrite:
uri: /
route:
- destination:
host: demo.demo.svc.cluster.local
port:
number: 80
127.0.0.1 localhost
10.101.143.xxx example.com
etc/hosts
piVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: demo
spec:
selector:
matchLabels:
app: demo
replicas: 1
template:
metadata:
labels:
app: demo
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello nginx1 > /usr/share/nginx/html/index.html"]
---
apiVersion: v1
kind: Service
metadata:
name: demo
namespace: demo
labels:
app: demo
spec:
ports:
- name: http-demo
port: 80
protocol: TCP
selector:
app: demo
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: demo-gw
namespace: demo
spec:
selector:
istio: ingressgateway
servers:
- port:
name: http
number: 80
protocol: HTTP
hosts:
- "example.com"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: demo-vs
namespace: demo
spec:
gateways:
- demo-gw
hosts:
- "example.com"
http:
- match:
- uri:
prefix: /
rewrite:
uri: /
route:
- destination:
host: demo.demo.svc.cluster.local
port:
number: 80
127.0.0.1 localhost
10.101.143.xxx example.com
测试
curl -v -H "host: example.com" http://10.101.143.xxx/
< HTTP/1.1 200 OK
curl -v example.com
< HTTP/1.1 200 OK
curl-v-H“主机:example.com”http://10.101.143.xxx/
希望您觉得这很有用。您的基础架构是什么(云,on-prem)?istio的版本是什么?您能添加网关和虚拟服务的YAML吗?你能补充一下你是如何使用curl的吗?@jt97我用额外的信息编辑了我的帖子。已经谢谢你调查了!istio中的所有内容都应该通过入口网关,因此,如果您使用
kubectl get svc istio ingresgateway-n istio system
进行检查,您应该拥有一个外部ip,例如,如果状态为挂起,那么您应该安装一些负载平衡器。您是否可以尝试使用thencurl-v-H“host:api.localhost”istio ingressgateway外部ip/
并检查它是否工作?看看我的例子中的测试。如果你认为有帮助的话,我可以给你看一个minikube的例子。外部IP是localhost
,所以我尝试了这个命令curl-v-H“host:api.localhost”localhost/user/me
,它给了我401
。所以这似乎是可行的,我想我会在webapp的请求中添加此标题以使其正常工作。如果您愿意,可以将网关主机从“api.localhost”更改为通配符,“*”
,并且在没有标题的情况下应该可以正常工作。感谢您的帮助。因为我无法使用React更改请求中的主机头(主机头始终为“localhost”)。浏览器不允许您这样做。你有没有关于如何在webapp中实现这一点的建议?我想到的唯一一件事是将主机从api.localhost更改为通配符,然后你就不需要使用头了。值得在您的案例中尝试它的工作方式。将其更改为通配符,但由于我的客户端/ui也使用通配符,因此会发生冲突,并且无法访问我的客户端,因为当我尝试访问我的客户端时,api返回了404。我将api保留在通配符上,并将重写从/api/
添加到/
。现在我可以访问我的api了,所以我将坚持这一点,我只是更喜欢api.localhost
,但我想它对浏览器不起作用。