Find why i am getting 502 Bad gateway error on kubernetes

3/11/2019

I am using kubernetes. I have Ingress service which talks my container service. We have exposed a webapi which works all fine. But we keep getting 502 bad gateway error. I am new to kubernetes and i have no clue how to go about debugging this issue. Server is a nodejs server connected to database. Is there anything wrong with configuration?

My Deployment file--


apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-pod
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-pod
    spec:
      containers:
      - name: my-pod
        image: my-image
        ports:
        - name: "http"
          containerPort: 8086
        resources:
          limits:
            memory: 2048Mi
            cpu: 1020m
---
apiVersion: v1
kind: Service
metadata:
  name: my-pod-serv
spec:
  ports:
    - port: 80
      targetPort: "http"
  selector:
     app: my-pod

My Ingress Service:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: gateway
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: abc.test.com
    http:
      paths:
      - path: /abc
        backend:
          serviceName: my-pod-serv
          servicePort: 80
-- Hacker
kubernetes

2 Answers

3/11/2019

I would need to set up a cluster in order to test your yml files.

Just to help you debugging, follow this steps:

1- get the logs of the my-pod container using kubectl logs my-pod-container-name, make sure everything is working

2- Use port-forward to expose your container and test it.

3- Make sure the service is working properly, change its type to load balancer, so you can reach it from outside the cluster.

If the three things are working there is a problem with your ingress configuration.

I am not sure if I explained it in a detailed way, let me know if something is not clear

-- Leandro Donizetti Soares
Source: StackOverflow

5/15/2019

In Your case:

I think that you get this 502 gateway error because you don't have Ingress controller configured correctly. Please try do do it with installed Ingress like in example below. It will do all automatically.

Nginx Ingress step by step:

1) Install helm

2) Install nginx controller using helm

$ helm install stable/nginx-ingress --name nginx-ingress

It will create 2 services. You can get their details via

$ kubectl get svc
NAME                            TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)                      AGE
kubernetes                      ClusterIP      10.39.240.1     <none>          443/TCP                      29d
nginx-ingress-controller        LoadBalancer   10.39.243.140   35.X.X.15   80:32324/TCP,443:31425/TCP       19m
nginx-ingress-default-backend   ClusterIP      10.39.252.175   <none>          80/TCP                       19m

nginx-ingress-controller - in short, it's dealing with requests to Ingress and directing

nginx-ingress-default-backend - in short, default backend is a service which handles all URL paths and hosts the nginx controller doesn't understand

3) Create 2 deployments (or use yours)

$ kubectl run my-pod --image=nginx
deployment.apps/my-pod created

$ kubectl run nginx1 --image=nginx
deployment.apps/nginx1 created

4) Connect to one of the pods

$ kubectl exec -ti my-pod-675799d7b-95gph bash

And add additional line to the output to see which one we will try to connect later.

$ echo "HELLO THIS IS INGRESS TEST" >> /usr/share/nginx/html/index.html 
$ exit

5) Expose deployments.

$ kubectl expose deploy nginx1 --port 80
service/nginx1 exposed
$ kubectl expose deploy my-pod --port 80
service/my-pod exposed

This will automatically create service and will looks like

apiVersion: v1
kind: Service
metadata:
  labels:
    app: my-pod
  name: my-pod
  selfLink: /api/v1/namespaces/default/services/my-pod
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: my-pod
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

6) Now its the time to create Ingress.yaml and deploy it. Each rule in ingress need to be specified. Here I have 2 services. Each service specification starts with -host under rule parameter.

Ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: two-svc-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: my.pod.svc
    http:
      paths:
      - path: /pod
        backend:
          serviceName: my-pod
          servicePort: 80
  - host: nginx.test.svc
    http:
      paths:
      - path: /abc
        backend:
          serviceName: nginx1
          servicePort: 80

$ kubectl apply -f Ingress.yaml
ingress.extensions/two-svc-ingress created

7) You can check Ingress and hosts

$ kubectl get ingress
NAME              HOSTS                       ADDRESS        PORTS     AGE
two-svc-ingress   my.pod.svc,nginx.test.svc   35.228.230.6   80        57m

8) Eplanation why I installed Ingress.

Connect to the ingress controller pod

$ kubectl exec -ti nginx-ingress-controller-76bf4c745c-prp8h bash
www-data@nginx-ingress-controller-76bf4c745c-prp8h:/etc/nginx$ cat /etc/nginx/nginx.conf

Because I have installed nginx ingress earlier, after deploying Ingress.yaml, the nginx-ingress-controller found changes and automatically added necessary code. In this file you should be able to find whole configuration for two services. I will not copy configuration but only headers.

start server my.pod.svc

start server nginx.test.svc

www-data@nginx-ingress-controller-76bf4c745c-prp8h:/etc/nginx$ exit

9) Test

$ kubectl get svc to get your nginx-ingress-controller external IP
$ curl -H "HOST: my.pod.svc" http://35.X.X.15/
default backend - 404

$ curl -H "HOST: my.pod.svc" http://35.X.X.15/pod
<!DOCTYPE html>
...
</html>
HELLO THIS IS INGRESS TEST

Please keep in mind Ingress needs to be in the same namespace like services. If you have a few services in many namespace you need to create Ingress for each namespace.

-- PjoterS
Source: StackOverflow