kubernetes Nginx入口控制器返回404

5w9g7ksd  于 2023-05-16  发布在  Kubernetes
关注(0)|答案(2)|浏览(179)

我正在尝试为我的kubernetes服务创建Ingress。我使用的是在WSL 2上运行的minikube。以下是我的部署、服务和入口设置

apiVersion: apps/v1
kind: Deployment
metadata:
    name: {{.Chart.Name}}-backend
    labels:
        app: {{.Chart.Name}}-backend
spec:
    replicas: {{ .Values.replicas }}
    selector:
        matchLabels:
            service: backend
            app: {{.Chart.Name}}-backend
    template:
        metadata:
            labels:
                service: backend
                app: {{.Chart.Name}}-backend
        spec:  
            containers:
            -   name: kuberdemo-app
                image: {{ .Values.image }}
                ports:
                -   containerPort: 8091
                    targetPort: 8091
                    protocol: TCP
                    name: app-port
                envFrom:
                - configMapRef:
                    name: kuberdemo-config
                startupProbe:
                    httpGet:
                        path: /actuator/health/liveness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                readinessProbe:
                    httpGet:
                        path: /actuator/health/readiness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                livenessProbe:
                    httpGet:
                        path: /actuator/health/liveness
                        port: 14000
                    failureTreshold: 2
                    periodSeconds: 10
                resources:
                    limits:
                        cpu: "2000m"
                        memory: "2Gi"
                    requests:
                        cpu: "2000m"
                        memory: "2Gi"
apiVersion: v1
kind: Service
metadata:
  name: kuberdemo-service
spec:
  type: ClusterIP
  selector:
    app: {{.Chart.Name}}-backend
  ports:
    - protocol: TCP
      port: 8090
      targetPort: 8091
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kuberdemo-ingress
spec:
  rules:
    - host: kuberdemo.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kuberdemo-service
                port:
                  number: 8090

服务工作正常,因为我在直接调用它时从它那里得到答案。

kubectl port-forward service/kuberdemo-service -n kuberdemo 8080:8090
curl --location 'http://localhost:8080/users'

答案是[](现在我的数据库中没有数据)。
但是当我试图使用host调用它或者在将localhost 8080端口转发到ingress-nginx端口之后,我从NGINX得到404错误。

kubectl port-forward -n ingress-nginx ingress-nginx-controller-6cc5ccb977-zzbcb  8080:80
curl --location 'http://localhost:8080/users'

答案是

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

这是我第一次使用kubernetes,所以如果你在我的配置文件中发现一些错误,那就太好了。我想,我在nginx配置方面遇到了一些问题,但我不知道如何解决它们。

f8rj6qna

f8rj6qna1#

你需要包含一个-H Host头。
ingress控制器使用的ingress定义:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kuberdemo-ingress
spec:
  rules:
    - host: kuberdemo.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kuberdemo-service
                port:
                  number: 8090

没有localhost的块,所以当您这样做时

curl --location 'http://localhost:8080/users'

Nginx查找一个有localhost块的入口,但没有找到,然后转到默认的404后端。
尝试:

curl --location 'http://localhost:8080/users' -H "Host: kuberdemo.info"`
8zzbczxx

8zzbczxx2#

您遗漏了ingress类名称,请添加ingressclassname,如下所示:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kuberdemo-ingress
spec:
  ingressClassName: nginx
  rules:
    - host: kuberdemo.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: kuberdemo-service
                port:
                  number: 8090

相关问题