Programmers often need help accessing resources in a virtual private network. For example, we have a database that we want to access from our PC. We shouldn’t expose database ports to the Internet because it is a potential danger. But when we have Kubernetes in our infrastructure, we can use it as a proxy.
Outline of our problem
socat tool is regarded as the advanced version of netcat. They do similar things, but socat has additional functionality, such as permitting multiple clients to listen on a port or reusing connections.
Using socat we can make port forwarding. Here is an example for multiple connections:
All we have to do is to run the above command in Kubernetes. To do this, we need to create a simple Pod. See the below example in `port-forward.yaml` file.
In this example we create docker container with port forward to postgresql database
To run following pod in kubernetes use kubeclt:
Then we can see a running container running on our k8s cluster:
The last thing we need to do is make a port forward from Pod to our local machine:
And now we can connect to a database in the cloud which is available on localhost on port 5432.
Let us know if you want to know more
These practical examples show you how to access the could resources using kubernetes. This practical solution can contribute to your future projects and products. Don’t hesitate to reach out if you’d like to know more about the practical uses of Kotlin or need some help with your current or next project development.