2

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Alex, do you mean by hardcoding the local IP address into database config? Will give it a try but was hoping for something more dynamic..
No, I mean configuring your Rails application to talk to the database using it's DNS name. If you create a service for the postgres pod, then you can tell Rails to connect to the DB using the postgres service's name. Right now it appears to be assuming that the DB is running on the same machine (hence its attempted use of a Unix domain socket).
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.