Postgres问题 – Google容器引擎上的Ruby on Rails(Postgres)

遇到一个小问题,我希望有人能指出我正确的方向。 我正在运行一个Rails + Postgres多容器,他们启动正常,除了当我尝试访问LoadBalancer的IP时,在日志中显示这个:

PG::ConnectionBad (could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?): 

我的两个容器pod文件和我的database.yml如下所示:

铁路POD

 apiVersion: v1 kind: Pod metadata: name: cartelhouse labels: name: cartelhouse spec: containers: - image: gcr.io/xyz/cartelhouse:v6 name: cartelhouse env: - name: POSTGRES_PASSWORD # Change this - must match postgres.yaml password. value: mypassword - name: POSTGRES_USER value: rails ports: - containerPort: 80 name: cartelhouse volumeMounts: # Name must match the volume name below. - name: cartelhouse-persistent-storage # Mount path within the container. mountPath: /var/www/html volumes: - name: cartelhouse-persistent-storage gcePersistentDisk: # This GCE persistent disk must already exist. pdName: rails-disk fsType: ext4​​  apiVersion: v1 kind: Pod metadata: name: cartelhouse labels: name: cartelhouse spec: containers: - image: gcr.io/xyz/cartelhouse:v6 name: cartelhouse env: - name: POSTGRES_PASSWORD # Change this - must match postgres.yaml password. value: mypassword - name: POSTGRES_USER value: rails ports: - containerPort: 80 name: cartelhouse volumeMounts: # Name must match the volume name below. - name: cartelhouse-persistent-storage # Mount path within the container. mountPath: /var/www/html volumes: - name: cartelhouse-persistent-storage gcePersistentDisk: # This GCE persistent disk must already exist. pdName: rails-disk fsType: ext4​​ 

POSTGRES POD

 apiVersion: v1 kind: Pod metadata: name: postgres labels: name: postgres spec: containers: - name: postgres image: postgres env: - name: POSTGRES_PASSWORD value: mypassword - name: POSTGRES_USER value: rails - name: PGDATA value: /var/lib/postgresql/data/pgdata ports: - containerPort: 5432 name: postgres volumeMounts: - name: postgres-persistent-storage mountPath: /var/lib/postgresql/data volumes: - name: postgres-persistent-storage gcePersistentDisk: # This disk must already exist. pdName: postgres-disk fsType: ext4​ 

DATABASE.YML文件

 ​production: <<: *default adapter: postgresql encoding: unicode database: app_production username: <%= ENV['PG_ENV_POSTGRES_USER'] %> password: <%= ENV['PG_ENV_POSTGRES_PASSWORD'] %> host: <%= ENV['PG_PORT_5432_TCP_ADDR'] %>  ​production: <<: *default adapter: postgresql encoding: unicode database: app_production username: <%= ENV['PG_ENV_POSTGRES_USER'] %> password: <%= ENV['PG_ENV_POSTGRES_PASSWORD'] %> host: <%= ENV['PG_PORT_5432_TCP_ADDR'] %> 

我认为它是一个链接问题,或者与我指定的PGDATApath有关?

你的Rails pod看起来像是被configuration为与本地运行的postgres实例进行通信。 您需要将其configuration为与postgres窗格或服务的IP地址进行通信。

感谢@ alex-robinson的指导和他的回答是正确的,虽然原来的职位configuration还存在另一个问题:

解决方法是使用正确的ENVvariables(用户名/密码)修改database.yml(它们不匹配OP中由YAML设置的ENVvariables),并且如Alex所述将服务的名称用于主机名。

 production: <<: *default adapter: postgresql encoding: unicode database: rails_production username: <%= ENV['POSTGRES_USER'] %> password: <%= ENV['POSTGRES_PASSWORD'] %> host: postgres