<p>My rails deployment is not able to connect to the postgres database. Error in the logs:</p>

<pre><code>PG::ConnectionBad (could not connect to server: Connection refused
  Is the server running on host "db" (10.0.105.11) and accepting
  TCP/IP connections on port 5432?
):
</code></pre>

<p>This is my <code>kubectl get services</code> output:</p>

<pre><code>NAME                     TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
load-balancer            LoadBalancer   10.0.5.147     60.86.4.33    80:31259/TCP   10m
db                       ClusterIP      10.0.105.11    &lt;none&gt;        5432/TCP       10m
kubernetes               ClusterIP      10.0.0.1       &lt;none&gt;        443/TCP        10m
web                      ClusterIP      10.0.204.107   &lt;none&gt;        3000/TCP       10m
</code></pre>

<p>db-deployment.yaml:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: db
  name: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
      - env:
        - name: POSTGRES_DB
          value: postgres
        - name: POSTGRES_HOST_AUTH_METHOD
          value: trust
        - name: POSTGRES_USER
          value: postgres
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        ports:
        - containerPort: 5432
        image: postgres
        imagePullPolicy: ""
        name: db
        resources: {}
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgres
      restartPolicy: Always
      serviceAccountName: ""
      volumes:
      - name: postgres
        persistentVolumeClaim:
          claimName: postgres
status: {}
</code></pre>

<p>db-service.yaml:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: db
  labels:
    app: db
spec:
  ports:
    - port: 5432
  selector:
    app: db
    tier: database
</code></pre>

<p>config/database.yml:</p>

<pre><code>default: &amp;default
  adapter: postgresql
  encoding: utf8
  host: db
  username: postgres
  pool: 5

development:
  &lt;&lt;: *default
  database: myapp_development

test:
  &lt;&lt;: *default
  database: myapp_test

production:
  &lt;&lt;: *default
  database: myapp_production
</code></pre>

<p>How do I ensure the postgres instance is running and accepting connections? Did I misconfigure something in the manifest files, or does the database.yml file need to point to a different host?</p>

usbToaster
<p>The first thing that jumps out at me is that your <code>db</code> service is targeting two selectors <code>app: db</code> and <code>tier: database</code>, however the corresponding <code>deployment.yml</code> only has the <code>db</code> label.  You will need to add the <code>tier</code> label to your deployment template metadata so that the service will appropriately target the right pod.</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: db
    tier: database 
  name: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
      tier: database
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: db
        tier: database
</code></pre>

<p>In general, if a service is not connecting to a backend pod, you can easily diagnose the issue with a simple kubectl command.<br>
This will tell you what current selectors are applied to your service </p>

<pre><code>kubectl describe service db -o yaml
</code></pre>

<p>Then you can fetch the pods that the selectors refer to and make sure that this is returning something.</p>

<pre><code>kubectl get pods -l app=db -l tier=database
</code></pre>

<p>Finally, I would recommend using the <em>DNS</em> name of the service setup through kube-proxy instead of the cluster IP address. I tend to view this as more resilient then a cluster up, as it will be automatically routed if the services IP ever changes. In your connection string use <code>db:5432</code> instead of <code>10.0.105.11</code>.</p>
