Foojay Today

Port Management in Local Kubernetes Clusters

November 29, 2021

Most of my talks contain a demo. A fair share of these demos require multiple "infrastructure" dependencies: a database (or more), Elasticsearch, you name it. To ease my setup and avoid messing up my machine, I use either Docker Compose or Kubernetes locally on my Mac. Both rely on Docker Desktop.

To expose a cluster Service on my host, I use nodePort. Hence, I set a dedicated node port for each service. I need to remember each of them for each demo. Worse, services might be (are) declared across different manifest files.

For a long time, I wanted to simplify my life. I've searched for Kubernetes-based solutions. I found that kube-forward was not stable enough.

My latest attempt was MetalLB. Even though I didn't manage to make it work, it bound port 8080 on my machine: none of my other regular Spring demos could work.

Last week, I decided to take another approach: a regular proxy in front of my local cluster. OSX comes with an existing Apache Web Server installation. You can check it with ls /etc/apache2:

extra                 httpd.conf.pre-update mime.types            other
httpd.conf            magic                 original              users

The following modules are necessary:

#httpd.conf
LoadModule proxy_module libexec/apache2/mod_proxy.so
LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so
LoadModule proxy_balancer_module libexec/apache2/mod_proxy_balancer.so

The requirement is straightforward: proxy calls from to . For this, we need to configure a virtual host:

#httpd-vhosts.conf
<VirtualHost *:80>
    ServerName zerodowntime.hz
    ProxyRequests off
    ProxyPass / http://localhost:30002/
    ProxyPassReverse / http://zerodowntime.hz
</VirtualHost>

To make sure everything works fine, we can use apachectl -S:

VirtualHost configuration:
*:80           zerodowntime.hz (/private/etc/apache2/extra/httpd-vhosts.conf:40)

Last but not least, let's configure the host file:

#./etc/hosts
127.0.0.1        zerodowntime.hz

At this point, we can access the application using the zerodowntime.hz URL:

Access the application with the local URL

Depending on the deployed application, this step might be the last one. It's unfortunately not my case, as my demo uses a redirect. By default, the redirect location sent to the client is the URL known to the application, , defeating the whole purpose. We need to configure the application to use the standard X-Forwarded-* HTTP headers.

I'm using Spring Boot, so that is just a matter of configuration:

#application.yml
server:
  forward-headers-strategy: native

At this point, everything works as expected!

To go further:

Originally published at A Java Geek on November 28th, 2021

Topics:

Related Articles

View All
  • A Case for Databases on Kubernetes from a Former Skeptic

    Looking back at the pitfalls of running databases on Kubernetes I encountered several years ago, most of them have been resolved.

    All of these problems are hard and require technical finesse and careful thinking. Without choosing the right pieces, we’ll end up resigning both databases and Kubernetes to niche roles in our infrastructure, as well as the innovative engineers who have invested so much effort in building out all of these pieces and runbooks.

    Read More
    Jul 13, 2021
  • CI/CD Workflow for Spring Boot Applications on Kubernetes via Skaffold

    Developing applications with Kubernetes is not such an easy task and that’s where Skaffold comes into play.

    It eases the development and deployment of your applications running on Kubernetes.

    It manages the entire workflow and you get instant feedback while developing and deploying your application locally or on a remote Kubernetes cluster.

    Read More
    Jan 19, 2021
  • Creating a Kubernetes Operator in Java

    Kubernetes is much more than a runtime platform for Docker containers.

    Through its API, you can not only create custom clients, but you can also extend Kubernetes. Those custom Controllers are called Operators and work with application-specific custom resource definitions. You can not only write those Kubernetes operators in Go, but you can do this also in Java.

    In this talk, delivered by Payara’s Rudy De Busscher at JCON 2020, you will be guided through setting up and your first explorations of the Kubernetes API within a plain Java program.

    Read More
    Jadon Ortlepp profile
    Jan 27, 2021

Author(s)

  • Nicolas Frankel

    Nicolas is a developer advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). ... Learn more

Comments (0)

Your email address will not be published.

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard