Lab environment and software version:
CentOS version: 6.6 (2.6.32.-504.el6.x86_64)
nginx Version: nginx-1.6.2
Keepalived version: keepalived-1.2.7
Preparatory work:
Shut down firewall: Service iptables stop
Chkconfig iptables off
Close Selinux:sed-i ' s/selinux=disabled/selinux=enforcing/g '/etc/selinux/config
Init 6 reboot system must be!
1. Prepare to install the dependent packages:
Yum-y install pcre pcre-devel OpenSSL openssl-devel zlib zlib-devel gcc gcc-c++ popt-devel
Installation keepalived appears configure:error:Popt libraries is required
Workaround: Yum-y Install Popt-devel
2. Download the installation package
wget http://nginx.org/download/nginx-1.6.2.tar.gz
wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
3. Installing Nginx
Tar zxvf nginx-1.6.2.tar.gz
CD nginx-1.6.2
./configure
Make && make install
4. Installing keepalived
Tar zxvf keepalived-1.2.7.tar.gz
CD keepalived-1.2.7
./configure
Make
Make install
cp/usr/local/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/
Mkdir/etc/keepalived
cp/usr/local/etc/keepalived/keepalived.conf/etc/keepalived/
cp/usr/local/sbin/keepalived/usr/sbin/
5. Join the boot-up service
Method One
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
echo "/etc/init.d/keepalived start" >>/etc/rc.local
Method Two
[email protected] keepalived-1.2.7]# cat >>/etc/rc.local<<eof
>/usr/local/nginx/sbin/nginx
>/etc/init.d/keepalived Start
> EOF
[Email protected] keepalived-1.2.7]# cat/etc/rc.local
#!/bin/sh
#
# This script is executed *after* all and the other init scripts.
# can put your own initialization stuff in here if you don ' t
# want to does the full Sys V style init stuff.
Touch/var/lock/subsys/local
/usr/local/nginx/sbin/nginx
/etc/init.d/keepalived start
6. Configuration
6.1 Configuring Nginx
The Nginx configuration of the two access servers is exactly the same, with the main configuration/usr/local/nginx/conf/nginx.conf http. Where multi-domain points are implemented through a virtual host (server configured under HTTP); Different virtual directories of the same domain name are passed through each
Different location implementations under each server.
HTTP {
Include Mime.types;
Default_type Application/octet-stream;
#log_format Main ' $remote _addr-$remote _user [$time _local] "$re
Quest "'
# ' $status $body _bytes_sent ' $http _referer '
# ' "$http _user_agent" "$http _x_forwarded_for" ';
#access_log Logs/access.log Main;
Sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
Keepalive_timeout 65;
#gzip on;
Upstream myserver{
Server 192.168.0.110:80 max_fails=3 fail_timeout=20s;
Server 192.168.0.111:80 max_fails=3 fail_timeout=20s;
Ip_hash;
}
server {
Listen 80;
server_name 192.168.0.150;
#charset Koi8-r;
#access_log Logs/host.access.log Main;
Location/{
root/var/www/html;
Index index.php index.htm index.html;
Proxy_redirect off;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_next_upstream http_500 http_502 http_503 Error Timeo
UT invalid_header;
Proxy_set_header x-forwarded-for $remote _addr;
Proxy_pass http://MyServer;
}
}
6.2 Configuring Keepalived
According to the installation method above, the keepalived configuration file is/etc/keepalived/keepalived.conf. master, slave server
Configuration is associated but different. As follows:
Master:
! Configuration File for Keepalived
Global_defs {
Notification_email {
[Email protected]
[Email protected]
[Email protected]
}
Notification_email_from [email protected]
Smtp_server smtp.etiantian.org
Smtp_connect_timeout 30
router_id Nginx_master
}
Vrrp_instance Vi_1 {
State MASTER
Interface eth0
VIRTUAL_ROUTER_ID 51
Priority 100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {
192.168.0.150
}
}
Backup:
! Configuration File for Keepalived
Global_defs {
Notification_email {
[Email protected]
[Email protected]
[Email protected]
}
Notification_email_from [email protected]
Smtp_server smtp.etiantian.org
Smtp_connect_timeout 30
router_id Nginx_backup
}
Vrrp_instance Vi_1 {
State BACKUP
Interface eth0
VIRTUAL_ROUTER_ID 51
Priority 99
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {
192.168.0.150
}
}
7. Verify that:
Start/usr/local/nginx/sbin/nginx on the master and slave servers successively
/etc/init.d/keepalived start
To see if a virtual IP (VIP) is already bound on the primary server: IP addr
[[Email protected] ~]# IP addr
1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN
Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00
inet 127.0.0.1/8 Scope host Lo
INET6:: 1/128 Scope Host
Valid_lft Forever Preferred_lft Forever
2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up Qlen 1000
Link/ether 00:0c:29:af:00:4e BRD FF:FF:FF:FF:FF:FF
inet 192.168.0.130/24 BRD 192.168.0.255 Scope Global eth0
inet 192.168.0.150/32 Scope Global eth0
Inet6 FE80::20C:29FF:FEAF:4E/64 Scope link
Valid_lft Forever Preferred_lft Forever
Stop keepalived:/etc/init.d/keepalived stop on the primary server and then check from the server to see if it is already tied
Virtual IP (VIP):
[[Email protected]_back ~]# IP Addr
1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN
Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00
inet 127.0.0.1/8 Scope host Lo
INET6:: 1/128 Scope Host
Valid_lft Forever Preferred_lft Forever
2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up Qlen 1000
Link/ether 00:0C:29:21:B6:B1 BRD FF:FF:FF:FF:FF:FF
inet 192.168.0.131/24 BRD 192.168.0.255 Scope Global eth0
inet 192.168.0.150/32 Scope Global eth0
Inet6 FE80::20C:29FF:FE21:B6B1/64 Scope link
Valid_lft Forever Preferred_lft Forever
Start keepalived:/etc/init.d/keepalived on the primary server and see if the primary server can re-take over the virtual IP (VIP)
[[Email protected] ~]# IP addr
1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN
Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00
inet 127.0.0.1/8 Scope host Lo
INET6:: 1/128 Scope Host
Valid_lft Forever Preferred_lft Forever
2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up Qlen 1000
Link/ether 00:0c:29:af:00:4e BRD FF:FF:FF:FF:FF:FF
inet 192.168.0.130/24 BRD 192.168.0.255 Scope Global eth0
inet 192.168.0.150/32 Scope Global eth0
Inet6 FE80::20C:29FF:FEAF:4E/64 Scope link
Valid_lft Forever Preferred_lft Forever
This article is from the "Lanzhou Linux operation and Maintenance" blog, please be sure to keep this source http://linuxzkq.blog.51cto.com/9379412/1631209
Nginx+keepalived primary and Standby load Balancing learning Notes