Nginx Ingress gives 404 on microk8s with working service

2/26/2022

Running microk8s v1.23.3 on Ubuntu 20.04.4 LTS. I have set up a minimal pod+service:

kubectl create deployment whoami --image=containous/whoami --namespace=default

This works as expected, curl 10.1.76.4:80 gives the proper reply from whoami. I have a service configured, see content of service-whoami.yaml:

apiVersion: v1
kind: Service
metadata:
  name: whoami
  namespace: default
spec:
  selector:
    app: whoami
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

This also works as expected, the pod can be reached through the clusterIP on curl 10.152.183.220:80. Now I want to expose the service using the ingress-whoami.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: whoami-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  defaultBackend:
    service:
      name: whoami
      port:
        number: 80
  rules:
  - http:
      paths:
      - path: /whoami
        pathType: Prefix
        backend:
          service:
            name: whoami
            port:
              number: 80

ingress addon is enabled.

microk8s is running
high-availability: no
  datastore master nodes: 127.0.0.1:19001
  datastore standby nodes: none
addons:
  enabled:
    ha-cluster           # Configure high availability on the current node
    ingress              # Ingress controller for external access

ingress seems to point to the correct pod and port. kubectl describe ingress gives

Name:             whoami-ingress
Labels:           <none>
Namespace:        default
Address:
Default backend:  whoami:80 (10.1.76.12:80)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *
              /whoami   whoami:80 (10.1.76.12:80)
Annotations:  <none>
Events:       <none>

Trying to reach the pod from outside with curl 127.0.0.1/whoami gives a 404:

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

Where did I go wrong? This setup worked a few weeks ago.

-- petwri
kubernetes
microk8s
nginx-ingress

1 Answer

2/26/2022

Ok, figured it out. I had forgotten to specify the ingress.class in the annotations-block. I updated ingress-whoami.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: whoami-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: public
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /whoami
        pathType: Prefix
        backend:
          service:
            name: whoami
            port:
              number: 80

Now everything is working.

-- petwri
Source: StackOverflow