Tutorial: Load Balancing with Caddy
- Purpose:
- Learn how to use Caddy as a web load balancer.
- Environment:
- lb01 (load balancer)
- web01 (web server)
- web02 (web server)
- Tools:
-
Command to Clone the Repository:
git clone https://github.com/jasonc/caddy-load-balancing-tutorial.git -
Change into the Project Directory:
cd caddy-load-balancing-tutorial
-
Vagrantfiles:
Vagrantfile:- Automates the installation and configuration of the virtual machines, web servers, and load balancer.
Vagrantfile.vms:-
Creates the virtual machines, but does not install Caddy or perform any configuration.
-
To use, replace Vagrantfile with Vagrantfile.vms:
cp Vagrantfile.vms Vagrantfile
-
-
Virtual Machine Creation:
vagrant up
- Caddyfile.random
- Default random load balancing policy.
- Caddyfile.ip_hash
- For IP hash load balancing policy.
- Caddyfile.rr
- For round-robin load balancing policy.
- Adds the Caddy stable repository.
- Installs Caddy on all nodes.
- Configures lb01 with the random load balacing policy by using the
Caddyfile.randomconfiguration file. - Sets up web server hostname in
index.htmlon web01 and web02.
- Install and Configure Caddy Manually:
- Follow the tutorial video here.
- Or duplicate the steps in the
install.shscript for each server
- Switching Load Balancer Configurations:
-
Copy the desired Caddyfile to
/etc/caddy/Caddyfileon lb01:cp Caddyfile.rr /etc/caddy/Caddyfile # or cp Caddyfile.ip_hash /etc/caddy/Caddyfile # or cp Caddyfile.random /etc/caddy/Caddyfile -
Reload Caddy:
sudo systemctl reload caddy
-
-
From the command line, use
curlto test different load balancing policies:curl localhostOr
curl 192.168.56.50 # the public facing IP of lb01