<p>I have deployed a Cloud SQL proxy and a service attached to it in Kubernetes. Both the proxy and the service are listening on port 5432. Other pods are not able to establish a connection to the Cloud SQL database through the proxy, but when I port forward to the pod of the proxy or to the service, I am able to connect to the database without any issue from my localhost.</p>
<p>The Kubernetes cluster and the Cloud SQL instance are both private. I have checked the service and deployment labels, the service and pod network configuration, the firewall rules, and the network configuration, but I am still unable to resolve the issue.</p>
<p>All pods are in the same namespace, logs of the proxy show no error, when I run <code>nc -v $service_name $port</code> in other pods, it yields no error and it doesn't show any sign of malfunctioning, it doesn't even print that the connection was successful. The problem is that these pods are not being able to establish a TCP connection to the service,</p>
<p>Here is an example of an error message:</p>
<pre><code>Caused by: com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Connection to airbyte-db-svc:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
</code></pre>
<p>If needed here is the manifest that deploys the service and the proxy:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: airbyte-db-svc
spec:
  type: ClusterIP
  ports:
    - port: 5432
      targetPort: 5432
      protocol: TCP
  selector:
    airbyte: db
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: airbyte-db-proxy
spec:
  replicas: 1
  selector:
    matchLabels:
      airbyte: db
  template:
    metadata:
      labels:
        airbyte: db
    spec:
      serviceAccountName: airbyte-admin
      containers:
      - name: cloud-sql-proxy
        image: gcr.io/cloudsql-docker/gce-proxy:latest
        command:
          - &quot;/cloud_sql_proxy&quot;
          - &quot;-enable_iam_login&quot;
          - &quot;-ip_address_types=PRIVATE&quot;
          - &quot;-instances=PROJECT_ID_HERE:REGION_HERE:INSTANCE_CONNECTION_HERE=tcp:5432&quot;
        ports:
          - containerPort: 5432
        securityContext:
          runAsNonRoot: true
</code></pre>
<p>The serviceAccount <code>airbyte-admin</code> has the good 'Cloud SQL Client'  and the workloadIdentity configured in the GCP Project.</p>
<p>What could be the problem and how can I fix it?</p>

<p>CloudSQL Proxy listens on localhost by default.  If you want to expose it via a service, you'll want to add <code>--address 0.0.0.0</code> to your cloud_sql_proxy command options.</p>
