Setting up load balancer in docker:

docker-compose.yaml:


services:
  nginx:
    image: nginx:latest
    container_name: lbk3s-nginx
    restart: unless-stopped
    volumes:
      - /home/user/lbk3s/nginx.conf:/etc/nginx/nginx.conf  
    security_opt:
      - no-new-privileges
    networks:
      - proxy
    ports:
      - 6443:6443
networks:
  proxy:
    external: true


nginx.conf:


#uncomment this next line if you are NOT running nginx in docker
#load_module /usr/lib/nginx/modules/ngx_stream_module.so;

events {}

stream {
  upstream k3s_servers {
    server 'ip-address-of-master1-k3s-server':6443;
    server 'ip-address-of-master2-k3s-server':6443;
  }

  server {
    listen 6443;
    proxy_pass k3s_servers;
  }
}