digital ocean: load balancer not working for flask app pods

86 views Asked by At

i am trying deploy a simple python flask application in digital ocean Kubernetes cluster using below deployment and service configuration. flask app is using 8080 port while running the code and same is used to expose through load balancer.

flask app

    if __name__ == '__main__':
        app.run(debug=True, host='0.0.0.0', port=8080)

deployment

    ubuntu@ubuntu-22lts:~$ cat deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: unit7-app-deploy
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: unit-app
      template:
        metadata:
          labels:
            app: unit-app
        spec:
          containers:
          - name: unit-app
            image: <username>/flask-app:latest
            imagePullPolicy: IfNotPresent
            resources:
              limits:
                memory: "128Mi"
                cpu: "500m"
            ports:
            - containerPort: 8080

and service for load balance creation

    apiVersion: v1
    kind: Service
    metadata:
      name: unit7-app-service
    spec:
      selector:
        app: unit7-app-deploy
      ports:
      - port: 8080
        targetPort: 8080
        protocol: TCP
      type: LoadBalancer

Now I am trying to access my app through Kubernetes external IP and 8080 port which is not working. pods logs are showing that my flaks is running.

    ubuntu@ubuntu-22lts:~$ kubectl --kubeconfig=k8s-1-27-2-do-0-blr1-cluster1-kubeconfig.yaml logs -f unit7-app-deploy-6568dss8-ddsds
     * Serving Flask app 'run'
     * Debug mode: on
    WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
     * Running on all addresses (0.0.0.0)
     * Running on http://127.0.0.1:8080
     * Running on http://10.244.0.214:8080

where am I wrong. Kindly help me.

1

There are 1 answers

0
VonC On BEST ANSWER

Just in case this is part of your issue, your Service's selector does not match your Deployment's label.
The selector in your service should match the labels in your deployment to establish the correct connection.

In your Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: unit7-app-deploy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: unit-app

The label for the pods being deployed is app: unit-app.

But in your Service:

apiVersion: v1
kind: Service
metadata:
  name: unit7-app-service
spec:
  selector:
    app: unit7-app-deploy

Here, the service selector is looking for pods with label app: unit7-app-deploy.

The selector in the service configuration should match the label you defined in your Deployment configuration for the pods, which is app: unit-app.
For instance, service configuration should look like:

apiVersion: v1
kind: Service
metadata:
  name: unit7-app-service
spec:
  selector:
    app: unit-app # Change from 'unit7-app-deploy' to 'unit-app'
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
  type: LoadBalancer

That mismatch is likely the reason your load balancer is not working correctly, as it is not able to find the correct pods to send traffic to.