您的位置:首页 > 运维架构 > 反向代理

LVS(负载均衡)+keepalived(HA)+Nginx(反向代理)+Web(动静态网站服务器)

2017-06-20 19:42 609 查看
考虑到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 = 0net.ipv4.conf.default.send_redirects = 0net.ipv4.conf.eth0.send_redirects = 0sysctl -pmodprobe ip_vsyum install -y ipvsadm
设置负载调度器模式ipvsadm -A -t 192.168.115.180:80 -s rripvsadm -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


安装keepalivedyum install -y gcc* kernel-devel openssl-devel popt-develtar -xvf keepalived-1.2.7.tar.gz./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-131.0.15.el6.i686make && make installchkconfig --add keepalivedchkconfig keepalived on
配置keepalived文件(LVS-M)global_defs { router_id LVS_R1}vrrp_instance VI_1 { state MASTERinterface eth0virtual_router_id 51 priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress { 192.168.115.180}}virtual_server 192.168.115.180 80 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPreal_server 192.168.115.176 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.115.177 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}}

配置keepalived文件(LVS-S)! Configuration File for keepalivedglobal_defs { router_id LVS_R2}vrrp_instance VI_1 { state SLAVEinterface eth0virtual_router_id 51 priority 90advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.115.180}}virtual_server 192.168.115.180 80 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPreal_server 192.168.115.176 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.115.177 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}}启动服务:service keepalived startchkconfig keepalived on

配置Nginx-M(Nginx-S也重做一遍)安装Nginx 和 keepalivedyum install -y pcre-devel zlib-develrpm -ivh nginx-1.8.1-1.el6.ngx.x86_64.rpmkeepalived的安装参考上面

配置Nginx反向代理



配置keepalived(Nginx-M)! Configuration File for keepalivedglobal_defs {notification_email {ops@wangshibo.cntech@wangshibo.cn}notification_email_from ops@wangshibo.cnsmtp_server 127.0.0.1smtp_connect_timeout 30router_id master-node}vrrp_script chk_http_port { script "/opt/chk_nginx.sh"interval 2weight -5fall 2rise 1}vrrp_instance VI_1 { state MASTER interface eth0 mcast_src_ip 192.168.115.176virtual_router_id 51 priority 101advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.115.180}track_script {chk_http_port}}

配置keepalived(Nginx-S)! Configuration File for keepalivedglobal_defs {notification_email {ops@wangshibo.cntech@wangshibo.cn}notification_email_from ops@wangshibo.cnsmtp_server 127.0.0.1smtp_connect_timeout 30router_id master-node}vrrp_script chk_http_port { script "/opt/chk_nginx.sh"interval 2weight -5fall 2rise 1}vrrp_instance VI_2 { state SLAVE interface eth0 mcast_src_ip 192.168.115.177virtual_router_id 51 priority 99advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.115.180}track_script {chk_http_port}}
在/opt下面编写脚本chk_nginx.sh(两台Nginx服务器都需要)#!/bin/bashcounter=$(ps -C nginx --no-heading|wc -l)if [ "${counter}" = "0" ]; thenservice nginx restartsleep 2counter=$(ps -C nginx --no-heading|wc -l)if [ "${counter}" = "0" ]; thenservice keepalived stopfifi赋予权限并执行开启keepalived服务浏览器访问:(在LVS上面任意停掉一台服务器看访问是否正常(断开网卡)、在Nginx服务器上面任意停掉一台Nginx服务看访问是否正常(断开Nginx服务service nginx stop))



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息