Istio可通过浏览器访问,但不能通过curl访问

时间:2020-04-29 19:23:28

标签: kubernetes kubernetes-ingress istio

所以我已经成功部署了istio,至少我认为如此,一切似乎都正常。我已经在Istio中部署了API,并且可以通过浏览器访问它。我什至可以使用邮递员测试我的API,但是当我尝试通过curl访问我的API时,它显示The remote name could not be resolved: 'api.localhost'。那是第一个危险信号,但我忽略了它。现在,我正在尝试从Web应用程序访问API,但Chrome会回复net:ERR_FAILED

似乎我的服务仅适用于主机,也就是我,仅此而已。我似乎在互联网上找不到解决方案,所以我希望有人能有经验并知道解决方法。

谢谢!


编辑:更多信息

我的基础架构全部是本地的,带Kubernetes的桌面版Docker 。我正在使用的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并重试时相同。通过邮递员和浏览器,它可以正常工作,但是curl和我的react webapp无法访问它。

1 个答案:

答案 0 :(得分:1)

正如我在评论中提到的那样,卷发应该看起来像这样

curl -v -H“主机: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” localhost / user / me,这给了我


Ubuntu minikube示例

另外,如果您想卷曲api.localhost本身,那么您将不得不在本地配置主机,由于您的外部IP是localhost,我不确定这在您的情况下如何工作。

但是,如果需要,您可以使用metallb(它是一个负载平衡器),以便您的istio-ingressgateway将获得可以在etc / hosts中配置的IP。

纱线

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

etc / hosts

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

希望您觉得这有用。