0

I am trying to create Ingress for my kubernetes service. I am using minikube running on WSL2. Here are my deployment, service and ingress settings

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

Service is working correctly, because I get answers from it when calling it directly.

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

the answer is [] (right now there are no data in my db).

But when I am trying to call it using host or after forwarding localhost 8080 port to ingress-nginx port, I am getting 404 error from NGINX.

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

the answer is

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

It's my first time working with kubernetes, so it will be great if you find some mistakes in my configuration files. I suppose, I have some troubles with nginx configuration, but I don't know how to fix them.

SkyWaet
  • 3
  • 1
  • While Stack Overflow does permit certain questions about Kubernetes, we require that they (like all questions asked here) be specifically related to programming. This question does not appear to be specifically related to programming, but ingress- and networking-related, which makes it off-topic here. You might be able to ask questions like this one on [sf] or [DevOps](https://devops.stackexchange.com/). – Turing85 May 13 '23 at 19:30

2 Answers2

0

You need to include a -H Host header.

Your ingress definition, which the ingress controller uses:

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

Doesn't have a block for localhost so when you do

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

Nginx looks for an ingress that has a block for localhost, doesn't find one, and goes to the default 404 backend.

Try:

curl --location 'http://localhost:8080/users' -H "Host: kuberdemo.info"
Blender Fox
  • 4,442
  • 2
  • 17
  • 30
0

You missed the ingress class name,add ingressclassname as shown below:

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
jmvcollaborator
  • 2,141
  • 1
  • 6
  • 17
  • That still wouldn't work because they are not providing a host header. So nginx can't even match the rule in the ingress. – Blender Fox May 14 '23 at 10:56