首页 > 解决方案 > Kubernetes nginx 入口未访问外部 API

问题描述

我想从 kubernetes 中的服务(site-api)访问外部 API,这个外部 API 只能通过 IP 白名单访问,所以我给了我的入口 IP(外部 IP),这个 ip 位于 kubernetes 仪表板中(发现和负载平衡/入口),但我仍然被拒绝访问,我必须提供的 ip 是否正确?它在入口 yaml 中缺少一些设置?

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: site-name
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/backend-protocol: HTTPS
    nginx.ingress.kubernetes.io/proxy-body-size: "15m"
spec:
  tls:
  - hosts:
      - site-name
    secretName: aks-ingress-tls
  rules:
  - host: site-name
    http:
      paths:
      - path: /?(.*) 
        backend:
          serviceName: site-web
          servicePort: 443
      - path: /message/?(.*) 
        backend:
          serviceName: site-message
          servicePort: 3001
      - path: /api/?(.*) 
        backend:
          serviceName: site-api
          servicePort: 8443   

标签: javaazurekubernetesdevops

解决方案


• 发现和负载平衡/入口仪表板中显示的 IP 不是要列入白名单的 IP 地址。相反,尝试访问外部 IP 的服务对象 (site-api) 将暴露部署以及需要列入白名单的外部 IP。

• 因此,首先创建一个公开站点api 部署的服务对象,并显示有关为公开部署而创建的该服务对象的信息。

• 一次,我们得到服务对象的简明信息,得到服务对象的详细信息,这将显示站点API 试图访问的外部IP 的详细信息。

• 因此,外部IP 地址将被此服务对象公开,需要将其列入白名单。此外,记下通过端口和节点端口访问站点 API 的端点 IP 地址及其端口。然后您可以使用显示信息中的外部IP地址和端口访问site-api服务。

• 请找到以下 Kubernetes 命令来执行上述步骤:-

kubectl expose deployment<site-api>--type=LoadBalancer --name=my-service  Service object creation for exposing the deployment

kubectl get services my-service  Display information about the service

kubectl describe services my-service  Display detailed information about the service

kubectl get pods --output=wide  Get endpoint information

curl http://<external-ip>:<port>  Access the site-api through external IP

请参阅以下链接以获取更多信息:-

https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/


推荐阅读