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… : - )

@csehra, can you please let me know if you used any proxy server? if not how did you exposed the service for external usage?

@Muralidhar_T
no we dind’t use any proxy server, we are using ingress controller and 1 Load balancer VIP to expose our service.

@csehra Thanks for responding…

I have similar case, but I want to expose kubernetes dashboad (which is in kubernetes-dashboard namespace) through ingress…

I am able to to expose an nginx pod in default namespace. any clue?

You can create an ingress resource to expose your kube dashboard service. Ingress works for every namespace. Let me know what error are you getting while exposing dashboard through ingress.

@csehra, Thanks for responding.

I have created ingress-controller as daemon as it will in all worker nodes - and also helping me in connecting with worker IP directly without LB.

Issue here is - if I apply ingress route in default namespace and my pod and svc is in different namespace i am not able to access the app using ingress. ( I also created a svc with ExternalName type in default namespace to connect with kubernetes-dashboard pod/svc

Below is the describe for ingress:

dockeras@ubuntu3:~$ kubectl describe ing nginx-ingress
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
Name:             nginx-ingress
Namespace:        default
Address:          192.168.1.31,192.168.1.32
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *
              /nginx       nginx-deploy-main:80   )
              /foo         kubernetes-dashboard:443 (<error: endpoints "kubernetes-dashboard" not found>)
              /dashboard   kdash-in-ns:443 (<error: endpoints "kdash-in-ns" not found>)
Annotations:  kubernetes.io/ingress.class: nginx
              nginx.ingress.kubernetes.io/rewrite-target: /$2
Events:
  Type    Reason  Age    From                      Message
  ----    ------  ----   ----                      -------
  Normal  CREATE  4m40s  nginx-ingress-controller  Ingress default/nginx-ingress

ExternalName type svc :

kind: Service
apiVersion: v1
metadata:
  name: kdash-in-ns
  namespace: default
spec:
  type: ExternalName
  externalName: kubernetes-dashboard.kubernetes-dashboard.svc.cluster.local
  ports:
  - name: https
    port: 443

The above didn’t help.

I have created separate query also - Kubernetes: Route Kubernetes dashboard through Ingress with out host and without proxy