浏览器可以访问Istio,但从curl无法访问。

问题描述 投票:0回答:1

所以我已经成功地部署了istio,至少我是这么认为的,一切似乎都很好。我已经在Istio中部署了我的API,我可以通过我的浏览器访问它。我甚至可以使用postman测试我的API,但当我试图通过curl访问我的API时,它说 The remote name could not be resolved: 'api.localhost'. 这是第一个危险信号,但我忽略了它。现在我试图从我的web应用中访问我的API,但Chrome浏览器的响应是 net:ERR_FAILED.

似乎我的服务只对主机,也就是我,而不是其他。我似乎找不到解决这个问题的办法,在互联网上,所以我希望有人有experience和知道一个修复。

谢谢!


EDIT: 更多信息

我的基础设施都是本地的。桌面上的Docker与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

当我想做 curl http://api.localhost/user/me 我希望 401但我得到的却是 The remote name could not be resolved: 'api.localhost' 如上所述。这个错误和我关闭桌面的Docker再试的时候一样。通过postman和浏览器可以正常工作,但curl和我的react webapp无法到达它。

kubernetes kubernetes-ingress istio
1个回答
1
投票

正如我在评论中提到的,curl应该是这样的

curl -v -H "host: api.localhost" istio-ingressgateway-external-ip

你可以用以下方法检查istio-ingressgateway-external ip

kubectl get svc istio-ingressgateway -n istio-system

正如@SjaakvBrabant所言

外部IP是localhost,所以我试了这个命令curl -v -H "host: api.localhost" localhostuserme,得到的结果是401。


Ubuntu minikube 示例

此外,如果你想自己来 curl api.localhost,那么你就必须在本地配置你的主机,我不知道这在你的情况下如何工作,因为你的外部 IP 是 localhost。

但如果你想,你可以使用 金属b 这是一个负载均衡器,所以你的istio-ingressgateway会得到一个IP,可以在etchosts中配置。

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

etchosts

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

希望你觉得有用。

© www.soinside.com 2019 - 2024. All rights reserved.