Docker HAProxy ile Round Robin Loadbalancing
Bu proje hızlı olarak HAProxy ile config test edilmesi için tasarlanmıştır. Ortam kurulumları ve config basit tutulmuştur. Projede 3 adet CentOS 7 container’ı kullandım. 1 tanesi HAProxy için 2 tanesi de Apache Web Server için.
Round Robin Loadbalancing:
Round Robin, varsayılan ve muhtemelen en yaygın olarak kullanılan loadbalancing yöntemidir. Gelen isteklerin eşit olarak ayaktaki sunuculara dağıtılması prensibiyle çalışır.
3 tane container ayağa kaldırıyorum.
docker run -d --name haproxy --hostname haproxy -it --privileged 72c478e5833d /usr/sbin/init
docker run -d --name webserver1 --hostname webserver1 -it --privileged 72c478e5833d /usr/sbin/init
docker run -d --name webserver2 --hostname webserver2 -it --privileged 72c478e5833d /usr/sbin/init
Container’larda hiç bir şey yüklü olmadığı için sırayla aşağıdaki komutları tüm containerlarda çalıştırıyorum. Net-tools paketiyle makinelerin IP adresini görebilirim.
yum update -y
yum install net-tools -y
Haproxy’yi loadbalancer olarak konumlandıracağım.
yum install haproxy -y
systemctl start haproxy
Web Server 1'e Apache Web Server kurup start ediyorum ve basit bir dosya içerisine HTML oluşturuyorum.
yum install httpd -y
echo "bu sunucu webserver1" > /var/www/html/index.html
systemctl start httpd
curl localhost
Web Server 2'e Apache Web Server kurup start ediyorum ve basit bir dosya içerisine HTML oluşturuyorum.
yum install httpd -y
echo "bu sunucu webserver2" > /var/www/html/index.html
systemctl start httpd
curl localhost
HAProxy Configim
global
log 127.0.0.1 local2 chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon # turn on stats unix socket
stats socket /var/lib/haproxy/statsdefaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000frontend web_server *:80
default_backend webserversbackend webservers
balance roundrobin
server webserver1 172.17.0.4:80 check
server webserver2 172.17.0.5:80 check
Test için bir döngüye curl isteğimizi yazıyoruz.
[root@centos ~]# for a in {1..10};do curl localhost;done
bu sunucu webserver1
bu sunucu webserver2
bu sunucu webserver1
bu sunucu webserver2
bu sunucu webserver1
bu sunucu webserver2
bu sunucu webserver1
bu sunucu webserver2
bu sunucu webserver1
bu sunucu webserver2
[root@centos ~]#