如何解决未找到 Kubernetes Ingress Nginx 控制器
Ingress Nginx 控制器为 React 应用程序返回 404 Not Found。我将范围缩小到 React 应用程序,因为如果我尝试点击 posts.com/posts,它实际上会返回现有帖子的 JSON 列表,但对于前端应用程序,它会一直显示 GET http://posts.com/ 404(未找到)
我查看了其他一些 stackoverflow 问题,但不幸的是无济于事。
ingress-srv.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
Metadata:
name: ingress-srv
annotations:
kubernetes.io/ingress.class: Nginx
Nginx.ingress.kubernetes.io/use-regex: "use"
spec:
rules:
- host: posts.com
http:
paths:
- path: /posts/create
pathType: Prefix
backend:
service:
name: posts-clusterip-srv
port:
number: 4000
- path: /posts
pathType: Prefix
backend:
service:
name: query-srv
port:
number: 4002
- path: /posts/?(.*)/comments
pathType: Prefix
backend:
service:
name: comments-srv
port:
number: 4001
- path: /?(.*)
pathType: Prefix
backend:
service:
name: client-srv
port:
number: 3000
client-depl.yaml
apiVersion: apps/v1
kind: Deployment
Metadata:
name: client-depl
spec:
replicas: 1
selector:
matchLabels:
app: client
template:
Metadata:
labels:
app: client
spec:
containers:
- name: client
image: brachikaa/client
imagePullPolicy: Always
---
apiVersion: v1
kind: Service
Metadata:
name: client-srv
spec:
selector:
app: client
ports:
- name: client
protocol: TCP
port: 3000
targetPort: 3000
前端Dockerfile
FROM node:alpine
ENV CI=true
workdir /app
copY package.json ./
RUN npm install
copY ./ ./
CMD ["npm","start"]
记录 Pod:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
normal Scheduled 11m default-scheduler Successfully assigned default/client-depl-f7cf996cf-cvh6m to minikube
normal Pulling 11m kubelet Pulling image "brachikaa/client"
normal Pulled 11m kubelet Successfully pulled image "brachikaa/client" in 42.832431635s
normal Created 11m kubelet Created container client
normal Started 11m kubelet Started container client
如果您需要任何其他日志,我很乐意提供。谢谢。
解决方法
在您的 yaml 中,有一个路径“/?...”用于处理查询参数,但此路径不会接收来自“/”路径的流量,因为没有前缀匹配。所以你必须创建一个带有类型前缀的路径“/”来解决这个问题。然后你可以忽略当前的“/?...”路径,因为它会匹配前缀与“/”路径。
请试试这个:
ingress-srv.yaml
__________________
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-srv
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "use"
spec:
rules:
- host: posts.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: client-srv
port:
number: 3000
- path: /posts/create
pathType: Prefix
backend:
service:
name: posts-clusterip-srv
port:
number: 4000
- path: /posts
pathType: Prefix
backend:
service:
name: query-srv
port:
number: 4002
- path: /posts/?(.*)/comments
pathType: Prefix
backend:
service:
name: comments-srv
port:
number: 4001
- path: /?(.*)
pathType: Prefix
backend:
service:
name: client-srv
port:
number: 3000
,
所以这个问题真的很愚蠢 - 我实际上设置了 nginx.ingress.kubernetes.io/use-regex: "use" 而不是 nginx.ingress.kubernetes.io/use-regex: "true" ...经过三天的查阅文档,我终于找到了。如果有人遇到类似的问题 - 就是这样。
,您必须在规范中添加以下规则
- path: /
pathType: Prefix
backend:
service:
name: client-srv
port:
number: 3000
这匹配所有路径。
参考 - https://kubernetes.io/docs/concepts/services-networking/ingress/#examples
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。