kubernetes getting 502 bad gateway

6/9/2019

Brand new to kubernetes and am having an issue where I am getting a 502 bad gateway when trying to hit the api.

My configs look like this

apiVersion: v1
kind: Service
metadata:
    name: api-cluster-ip-service
spec:
    type: ClusterIP
    selector:
        component: api
    ports:
        - port: 80
          targetPort: 5000


apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: api
  template:
    metadata:
      labels:
        component: api
    spec:
      containers:
        - name: books-api
          image: mctabor/books-api
          ports:
            - containerPort: 5000


apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: books-ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - http:
        paths:
          - path: /api/?(.*)
            backend:
              serviceName: api-cluster-ip-service
              servicePort: 80

and in my flask app I have the following:

if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)

Not sure where I went wrong here

my minikube ip is 192.168.99.104 and I'm trying to hit the api route of 192.168.99.104/api/status

-- Matthew The Terrible
flask
kubernetes
kubernetes-ingress
python

1 Answer

6/9/2019

You didn't expose your service properly. First of all, a service of type ClusterIP is only available within the cluster. As you are using minikube, you should try changing the type do NodePort.

In second place, the port declared in the yaml is the port which makes the service visible to other services within the cluster.

After creating the a NodePort Service, execute kubectl get svc to see the external port assigned to the service. You will see something like 80:30351/TCP. This means you can access the service at 192.168.99.104:30351.

This is a great answer at explaining how to expose a service in minikube

-- victortv
Source: StackOverflow