<p><strong>Issue:</strong></p>

<p>My flask API is unable to connect to my Postgres instance. I've verified that the database and api are both working as expected on their own, the deployments and services are running within kubernetes. It must be the connection itself.  The connection is defined inside of the Flask config file so perhaps I'm specifying it incorrectly?  I'm at a loss of next steps to take.</p>

<p><strong>Error</strong></p>

<p>This is the error I see when I check the logs of the pod specific to the API which is trying to reach out to postgres.</p>

<pre><code>sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection timed out
       Is the server running on host "postgres" (198.105.244.228) and accepting
       TCP/IP connections on port 5432?
</code></pre>

<p><strong>Stack info</strong></p>

<ul>
<li>Minikube </li>
<li>Kubernetes </li>
<li>Docker </li>
<li>Flask </li>
<li>Postgres </li>
<li>SQLAlchemy</li>
</ul>

<p><strong>Flask/Config.py</strong></p>

<p><code>SQLALCHEMY_DATABASE_URI = 'postgres://postgres:postgres@postgres:5432/postgres'</code>.  This is identical to the one I was using with Docker Compose before the switch to Kubernetes.</p>

<p><strong>Kubernetes/postgres-cluster-ip-service.yaml</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: postgres-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: postgres
  ports:
    - port: 5432
      targetPort: 5432
</code></pre>

<p><strong>Kubernetes/postgres-deployment.yaml</strong></p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim
      imagePullSecrets:
        - name: regcred
      containers:
        - name: postgres
          image: my/image-db
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_PASSWORD
              value: postgres
            - name: POSTGRES_USER
              value: postgres
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
              subPath: postgres
</code></pre>

<p><strong>Kuberenetes/database-persistent-volume-claim</strong></p>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: database-persistent-volume-claim
spec:
  # Access mode gets some instance of storage.
  # ReadWriteOncence means that it can be used by a single node.
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      # find a storage option that has at least 2 gigs of space
      storage: 2Gi
</code></pre>

<p>Happy to add any other files that would help! </p>

dizzy
<p>The name of the Service is a host name, so you should be connecting to <code>postgres-cluster-ip-service.default.svc.cluster.local</code> (change <code>default</code> if you're deploying to some different Kubernetes namespace).  Your error message looks like you're connecting to some other system named <code>postgres</code> outside of your cluster environment.</p>
