Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Debian 12 - Set Up Nginx Load Balancer For K3s

The tutorial below is how I set up my load balancer on Debian 12 for Kubernetes.

Resource Requirements

I would recommend at least:

  • 2 vCPUs
  • 512 MB of RAM

Steps

Install Nginx

sudo apt update && sudo apt install nginx-full -y

You must install the nginx-full package instead of just nginx. Otherwise you will have an issue with starting the nginx server later due to the use of the stream directive.

Now replace your Nginx configuration using the command below. Be sure to change the IP addresses of the k3s_servers block as appropriate for your Kubernetes API servers.

sudo echo "user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {}

stream {
    upstream k3s_servers {
        server 192.168.0.68:6443;
        server 192.168.0.69:6443;
        server 192.168.0.70:6443;
    }

    server {
        listen 6443;
        proxy_pass k3s_Servers;
    }
}" | sudo tee /etc/nginx/nginx.conf

Reload Nginx:

sudo nginx -s reload

References

Last updated: 15th January 2025
First published: 13th January 2025

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch