<p>We launched a Cloud Composer cluster and want to use it to move data from Cloud SQL (Postgres) to BQ. I followed the notes about doing this mentioned at these two resources:</p>

<p><a href="https://stackoverflow.com/questions/50154306/google-cloud-composer-and-google-cloud-sql">Google Cloud Composer and Google Cloud SQL</a></p>

<p><a href="https://cloud.google.com/sql/docs/postgres/connect-kubernetes-engine" rel="nofollow noreferrer">https://cloud.google.com/sql/docs/postgres/connect-kubernetes-engine</a></p>

<p>We launch a pod running the cloud_sql_proxy and launch a service to expose the pod. The problem is that Cloud Composer cannot see the service stating the error when attempting to use an ad-hoc query to test:</p>

<p><code>cloud not translate host name "sqlproxy-service" to address: Name or service not known"</code></p>

<p>Trying by the service IP address results in the page timing out.</p>

<p>The <code>-instances</code> passed to cloud_sql_proxy work when used in a local environment or cloud shell. The log files seem to indicate no connection is ever attempted</p>

<pre><code>me@cloudshell:~ (my-proj)$ kubectl logs -l app=sqlproxy-service
me@2018/11/15 13:32:59 current FDs rlimit set to 1048576, wanted limit is 8500. Nothing to do here.

    2018/11/15 13:32:59 using credential file for authentication; email=my-service-account@service.iam.gserviceaccount.com
    2018/11/15 13:32:59 Listening on 0.0.0.0:5432 for my-proj:my-ds:my-db
    2018/11/15 13:32:59 Ready for new connections
</code></pre>

<p>I see a comment here <a href="https://stackoverflow.com/a/53307344/1181412">https://stackoverflow.com/a/53307344/1181412</a> that possibly this isn't even supported?</p>

<p><strong>Airflow</strong></p>

<p><a href="https://i.stack.imgur.com/TWWCZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TWWCZ.png" alt="enter image description here"></a></p>

<p><strong>YAML</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: sqlproxy-service
  namespace: default
  labels:
    app: sqlproxy
spec:
  ports:
  - port: 5432
    protocol: TCP
    targetPort: 5432
  selector:
    app: sqlproxy
  sessionAffinity: None
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sqlproxy
  labels:
    app: sqlproxy
spec:
  selector:
    matchLabels:
      app: sqlproxy
  template:
    metadata:
      labels:
        app: sqlproxy
    spec:
      containers:
        - name: cloudsql-proxy
          ports:
          - containerPort: 5432
            protocol: TCP
          image: gcr.io/cloudsql-docker/gce-proxy:latest
          imagePullPolicy: Always
          command: ["/cloud_sql_proxy",
                    "-instances=my-proj:my-region:my-db=tcp:0.0.0.0:5432",
                    "-credential_file=/secrets/cloudsql/credentials.json"]
          securityContext:
            runAsUser: 2  # non-root user
            allowPrivilegeEscalation: false
          volumeMounts:
            - name: cloudsql-instance-credentials
              mountPath: /secrets/cloudsql
              readOnly: true
      volumes:
        - name: cloudsql-instance-credentials
          secret:
            secretName: cloudsql-instance-credentials
</code></pre>

<p>The information you found in the answer you linked is correct - ad-hoc queries from the Airflow web server to cluster-internal services within the Composer environment are not supported. This is because the web server runs on App Engine flex using its own separate network (not connected to the GKE cluster), which you can see in the <a href="https://cloud.google.com/composer/docs/concepts/overview#architecture" rel="nofollow noreferrer">Composer architecture diagram</a>.</p>

<p>Since that is the case, your SQL proxy must be exposed on a public IP address for the Composer Airflow web server to connect to it. For any services/endpoints listening on RFC1918 addresses within the GKE cluster (i.e. not exposed on a public IP), you will need additional network configuration to accept external connections.</p>

<p>If this is a major blocker for you, consider running a <a href="https://cloud.google.com/composer/docs/how-to/managing/deploy-webserver" rel="nofollow noreferrer">self-managed Airflow web server</a>. Since this web server would run in the same cluster as the SQL proxy you set up, there would no longer be any issues with name resolution.</p>
