[英]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 文件
apiVersion: v1
kind: Service
metadata:
name: frontend-clusterip
spec:
type: ClusterIP
ports:
- port: 59000
targetPort: 3000
selector:
app: frontend
apiVersion: v1
kind: Service
metadata:
name: backend-svc-nodeport
spec:
type: ClusterIP
selector:
app: backend-svc
ports:
- port: 5901
targetPort: 59001
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
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. KubernetesServiceTypes
允许您指定所需的服务类型。 The default isClusterIP
.默认值为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 defaultServiceType
.这是默认的ServiceType
。NodePort
: Exposes the Service on each Node's IP at a static port (theNodePort
).NodePort
:在 static 端口(NodePort
)上公开每个节点的 IP 上的服务。 AClusterIP
Service, to which theNodePort
Service routes, is automatically created.自动创建NodePort
服务路由到的ClusterIP
服务。 You'll be able to contact theNodePort
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
andClusterIP
Services, to which the external load balancer routes, are automatically created.自动创建外部负载均衡器路由到的NodePort
和ClusterIP
服务。
ExternalName
: Maps the Service to the contents of theexternalName
field (egfoo.bar.example.com
), by returning aCNAME
recordExternalName
:通过返回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.我建议改用NodePort
或LoadBalancer
服务类型。
Refer to above documentation links for examples.有关示例,请参阅上述文档链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.