Ock
Ock

Reputation: 1330

502 Bad Gateway with Kubernetes Ingress

I have a kubernetes setup with the configuration like below:

#---
kind: Service
apiVersion: v1
metadata:
  name: myservice
spec:
  selector:
    app: my-service
  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8080
      # Port to forward to inside the pod
      targetPort: 80



---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-service
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-service
    spec:
      containers:
        - name: my-service
          image: my-custom-docker-regisry/my-service:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
      imagePullSecrets:
      - name: regcred

and my ingress:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /myservice
        backend:
          serviceName: myservice
          servicePort: 80

What I tried to do is pulling the image from my docker registry and run it in the kubernetes. I have configured here one deployment and one service and expose the service to the outside with the ingress.

My minikube is running under ip 192.168.99.100 and when I tried to access my application with address: curl 192.168.99.100:80/myservice, I got 502 Bad Gateway.

Does anyone have an idea why it happended or did I do something wrong with the configuration? Thank you in advanced!

Upvotes: 41

Views: 109185

Answers (2)

kalyani chaudhari
kalyani chaudhari

Reputation: 7849

In my case it was ingress-controller which was not up, do verify serviceaccounts as well.

Use the below commands:

controlplane ~ ➜  kubectl get all  -n ingress-nginx
NAME                                            READY   STATUS      
RESTARTS   AGE
pod/ingress-nginx-admission-create-nrwn9        0/1     Completed   0          
15m
pod/ingress-nginx-controller-5876c7c579-vpsc9   1/1     Running     0          
11m

NAME                                         TYPE        CLUSTER-IP       
EXTERNAL-IP   PORT(S)        AGE
service/ingress-nginx-controller             NodePort    10.96.91.54      
.  none>        80:30080/TCP   11m
service/ingress-nginx-controller-admission   ClusterIP   10.102.162.130   
<none>        443/TCP        25m

NAME                                       READY   UP-TO-DATE   
AVAILABLE   AGE
deployment.apps/ingress-nginx-controller   1/1     1            1           
11m

NAME                                                  DESIRED   CURRENT   
READY   AGE
replicaset.apps/ingress-nginx-controller-5876c7c579   1         1         
1       11m

NAME                                       COMPLETIONS   DURATION   AGE
job.batch/ingress-nginx-admission-create   1/1           10m        25m
job.batch/ingress-nginx-admission-patch    0/1           25m        25m

controlplane ~ ➜  kubectl get sa -n ingress-nginx 
NAME                      SECRETS   AGE
default                   0         26m
ingress-nginx             0         21m
ingress-nginx-admission   0         20m

Upvotes: 0

Kos
Kos

Reputation: 72299

Your ingress targets this service:

     serviceName: myservice
     servicePort: 80

but the service named myservice exposes port 8080 rather than 80:

  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8080
      # Port to forward to inside the pod
      targetPort: 80

Your ingress should point to one of the ports exposed by the service.

Also, the service itself targets port 80, but the pods in your deployment seem to expose port 8080, rather than 80:

  containers:
    - name: my-service
      image: my-custom-docker-regisry/my-service:latest
      imagePullPolicy: Always
      ports:
        - containerPort: 8080

So long story short, looks like you could swap port with targetPort in your service so that:

  • the pods expose port 8080
  • the service exposes port 8080 of all the pods under service name myservice port 80,
  • the ingress configures nginx to proxy your traffic to service myservice port 80.

Upvotes: 54

Related Questions