authentication {
auth_type PASS # authentication အမျိုးအစားထဲမှာမှ passwordကိုသုံးထားပါတယ်။
auth_pass secret # ဒါကတော့ keepalivedအတွက် password ပါ။
}
# the virtual ip address shared between the two servers
virtual_ipaddress {
192.168.56.69 # ကိုယ်သုံးချင် Virtual IP (VIP) ပါ။
}
}
web1 မှာ sudo systemctl status keepalived ဆိုပြီးတော့ service status ကြည့်လိုက်ရင် အောင်ကအတိုင်းတွေ့ရမှာပါ။
vagrant@web1:~$ sudo systemctl status keepalived
● keepalived.service - Keepalive Daemon (LVS and VRRP)
Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-06-11 06:15:31 UTC; 6min ago
Main PID: 15463 (keepalived)
Tasks: 2 (limit: 1071)
Memory: 1.8M
CGroup: /system.slice/keepalived.service
├─15463 /usr/sbin/keepalived --dont-fork
└─15475 /usr/sbin/keepalived --dont-fork
Jun 11 06:15:31 web1 Keepalived[15463]: WARNING - keepalived was build for newer Linux 5.4.166, running>
Jun 11 06:15:31 web1 Keepalived[15463]: Command line: '/usr/sbin/keepalived' '--dont-fork'
Jun 11 06:15:31 web1 Keepalived[15463]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 11 06:15:31 web1 Keepalived[15463]: Starting VRRP child process, pid=15475
Jun 11 06:15:31 web1 Keepalived_vrrp[15475]: Registering Kernel netlink reflector
Jun 11 06:15:31 web1 Keepalived_vrrp[15475]: Registering Kernel netlink command channel
Jun 11 06:15:31 web1 Keepalived_vrrp[15475]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 11 06:15:31 web1 Keepalived_vrrp[15475]: Registering gratuitous ARP shared channel
Jun 11 06:15:31 web1 Keepalived_vrrp[15475]: (vi_1) Entering BACKUP STATE (init)
Jun 11 06:15:35 web1 Keepalived_vrrp[15475]: (vi_1) Entering MASTER STATE
web2 မှာကြည့်ရင်တော့ အခုလိုမျိုးတွေ့ရမှာပါ။
vagrant@web2:~$ sudo systemctl status keepalived
● keepalived.service - Keepalive Daemon (LVS and VRRP)
Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-06-11 06:17:53 UTC; 5min ago
Main PID: 15143 (keepalived)
Tasks: 2 (limit: 1071)
Memory: 1.8M
CGroup: /system.slice/keepalived.service
├─15143 /usr/sbin/keepalived --dont-fork
└─15153 /usr/sbin/keepalived --dont-fork
Jun 11 06:17:53 web2 Keepalived[15143]: Starting Keepalived v2.0.19 (10/19,2019)
Jun 11 06:17:53 web2 Keepalived[15143]: WARNING - keepalived was build for newer Linux 5.4.166, running >
Jun 11 06:17:53 web2 Keepalived[15143]: Command line: '/usr/sbin/keepalived' '--dont-fork'
Jun 11 06:17:53 web2 Keepalived[15143]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 11 06:17:53 web2 Keepalived[15143]: Starting VRRP child process, pid=15153
Jun 11 06:17:53 web2 Keepalived_vrrp[15153]: Registering Kernel netlink reflector
Jun 11 06:17:53 web2 Keepalived_vrrp[15153]: Registering Kernel netlink command channel
Jun 11 06:17:53 web2 Keepalived_vrrp[15153]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 11 06:17:53 web2 Keepalived_vrrp[15153]: Registering gratuitous ARP shared channel
Jun 11 06:17:53 web2 Keepalived_vrrp[15153]: (vi_1) Entering BACKUP STATE (init)
ကိုယ်ကစမ်းချင်တယ်ဆိုရင်တော့ web browser ပဲဖြစ်ဖြစ်၊ terminalထဲမှာပဲဖြစ်ဖြစ် အောက်ကအတိုင်းရိုက်ထည့်ပြီးတော့ ကြည့်လို့ရပါတယ်။
$ watch -n 30 curl 192.168.56.69
Every 30.0s: curl 192.168.56.69 e32: Sat Jun 11 16:35:29 2022
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 5 100 5 0 0 5000 0 --:--:-- --:--:-- --:--:-- 5000
web1
$ watch -n 30 curl 192.168.56.69
Every 30.0s: curl 192.168.56.69 e32: Sat Jun 11 16:35:29 2022
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 100 5 100 5 0 0 5000 0 --:--:-- --:--:-- --:--:-- 5000
web2
ရှင်းရှင်းလင်းလင်းတွေ့ချင်ရင်တော့ sudo systemctl status keepalived ကိုကြည့်လိုက်ရင် အခုလိုတွေ့ရမှာပါ။
vagrant@web1:~$ sudo systemctl status keepalived
● keepalived.service - Keepalive Daemon (LVS and VRRP)
Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-06-11 06:26:53 UTC; 4min 16s ago
Main PID: 638 (keepalived)
Tasks: 2 (limit: 1071)
Memory: 4.6M
CGroup: /system.slice/keepalived.service
├─638 /usr/sbin/keepalived --dont-fork
└─723 /usr/sbin/keepalived --dont-fork
Jun 11 06:26:54 web1 Keepalived_vrrp[723]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 11 06:26:54 web1 Keepalived_vrrp[723]: Registering gratuitous ARP shared channel
Jun 11 06:26:54 web1 Keepalived_vrrp[723]: (vi_1) Entering BACKUP STATE (init)
Jun 11 06:26:55 web1 Keepalived_vrrp[723]: (vi_1) received lower priority (100) advert from 192.168.56.>
Jun 11 06:26:56 web1 Keepalived_vrrp[723]: (vi_1) received lower priority (100) advert from 192.168.56.>
Jun 11 06:26:57 web1 Keepalived_vrrp[723]: (vi_1) received lower priority (100) advert from 192.168.56.>
Jun 11 06:26:57 web1 Keepalived_vrrp[723]: (vi_1) Entering MASTER STATE
Jun 11 06:27:01 web1 Keepalived_vrrp[723]: (vi_1) Entering BACKUP STATE
Jun 11 06:27:01 web1 Keepalived_vrrp[723]: (vi_1) sent 0 priority
Jun 11 06:27:01 web1 Keepalived_vrrp[723]: (vi_1) Entering MASTER STATE
vagrant@web2:~$ sudo systemctl status keepalived
● keepalived.service - Keepalive Daemon (LVS and VRRP)
Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-06-11 06:17:53 UTC; 13min ago
Main PID: 15143 (keepalived)
Tasks: 2 (limit: 1071)
Memory: 1.8M
CGroup: /system.slice/keepalived.service
├─15143 /usr/sbin/keepalived --dont-fork
└─15153 /usr/sbin/keepalived --dont-fork
Jun 11 06:17:53 web2 Keepalived_vrrp[15153]: Registering Kernel netlink reflector
Jun 11 06:17:53 web2 Keepalived_vrrp[15153]: Registering Kernel netlink command channel
Jun 11 06:17:53 web2 Keepalived_vrrp[15153]: Opening file '/etc/keepalived/keepalived.conf'.
Jun 11 06:17:53 web2 Keepalived_vrrp[15153]: Registering gratuitous ARP shared channel
Jun 11 06:17:53 web2 Keepalived_vrrp[15153]: (vi_1) Entering BACKUP STATE (init)
Jun 11 06:23:28 web2 Keepalived_vrrp[15153]: (vi_1) Backup received priority 0 advertisement
Jun 11 06:23:29 web2 Keepalived_vrrp[15153]: (vi_1) Entering MASTER STATE
Jun 11 06:26:58 web2 Keepalived_vrrp[15153]: (vi_1) Master received advert from 192.168.56.11 with highe>
Jun 11 06:26:58 web2 Keepalived_vrrp[15153]: (vi_1) Entering BACKUP STATE
Jun 11 06:27:01 web2 Keepalived_vrrp[15153]: (vi_1) Backup received priority 0 advertisement
web1 နဲ့ web2 Ubuntu 20.04 LTS server နှစ်လုံးကို အကုန်လုံး install လုပ်တာနဲ့ configure လုပ်တာ အချိန်ပေးရပါလိမ့်မယ်။ ဒီ့အတွက် စာရေးသူ Vagrant ကိုအသုံးပြုပြီးတော့ server နှစ်ခုကိုအောက်ကအတိုင်း stand up လုပ်ထားပါတယ်။
# -*- mode: ruby -*-
# vi: set ft=ruby :
BOX_IMAGE = "bento/ubuntu-20.04"
NODE_COUNT = 2
Vagrant.configure("2") do |config|
(1..NODE_COUNT).each do |x|
config.vm.define "web#{x}" do |subconfig|
subconfig.vm.box = BOX_IMAGE
subconfig.vm.hostname = "web#{x}"
subconfig.vm.network :private_network, ip: "192.168.56.#{x + 10}"
# subconfig.vm.provision :shell, path: "web_bootstrap.sh"
subconfig.vm.provision "shell", inline: <<-Web
sudo -i
apt update && apt install -y apache2 keepalived
echo web#{x} > /var/www/html/index.html
systemctl enable apache2 keepalived
systemctl start apache2 keepalived
Web
if (subconfig.vm.hostname=="web1") then
subconfig.vm.provision "file", source: "./keepalived_master.conf", destination: "~/keepalived.conf"
subconfig.vm.provision "shell", inline: <<-W1
sudo cp /home/vagrant/keepalived.conf /etc/keepalived/keepalived.conf
W1
else
subconfig.vm.provision "file", source: "./keepalived_backup.conf", destination: "~/keepalived.conf"
subconfig.vm.provision "shell", inline: <<-W2
sudo cp /home/vagrant/keepalived.conf /etc/keepalived/keepalived.conf
W2
end
subconfig.vm.provision "shell", inline: <<-W3
sudo systemctl restart apache2 keepalived
W3
end
end
# copy ssh public key to the VM and update then reboot
config.vm.provision "file", source: "./me.pub", destination: "~/.ssh/me.pub"
config.vm.provision "shell", inline: <<-SHELL
cat /home/vagrant/.ssh/me.pub >> /home/vagrant/.ssh/authorized_keys
SHELL
end
Vagrant ကို run ဖို့အတွက် လိုအပ်တဲ့ file တွေအားလုံးကို GitHub ပေါ်မှာတင်ပေးထားပါတယ်။