问题描述
问题:
我的flask API无法连接到我的Postgres实例.我已经验证了数据库和api均能按预期工作,并且部署和服务都在kubernetes中运行.它必须是连接本身.该连接是在Flask配置文件中定义的,所以也许我没有正确指定它?我不知道下一步该怎么做.
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.
错误
这是我在检查特定于API的Pod日志时看到的错误,该API试图与Postgres取得联系.
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?
堆栈信息
- Minikube
- Kubernetes
- Docker
- 烧瓶
- Postgres
- SQLAlchemy
Flask/Config.py
SQLALCHEMY_DATABASE_URI = 'postgres://postgres:postgres@postgres:5432/postgres'
.这与我切换到Kubernetes之前使用Docker Compose的情况相同.
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/数据库持久卷声明
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!
推荐答案
服务的名称是主机名,因此您应该连接到postgres-cluster-ip-service.default.svc.cluster.local
(如果要部署到其他不同的Kubernetes,请更改default
命名空间).您的错误消息似乎是在群集环境之外连接到其他名为postgres
的系统.
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.
这篇关于Flask Postgres连接因Kubernetes和Docker而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!