Nginx ingress resource not working without host parameter

Cluster information:

Kubernetes version:v1.18.6
Cloud being used: (put bare-metal if not on a public cloud) - pure bare-metal
Installation method: kubeadm
Host OS: CentOS 7
CNI and version: Docker version 19.03.12
CRI and version: Calico v3.14

I have deployed nginx ingress controller, Currently I’m using nginx/nginx-ingress:1.8.0 image for controller, But I have tried multiple images (i.e. 0.27, 0.31.1 ) to resolve this but no luck.
it is working in the case where i define an host entry in the ingress resource file see example below.

[kadmin@k8s-master ~]$ cat tsting.yaml
#apiVersion: networking.k8s.io/v1beta1
#kind: Ingress
#metadata:
#  name: test-ingress
#  annotations:
#    nginx.ingress.kubernetes.io/rewrite-target: /
#spec:
#  rules:
#    backend:
#      serviceName: nginx-lb
#      servicePort: 80
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: cafe-ingress
  annotations:
    nginx.org/rewrites: "serviceName=nginx-lb rewrite=/;serviceName=http-lb rewrite=/"
spec:
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: /tea/
        backend:
          serviceName: nginx-lb
          servicePort: 80
      - path: /coffee/
        backend:
          serviceName: http-lb
          servicePort: 80
[kadmin@k8s-master ~]$
curl -Lk cafe.example.com/tea
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

But it is not working if i do not define the host entry in the resource file. see below.

[kadmin@k8s-master ~]$ cat ingress-tst.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-without-host
spec:
rules:

  • http:
    paths:
  • path: /foo
    backend:
    serviceName: nginx-lb
    servicePort: 80
  • path: /bar
    backend:
    serviceName: http-lb
    servicePort: 80
    [kadmin@k8s-master ~]$ kubectl describe ingress ingress-without-host
    Name: ingress-without-host
    Namespace: default
    Address:
    Default backend: default-http-backend:80 (<error: endpoints “default-http-backend” not found>)
    Rules:
    Host Path Backends

  •   /foo   nginx-lb:80 (172.16.140.21:80)
      /bar   http-lb:80 (172.16.230.25:80)
    

Annotations: Events:
Type Reason Age From Message


Normal CREATE 40s nginx-ingress-controller Ingress default/ingress-without-host
Warning Rejected 40s nginx-ingress-controller default/ingress-without-host was rejected: Ingress rule contains empty host
Warning Rejected 40s nginx-ingress-controller default/ingress-without-host was rejected: Ingress rule contains empty host
[kadmin@k8s-master ~]$
[11:55 AM] Pradeep Kumar M
➤ curl --header “host: abc.example.com/foo” 192.168.1.41

400 Bad Request

400 Bad Request



nginx/1.19.1

​[11:55 AM] Pradeep Kumar M
    
 ping abc.example.com


Pinging abc.example.com [192.168.1.41] with 32 bytes of data:
Reply from 192.168.1.41: bytes=32 time<1ms TTL=64
Reply from 192.168.1.41: bytes=32 time<1ms TTL=64
 
curl abc.example.com:32167
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

let me know if you need some more details to understand the issue.

Use Baremetal deployment from

https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.34.1/deploy/static/provider/baremetal/deploy.yaml

To use hostNetwork, go to the deployment section of the above yml file and add

spec:
  hostNetwork: true
  dnsPolicy: ClusterFirst
  containers:

Let us know if you are able to create ingress resource ?

Wow, That worked
Thanks Alot Ranjith… : - )