skip to Main Content

In K8s setup with Nginx-ingress-controller, can we use both the ingress LB IP and the domain name together to access an application? I have configured an ingress resource using a dummy domain name with "example.local". This works fine and when I access the app using this domain name. However when I access it using the corresponding LB IP, it returns 404 page not found.

~]# kubectl describe ingress 
Name:             hello-world-ing
Labels:           <none>
Namespace:        default
Address:          192.168.122.241
Ingress Class:    <none>
Default backend:  <default>
Rules:
  Host           Path  Backends
  ----           ----  --------
  example.local  
                 /   apache:80 (10.32.0.4:80)
Annotations:     kubernetes.io/ingress.class: nginx
                 nginx.ingress.kubernetes.io/rewrite-target: /
Events:
  Type    Reason  Age                  From                      Message
  ----    ------  ----                 ----                      -------
  Normal  Sync    2m8s (x10 over 83m)  nginx-ingress-controller  Scheduled for sync

Now when I access the backend application using the domain name, it works fine as expected. However, if I use the IP "192.168.122.241", it returns 404 page not found.

2

Answers


  1. if I use the IP "192.168.122.241", it returns 404 page not found.

    Answering your above question, You have to remove the host section from the ingress YAML configuration.

    As you have mentioned the example.local as host Nginx ingress will try to find the path however you are trying to access it directly using the IP it won’t be to resolve it and throw 404 not found error.

    Try without using the Host

    Example:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: example-ingress
    spec:
      rules:
      - http:
          paths:
            - path: /apple
              backend:
                serviceName: apple-service
                servicePort: 5678
            - path: /banana
              backend:
                serviceName: banana-service
                servicePort: 5678
    

    You can read more about the solution on official doc : https://kubernetes.io/docs/concepts/services-networking/ingress/#single-service-ingress

    it would be always better to use the Hostname for routing however if it’s your requirement you can use the reserved external IP with ingress.

    Login or Signup to reply.
  2. I believe this is happening because the ingress only has a rule for the hostname and not for an IP address. Perhaps, if you add another ingress rule that forwards traffic based on the IP address, you will be able to route based on IP address as well.

    However, I HIGLY DISCOURAGE YOU from doing this. This is because IP address assignments in a cluster are almost always changing, and this will make your ingress rules incorrect when IP address of your Ingress changes, so stay away from using such a configuration. But for the sake of learning you can add it to get your routing configured.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search