DEV Community

Cover image for Docker HAProxy ile Round Robin Loadbalancing
Emre Emanet
Emre Emanet

Posted on • Updated on

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.

Image description

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Haproxy'yi loadbalancer olarak konumlandıracağım.

yum install haproxy -y
systemctl start haproxy
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

vi yada nano gibi bir editörle /etc/haproxy/haproxy.conf dosyası aşağıdaki gibi edilenir.

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/stats

defaults
    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                 3000

frontend web_server *:80
 default_backend webservers

backend webservers
   balance      roundrobin
   server   webserver1 172.17.0.4:80 check
   server   webserver2 172.17.0.5:80 check
Enter fullscreen mode Exit fullscreen mode

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 ~]#
Enter fullscreen mode Exit fullscreen mode

Top comments (0)