1. 程式人生 > >LVS(負載均衡)+keepalived(HA)+Nginx(反向代理)+Web(動靜態網站服務器)

LVS(負載均衡)+keepalived(HA)+Nginx(反向代理)+Web(動靜態網站服務器)

網站服務器 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(動靜態網站服務器)