1. Architecture
In the Keepalived + Nginx high-reliability load balancing architecture, keepalived is responsible for achieving High-availability
(HA)
The function controls the VIP (virtual network address) of the front-end machine. When a device fails, the hot standby server can automatically switch over to the VIP in an instant. In actual operation, the switching time is only 2 seconds. The DNS service can be responsible for the front-end VIP. load balancing.
2. Topology
3. Simple principle
Both Nginx-Keepalived-M (nginx master server) and Nginx-Keepalived-S (standby server) use keepalived software to tie the eth0 network card to a virtual IP (VIP1) address: 172.16.1.20 , this VIP1 is currently bound to whose eth0 is whoever carries the service. When Nginx-Keepalived-M fails, Nginx-Keepalived-S will pass the heartbeat time advert_int set in the /etc/keepalived/keepalived.conf file
1 check, unable to obtain the normal state of Nginx-Keepalived-M, switch to Nginx-Keepalived-S instantly to achieve hot dual-machine load balancing, when Nginx-Keepalived-M recovers, keepalived will pass the priority
Parameter judgment priority Rebind the virtual VIP1 address 172.16.1.20 to the eth0 network card of Nginx-Keepalived-M; similarly, the virtual IP (VIP2) address 172.16.1.21 regards the previous Nginx-Keepalived-S as the main server, Nginx -Keepalived-M is used as a secondary server to achieve load balancing of hot dual-machine mutual backup.
4. Environment
1. Centos6.3 Mini x64
(Nginx-Keepalived-M)IP:172.16.1.22
Vip: 172.16.1.20
2. Centos6.3 Mini x64 (Nginx-Keepalived-S)
IP:172.16.1.23 Vip:172.16.1.21
5. Installation and configuration
Install nginx on Nginx-Keepalived-M and Nginx-Keepalived-S respectively
As well as keepalived, the installation process is skipped.
1. Software installation path
/usr/local/nginx
/usr/local/keepalived
#cp /usr/local/keepalived/etc/keepalived /etc/keepalived
2. Keepalived configuration of nginx main server
#vi /etc/keepalived/keepalived.conf
??????????????????????? –
!Configuration File for keepalived
# written byucg.me
global_defs {
router_id Nginx_Id_1
}
vrrp_script chk_nginx {
script “/usr/local/keepalived/etc/check_http.sh” #Define the detection script
interval 2 #detection interval
weight 2
}
vrrp_instance Nginx1 { #Define an instance
state MASTER #Defined as master
interface eth0
virtual_router_id 51 # 0-255 Consistent in the same instance Unique in the entire vrrp
priority 150 #priority, the one with the highest priority will become the master
authentication {
auth_type PASS
auth_pass 1111
}
track_script { #check script
chk_nginx
}
virtual_ipaddress { #The floating IP of this instance
172.16.1.20
}
}
vrrp_instance Nginx2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 110
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.21
}
}
save and exit
???????????????????????C
Detection script:
#vi /usr/local/keepalived/etc/check_http.sh
?????????????????????
#!/bin/bash
if [ “$(ps -ef | grep “nginx: master process”| grep -v grep )” ==
“” ]
then
killall -9 keepalived
the fi
save and exit
????????????????
3. Keepalived configuration of nginx standby server
vi /etc/keepalived/keepalived.conf
???????????????????
!Configuration File for keepalived
# written byucg.me
global_defs {
router_id Nginx_Id_2
}
vrrp_script chk_nginx {
script “/usr/local/keepalived/etc/check_http.sh”
interval 2
weight 2
}
vrrp_instance Nginx1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 110
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.20
}
}
vrrp_instance Nginx2 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
172.16.1.21
}
}
6. Summary
This is the end of nginx+keepalived dual-machine mutual backup.