Flask Postgres Connection Fails with Kubernetes and Docker

1/17/2019

Issue:

My flask API is unable to connect to my Postgres instance. I've verified that the database and api are both working as expected on their own, the deployments and services are running within kubernetes. It must be the connection itself. The connection is defined inside of the Flask config file so perhaps I'm specifying it incorrectly? I'm at a loss of next steps to take.

Error

This is the error I see when I check the logs of the pod specific to the API which is trying to reach out to postgres.

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection timed out
       Is the server running on host "postgres" (198.105.244.228) and accepting
       TCP/IP connections on port 5432?

Stack info

  • Minikube
  • Kubernetes
  • Docker
  • Flask
  • Postgres
  • SQLAlchemy

Flask/Config.py

SQLALCHEMY_DATABASE_URI = 'postgres://postgres:postgres@postgres:5432/postgres'. This is identical to the one I was using with Docker Compose before the switch to Kubernetes.

Kubernetes/postgres-cluster-ip-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: postgres-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: postgres
  ports:
    - port: 5432
      targetPort: 5432

Kubernetes/postgres-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim
      imagePullSecrets:
        - name: regcred
      containers:
        - name: postgres
          image: my/image-db
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_PASSWORD
              value: postgres
            - name: POSTGRES_USER
              value: postgres
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
              subPath: postgres

Kuberenetes/database-persistent-volume-claim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: database-persistent-volume-claim
spec:
  # Access mode gets some instance of storage.
  # ReadWriteOncence means that it can be used by a single node.
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      # find a storage option that has at least 2 gigs of space
      storage: 2Gi

Happy to add any other files that would help!

-- dizzy
docker
flask
kubernetes
minikube
postgresql

2 Answers

1/23/2019

Just to add to @David's correct answer above for those searching:

The format for SQLALCHEMY_DATABASE_URI is:

schema://username:password@SERVER:PORT/databasename

The reason mine was failing was because I thought my server name was Postgres as defined by my Kubernetes postgres-deployment file. However, I had a separately "service" file called postgres-cluster-ip-service. The second I set that as my server name, the connection began working. Make sure you're pointing the server not directly at the server, but the IP service sitting in front of it for Kubernetes.

Example:

postgres://username:password@postgres-cluster-ip-service:5432/databasename

-- dizzy
Source: StackOverflow

1/17/2019

The name of the Service is a host name, so you should be connecting to postgres-cluster-ip-service.default.svc.cluster.local (change default if you're deploying to some different Kubernetes namespace). Your error message looks like you're connecting to some other system named postgres outside of your cluster environment.

-- David Maze
Source: StackOverflow