<p>I'm new to k8s and need some direction on how to troubleshoot.</p>
<p>I have a postgres container and a graphql container. The graphql container is tries to connect to postgres on startup.</p>
<h2>Problem</h2>
<p>The graphql container can't connect to postgres. This is the error on startup:</p>
<p>{&quot;internal&quot;:&quot;could not connect to server: Connection refused\n\tIs the server running on host &quot;my-app&quot; (xxx.xx.xx.xxx) and accepting\n\tTCP/IP connections on port 5432?\n&quot;,
&quot;path&quot;:&quot;$&quot;,&quot;error&quot;:&quot;connection error&quot;,&quot;code&quot;:&quot;postgres-error&quot;}</p>
<p>My understanding is that the graphql-container doesn't recognize the IP <code>my-app (xxx.xx.xx.xxx)</code>. This is the actual Pod Host IP, so I'm confused as to why it doesn't recognize it. How do I troubleshoot errors like these?</p>
<h2>What I tried</h2>
<ul>
<li><p>Hardcoding the host in the connection uri in deployment.yaml to the actual pod host IP. Same error.</p>
</li>
<li><p>Bashed into the graphql container and verified that it had the correct env values with the <code>env</code> command.</p>
</li>
</ul>
<h2>deployment.yaml</h2>
<pre><code>spec:
  selector:
    matchLabels:
      service: my-app
  template:
    metadata:
      labels:
        service: my-app
...
  - name: my-graphql-container
    image: image-name:latest
    env:
    - name: MY_POSTGRES_HOST
      value: my-app
    - name: MY_DATABASE
      value: db
    - name: MY_POSTGRES_DB_URL # the postgres connection url that the graphql container uses
      value: postgres://$(user):$(pw)@$(MY_POSTGRES_HOST):5432/$(MY_DATABASE)
...
  - name: my-postgres-db
    image: image-name:latest
</code></pre>

tbd_
<p>In <a href="https://kubernetes.io/docs/concepts/workloads/pods/#using-pods" rel="nofollow noreferrer">k8s docs about pods</a> you can read:</p>
<blockquote>
<p>Pods in a Kubernetes cluster are used in two main ways:</p>
<ul>
<li><p><strong>Pods that run a single container.</strong> [...]</p>
</li>
<li><p><strong>Pods that run multiple containers that need to work together</strong>. [...]</p>
</li>
</ul>
<blockquote>
<p><em><strong>Note</strong></em>: Grouping multiple co-located and co-managed containers in a
single Pod is a relatively advanced use case. You should use this
pattern only in specific instances in which your containers are
tightly coupled.</p>
</blockquote>
<p><em><strong>Each Pod is meant to run a single instance of a given
application</strong></em>. [...]</p>
</blockquote>
<hr />
<p>Notice that your deployment doesn't fit this descriprion because you are trying to run two applications in one pod.</p>
<p>Remember to always use one pod per container and only use multiple containers per pod if it's impossible to separate them (and for some reason they have to run together).</p>
<p>And the rest was already mentioned by David.</p>
