Load Balancing Docker Swarm Cluster with Nginx on AlmaLinux 8

Load Balancing Docker Swarm Cluster dengan Nginx pada AlmaLinux 8

  • Minimum Requirement: 2 CPU & 1GB RAM
  • Node Controller: 192.168.88.100
  • Node Worker01: 192.168.88.11
  • Node Worker02: 192.168.88.12
  • Node Load Balancer: 192.168.88.88

Install Docker CE pada Node Controller, Worker, Load Balancer

  • Update repository almalinux dan install dependencies
yum update -y
yum install -y yum-utils lvm2 device-mapper device-mapper-persistent-data device-mapper-event device-mapper-libs device-mapper-event-libs net-tools wget htop nano
  • Install Docker CE
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo && yum makecache
yum install docker-ce docker-ce-cli containerd.io -y
  • Jalankan service docker
systemctl enable docker
systemctl start docker
systemctl status docker

Install Docker Compose CE pada Node Controller, Worker, Load Balancer

  • Install docker compose
curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url  | grep docker-compose-linux-x86_64 | cut -d '"' -f 4 | wget -qi -
chmod +x docker-compose-linux-x86_64
sudo cp -R docker-compose-linux-x86_64 /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Setting Firewalld pada Node Controller dan Worker

  • Firewalld untuk Node Controller
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=2377/tcp
firewall-cmd --permanent --add-port=7946/tcp
firewall-cmd --permanent --add-port=7946/udp
firewall-cmd --permanent --add-port=4789/udp
firewall-cmd --reload
  • Firewalld untuk Node Worker
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=7946/tcp
firewall-cmd --permanent --add-port=7946/udp
firewall-cmd --permanent --add-port=4789/udp
firewall-cmd --reload

Install Docker Swarm pada Node Controller

  • Install Docker Swarm pada Node Controller
docker swarm init --advertise-addr 192.168.88.100
---<output>---
Swarm initialized: current node (u84mzdp4gqqi0ay75hyoii04d) is now a manager.
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-4l29ny2l6u0s6fhujwxrxypzwy73a1u3cwxykac0s00vtpijib-1mgwa9pw2j3fijatvbe4gmrsb 192.168.88.100:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
  • Join Docker Swarm
  • Copy perintah docker swarm join –token * pada semua Node Worker
docker swarm join --token SWMTKN-1-4l29ny2l6u0s6fhujwxrxypzwy73a1u3cwxykac0s00vtpijib-1mgwa9pw2j3fijatvbe4gmrsb 192.168.88.100:2377
  • Cek status docker swarm pada Node Controller
docker node ls
---<output>---
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
u84mzdp4gqqi0ay75hyoii04d *   controller   Ready     Active         Leader           23.0.1
luutqruwq1yh4zd441hlr5y8j     worker1      Ready     Active                          23.0.1
q6rkgrtqkaislbppzmh75gk5q     worker2      Ready     Active                          23.0.1

Setting Firewalld untuk Service pada Node Controller dan Worker

  • Allow port 8080 dan port 8081
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --permanent --add-port=8081/tcp
firewall-cmd --reload

Deploy Service di Docker Swarm Cluster pada Node Controller

  • Deploy service nginx+php dengan nama nginx-web menggunakan image sysopsid/nginx-php dengan publish port 8080 ke port 8080 yang ada pada local service.
docker service create --publish 8080:8080 --name nginx-web --replicas 3 sysopsid/nginx-php
---<output>---
l1ofw7m1pekuny6wlvci0gkad
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
  • Cek status docker service
docker service ls
---<output>---
ID             NAME        MODE         REPLICAS   IMAGE                       PORTS
l1ofw7m1peku   nginx-web   replicated   3/3        sysopsid/nginx-php:latest   *:8080->8080/tcp
-------------------------------------------------------------
docker service ps nginx-web
---<output>---
ID             NAME          IMAGE                       NODE         DESIRED STATE   CURRENT STATE                ERROR     PORTS
nhsxdi65f7o9   nginx-web.1   sysopsid/nginx-php:latest   worker2      Running         Running about a minute ago
o9rp51ubxm6a   nginx-web.2   sysopsid/nginx-php:latest   controller   Running         Running about a minute ago
rjimwp8g0syk   nginx-web.3   sysopsid/nginx-php:latest   worker1      Running         Running about a minute ago
  • Akses docker service dengan IP node url: http://192.168.88.100:8080

Setting Node Load Balancer

  • Install Docker Swarm pada Node Load Balancer
docker swarm init --advertise-addr 192.168.88.88
---<output>---
Swarm initialized: current node (hairw06k1gjf18wwu0uc5xcyp) is now a manager.
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-0wwjzcjrzt29jypvlvji24i61ga0z6vdpobhoqmruojiesq7ht-e7ywde8on3jc8nk4i7yu3e86g 192.168.88.88:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
  • Setting firewalld allow port 80
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
  • Buat file konfigurasi nginx
mkdir -p /root/lb
nano /home/data/loadbalancer/default.conf
  • Isi file default.conf
upstream backend {
        server 192.168.88.11:8080;
        server 192.168.88.12:8080;
        server 192.168.88.100:8080;
    }

    server {
        listen      80;
        server_name 192.168.88.88;
        error_log /var/log/nginx/error.log error;
        access_log off;

        location / {
                proxy_redirect      off;
                proxy_set_header    X-Real-IP $remote_addr;
                proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header    Host $http_host;
                proxy_pass http://backend;
        }
}
  • Deploy service nginx denga nama loadbalancer dengan source mount: /root/lb dan target mount: /etc/nginx/conf.d dan publish port 80 ke port 80 yang ada pada local service.
docker service create --name loadbalancer --replicas 2 --mount type=bind,source=/root/lb,target=/etc/nginx/conf.d --publish 80:80 nginx
---<output>---
cj8jj3tfqhbkwh27sjoiq60ge
overall progress: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service converged
  • Cek status service loadbalancer
docker service ps loadbalancer
---<output>---
ID             NAME             IMAGE          NODE            DESIRED STATE   CURRENT STATE           ERROR     PORTS
aqzy7o3075eq   loadbalancer.1   nginx:latest   loadbalanceer   Running         Running 5 minutes ago
1nvlhqk28kdj   loadbalancer.2   nginx:latest   loadbalanceer   Running         Running 5 minutes ago
  • Akses load balancer dengan url: http://192.168.88.88

Setting Nginx Load Balancer Multi Backend Service

  • Deploy service baru pada Node Controller dengan nama nginx-test menggunakan image nginx dengan publish port 8081 ke port 80 yang ada pada local service.
docker service create --publish 8081:80 --name nginx-test --replicas 3 nginx
---<output>---
5aexydzzk7ufu10mdnjaidx07
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
  • Cek status docker service
docker service ls
---<output>---
ID             NAME         MODE         REPLICAS   IMAGE                       PORTS
5aexydzzk7uf   nginx-test   replicated   3/3        nginx:latest                *:8081->80/tcp
l1ofw7m1peku   nginx-web    replicated   3/3        sysopsid/nginx-php:latest   *:8080->8080/tcp
-------------------------------------------------------------
docker service ps nginx-web
---<output>---
ID             NAME          IMAGE                       NODE         DESIRED STATE   CURRENT STATE            ERROR     PORTS
nhsxdi65f7o9   nginx-web.1   sysopsid/nginx-php:latest   worker2      Running         Running 47 minutes ago
o9rp51ubxm6a   nginx-web.2   sysopsid/nginx-php:latest   controller   Running         Running 47 minutes ago
rjimwp8g0syk   nginx-web.3   sysopsid/nginx-php:latest   worker1      Running         Running 47 minutes ago
-------------------------------------------------------------
docker service ps nginx-test
---<output>---
ID             NAME           IMAGE          NODE         DESIRED STATE   CURRENT STATE           ERROR     PORTS
4ql1ic4l0ep2   nginx-test.1   nginx:latest   worker2      Running         Running 3 minutes ago
09nfuxlmg9sy   nginx-test.2   nginx:latest   controller   Running         Running 3 minutes ago
qesfur4fo634   nginx-test.3   nginx:latest   worker1      Running         Running 3 minutes ago

Edit File Nginx Load Balancer

  • DNS record server01.local & server02.local di pointing ke IP Node Load Balancer 192.168.88.88
  • Domain server01.local akan di arahkan ke backend service: nginx-web
  • Domain server02.local akan di arahkan ke backend service: nginx-test
  • Buat file dengan nama lb01.conf dan lb02.conf pada Node Load Balancer di folder /root/lb
  • Isi file lb01.conf
upstream backend01 {
        server 192.168.88.11:8080;
        server 192.168.88.12:8080;
        server 192.168.88.100:8080;
    }

    server {
        listen      80;
        server_name server01.local;
        error_log /var/log/nginx/error.log error;
        access_log off;

        location / {
                proxy_redirect      off;
                proxy_set_header    X-Real-IP $remote_addr;
                proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header    Host $http_host;
                proxy_pass http://backend01;
        }
}
  • Isi file lb02.conf
upstream backend02 {
        server 192.168.88.11:8081;
        server 192.168.88.12:8081;
        server 192.168.88.100:8081;
    }

    server {
        listen      80;
        server_name server02.local;

        location / {
                proxy_redirect      off;
                proxy_set_header    X-Real-IP $remote_addr;
                proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header    Host $http_host;
                proxy_pass http://backend02;
        }
}
  • Update service loadbalancer untuk menambahkan mounting file lb01.conf dan lb02.conf ke dalam service
docker service update --mount-add type=bind,source=/root/lb,target=/etc/nginx/conf.d loadbalancer
---<output>---
loadbalancer
overall progress: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service converged
-------------------------------------------------------------
docker service ls
---<output>---
ID             NAME           MODE         REPLICAS   IMAGE          PORTS
cj8jj3tfqhbk   loadbalancer   replicated   2/2        nginx:latest   *:80->80/tcp
  • Akses domain http://server01.local dan http://server02.local
C:\Users\Exa-Ops>ping server01.local

Pinging server01.local [192.168.88.88] with 32 bytes of data:
Reply from 192.168.88.88: bytes=32 time<1ms TTL=64
Reply from 192.168.88.88: bytes=32 time=1ms TTL=64
Reply from 192.168.88.88: bytes=32 time<1ms TTL=64
Reply from 192.168.88.88: bytes=32 time<1ms TTL=64


C:\Users\Exa-Ops>ping server02.local

Pinging server02.local [192.168.88.88] with 32 bytes of data:
Reply from 192.168.88.88: bytes=32 time<1ms TTL=64
Reply from 192.168.88.88: bytes=32 time<1ms TTL=64
Reply from 192.168.88.88: bytes=32 time=1ms TTL=64
Reply from 192.168.88.88: bytes=32 time<1ms TTL=64

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.