考慮到LVS和Nginx的缺點(因爲LVS採用的是同步請求轉發策略而Nginx採用的是異步轉發策略,結合二者的缺點:做爲負載均衡服務器的Nginx和LVS處理相同的請求時,全部的請求和響應流量都會通過Nginx服務器,可是使用LVS時,僅請求流量通過LVS的網絡,響應流量由後端的服務器的網絡返回,也就是說,當後端web服務器規模較大時,Nginx的網絡帶寬就成了一個巨大的瓶頸,可是僅僅使用LVS做爲負載均衡使用時,一旦後端接收到請求的服務器出了問題,那麼此次請求就失敗了,若是在LVS後端添加一層Nginx代理羣,結合二者的優點,就避免以上的狀況出現)再結合Keepalived實現LVS和Nginx的高可用node
條件:nginx
六臺虛擬機:web
兩臺LVS後端
兩臺Nginx瀏覽器
兩臺web服務器bash
LVS-M上面:(LVS-S也重作一遍)服務器
優化環境(/etc/sysctl.conf)網絡
net.ipv4.conf.all.send_redirects = 0負載均衡
net.ipv4.conf.default.send_redirects = 0異步
net.ipv4.conf.eth0.send_redirects = 0
sysctl -p
modprobe ip_vs
yum install -y ipvsadm
設置負載調度器模式
ipvsadm -A -t 192.168.115.180:80 -s rr
ipvsadm -a -t 192.168.115.180:80 -r 192.168.115.176:80 -g(176和177分別指向兩個nginx代理服務器)
ipvsadm -a -t 192.168.115.180:80 -r 192.168.115.177:80 -g
查看:
ipvsadm -Ln
安裝keepalived
yum install -y gcc* kernel-devel openssl-devel popt-devel
tar -xvf keepalived-1.2.7.tar.gz
./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-131.0.15.el6.i686
make && make install
chkconfig --add keepalived
chkconfig keepalived on
配置keepalived文件(LVS-M)
global_defs {
router_id LVS_R1
}
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.115.180
}
}
virtual_server 192.168.115.180 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.115.176 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.115.177 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
配置keepalived文件(LVS-S)
! Configuration File for keepalived
global_defs {
router_id LVS_R2
}
vrrp_instance VI_1 {
state SLAVE
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.115.180
}
}
virtual_server 192.168.115.180 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.115.176 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.115.177 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
啓動服務:
service keepalived start
chkconfig keepalived on
配置Nginx-M(Nginx-S也重作一遍)
安裝Nginx 和 keepalived
yum install -y pcre-devel zlib-devel
rpm -ivh nginx-1.8.1-1.el6.ngx.x86_64.rpm
keepalived的安裝參考上面
配置Nginx反向代理
配置keepalived(Nginx-M)
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from ops@wangshibo.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id master-node
}
vrrp_script chk_http_port {
script "/opt/chk_nginx.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
mcast_src_ip 192.168.115.176
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.115.180
}
track_script {
chk_http_port
}
}
配置keepalived(Nginx-S)
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from ops@wangshibo.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id master-node
}
vrrp_script chk_http_port {
script "/opt/chk_nginx.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_2 {
state SLAVE
interface eth0
mcast_src_ip 192.168.115.177
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.115.180
}
track_script {
chk_http_port
}
}
在/opt下面編寫腳本chk_nginx.sh(兩臺Nginx服務器都須要)
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
service nginx restart
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
service keepalived stop
fi
fi
賦予權限並執行
開啓keepalived服務
瀏覽器訪問:(在LVS上面任意停掉一臺服務器看訪問是否正常(斷開網卡)、在Nginx服務器上面任意停掉一臺Nginx服務看訪問是否正常(斷開Nginx服務service nginx stop))