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!