1

I installed pods and services with deployment.yml,service.yml and ingress.yml like this below.

deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apa000dep
spec:
  selector:
    matchLabels:
      app: apa000kube
  replicas : 3
  template:
    metadata:
      labels:
        app: apa000kube
    spec:
      containers:
        - name: apa000ex91
          image: httpd
          ports:
          - containerPort: 80
      

service.yml

apiVersion: v1
kind: Service
metadata: 
  name: apa000ser
spec:
  type: NodePort
  ports:
  - port: 8099
    targetPort: 80
    protocol: TCP
    nodePort: 30080
  selector:
    app: apa000kube

Both works well and, I can access pod directly localhost:30080

then i installed ingress.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: demo.localdev.me
    http:
      paths:
      - pathType: Prefix
        path: /*
        backend:
          service:
            name: apa000ser
            port:
              number: 8099

The hostname is set correctly

$kubectl get ingress 
    
NAME              CLASS    HOSTS              ADDRESS   
PORTS   AGE
example-ingress   <none>   demo.localdev.me             80      95m

However when I access http://demo.localdev.me it returns 404 error.

http://localhost:30080/ returns <span>it works!</span>

So, I guess some ingress setting is wrong. where should I check?


Even I stop example-ingress, result is same.

So it means.

nginx-ingress -> ingress -> service -> pods.

Every request is fetchd in first nginx-ingress, and not sent to ingress?

whitebear
  • 11,200
  • 24
  • 114
  • 237
  • I spend some Euro for a domain using local IP address for the entry and let certmanager create a cert with DNS challenge. You can even use external DNS as kubernetes component to set DNS entries automatically. You get next to a real domain name a real certificate to test secure http connections. – Manuel Apr 27 '23 at 22:15
  • @Manuel Thank you for your comment, when I need https access, I should do something like that, however for now, I am just testing. – whitebear Apr 27 '23 at 23:06

1 Answers1

1

Two things that caught my eye:

  1. The Ingress controller you are using seems to might require an ingressClassName defined in the Ingress resource, so that the ingress controller takes this resource (and its configs) into account.

    More about Ingress class:

  2. You should change the path: /* to path: /.

    • path: /* makes the Ingress controller create a location block à la

      location /* {
        ...
      }
      

      /* is interpreted as a normal prefix in this case and has nothing to do with regex (probably not what you assumed).

      In your case: the 404 comes rather from the ingress itself (since request URI / not found - must be '/*' instead). In order for a request to be proxied to the httpd server at all, the request must be as follows: "http://demo.localdev.me/*", to which the httpd would again respond with 404 (since the resource '/*' also doesn't exist on httpd by default).

    • Whereas path: / does the following:

      location / {
        ...
      }
      

      The location / block is a special case that matches any URI that starts with a slash (/), which includes all URIs. (This is also the default location block that Nginx uses if no other location block matches the request's URI.)

    More about nginx location:

Final result:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  ingressClassName: nginx   # add
  rules:
  - host: demo.localdev.me
    http:
      paths:
      - pathType: Prefix
        path: /           # change
        backend:
          service:
            name: apa000ser
            port:
              number: 8099
Kenan Güler
  • 1,868
  • 5
  • 16