Keepalived for Nginx Load balance on Debian
Keepalived คือโปรแกรมที่ทำหน้าที่เช็คสถานะของเครื่อง load balance โดยตัวมันจะทำหน้าที่เช็ค ถ้าเครื่องใดเครื่องหนึ่งตาย มันก็จะเปลี่ยน Virtual IP ไปเป็นอีกเครื่อง
- ติดตั้ง Keepalived และ config โดยทำเหมือนกันทุกเครื่องที่เป็น loadbalance
apt-get update
apt-get install -y keepalived
Config แบบ Master-Master คือ ให้ความสำคัญทุกโหนดเท่ากัน
เมื่อเครื่องแรกขาดการติดต่อ ip จะไปอยุ่เครื่องที่สอง ไม่กลับไปเครื่องแรกจนกว่าเครื่องสองขาดการติดต่อ
- Config Keepalived on LB1 Master-Master
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 101
priority 101
advert_int 1
authentication {
auth_type PASS
#ไม่เกิน 8 ตัว ไม่มีอักขระพิเศษ#
auth_pass 1111
}
virtual_ipaddress {
# IP for Keepalive
192.168.10.121
}
}
- Config Keepalived on LB2 Master-Master
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 101
priority 101
advert_int 1
authentication {
auth_type PASS
#ไม่เกิน 8 ตัว ไม่มีอักขระพิเศษ#
auth_pass 1111
}
virtual_ipaddress {
# IP for Keepalive
192.168.10.121
}
}
Config แบบ Master-Slave คือ เมื่อเครื่อง Master ขาดการติดต่อ IP จะไปอยู่ที่เครื่อง Slave และเมื่อ Master กลับมา IP จะกลับไปที่เครื่อง Master
ค่า priority จะเป็นตัวบอกว่าเครื่องไหน เป็น MASTER โดยค่าที่มากที่สุดจะเป็น Master ค่าของ MASTER ควรห่างจากเครื่อง SLAVE 50 ตาม config ตัวอย่าง
- Config Keepalived on LB1 Master-Slave
! Configuration File for keepalived
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 101
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# IP for Keepalive
192.168.100.90
}
}
- Config Keepalived on LB2 Master-Slave
! Configuration File for keepalived
vrrp_instance VI_1 {
state SLAVE
interface ens33
virtual_router_id 101
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# IP for Keepalive
192.168.100.90
}
}
Start KeepAlived Service
systemctl start keepalived
สามารถทดสอบว่าทำงานหรือ ไม่โดยสั่งคำสั่งนี้ที่โหนดที่มี IP อยู่
ตัวอย่าง Config Master-Master
systemctl stop keepalived.service


ตัวอย่าง Config Master-Slave


References website
How to Setup IP Failover with KeepAlived on Ubuntu & Debian