简体   繁体   English

Kubernetes(minikube) + React 前端 + .netcore api + 集群 IP 服务 + ingress + net::ERR_NAME_NOT_RESOLVED

[英]Kubernetes(minikube) + React Frontend + .netcore api + Cluster IP service + ingress + net::ERR_NAME_NOT_RESOLVED

Not able to resolve an API hosted as a ClusterIP service on Minikube when calling from the React JS frontend.从 React JS 前端调用时,无法解析在 Minikube 上托管为 ClusterIP 服务的 API。

The basic architecture of my application is as follows React --> .NET core API我的应用的基本架构如下 React --> .NET core API

Both these components are hosted as ClusterIP services.这两个组件都作为 ClusterIP 服务托管。 I have created an ingress service with http paths pointing to React component and the .NET core API.我创建了一个入口服务,其中 http 路径指向 React 组件和 .NET 核心 API。

However when I try calling it from the browser, react application renders, but the call to the API fails with net::ERR_NAME_NOT_RESOLVED但是,当我尝试从浏览器调用它时,反应应用程序会呈现,但是对 API 的调用失败并出现 net::ERR_NAME_NOT_RESOLVED

Below are the.yml files for下面是 .yml 文件


1. React application 1.反应应用

apiVersion: v1
kind: Service
metadata:
    name: frontend-clusterip
spec:
    type: ClusterIP
    ports:
    - port: 59000
        targetPort: 3000
    selector:
    app: frontend

2. .NET core API 2. .NET 核心 API

apiVersion: v1
kind: Service
metadata:
    name: backend-svc-nodeport
spec:
    type: ClusterIP
    selector:
    app: backend-svc
    ports:
    - port: 5901
        targetPort: 59001

3. ingress service 3.入口服务

apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
    name: ingress-service
    annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
    rules:
    - http:
        paths:
        - path: /?(.*)
            backend: 
            serviceName: frontend-clusterip
            servicePort: 59000
        - path: /api/?(.*)
            backend:
            serviceName: backend-svc-nodeport
            servicePort: 5901

4. frontend deployment 4.前端部署

apiVersion: apps/v1
kind: Deployment
metadata:
    name: frontend
spec:
    selector:
    matchLabels:
        app: frontend
    replicas: 1
    template:
        metadata:
        labels:
        app: frontend
    spec:
        containers:
        - name: frontend
        image: upendra409/tasks_tasks.frontend
        ports:
        - containerPort: 3000
        env:
        - name: "REACT_APP_ENVIRONMENT"
            value: "Kubernetes"
        - name: "REACT_APP_BACKEND"
            value: "http://backend-svc-nodeport"
        - name: "REACT_APP_BACKENDPORT"
            value: "5901"

This is the error I get in the browser:这是我在浏览器中遇到的错误:

xhr.js:166 GET 
http://backend-svc-nodeport:5901/api/tasks net::ERR_NAME_NOT_RESOLVED

I installed curl in the frontend container to get in the frontend pod to try to connect the backend API using the above URL, but the command doesn't work我在前端容器中安装了 curl 以进入前端 pod 以尝试使用上述 URL 连接后端 API,但该命令不起作用

C:\test\tasks [develop ≡ +1 ~6 -0 !]> kubectl exec -it frontend-655776bc6d-nlj7z --curl http://backend-svc-nodeport:5901/api/tasks

Error: unknown flag: --curl

You are getting this error from local machine because ClusterIP service is wrong type for accessing from outside of the cluster.您从本地计算机收到此错误,因为ClusterIP服务类型错误,无法从集群外部访问。 As mentioned in kubernetes documentation ClusterIP is only reachable from within the cluster.如 kubernetes 文档中所述, ClusterIP只能从集群内部访问。

Publishing Services (ServiceTypes)发布服务(ServiceTypes)

For some parts of your application (for example, frontends) you may want to expose a Service onto an external IP address, that's outside of your cluster.对于您的应用程序的某些部分(例如,前端),您可能希望将服务公开到外部 IP 地址,该地址位于您的集群之外。

Kubernetes ServiceTypes allow you to specify what kind of Service you want. Kubernetes ServiceTypes允许您指定所需的服务类型。 The default is ClusterIP .默认值为ClusterIP

Type values and their behaviors are: Type值及其行为是:

  • ClusterIP : Exposes the Service on a cluster-internal IP. ClusterIP :在集群内部 IP 上公开服务。 Choosing this value makes the Service only reachable from within the cluster.选择此值使服务只能从集群内访问。 This is the default ServiceType .这是默认的ServiceType
  • NodePort : Exposes the Service on each Node's IP at a static port (the NodePort ). NodePort :在 static 端口( NodePort )上公开每个节点的 IP 上的服务。 A ClusterIP Service, to which the NodePort Service routes, is automatically created.自动创建NodePort服务路由到的ClusterIP服务。 You'll be able to contact the NodePort Service, from outside the cluster, by requesting <NodeIP>:<NodePort> .您可以通过请求<NodeIP>:<NodePort>从集群外部联系NodePort服务。
  • LoadBalancer : Exposes the Service externally using a cloud provider's load balancer. LoadBalancer :使用云提供商的负载均衡器在外部公开服务。 NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.自动创建外部负载均衡器路由到的NodePortClusterIP服务。
  • ExternalName : Maps the Service to the contents of the externalName field (eg foo.bar.example.com ), by returning a CNAME record ExternalName :通过返回CNAME记录将服务映射到externalName字段的内容(例如foo.bar.example.com

    with its value.以其价值。 No proxying of any kind is set up.没有设置任何类型的代理。

    Note: You need CoreDNS version 1.7 or higher to use the ExternalName type.注意:您需要 CoreDNS 版本 1.7 或更高版本才能使用ExternalName类型。

I suggest using NodePort or LoadBalancer service type instead.我建议改用NodePortLoadBalancer服务类型。

Refer to above documentation links for examples.有关示例,请参阅上述文档链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在我的反应项目中 axios 获取/发布请求时出现错误 net::ERR_NAME_NOT_RESOLVED - Getting error net::ERR_NAME_NOT_RESOLVED while axios get/post request in my react project 如何删除“net::ERR_NAME_NOT_RESOLVED”? - How can I remove “net::ERR_NAME_NOT_RESOLVED”? Google Firebase httpsCallable raise net::ERR_NAME_NOT_RESOLVED - Google Firebase httpsCallable raising net::ERR_NAME_NOT_RESOLVED ERR_NAME_NOT_RESOLVED 使用 Docker 网络在后端容器和 React 容器之间进行通信 - ERR_NAME_NOT_RESOLVED using Docker network to communicate between a backend container and a React container 通过 html 脚本标签获取源代码会在某些 Android 设备上导致 net::ERR_NAME_NOT_RESOLVED - get source via html script tag causes net::ERR_NAME_NOT_RESOLVED on some Android devices 启用 Cors NetCore React 前端 - Enable Cors NetCore React frontend 对 Kubernetes Ingress 做出反应 - React on Kubernetes Ingress 为什么在 Kube.netes 集群中部署时无法通过入口在浏览器中打开 React web 应用程序 - Why can I not open a React web app in a browser through an ingress when deploying it in a Kubernetes cluster 通过 ingress-nginx 作为代理从在 localhost 上运行的 react 应用程序访问 Kubernetes 集群 - Accessing Kubernetes Cluster through ingress-nginx as proxy from react app running on localhost 在 Kubernetes Ingress 后面为静态反应应用程序提供服务 - Serving a static react application behind a Kubernetes Ingress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM