Lvs+keepalived+nginx environment building
Redhat builds centos image as yum source
1. Query the yum installation package for redhat
[root@localhost ~] # rpm-qa | grep yum
Yum-plugin-fastestmirror-1.1.30-37.el6.noarch
Yum-metadata-parser-1.1.2-16.el6.i686
Yum-utils-1.1.30-37.el6.noarch
Yum-plugin-security-1.1.30-37.el6.noarch
Yum-3.2.29-75.el6.centos.noarch
Yum-rhn-plugin-0.9.1-43.el6.noarch
two。 Delete the original yum
[root@localhost ~] # rpm-qa | grep yum | xargs rpm-e-- nodeps
[root@localhost ~] # rpm-qa | grep yum
3. Download the required installation package
Python-urlgrabber-3.9.1-11.el6.noarch.rpm
Yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
Yum-3.2.29-81.el6.centos.noarch.rpm
Yum-plugin-fastestmirror-1.1.30-40.el6.noarch.rpm
Yum-utils-1.1.30-40.el6.noarch.rpm
4. Install the RPM package
[root@localhost soft] # rpm-ivh-- force python-urlgrabber-3.9.1-11.el6.noarch.rpm
[root@localhost soft] # rpm-ivh yum-*.rpm
[root@localhost ~] # mkdir / yum
Upload the image to the / yum directory and configure the yum source
[root@localhost soft] # cd / etc/yum.repos.d/
[root@localhost yum.repos.d] # vi redhat.repo
[base]
Name=RedHat
Baseurl= file:///yum
Enabled=1
Gpgcheck=0
Gpgkey= http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
[root@localhost yum.repos.d] # yum clean all
[root@localhost yum.repos.d] # yum repolist
Configuration of Nginx Agent HA Cluster
1. Install keepalived on both machines to act as a HA cluster for nginx agents.
Host installs keepalived
[root@localhost soft] # tar-zxvf keepalived-1.2.7.tar.gz
[root@localhost soft] # cd keepalived-1.2.7
[root@localhost keepalived-1.2.7] # yum-y install popt-devel
[root@localhost keepalived-1.2.7] #. / configure-prefix=/usr/local/keepalived/
[root@localhost keepalived-1.2.7] # make
[root@localhost keepalived-1.2.7] # make install
[root@localhost keepalived-1.2.7] # cp / usr/local/keepalived/sbin/keepalived / usr/sbin/
[root@localhost keepalived-1.2.7] # cp / usr/local/keepalived/etc/sysconfig/keepalived / etc/sysconfig/
[root@localhost keepalived-1.2.7] # cp / usr/local/keepalived/etc/rc.d/init.d/keepalived / etc/init.d/
[root@localhost keepalived-1.2.7] # mkdir / etc/keepalived
[root@localhost keepalived-1.2.7] # cp / usr/local/keepalived/etc/keepalived/keepalived.conf / etc/keepalived/
[root@localhost keepalived-1.2.7] # cd / etc/keepalived/
[root@localhost keepalived-1.2.7] # cat / etc/keepalived/keepalived.conf
! Configuration File for keepalived
Global_defs {
Notification_email {
Acassen@firewall.loc
Failover@firewall.loc
Sysadmin@firewall.loc
}
Notification_email_from Alexandre.Cassen@firewall.loc
Smtp_server 192.168.200.1
Smtp_connect_timeout 30
Router_id LVS_DEVEL
}
Vrrp_instance VI_1 {
State MASTER (identity of the Lord)
Interface eth0 (main network card interface)
Virtual_router_id 51
Mcast_src_ip 192.168.0.111 (primary IP address)
Priority 100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
Virtual_ipaddress {
192.168.0.114
}
}
Standby installation keepalived
The installation process is the same as above, and the configuration file is as follows
[root@localhost keepalived] # cat / etc/keepalived/keepalived.conf
! Configuration File for keepalived
Global_defs {
Notification_email {
Acassen@firewall.loc
Failover@firewall.loc
Sysadmin@firewall.loc
}
Notification_email_from Alexandre.Cassen@firewall.loc
Smtp_server 192.168.200.1
Smtp_connect_timeout 30
Router_id LVS_DEVEL
}
Vrrp_instance VI_1 {
State SLAVER
Interface eth0
Virtual_router_id 51
Mcast_src_ip 192.168.0.112
Priority 90
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
Virtual_ipaddress {
192.168.0.114
}
}
two。 Start the keepalived service on both machines
[root@localhost ~] # service keepalived restart
3. Check if there is a virtual IP address on the host
4. Monitoring and active / standby switching
Write a script
[root@localhost ~] # cat nginx_check.sh
#! / bin/bash
A = `ps-C nginx-- no-header | wc-l`
If [$a-eq 0]; then
/ usr/local/nginx/sbin/nginx & > / dev/null
Sleep 3
If [`ps-C nginx-- no-header | wc-l`-eq 0]; then
Service keepalived stop & > / dev/null
Fi
Fi
[root@localhost ~] # chmod + x / root/nginx_check.sh
After the script has been written, now call the script in the configuration file of keepalived
[root@localhost ~] # cat / etc/keepalived/keepalived.conf
! Configuration File for keepalived
Global_defs {
Notification_email {
Acassen@firewall.loc
Failover@firewall.loc
Sysadmin@firewall.loc
}
Notification_email_from Alexandre.Cassen@firewall.loc
Smtp_server 192.168.200.1
Smtp_connect_timeout 30
Router_id LVS_DEVEL
}
Vrrp_script check_nginx_status {
Script "/ root/nginx_check.sh"
Interval 2
}
Vrrp_instance VI_1 {
State MASTER
Interface eth0
Virtual_router_id 51
Mcast_src_ip 192.168.0.111
Priority 100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
Track_script {
Check_nginx_status
}
Virtual_ipaddress {
192.168.0.114
}
}
Lvs load balancing
1. Install ipvsadm on the host machine
[root@bogon ~] # yum-y install ipvsadm
two。 Use keepalived to manage lvs
[root@bogon ~] # cat / etc/keepalived/keepalived.conf
! Configuration File for keepalived
Global_defs {
Notification_email {
Acassen@firewall.loc
Failover@firewall.loc
Sysadmin@firewall.loc
}
Notification_email_from Alexandre.Cassen@firewall.loc
Smtp_server 192.168.200.1
Smtp_connect_timeout 30
Router_id LVS_DEVEL
}
Vrrp_script check_nginx_status {
Script "/ root/nginx_check.sh"
Interval 2
}
Vrrp_instance VI_1 {
State MASTER
Interface eth0
Virtual_router_id 51
Priority 100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
Track_script {
Check_nginx_status
}
Virtual_ipaddress {
192.168.0.114
}
}
Virtual_server 192.168.0.114 80 {
Delay_loop 6
Lb_algo rr
Lb_kind DR
Nat_mask 255.255.255.0
Persistence_timeout 50
Protocol TCP
Real_server 192.168.0.111 80 {
Weight 1
TCP_CHECK {
Connect_timeout 3
Nb_get_retry 3
Delay_before_retry 3
}
}
Real_server 192.168.0.112 80 {
Weight 1
TCP_CHECK {
Connect_timeout 3
Nb_get_retry 3
Delay_before_retry 3
}
}
}
3. Write lvs management scripts
[root@bogon ~] # cat lvs_keepd.sh
#! / bin/sh
# description:start realserver
Vip=192.168.0.114
Source / etc/rc.d/init.d/functions
Case "$1" in
Start)
Echo "start server"
/ sbin/ifconfig lo:0$ vip broadcast $vip netmask 255.255.255.255 up
Echo 1 > / proc/sys/net/ipv4/conf/lo/arp_ignore
Echo 2 > / proc/sys/net/ipv4/conf/lo/arp_announce
Echo 1 > / proc/sys/net/ipv4/conf/all/arp_ignore
Echo 2 > / proc/sys/net/ipv4/conf/all/arp_announce
Stop)
Echo "stop server"
/ sbin/ifconfig lo:0 down
Echo "0" > / proc/sys/net/ipv4/conf/lo/arp_ignore
Echo "0" > / proc/sys/net/ipv4/conf/lo/arp_announce
Echo "0" > / proc/sys/net/ipv4/conf/all/arp_ignore
Echo "0" > / proc/sys/net/ipv4/conf/all/arp_announce
*)
Echo "Usage: $0 (start | stop)"
Exit 1
Esac
Exit 0
4. Startup script
[root@bogon ~] # bash / root/lvs_keepd.sh start
5. Test whether lvs is started
[root@bogon ~] # ipvsadm-ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.114:80 rr persistent 50
-> 192.168.0.111 purl 80 Local 11 0
-> 192.168.0.112 Route 80 1 0
The same is true for the operation on the standby machine.