<p>so I have a basic minikube cluster configuration for K8s cluster with only 2 pods for Postgres DB and my Spring app. However, I can't get my app to connect to my DB. I know that in <code>Docker</code> such issue could be solved with networking but after a lot of research I can't seem to find the problem and the solution to my issue.</p>
<p>Currently, given my configuration I get a Connection refused error by postgres whenever my Spring App tries to start:</p>
<p><code>Caused by: org.postgresql.util.PSQLException: Connection to postgres-service:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.</code></p>
<p>So my spring-app is a basic REST API with some open endpoints where I query for some data. The app works completely fine and here is my <code>application.properties</code>:</p>
<pre><code>spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
spring.datasource.username=${POSTGRES_USER}
spring.datasource.password=${POSTGRES_PASSWORD}
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
</code></pre>
<p>The way I create my Postgres component is by creating a <code>ConfigMap</code>, a <code>Secret</code> and finally a <code>Deployment</code> with it's <code>Service</code> inside. They look like so:</p>
<p><code>postgres-config.yaml</code></p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
data:
  postgres-url: postgres-service
  postgres-port: &quot;5432&quot;
  postgres-db: &quot;test&quot;
</code></pre>
<p><code>postgres-secret.yaml</code></p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: postgres-secret
type: Opaque
data:
  postgres_user: cm9vdA== #already encoded in base64
  postgres_password: cm9vdA== #already encoded in base64
</code></pre>
<p><code>postgres.yaml</code></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
  labels:
    app: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgresdb
          image: postgres
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: postgres_user
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: postgres_password
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: postgres-config
                  key: postgres-db
---
apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  selector:
    app.kubernetes.io/name: postgres
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
</code></pre>
<p>and finally here's my <code>Deployment</code> with it's <code>Service</code> for my spring app</p>
<p><code>spring-app.yaml</code></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring-app-deployment
  labels:
    app: spring-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: spring-app
  template:
    metadata:
      labels:
        app: spring-app
    spec:
      containers:
        - name: spring-app
          image: app #image is pulled from my docker hub
          ports:
            - containerPort: 8080
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: postgres_user
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-secret
                  key: postgres_password
            - name: POSTGRES_HOST
              valueFrom:
                configMapKeyRef:
                  name: postgres-config
                  key: postgres-url
            - name: POSTGRES_PORT
              valueFrom:
                configMapKeyRef:
                  name: postgres-config
                  key: postgres-port
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: postgres-config
                  key: postgres-db
---
apiVersion: v1
kind: Service
metadata:
  name: spring-app-service
spec:
  type: NodePort
  selector:
    app.kubernetes.io/name: spring-app
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 30001
</code></pre>

Richard
<p>A connection refused means that the host you are connecting to, does not have the port you mentioned opened.</p>
<p>This leads me to think that the postgres pod isnt running correctly, or the service is not pointing to those pods correctly.</p>
<p>By checking the Yamls I can see that the service's pod selector isnt configured correctly:</p>
<p>The service is selecting pods with label: <code>app.kubernetes.io/name: postgres</code></p>
<p>The deployment is configured with pods with label: <code>app: postgres</code></p>
<p>The correct service manifest should look like:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  selector:
    app: postgres
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
</code></pre>
<p>You can double check that by describing the service using kubectl describe service postgres-service.</p>
<p>The output should contain the postgres pods IPs for Endpoints.</p>
