1

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
  • 1,589
  • 5
  • 31
  • 53

1 Answers1

1

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
  • 7,874
  • 2
  • 23
  • 27
  • I don't think that is correct. I have this exact setup in another project but with an Express API instead of a flask app. Ingress should be routing to the correct pod. there is something different with this project than the express one. I thought it might have something to do with the rewrite. In my other project I could hit the API easily by hitting minikube ip/API/values without even specifying a port – Matthew The Terrible Jun 09 '19 at 20:10
  • 1
    Sorry I missed the part where you are using nginx-ingress. Just to check if the problem is in the rewrite, can you try substituting the rewrite-target _/$1_ to _/service_ and the path to _/api/service_ ? I assume you have `@app.route("/service")` in your app – victortv Jun 10 '19 at 01:00
  • I don't know how to do that. But what I did do was went and changed my api image to a different one from a class I took that had an express api rather than the flask api. And this one worked without changing anything else. so it must be how my flask api is set up or I'm missing a piece somewhere that flask needs. – Matthew The Terrible Jun 11 '19 at 00:42