kubernetes ingress-nginx gives 502 error and the address field is empty

5/20/2020

I am setting up kubernetes on a AWS environment using kubeadm. I have setup ingress-nginx to access the service on port 443. I have checked the service configurations which look good. I am receiving 502 bad gateway and also the Address field in ingress is empty.

Front end service

apiVersion: v1
kind: Service
metadata:
  labels:
    name: voyager-configurator-webapp
  name: voyager-configurator-webapp
spec:
  ports:
    -
      port: 443
      targetPort: 443
  selector:
    component: app
    name: voyager-configurator-webapp
  type: ClusterIP

Ingress yml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress-resource
spec:
  tls:
  - hosts:
      - kubernetes-test.xyz.com
    secretName: default-server-secret
  rules:
  - host: kubernetes-test.xyz.com
    http:
      paths:
      - backend:
          serviceName: voyager-configurator-webapp
          servicePort: 443
NAME                     CLASS    HOSTS                         ADDRESS   PORTS     AGE
nginx-ingress-resource   <none>   kubernetes-test.xyz.com             80, 443   45m

What could be the issue here ? Any help will be appreciated.

-- Karthik K
kubernetes
kubernetes-ingress

1 Answer

5/21/2020

Make sure that your service is created in proper namespace - if not add namespace field in service definition. It is not good approach to add label called name with the same name as your service, instead you can use different one to avoid mistake and configurations problem. Read more about selectors and labels: labels-selectors.

Your frontend service should look like that:

piVersion: v1
kind: Service
name: voyager-configurator-webapp
metadata:
  labels:
    component: app
    appservice: your-example-app
spec:
  ports:
    - protocol: TCP
      port: 443
      targetPort: 443
  selector:
    component: app
    app: your-example-app
  type: ClusterIP

Your ingress should look like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress-resource
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  tls:
  - hosts:
      - kubernetes-test.xyz.com
        secretName: default-server-secret
  rules:
  - host: kubernetes-test.xyz.com
    http:
      paths:
      - path: /
        backend:
          serviceName: voyager-configurator-webapp
          servicePort: 443

You have to define path to backend to with Ingress should send traffic.

Remember that is good to follow some examples and instructions during setup to avoid problems and waste of time during debugging.

Take a look: nginx-ingress-502-bad-gateway, aws-kubernetes-ingress-nginx.

-- MaggieO
Source: StackOverflow