實驗環境,四臺虛擬機,兩臺作負載均衡,兩臺作RS
IP地址:兩臺負載均衡分別爲:10.0.0.7;10.0.0.8(高可用keepalived)
兩臺 RS主機地址爲: 10.0.0.9;10.0.0.10
系統:centos6.6
介紹說明
實現Nginx負載均衡的組件主要有兩個,
ngx_http_proxy_module proxy代理模塊,用於把請求拋給服務器節點或者upstream服務池
ngx_http_unpstream_module 負載均衡模塊,能夠實現網站的負載均衡功能以及節點的健康檢查html
其中安裝過程以下,nginx
#安裝Nginx須要的依賴包 yum -y install openssl openssl-devel pcre pcre-devel #下載Nginx源碼包 wget -q http://nginx.org/download/nginx-1.6.3.tar.gz #解壓Nginx源碼包 tar xvf nginx-1.6.3.tar.gz #進入解壓以後的Nginx目錄 cd nginx-1.6.3 #建立Nginx的組 groupadd nginx #建立Nginx的用戶,而且不容許登陸操做系統 useradd -s /sbin/nologin -g nginx nginx #進行編譯 ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module #編譯後安裝 make && make install #建立一個軟鏈接 ln -s /usr/local/nginx-1.6.3/sbin/nginx /etc/init.d/nginx
啓動Nginx服務
/usr/local/nginx-1.6.3/sbin/nginx -c /usr/local/nginx-1.6.3/conf/nginx.conf
添加80端口到防火牆,被容許訪問
sed -i ‘10i -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT’ /etc/sysconfig/iptables
重啓防火牆
/etc/init.d/iptables restartweb
其中,兩臺RS的nginx.conf配置以下:vim
#Nginx的進程數 worker_processes 1; events { worker_connections 1024; } #主配置文件 http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" '; #進行虛擬主機等配置的模塊 server { listen 80; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } access_log logs/access_bbs.log main; } server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } access_log logs/access_bbs.log main; } }
而後分別在兩臺上執行如下命令centos
[root@web01 ~]# mkdir /usr/local/nginx-1.6.3/html/{www,bbs} [root@web01 ~]# for dir in www bbs;do echo "`ifconfig eth0|grep -o "10.0.0.[109]."` $dir " > /usr/local/nginx-1.6.3/html/$dir/index.html;done [root@web01 ~]# for dir in www bbs;do cat /usr/local/nginx-1.6.3/html/$dir/index.html ;done
[root@web01 ~]# mkdir /usr/local/nginx-1.6.3/html/{www,bbs} [root@web02 ~]# for dir in www bbs;do echo "`ifconfig eth0|grep -o "10.0.0.[109]."` $dir " > /usr/local/nginx-1.6.3/html/$dir/index.html;done [root@web02 ~]# for dir in www bbs;do cat /usr/local/nginx-1.6.3/html/$dir/index.html ;done
而後在主備負載均衡器:10.0.0.7,8兩臺機器上配置nginx.conf文件服務器
[root@lb01 ~]# vim /usr/local/nginx-1.6.3/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #定義web服務池,其中包含了兩個節點 upstream www_server_pools { server 10.0.0.9:80 weight=1; server 10.0.0.10:80 weight=1; } server { listen 80; server_name www.etiantian.org; location / { #訪問web服務器池的節點 proxy_pass http://www_server_pools; } } }
測試
因爲我本實驗沒有dns域名服務器解析IP地址,因此咱們得要在hosts文件裏面添加ip和對應的域名
首先在兩臺RS/etc/hosts分別加入app
10.0.0.9 www.etiantian.org 10.0.0.9 bbs.etiantian.org 10.0.0.10 www.etiantian.org 10.0.0.10 bbs.etiantian.org
而後在Nginx主負載均衡服務器上/etc/hosts負載均衡
10.0.0.7 www.etiantian.org
VRRP是虛擬路由冗餘協議,它是爲了解決靜態路由的單點故障的
VRRP是經過一種競選協議機制來將路由任務交給某臺VRRP路由器的
VRRP用IP多播的方式實現高可用之間的通訊
VRRP工做是主節點發包,備節點接包,檔備節點收不到主節點發的數據包的時候,就啓動接管程序接管主節點的資源。備節點能夠有不少個,經過優先級競選,但通常keepalive系統運維中都是一對存在的運維
1. 所以,keepalive是經過VRRP進行通訊的,VRRP是經過競選機制進行肯定主備的,主的優選級高於備的優級,工做時候,主首先得到全部資源,備節點處於等待狀態,當主節宕機的時候,備節點就會接管主節點的全部資源,而後頂替主節點對外提供全部服
開始安裝keepalived軟件
yum -y install keepalived
/etc/init.d/keepalived start
修改配置文件
主節點tcp
1 ! Configuration File for keepalived
2
3 global_defs {
4 notification_email {
5 919497370@qq.com
6 #failover@firewall.loc
7 #sysadmin@firewall.loc
8 }
9 notification_email_from Alexandre.Cassen@firewall.loc
10 smtp_server smtp.qq.com
11 smtp_connect_timeout 30
12 router_id lb01
13 }
14
15 vrrp_instance VI_1 {
16 state MASTER
17 interface eth0
18 virtual_router_id 55
19 priority 150
20 advert_int 1
21 authentication {
22 auth_type PASS
23 auth_pass 1111
24 }
25 virtual_ipaddress {
26 #192.168.200.16
27 #192.168.200.17
28 #192.168.200.18
29 10.0.0.12/24 dev eth0 label eth0:1
30 }
31 }
備節點
1 ! Configuration File for keepalived 2 3 global_defs { 4 notification_email { 5 919497370@qq.com 6 #failover@firewall.loc 7 #sysadmin@firewall.loc 8 } 9 notification_email_from Alexandre.Cassen@firewall.loc 10 smtp_server smtp.qq.com 11 smtp_connect_timeout 30 12 router_id lb02 13 } 14 15 vrrp_instance VI_1 { 16 state BACKUP 17 interface eth0 18 virtual_router_id 55 19 priority 100 20 advert_int 1 21 authentication { 22 auth_type PASS 23 auth_pass 1111 24 } 25 virtual_ipaddress { 26 #192.168.200.16 27 #192.168.200.17 28 #192.168.200.18 29 10.0.0.12/24 dev eth0 label eth0:1 30 } 31 } 32