Home > OS >  Kubernates Nginx-Ingress rule not working
Kubernates Nginx-Ingress rule not working

Time:12-01

I installed the ingress controller using the following command:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/cloud/deploy.yaml

And the result of kubectl get pods --namespace=ingress-nginx is:

NAME                                        READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create-x4mss        0/1     Completed   0          28m
ingress-nginx-admission-patch-jn9cz         0/1     Completed   1          28m
ingress-nginx-controller-8574b6d7c9-k4jbj   1/1     Running     0          28m

For kubectl get service ingress-nginx-controller --namespace=ingress-nginx I get:

NAME                       TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx-controller   LoadBalancer   10.106.134.128   localhost     80:32294/TCP,443:30997/TCP   30m

As for my deployment and service I have the following:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: app
  name: app
  namespace: namespace
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app
  template:
      labels:
        app: app
    spec:
      containers:
        - image: image
          name: app
          ports:
            - containerPort: 5000
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: app-service
  namespace: namespace
spec:
 type: ClusterIP
 selector:
   app: app
  ports:
    - name: app-service
      port: 5000
      targetPort: 5000

My Ingress is as follows:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress
  namespace: namespace
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: com.host.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: app-service
            port:
              number: 5000

My pod and service are both running fine. The result of running kubectl describe pod command is:

Name:             app-6b9f7fc47b-sh6nc
Namespace:        namespace
Priority:         0
Service Account:  default
Node:             docker-desktop/192.168.65.4
Start Time:       Wed, 30 Nov 2022 16:22:04 -0500
Labels:           app=app
                  pod-template-hash=6b9f7fc47b
Status:           Running
IP:               10.1.0.237
IPs:
  IP:           10.1.0.237
Controlled By:  ReplicaSet/app-6b9f7fc47b
Containers:
  app:
    Container ID:   docker://ba77235d044c24b0f1391c56a2e8653a598a5c130ea4d15ff3b41cd96659fd4a
    Image:          image
    Image ID:       docker://sha256:912cb58ab1c3f2dd628c0b7db4d7f9ac6df4efbe4fcb86979b6a84614db8a675
    Port:           5000/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 30 Nov 2022 16:22:05 -0500
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-8pmjz (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-8pmjz:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  29m   default-scheduler  Successfully assigned namespace/app-6b9f7fc47b-sh6nc to docker-desktop
  Normal  Pulled     29m   kubelet            Container image "image" already present on machine
  Normal  Created    29m   kubelet            Created container app
  Normal  Started    29m   kubelet            Started container app

Running the following command kubectl get ingress --all-namespaces yields:

NAMESPACE           NAME             CLASS   HOSTS               ADDRESS   PORTS   AGE
namespace          ingress           nginx   com.host.com                   80      7s

I have tried using different ports, changing the controller, using a load balance type instead of cluster ip and yet nothing works when it comes to trying to make the ingress rule work. I have set the ingress-controller external ip as com.host.com in my hosts file as well. Furthermore, I am using docker-desktop as my node, however, I'm having this issue on minikube as well. Any help is appreciated.

CodePudding user response:

Your container's containerPort is 3000 while your service targetPort is 5000. Make sure that your service' targetPort, your container's containerPort and last but not least the listening port of your app are all the same.

Update:

So I guess it turned out that the problem is with name resolution.

com.host.com is not resolvable on your machine.

Using the localhost or 127.0.0.1 didn't match the host from the ingress rule -> that's why the default 404 handler was chosen.

So either fix name resolution or remove the host from the ingress rule.

CodePudding user response:

Bear in mind that the ingress controller will only update the IP of the ingress if the mapped service is up and ready. Your ingress shows that it's using app-service on port 5000 as its backend service, but your question does not show a listing of the pods on the namespace namespace where it appears your application pods are. Please could you add that to the question -- it is possible that either your pods aren't coming up successfully, or they're listening on a different port to 5000


UPDATE

Please also try the following:

  1. You're specifying that the container uses port 5000. But is it actually using 5000? try:

kubectl exec -it -n namespace app-6b9f7fc47b-sh6nc -- bash (or sh depending on what the default shell is for your app)

and then try curl localhost:5000 to see if it responds.

  1. Check the ingress controller logs:

kubectl logs -f -n ingress-nginx and see if there's any log messages that might help you identify what's going on.

  • Related