Chris
Chris

Reputation: 448

Postgres issue - Ruby on Rails (Postgres) on Google Container Engine

Run into a little problem and I'm hoping someone can point me in the right direction. Im running a Rails+Postgres multi-container and they start up fine, except rails shows this in the logs when I try access the IP of the LoadBalancer:

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"?):

My two container pod files and my database.yml are as follows:

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 FILE

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

I assume its a linking issue, or something to do with PGDATA paths I specified?

Upvotes: 2

Views: 495

Answers (2)

Chris
Chris

Reputation: 448

Thanks to @alex-robinson for the guidance and his answer is correct, although there was another problem with the original post's configuration:

Solution was to modify database.yml with the correct ENV variables for username/password (they didnt match the ENV variables set by the YAML in the OP) and as Alex mentioned to use the service's name for the hostname.

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

Upvotes: 0

Alex Robinson
Alex Robinson

Reputation: 13397

Your Rails pod looks like it's configured to talk to a postgres instance running locally. You need to configure it to talk to the IP address of the postgres pod or service.

Upvotes: 1

Related Questions