Access your home-lab Kairos cluster over a Wireguard VPN (2024)

By Dimitris Karakasilis(Personal page) (GitHub) (Codeberg) |

The problem

You got yourself a Rabserry Pi (or more), and you want to put them to good use.You decide to make a Kubernetes cluster out of them, so that you can utilise the resources better, use familiar tools and implement infrastructure-as-code.

Up to this point, kudos to you for demanding no less than a real cloud from your home infra.

Like a smart person you are, you probably used Kairos to create your cluster and it’s now up and running.It’s now time to run some workloads.

Here is my list if you need some ideas:

None of these workloads is intended for public access. There are ways to expose the cluster to the world (e.g. like I described in another post)but it would be better if only devices within a VPN would have access to it.

Once again, there are many VPN solutions out there, but for this blog post, we’ll go with Wireguard.

So here is the problem in one sentence:

“How do we expose our (possibly behind NAT) cluster, to machines inside the same Wireguard VPN?”

“NAT” is the main part of the problem because otherwise this would simply be a blog post on how to create a Wireguard VPN. There are many nice tutorials already out there for that.

A Solution

While trying to solve the problem, I learned 2 things about Wireguard that I didn’t know:

  1. Wireguard doesn’t distinguish between a “server” and a “client”. All peers are made equal.
  2. Wireguard doesn’t provide a solution for NAT traversal. How you access nodes behind NAT, is up to you.

So imagine you have your cluster behind your home router (NAT) and your mobile phone on another network (behind NAT too) trying to access a service on the cluster.That’s not possible, unless there is some public IP address that somehow forwards requests to the cluster.

And that’s the idea this solution is based on.

High level view

Access your home-lab Kairos cluster over a Wireguard VPN (1)

The idea is almost similar to the one I described in another post.The only difference is, that this time we expose the cluster only to machines inside the VPN.

Prerequisites:

  • A VM with a public IP address and SSH access (as small as it gets, it’s good enough)
  • kubectl access to the cluster we want to expose (it doesn’t have to be Kairos, even k3d and kind will do)
  • A machine to test the result (a smartphone where Wireguard can be installed is fine)

Step by step

From this point on, we will use the IP address 1.2.3.4 as the public IP address of the VM in the cloud.Replace it with the one matching your VM. We also assume, that the user with SSH access is root. Replace if necessary.

Setup the cloud VM

SSH to the machine:

$ ssh root@1.2.3.4

Create Wireguard keys:

$ wg genkey | tee privatekey | wg pubkey > publickey

Create Wireguard config:

$ cat << EOF > /etc/wireguard/wg0.conf[Interface]Address = 192.168.6.1/24PrivateKey = $(cat privatekey)ListenPort = 41194# Mobile client[Peer]PublicKey = <public key from next step>AllowedIPs = 192.168.6.2/32EOF

Start and enable the Wireguard service:

$ sudo systemctl enable --now wg-quick@wg0

Allow binding non-loopback interfaces when creating an SSH reverse tunnelby setting GatewayPorts clientspecified in /etc/ssh/sshd_config.

Setup the test machine (mobile?)

On some computer with wg installed, generate the keys:

$ wg genkey | tee privatekey | wg pubkey > publickey

Create the Wireguard configuration. Follow the instructions for your favorite application.For Android, you can use this: https://play.google.com/store/apps/details?id=com.wireguard.android

If setting up a Linux machine, you can create the configuration like this:

$ cat << EOF > /etc/wireguard/wg0.conf[Interface]Address = 192.168.6.2/24PrivateKey = $(cat privatekey)# The cloud VM[Peer]PublicKey = <public key from the previous step>AllowedIPs = 192.168.6.1/32Endpoint = 1.2.3.4:41194EOF

Start and enable the Wireguard service. If on a Linux machine, something like this will do:

$ sudo systemctl enable --now wg-quick@wg0

On a mobile, follow the instructions of your application.

After a while, your client should be able to ping the IP address of the VM: 192.168.6.1.You may find the output of wg show useful, while waiting for the peers to connect.

Setup the cluster

Deploy the helper Pod. We will use an image created with this Dockerfile andpublished here. The image’s entrypoint works with a configdescribed here.The image is not multiarch, but there is one suitable for RasberryPi 4 (see the comment in the file).

If you are are going to create a fresh Kairos cluster, you can use a config like the following to automatically set up the helper Pod (make sure you replace the id_rsa and id_rsa.pub keys).If you prefer to not have the keys stored on your Kairos host filesystem, you can simply create the same resources using kubectl apply -f after your cluster is up an running.

#cloud-configusers:- name: kairospasswd: kairosstages:after-install-chroot: - files: - path: /var/lib/rancher/k3s/server/manifests/rproxy-pod.yaml content: | --- apiVersion: v1 data: id_rsa: the_vms_private_key_in_base64 id_rsa.pub: the_vms_public_key_in_base64 kind: Secret metadata: name: jumpbox-ssh-key type: Opaque --- apiVersion: v1 kind: ConfigMap metadata: name: proxy-config data: config.json: | { "services": [ { "bindIP": "192.168.6.1", "bindPort": "443", "proxyAddress": "traefik.kube-system.svc", "proxyPort": "443" }, { "bindIP": "192.168.6.1", "bindPort": "80", "proxyAddress": "traefik.kube-system.svc", "proxyPort": "80" } ], "jumpbox": { "url": "1.2.3.4", "user": "root", "sshKeyFile": "/ssh/id_rsa" } } --- apiVersion: apps/v1 kind: Deployment metadata: annotations: name: nginx-ssh-reverse-proxy spec: replicas: 1 selector: matchLabels: app.kubernetes.io/instance: nginx-ssh-reverse-proxy app.kubernetes.io/name: nginx-ssh-reverse-proxy template: metadata: labels: app.kubernetes.io/instance: nginx-ssh-reverse-proxy app.kubernetes.io/name: nginx-ssh-reverse-proxy spec: containers: - name: proxy # Change to quay.io/jimmykarily/nginx-ssh-reverse-proxy-arm64:latest # if you are running on a RasberryPi 4 image: quay.io/jimmykarily/nginx-ssh-reverse-proxy:latest command: ["/start.sh", "/proxy-config/config.json"] imagePullPolicy: Always volumeMounts: - name: ssh-key mountPath: /ssh - name: config-volume mountPath: /proxy-config/ volumes: - name: ssh-key secret: secretName: jumpbox-ssh-key defaultMode: 0400 - name: proxy-config - name: config-volume configMap: name: proxy-config

In a nutshell, the config above is creating a reverse SSH tunnel from the VMto the Pod. Inside the Pod, nginx redirects traffic to the traefik load balancer runningon the cluster. This has the effect, that any request landing on the VM on ports 80 and 443will eventually reach the Traefik instance inside the cluster on ports 80 and 443.As a result, you can point any domain you want to the VM and it will reach the corresponding Ingress defined on your cluster.

NOTE: The SSH tunnel will only bind the IP address 192.168.6.1 on the VM, which means, anyone trying to access the VM using its public IP address, will not be able to access the cluster. Only machines that can talk to 192.168.6.1 have access, in other words, machines inside the VPN.

Test the connection

  • Try to access the cluster with the VPN IP address (should work).From your test peer, open http://192.168.6.1. You should see a 404 message from Traefik.You can also verify it is a response from Traefik in your cluster, by calling curlon the https endpoint (on a “default” k3s installation):

    $ curl -k -v https://192.168.6.1 2>&1 | grep TRAEFIK* subject: CN=TRAEFIK DEFAULT CERT* issuer: CN=TRAEFIK DEFAULT CERT
  • Try to access the cluster with domain pointing to the VPN IP address (should work)You can create a wildcard DNS record and point it to the VPN IP address ifyou want to make it easier for people to access the services you are running.E.g. by creating an A record like this: *.mydomainhere.org -> 192.168.6.1you will be able create Ingresses for your applications like:app1.mydomainhere.org, app2.mydomainhere.org.

  • Try to access the cluster using the public IP address (should not work)

    $ curl http://1.2.3.4

    This command should fail to connect to your cluster

Conclusion

For non-critical workloads, when 100% uptime is not a hard requirement, the solution we described allows one to use services that would otherwise cost multiple times more by hostingthose on their own hardware. It does so, without exposing the home network to the public.

If you liked this solution or if you have comments, questions or recommendations for improvements, please reach out!

Useful links

  • ←Previous
  • Next→
Access your home-lab Kairos cluster over a Wireguard VPN (2024)

FAQs

How do I connect to a VPN in WireGuard? ›

Log into your WireGuard VPN Server

If you would like to connect an additional device or perform any changes to your VPN server, then you would need to log into the server control panel. Open up your web browser and paste in your server's IP address and port and hit 'Enter'.

Should I use WireGuard VPN? ›

Is WireGuard secure? WireGuard is considered by many to be one of the safest, most secure VPN protocol options available today. Simplified design using less code equals fewer bugs and security vulnerabilities, while WireGuard's faster state-of-the-art cryptography employs superior default security settings.

How to setup WireGuard VPN server at home? ›

  1. Step 1: Expose Wireguard VPN Server to the Internet. Your Public IP Address. ...
  2. Step 2: Setup Wireguard VPN Server. Install the wireguard software and dependencies. ...
  3. Step 3: Setup client connections. ...
  4. Step 4: Setup clients. ...
  5. Step 5: Test Connection.
Sep 29, 2023

What does WireGuard mean in VPN? ›

WireGuard is a communication protocol and free and open-source software that implements encrypted virtual private networks (VPNs), and was designed with the goals of ease of use, high speed performance, and low attack surface.

Does WireGuard require a server? ›

A WireGuard VPN usually involves a client (the app on your phone, for example) and a VPN server. Like other encryption protocols, WireGuard communicates with the server and establishes an encrypted tunnel between server and client.

How do I setup a WireGuard on my router? ›

Go to [VPN] > [VPN Server] > enable and click [WireGuard® VPN] > click add button. 4. For general devices like laptops or phones, you can just click the Apply button.

What are the negatives of WireGuard? ›

Unreliable Monotonic Counter. WireGuard uses the system time as a reliable monotonic counter. If this jumps forward, a user might DoS their own keys, by making it impossible to later have a value larger, or an adversary controlling system time could store a handshake initiation for use later.

Is there anything better than WireGuard? ›

Tailscale does more than WireGuard, so that will always be true. We aim to minimize that gap, and Tailscale generally offers good bandwidth and excellent latency, particularly compared to non-WireGuard VPNs.

Does WireGuard cost money? ›

Users can install WireGuard and OpenVPN for free because they are both open-source programs. Users will only be required to pay for the related VPN.

How to tell if WireGuard is working? ›

To view the status of one or more WireGuard tunnels, use the show wireguard [<instance>] command. This command prints the status of all WireGuard tunnels and can optionally limit the output to a specific instance.

Which is better OpenVPN or WireGuard Home server? ›

The biggest notable differences between WireGuard and OpenVPN are speed and security. While WireGuard is generally faster, OpenVPN provides heavier security. The differences between these two protocols are also what make up their defining features.

What is the default password for WireGuard? ›

Run WireGuard-UI

⚠️ The default username and password are admin .

Is WireGuard safe now? ›

Yes. WireGuard is designed with a strong focus on security, leveraging state-of-the-art cryptography to provide high-end protection for data in transit. Its choice of modern cryptographic primitives provides a robust foundation against various attacks.

What can I do with WireGuard? ›

WireGuard is an open-source communication protocol for setting up secure Virtual Private Networks (VPNs). Using advanced cryptographic primitives to secure exchanged data, it seals it within an encrypted tunnel.

Which VPN protocol is best? ›

The best VPN protocol for you depends on a number of factors, including what device you're using, how much balance between security and speed you want, what type of activities you're doing online, and more. OpenVPN and WireGuard are generally considered the best VPN protocols for day-to-day use.

How do I connect to NordVPN with WireGuard? ›

How to set the NordVPN WireGuard on TP-Link Wireless Router
  1. Log in to the web-based interface of the router. ...
  2. In the Server List section below, there is an 'Add' option in the upper right corner. ...
  3. It needs a token from NordVPN to proceed. ...
  4. After entering the token and logging in, you can now select a server.
Apr 12, 2024

How to connect VPN through proxy? ›

To set up a proxy server for a VPN connection
  1. Select the Start button, then select Settings > Network & Internet > VPN.
  2. Select the VPN connection, then select Advanced options.
  3. Under VPN proxy settings, select the type of proxy setup you want to use, then enter the proxy server information for that VPN connection.

How do I activate the tunnel in WireGuard? ›

Start or Stop WireGuard Tunnel on Windows

msc . Click “Yes” to any security warning. Scroll down to the bottom and you will see the available WireGuard tunnels. Right-click and select Start or Stop, as appropriate.

How do I allow VPN to connect? ›

Connect to a VPN from the Windows Settings page:
  1. Select Start > Settings > Network & internet > VPN.
  2. Next to the VPN connection you want to use, select Connect.
  3. If prompted, enter your username and password or other sign in info.

Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5797

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.