How to connect local java service to remote kubernetes services How to connect local java service to remote kubernetes services kubernetes kubernetes

How to connect local java service to remote kubernetes services


There are two ways to go about this : expose the services you need to connect to to the public (NodePort/LB/Ingress) or make it possible for the developers to actually reach inside your cluster so they can connect to these services.

The first one should be easy with something like NginX IngressController and vhost based exposing of services (you can also protect them with ie. basic auth).

The second one can be done with ie. Weave Net (if that is/can be the overlay network you use). If you connect your devs to the central k8s cluster like this, they will only need to run kube-proxy on their nodes and point containers to k8s DNS. Obviously this is something I would advise only in case of dev/stage clusters and not for prod.


Use the kubernetes port forwarding to access a service in the kubernetes network from your local host during development. Once port forwarding is setup, alter the source code in the service that must access the other service on the kubernetes network during development time

To find your POD:

kubectl get pods

Your POD name is something like my-service-4254283044-n9ik9. Now setup port forwarding:

kubectl port-forward <POD-TO-FORWARD-TO> 8083:80

EDIT: Note that you need to login to google cloud from your terminal (once) before you can do port forwarding! Here is an example, make sure to put in your zone, cluster and project: gcloud container clusters get-credentials example-cluster --zone europe-west1-c --project example-148812

-------- Some Examples --------

Java/Spring/Ribbon, change application.yml where

account-service.ribbon.listOfServers: http://account-service

gets replaced with

account-service.ribbon.listOfServers: http://localhost:8081

Java/Feign, change the target of the Feign/HystrixFeign builder

HystrixFeign.builder().target(http://localhost:8081, <fallback>);

C#:

client = new HttpClient{    BaseAddress = new Uri(@"http://localhost:8081")};

etc.

The source code modification needed can of course be done by configuration...


You can also set up a service using custom Endpoints. Normally services point to deployments, but you can make them point to specific IPs. If you set the EndPoint to your hosts' IP, services will route out of the cluster to your IDE environment. Vice versa, from your IDE you can still use services exposed via service type LoadBalancer.

Keep in mind that in your IDE you would miss the environment variables set by Kubernetes. I've solved this by detecting if I'm running in an IDE, and then getting what I need from the Kubernetes API. Instead of using kubedns, you'll have to replace the URLs by localhosts:<port>.

I've written a blog post on how to setup the custom endpoints which includes a working demo, if you're interested.