KeepAlived + Nginx for high-availability load, keepalivednginx
I. Environment and installation version:
Centos6.5, Nginx1.4.7, keepalived1.3.2
Virtual IP Address |
Real IP |
Nginx Port |
Master-slave allocation |
10.0.90.215 |
10.0.90.217 |
80 |
MASTER |
10.0.90.215 |
10.0.90.218 |
80 |
BACKUP |
Ii. Install Nginx
Install Nginx on 10.0.90.217 and 10.0.90.218 respectively.
1. Install the installation package required by Nginx
# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
2. Upload nginx(nginx-1.4.7.tar.gz) to the/usr/local/src directory.
3. Compile and install Nginx
Go to the Nginx upload directory
# cd /usr/local/nginx
Decompress the installation package
# tar –zxvf nginx-1.4.7.tar.gz
Enter the decompressed folder
# cd nginx-1.4.7
Specify the compilation location
# ./configure --prefix=/usr/local/nginx
Compile and install
# make && make install
4. Configure Nginx
Modify the configuration information of nginx by modifying the/conf/Nginx. conf file.
# vim /usr/local/nginx/conf/nginx.conf
5. enable or disable the corresponding port 80 in the system firewall.
# vim /etc/sysconfig/iptables
Add:
## nginx-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
Restart the firewall after adding it:
# service iptables restart
Or disable the firewall directly without completing step 1 (not recommended)
# service iptables stop
6. Start nginx
# /usr/local/nginx/sbin/nginx
Check whether startup is successful
The actual IP address of the machine where the browser accesses Nginx is located. The following shows that the IP address is successfully accessed.
View Nginx process # ps-ef | grep nginx stop Nginx #/usr/local/nginx/sbin/nginx-s stop restart Nginx #/usr/local/nginx/sbin/nginx- s reload
3. Install Keepalived on 10.0.90.217 and 10.0.90.218, respectively)
1. upload or download keepalived(keepalived-1.3.2.tar.gz) to the/usr/local/src directory.
2. Unzip and install
# cd /usr/local/src/# tar -zxvf keepalived-1.3.2.tar.gz# ./configure --prefix=/usr/local/keepalived# make && make install
3. Install keepalived as a Linux System Service
Because keepalived is not installed in the default path (/usr/local by default), you need to manually copy the default configuration file to the default path after installation.
# Mkdir/etc/keepalived # cp/usr/local/keepalived/etc/keepalived. conf/etc/keepalived/# The following copy may sometimes fail to find the file or folder, leading to the inability to copy # cp/usr/local/keepalived/etc/rc. d/init. d/keepalived/etc/init. d/(may not succeed) # Replace with the following copy: # cp/usr/local/src/keepalived-1.3.2/keepalived/etc/init. d/keepalived/etc/init. d/
# Continue the following command
# Cp/usr/local/keepalived/etc/sysconfig/# ln-s/usr/local/sbin/keepalived/usr/sbin/# ln-s /usr/local/keepalived/sbin/
Set the keepalived service to start upon startup
# chkconfig keepalived on
4. Modify the keepalived configuration file.
# vim /etc/keepalived/keepalived.conf
The configuration is as follows:
! Configuration File for keepalivedglobal_defs {# The sendmail service must be enabled for the built-in email reminder of keepalived. We recommend that you use independent monitoring or a third-party SMTP router_id server1 # To identify the note string of the current node, usually it is hostname} vrrp_script chk_nginx {script "/etc/keepalived/check_nginx.sh" # Check nginx status script path interval 2 # Check interval weight-20 # if the condition is true, weight-20} vrrp_instance VI_1 {state MASTER # The MASTER node is the MASTER node, and the corresponding BACKUP node is the BACKUP interface eth0 # Network interface bound to the virtual IP address, same as the network interface where the local IP address is located, my network interface is eth0 virtual_router_id 146 # ID of the virtual route, the two nodes must be set to the same mcast_src_ip 10.0.90.218 # priority 100 # Node priority. The value ranges from 0 to 254. The MASTER node must be higher than the BACKUP node, the two node settings must be the same. The default 1 s authentication {# sets the authentication information, the two nodes must be consistent with auth_type PASS auth_pass 1111} track_script {chk_nginx # service for executing Nginx monitoring} virtual_ipaddress {10.0.90.215 # virtual ip address, multiple ip addresses can be defined }}
View Code
The check_nginx.sh script code is as follows:
1 #!/bin/bash 2 # curl -IL http://localhost/index.htm 3 count=0 4 for (( k=0; k<2; k++ )) 5 do 6 check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" http://localhost/index.html -o /dev/null ) 7 if [ "$check_code" != "200" ]; then 8 count=$(expr $count + 1) 9 sleep 310 continue11 else12 count=013 break14 fi15 done16 if [ "$count" != "0" ]; then17 /etc/init.d/keepalived stop18 # exit 119 else20 exit 021 fi
View Code
5. The most important thing is to add executable permissions for check_nginx.sh. Otherwise, no switchover is required and no error is reported.
# chmod +x /etc/check_nginx.sh
6. Modify the Nginx homepage to add the local IP address, so that you can view the effect after the Master/Slave switchover.
# vim /usr/local/nginx/html/index.html
7. Start keepalived (both keepalived servers must be started)
# Service keepalived start is displayed as follows: keepalived is successfully started: [OK]
8. run commands on the MASTER machine after startup.
# ip add
The configured virtual ip address 10.0.90.215 is sent to the Master.
Webpage Access Effect
9. Stop the Master service.
# service keepalived stop
10. Run on the BACKUP machine
# ip add
The virtual IP address has been moved to the BUCKUP machine.
Webpage access effect:
KeepAlived + nginx configuration is complete.
For more information about the KeepAlived parameters, see:
Http://blog.csdn.net/fruler/article/details/53334082