Search code examples
kubernetesnext.jskubernetes-helmminikubenginx-ingress

Can not access ingress service from within cluster


I am new to kubernetes and I have minikube setup on my linux mint 20. I am trying to implement server side rendering with nextjs, I have installed ingress-nginx using helm.

ingess-service.yaml :

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - host: example.dev
      http:
        paths:
          - backend:
              serviceName: users-srv
              servicePort: 4000
            path: /api/users/?(.*)

          - backend:
              serviceName: ui-srv
              servicePort: 3000
            path: /?(.*)

in next js app ui I want to access ingress controller in order to make api calls from server side. I tried:

axios.get('http://ingress-nginx-controller-admission/api/users/currentuser')

axios.get('http://ingress-nginx-controller/api/users/currentuser')

axios.get('http://ingress-service/api/users/currentuser')

but nothing is working.

kubctl get services:

 NAME                                 TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)                      AGE 
ingress-nginx-controller             LoadBalancer   10.107.45.123    172.42.42.100   80:31205/TCP,443:32568/TCP   80m 
ingress-nginx-controller-admission   ClusterIP      10.111.229.112   <none>          443/TCP                      80m 
kubernetes                           ClusterIP      10.96.0.1        <none>          443/TCP                6d1h 
ui-srv                               ClusterIP      10.99.20.51      <none>          3000/TCP                     89s 
users-mongo-srv                      ClusterIP      10.103.187.200   <none>          27017/TCP              89s 
users-srv                            ClusterIP      10.99.15.244     <none>          4000/TCP                     89s

can anyone help me out ? Thanks in advance...


Solution

  • The ingress is designed to handle external traffic to the cluster and as such, it is expecting the request to arrive at the domain you specified (aka example.dev)

    To access your APIs from inside a Pod, you should most definitely use directly the services that are served by the Ingress, such as users-srv or ui-srv.

    If you really want to contact the ingress instead of the Service, you could try a couple things:

    • Make so that example.dev points to the LoadBalancer IP address, for example adding it to /etc/hosts of the cluster's nodes should work 8or even internally in the Pod). But take into consideration that this means accessing the services by a long route when you could just access them with the service name.

    • Remove the host parameter from your rules, meaning the services should be served generally at the IP address of the nginx-controller, this should make using ingress-nginx-controller work as expected. This is not supported by all Ingress Controllers but it could work.