1

I'm fairly new to Kubernetes and I have played around with it for a few days now to get a feeling for it. Trying out to set up an Nginx Ingress controller on the google-cloud platform following this guide, I was able to set everything up as written there - no problems, I got to see the hello-app output.

However, when I tried replicating this in a slightly different way, I encountered a weird behavior that I am not able to resolve. Instead of using the image --image=gcr.io/google-samples/hello-app:1.0 (as done in the tutorial) I wanted to deploy a standard nginx container with a custom index page to see if I understood stuff correctly. As far as I can tell, all the steps should be the same except for the exposed port: While the hello-app exposes port 8080 the standard port for the nginx container is 80. So, naively, I thought exposing (i.e., creating a service) with this altered command should do the trick:

kubectl expose deployment hello-app --port=8080 --target-port=80

where instead of having target-port=8080 as for the hello-app, I put target-port=80. As far as I can tell, all other thins should stay the same, right? In any way, this does not work and when I try to access the page I get a "404 - Not Found" although the container is definitely running and serving the index page (I checked by using port forwarding from the google cloud which apparently directly makes the page accessible for dev purposes). In fact, I also tried several other combinations of ports (although I believe the above one should be the correct one) to no avail. Can anyone explain to my why the routing does not work here?

rammelmueller
  • 1,092
  • 1
  • 14
  • 29

1 Answers1

2

If you notice the tutorial inside the ingress configuration path: "/hello"

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-resource
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - host: "34.122.88.204.nip.io"
    http:
      paths:
      - pathType: Prefix
        path: "/hello"
        backend:
          service:
            name: hello-app
            port:
              number: 8080

you might have updated port number and service name config however if path /hello which means you request is going to Nginx container but not able to file the page hello.html so it's giving you 404.

You hit endpoint IP/hello (Goes to Nginx ingress controller)--> checked for path /hello and forwarded request to service --> hello-app (service forwarded request to PODs) --> Nginx POD (it doesn't have anything at path /hello so 404)

404 written by Nginx side, in your case either it will be Nginx ingress controller or else container(POD) itself.

So try you ingress config without setting path path: "/" and hit the endpoint you might see the output from Nginx.

Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
  • Thank you very much, that was it. But I don't quite understand what's going on here. I thought, the endpoint `/hello` that we reach points to the root enpoint of the pod, i.e. the index page of the nginx server? Why is this not the case? – rammelmueller Mar 24 '22 at 09:18
  • in annotation if you will add this `nginx.ingress.kubernetes.io/rewrite-target: /$2` you might hit to root of container while in the domain you will be firing `IP/hello` – Harsh Manvar Mar 24 '22 at 09:20
  • read more about path re-writing at : https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite-target or else you can also check `app-root` – Harsh Manvar Mar 24 '22 at 09:20
  • so, to understand this correctly: the endpoint is also sent to the pod that I'm targeting? So, if I'm setting up an ingress rule that points from `IP/hello` to some pod, the pod will be contacted also at the endpoint `/hello`? – rammelmueller Mar 24 '22 at 09:23
  • 1
    yes, unless you specify any logic or rewriting path in `annotation` or `Type` – Harsh Manvar Mar 24 '22 at 09:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243276/discussion-between-harsh-manvar-and-rammelmueller). – Harsh Manvar Mar 24 '22 at 09:29