Kubernetes Ingress Nginx控制器未找到

l7wslrjt  于 2023-11-17  发布在  Kubernetes
关注(0)|答案(3)|浏览(121)

Ingress Nginx控制器为React应用程序返回404 Not Found。我将其缩小到React应用程序,因为如果我尝试点击posts.com/posts,它实际上会返回现有帖子的JSON列表,但对于前端应用程序,它一直显示GET http://posts.com/ 404(Not Found)
我看了一些其他的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: br/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 "br/client"
  Normal  Pulled     11m   kubelet            Successfully pulled image "br/client" in 42.832431635s
  Normal  Created    11m   kubelet            Created container client
  Normal  Started    11m   kubelet            Started container client


如果你需要任何其他日志,我很乐意提供他们.谢谢.

46scxncf

46scxncf1#

在您的yamls中,有一个路径“/?...”用于处理查询参数,但该路径将不会接收来自“/”路径的流量,因为没有前缀匹配。因此,您必须创建一个具有前缀类型的路径“/”来解决问题。然后,您可以忽略当前的“/?...”路径,因为它将匹配前缀与“/”路径。
请试试这个:

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

字符串

whlutmcx

whlutmcx2#

所以这个问题是一个非常愚蠢的问题-我实际上设置了nginx.ingress.kubernetes.io/use-regex:“use”而不是nginx.ingress.kubernetes.io/use-regex:“true”.经过三天的文档检查,我终于找到了它。如果有人遇到类似的问题-你有它。

x9ybnkn6

x9ybnkn63#

您必须在规范中添加以下规则

- path: /
            pathType: Prefix
            backend:
              service:
                name: client-srv
                port:
                  number: 3000

字符串
这匹配所有路径。
参考-https://kubernetes.io/docs/concepts/services-networking/ingress/#examples

相关问题