Build high availability for top up game service as a solution to reduce excessive use of resources on the server side. Using HAProxy with roundrobin algorithm.
Created by Muhammad Zaidan.
- Getting Started
- Setup App Source Code
- Initialization Swarm
- Build & Push Image
- Deploy Stack
- Monitoring
- Test Application
We need to setup 3 Virtual Machine using VirtualBox, then install docker & setup user to run docker command without root privileged.
In this lab environment we use the Ubuntu 22.04 OS, with the following specifications
| No | Virtual Machine | Spesification | NATNetwork | Host-Only Network |
|---|---|---|---|---|
| 1 | Manager-01 | 1vcpu, 2GB Ram | 192.168.16.10/24 | 16.16.16.10/24 |
| 2 | Worker-01 | 1vcpu, 1GB Ram | 192.168.16.11/24 | 16.16.16.11/24 |
| 3 | Worker-02 | 1vcpu, 1GB Ram | 192.168.16.12/24 | 16.16.16.12/24 |
There are 2 networks, namely NAT Network for internet access needs and Host-Only Network for remote VM needs from the Host and used for communication between vm.
Edit /etc/netplan/00-installer-config.yaml on each node :

Apply network configuration & change hostname
sudo netplan apply
sudo hostnamectl hostname manager-01sudo netplan apply
sudo hostnamectl hostname worker-01sudo netplan apply
sudo hostnamectl hostname worker-02Map host in /etc/hosts on each node

Create a ssh-keygen on windows host so you can ssh without a password
PS C:\Users\Zai> ssh-keygen
PS C:\Users\Zai> type $env:USERPROFILE\.ssh\id_rsa.pub | ssh zai@16.16.16.10 "cat >> .ssh/authorized_keys"Check if the host can access without using a password (passwordless).
PS C:\Users\Zai> ssh zai@16.16.16.10 "whoami; hostname"
zai
manager-01
PS C:\Users\Zai>There is two method to install Docker Engine, using docker install script or using script that i made.
- Docker install script from docker.com
curl -sSL https://get.docker.com/ | sudo sh- Docker install script from my git repository
git clone https://github.com/zaidanm16/docker-ha.git
cd installation_docker
sudo chmod u+x install-jammy.sh
sudo ./install-jammy.shTest Installed Docker Engine
docker version
docker info
sudo systemctl status dockerAdd user to docker group, so user can run docker command without root privileged.
Run user.sh from my git repository
sudo chmod u+x user.sh
sudo ./user.shor you can manually run
sudo usermod -aG docker $USER
sudo chmod 666 /var/run/docker.sockMove all your application source code into the web/app directory
cd web/app
git clone https://github.com/zaidanm16/widan-store.git .Steps to initialization docker swarm cluster with 1 manager node and 2 worker node
You need login to dockerhub with your account then init the swarm cluster
docker login -u [username]
docker swarm init --advertise-addr 16.16.16.10Copy command docker join, token will be used by worker node
docker swarm join-token workerRun this command after worker node already join
docker node lsYou can get your swarm [TOKEN] from manager node
docker swarm join --token [TOKEN] 16.16.16.10:2377In this section we will build image for our application & push it to docker hub
Build database image using Dockerfile.
Before build the image, edit the Dockerfile according to your needs. I'm using mariadb image with version 10.7.8. You can read comments in the Dockerfile, to see explanation for every steps in building the image.
Note :
[username] is your docker hub account
[image-name] is your image name
[tag] is your image tag or your image version
[path] is path to Dockerfile, if Dockerfile is in the same directory you can use dot (.)
cd db
docker build -t [username]/[image-name]:[tag] [path]So mine is :
docker build -t kevinmz/mariadb:10.7.8 .After building succeed, you can push the image to dockerhub
docker push [username]/[image-name]:[tag]So mine is :
docker push kevinmz/mariadb:10.7.8Build your application using Dockerfile. Before build the image, edit the Dockerfile according to your needs. I'm using php image with version 8.1 and using Apache HTTPD as web Server. You can read comments in the Dockerfile, to see explanation for every steps in building the image.
Note :
[username] is your docker hub account
[image-name] is your image name
[tag] is your image tag or your image version
[path] is path to Dockerfile, if Dockerfile is in the same directory you can use dot (.)
cd web
docker build -t [username]/[image-name]:[tag] [path]So mine is :
docker build -t kevinmz/app-widan:v1 .After building succeed, you can push the image to dockerhub
docker push [username]/[image-name]:[tag]So mine is :
docker push kevinmz/app-widan:v1Before deploy the stack, you need to configure haproxy.conf according your needs.
Deploy your stack application in widan.yml & create secret for db password in widan.yml
- Using script to deploy & create secret
sudo chmod u+x deploy.sh
sudo ./deploy.sh- Manually create secret & deploy stack
echo '[your_password]' | docker secret create mysql_root_password -
docker stack deploy -c [compose-file] [stack-name] --with-registry-auth
Example :
echo 'pa$$w0rd' | docker secret create mysql_root_password -
docker stack deploy -c widan.yml widan --with-registry-authAfter deploy succeed, run docker service to see that service is replicated & running or not.
docker service lsYou can check each service
docker service ps widan_web
docker service ps widan_db
docker service ps widan_lbRun the Application from Windows Host at
http://[manager-ip]
http://16.16.16.10
See your haproxy load balancing stats at http://[manager-ip]/haproxy
http://16.16.16.10/haproxy
Login using credentials that you create at haproxy.cfg
Testing Performance of the Application
BELUM BERES


