<p>I created a managed Postgres database in Google Cloud. This database got a external IP address. 
In a second step I created a Kubernetes cluster.
In the k8s I want access this external database. Therefore I created a service without label selector but with a external endpoint pointing to my Postgres-database.</p>

<p>I had to allow the Postgres database to get access from the (three) cluster nodes. I configured that in the Google Cloud Console (SQL).</p>

<p>My first question: Is this the right way to integrate an external database? Especially this IP access configuration?</p>

<p>To test my connection against the database my first try was to establish a port-forwarding from my local host. My idea was to access this database via my Database-IDE(datagrip). However when trying to establish a port forwarding I get the following error:</p>

<pre><code>error: cannot attach to *v1.Service: invalid service 'postgres-bla-bla': Service is defined without a selector
</code></pre>

<p>Second question: How to access this service locally? </p>

<p>In a third step I created a pod with 'partlab/ubuntu-postgresql' docker-image. I did a 'kctrl exec -it ... ' and could access my Postgres database with</p>

<pre><code>psql -h postgres-bla-bla ...
</code></pre>

<p>So basically it works. But I'm sure my solution has some flaws.
What can I do better? How to fix the problem from question 2?</p>

Thomas Seehofchen
<p>The problem was discussed <a href="https://github.com/txn2/kubefwd/issues/35" rel="noreferrer">here</a> and there is a solution to set up port forwarding to a service without selector/pod (e.g. ExternalName service) by deploying a proxy pod inside K8s:</p>

<pre><code>kubectl -n production run mysql-tunnel-$USER -it --image=alpine/socat --tty --rm --expose=true --port=3306 tcp-listen:3306,fork,reuseaddr tcp-connect:your-internal-mysql-server:3306
kubectl -n production port-forward svc/mysql-tunnel-$USER 3310:3306
</code></pre>

<p>In the example above the MySQL server at <code>your-internal-mysql-server:3306</code> will be available on <code>localhost:3310</code> on your machine.</p>
