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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS(负载均衡)+keepalived(HA)+Nginx(反向代理)+Web(动静态网站服务器)相关的知识,希望对你有一定的参考价值。

考虑到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 [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)

技术分享

技术分享

本文出自 “Change life Start fresh.” 博客,请务必保留此出处http://ahcwy.blog.51cto.com/9853317/1940296

以上是关于LVS(负载均衡)+keepalived(HA)+Nginx(反向代理)+Web(动静态网站服务器)的主要内容,如果未能解决你的问题,请参考以下文章

Nginx安装 配置反向代理 负载均衡 upstream ssl证书提供https访问 ha nginx keepalived双主热备 LVS实现高可用负载 Keepalived+Lvs+Nginx

集群------LVS+Keepalived高可用负载均衡群集

LVS+Keepalived搭建高可用负载均衡

LVS&Keepalived—集群负载均衡企业高可用详解

mysql HA 负载均衡

Linux 高可用(HA)集群之keepalived详解