实验环境,四台虚拟机,两台作负载均衡,两台作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