考虑到LVS和Nginx的缺点(由于LVS采用的是同步请求转发策略而Nginx采用的是异步转发策略,结合两者的缺点:作为负载均衡服务器的Nginx和LVS处理相同的请求时,所有的请求和响应流量都会经过Nginx服务器,但是使用LVS时,仅请求流量经过LVS的网络,响应流量由后端的服务器的网络返回,也就是说,当后端web服务器规模较大时,Nginx的网络带宽就成了一个巨大的瓶颈,但是仅仅使用LVS作为负载均衡使用时,一旦后端接收到请求的服务器出了问题,那么这次请求就失败了,如果在LVS后端添加一层Nginx代理群,结合两者的优势,就避免以上的情况出现)再结合Keepalived实现LVS和Nginx的高可用
条件:
六台虚拟机:
两台LVS
两台Nginx
两台web服务器
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 {
[email protected]
[email protected]
}
notification_email_from [email protected]
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 {
[email protected]
[email protected]
}
notification_email_from o[email protected]
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))