Flask Postgres 与 Kubernetes 和 Docker 的连接失败
Flask Postgres Connection Fails with Kubernetes and Docker
问题:
我的烧瓶 API 无法连接到我的 Postgres 实例。我已经验证数据库和 api 都按预期工作,部署和服务在 kubernetes 中 运行。它必须是连接本身。连接是在 Flask 配置文件中定义的,所以也许我指定的不正确?我不知所措。
错误
这是我在检查特定于 API 的 pod 的日志时看到的错误,该 pod 正在尝试联系 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?
堆栈信息
- 迷你库
- Kubernetes
- Docker
- 烧瓶
- Postgres
- SQLAlchemy
Flask/Config.py
SQLALCHEMY_DATABASE_URI = 'postgres://postgres:postgres@postgres:5432/postgres'
。这与我在切换到 Kubernetes 之前使用 Docker Compose 时使用的相同。
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
很高兴添加任何其他有帮助的文件!
服务的名称是主机名,因此您应该连接到 postgres-cluster-ip-service.default.svc.cluster.local
(如果您要部署到某个不同的 Kubernetes 名称空间,请更改 default
)。您的错误消息看起来像是您正在连接到集群环境之外的其他名为 postgres
的系统。
只是为那些正在搜索的人添加上面@David 的正确答案:
SQLALCHEMY_DATABASE_URI的格式为:
schema://username:password@SERVER:PORT/databasename
我失败的原因是因为我认为我的服务器名称是我的 Kubernetes postgres-deployment 文件定义的 Postgres。但是,我有一个单独的 "service" 文件,名为 postgres-cluster-ip-service。在我将其设置为我的服务器名称的那一刻,连接就开始工作了。确保你不是直接指向服务器,而是指向 Kubernetes 前面的 IP 服务。
示例:
postgres://username:password@postgres-cluster-ip-service:5432/databasename
问题:
我的烧瓶 API 无法连接到我的 Postgres 实例。我已经验证数据库和 api 都按预期工作,部署和服务在 kubernetes 中 运行。它必须是连接本身。连接是在 Flask 配置文件中定义的,所以也许我指定的不正确?我不知所措。
错误
这是我在检查特定于 API 的 pod 的日志时看到的错误,该 pod 正在尝试联系 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?
堆栈信息
- 迷你库
- Kubernetes
- Docker
- 烧瓶
- Postgres
- SQLAlchemy
Flask/Config.py
SQLALCHEMY_DATABASE_URI = 'postgres://postgres:postgres@postgres:5432/postgres'
。这与我在切换到 Kubernetes 之前使用 Docker Compose 时使用的相同。
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
很高兴添加任何其他有帮助的文件!
服务的名称是主机名,因此您应该连接到 postgres-cluster-ip-service.default.svc.cluster.local
(如果您要部署到某个不同的 Kubernetes 名称空间,请更改 default
)。您的错误消息看起来像是您正在连接到集群环境之外的其他名为 postgres
的系统。
只是为那些正在搜索的人添加上面@David 的正确答案:
SQLALCHEMY_DATABASE_URI的格式为:
schema://username:password@SERVER:PORT/databasename
我失败的原因是因为我认为我的服务器名称是我的 Kubernetes postgres-deployment 文件定义的 Postgres。但是,我有一个单独的 "service" 文件,名为 postgres-cluster-ip-service。在我将其设置为我的服务器名称的那一刻,连接就开始工作了。确保你不是直接指向服务器,而是指向 Kubernetes 前面的 IP 服务。
示例:
postgres://username:password@postgres-cluster-ip-service:5432/databasename