首页 > 解决方案 > Kubernetes Pod 无法使用 Hasura 映像连接到本地 postgres 服务器

问题描述

我正在按照本教程将 hasura kubernetes pod 连接到我的本地 postgres 服务器。

当我创建部署时,pod 的容器无法连接到 postgres(CrashLoopBackOff 并不断重试),但没有给出任何原因。以下是日志:

{"type":"pg-client","timestamp":"2020-05-03T06:22:21.648+0000","level":"warn","detail":{"message":"postgres connection failed, retrying(0)."}}

我的部署.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: hasura
    hasuraService: custom
  name: hasura
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hasura
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: hasura
    spec:
      containers:
      - image: hasura/graphql-engine:v1.2.0
        imagePullPolicy: IfNotPresent
        name: hasura
        env:
        - name: HASURA_GRAPHQL_DATABASE_URL
          value: postgres://USER:@localhost:5432/my_db
        - name: HASURA_GRAPHQL_ENABLE_CONSOLE
          value: "true"
        ports:
        - containerPort: 8080
          protocol: TCP
        resources: {}

postgres://USER:@localhost:5432/MY_DB用作 postgres url - 这里的“localhost”是正确的地址吗?

当我尝试(无密码)时,我验证了上面的 postgres url 有效:

> psql postgres://USER:@localhost:5432/my_db
psql (12.2)
Type "help" for help.

> my_db=#

我还能如何解决它?日志不是很有帮助...

标签: postgresqlkuberneteskubernetes-podhasura

解决方案


如果我理解正确,问题是 Pod(从 Minikube“内部”)无法通过localhost.

如果是这种情况,请检查此线程

... Minikube VM 可以在 192.168.99.1 上访问主机的 localhost(来自 Minikube 的 127.0.0.1 仍然是 Minicube 的 localhost)。

从技术上讲,对于 Pod 来说,localhost就是 Pod 本身。Host 机器和 Minikube 通过网桥连接。您可以使用Minikube 主机上的infconfig和找到确切的 IP 地址和路由。route -n


推荐阅读