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

时间:2016-04-03 12:46:20

标签: ruby-on-rails postgresql docker google-kubernetes-engine

遇到一个小问题,我希望有人可以指出我正确的方向。我正在运行Rails + Postgres多容器,它们启动正常,除了rails在我尝试访问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如下:

RAILS 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
​
​

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'] %>

我认为它是一个链接问题,或与我指定的PGDATA路径有什么关系?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

感谢@ alex-robinson的指导,他的回答是正确的,尽管原帖的配置还有另一个问题:

解决方法是使用正确的用户名/密码的ENV变量修改database.yml(它们与OP中的YAML设置的ENV变量不匹配),并且正如Alex提到的那样,使用服务的名称作为主机名。

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