本实验用4台 centos6 虚拟机,2台做负载均衡,2台做web服务器,都先装上nginx

lb01:192.168.0.235  --主负载均衡器

lb02:192.168.0.236  --备负载均衡器

web1:192.168.0.237

web2:192.168.0.238

VIP:192.168.0.248

一. 在web1和web2上配置基于域名的虚拟主机,以web1为例

1. 配置nginx.conf,  推荐方法 egrep -v "#|^$" nginx.conf.default > nginx.conf生成简洁配置再修改

worker_processes  ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
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 ;
server_name bbs.axinfu.com;
location / {
root html/bbs; #站点目录
index index.html index.htm;
}
access_log logs/access_bbs.log main; #这里用于测试proxy_set_header X-Forwarded-For $remote_addr命令
} server {
listen ;
server_name www.axinfu.com;
location / {
root html/www; #站点目录
index index.html index.htm; }
access_log logs/access_www.log main;
}
}

注意

1. 这里有2个虚拟主机,bbs.axinfu.com和www.axinfu.com,这里故意把bbs放在上面,以便测试lb01和lb02上nginx的配置命令proxy_set_header Host $host;

2.类似于$remote_addr(客户端地址),$host这样的内置变量可参考 http://www.cnphp.info/nginx-embedded-variables-lasted-version.html

2. 在站点目录建立测试文件

我的nginx目录为/usr/local/nginx/,在此目录下新建bbs,www两个目录,都新建index.html,分别写入

hello, web1, i am bbs

hello, web1, i am www

在web2上配置相似,在index.html中分别写入hello, web2, i am bbs/ hello, web2, i am www

3. 检查语法并重启nginx服务

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx -s reload

netstat -lntup | grep 80

4. 把域名加入hosts解析,在/etc/hosts写入

192.168.0.237  bbs.axinfu.com

192.168.0.237  www.axinfu.com

5. 测试

curl bbs.axinfu.com  应该返回 hello, web1, i am bbs

curl www.axinfu.com  应该返回 hello, web1, i am www

二.  在lb01和lb02上配置nginx负载均衡如下,cat nginx.conf

worker_processes  ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;
upstream www_server_pools {
server 192.168.0.237: weight=;
server 192.168.0.238: weight=; }
server {
listen 192.168.0.248; #指定监听的VIP
server_name www.axinfu.com;
location / {
proxy_pass http://www_server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
} }
}

此配置仅代理里www.etiantian.org域名

命令说明

1. proxy_set_header Host $host;

这个命令的作用是识别代理的哪个虚拟主机,这里代理的是www.axinfu.com( server_name写明了)

本实验在web服务器上配置了2个虚拟主机,如果不加这个命令,访问192.168.0.248时就把节点服务器的第一个虚拟主机发给反向代理,也就是bbs.axinfu.com

2. proxy_set_header X-Forwarded-For $remote_addr;

此命令与web1/2服务器上虚拟主机的访问日志有关,也就是 /usr/local/nginx/logs/access_www.log,正常情况下,这个文件最后一个字段应显示访客IP

比如使用192.168.0.230作为客户端进行测试时,不加这个命令第一个字段显示的是反向代理服务器的IP,最后一个字段显示的为"-",加上上面的命令,日志的最后一个字段显示的是客户端访问IP,”192.168.0.230"

这个命令要在web服务器上开启log_format功能,就是在nginx配置文件中的log_format块加上”http_x_forwarded_for"

3. 如果不要负载均衡,只是需要转发功能。那么删掉upstream代码块,在proxy_pass直接写要转发到的地址就好,比如proxy_pass http://127.0.0.1:9000

三.  在lb01和lb02上配置keepalived服务

1. 在lb01上keepalived服务单实例主节点的配置如下,cat keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout
router_id lb01

} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 55 #主备的虚拟路由ID号要一样
priority 150

advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.0.248/ dev eth0 label eth0:
}
}

2. lb02上keepalived服务单实例备节点配置如下,cat keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout
router_id lb02

} vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id
priority 100

advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.0.248/ dev eth0 label eth0: }
}

四. 测试

先开启lb01的nginx和keepalived, 然后开启lb02的nginx和keepalived

在网页上输入 http://192.168.0.248,刷新即可看到切换hello, web1, i am www, hello, web2, i am www.

关闭lb01的keepalived,可以看到lb02启动切换到VIP:192.168.0.248, 可用ip add | grep 192.168.0.248查看

刷新页面192.168.0.248,内容还和之前一样,则说明服务正常

五. 可能存在的问题及注意

1. 要把防火墙关闭

2. lb02上开启nginx有时候会出现VIP地址冲突,不能分配的问题

在/etc/sysctl.conf加入内核参数配置,net.ipv4.ip_nonlocal_bind = 1 表示启动nginx而忽略配置中监听的IP是否存在, 执行sysctl -p使修改生效,注意环境是centos6

3. 默认情况下keepalived仅在对方机器停机或者keepalived停掉的时候才会接管业务,但在实际工作中,如果nginx服务停止而keepalived还在工作的情况下,就会导致用户访问的VIP无法找到对应的业务

通俗点说,就是lb01的nginx挂了,但是keeplived是好的,怎么让lb02来接管业务

第一种方法,写个守护进程脚本,当nginx挂了就停掉本地的keepalived

#!/bin/bash
while true
do
if [ `netstat -lntup | grep nginx | wc -l` -ne ];then
/etc/init.d/keepalived stop
fi
sleep
done

第二种方法,在keepalived的配置文件中,加入检测服务脚本,检测监本如下

cat chk_nginx_proxy.sh

#!/bin/bash
if [ `netstat -lntup | grep nginx | wc -l` -ne ];then
/etc/init.d/keepalived stop
fi

增加执行权限,chmod +x chk_nginx_proxy.sh。 然后修改keepalived.conf,完整配置如下

lb01 # cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout
router_id lb01
} vrrp_script chk_nginx_proxy { #定义vrrp脚本
script "/server/scripts/chk_nginx_proxy.sh" #脚本位置
interval 2
weight 2
} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.0.248/ dev eth0 label eth0:
} track_script{
chk_nginx_proxy #触发检查
}
}

当lb01停掉nginx后,keepalived2秒内会被自动停掉,VIP释放,由lb02接管,这样就实现了即使服务宕机也会进行IP漂移,业务切换

最新文章

  1. Libevent初探
  2. pt-table-checksum 检查主从数据一致性
  3. mysql启动不成功显示The server quit without updating PID file的解决方法
  4. PresentViewController切换界面(一些系统自带的页面切换动画)
  5. golang 移动应用例子 example/basic 源码框架分析
  6. onbeforeunload、beforeunload
  7. ios 中的半屏幕底部弹出框
  8. Oracle的SCN与检查点机制
  9. ZOJ2112 Dynamic Rankings 动态区间第K最值 平方分割
  10. C#的dictionary使用总结
  11. Zigbee Class 直播公告2016-10-10
  12. CSS基础:块级元素与盒模型
  13. SpringBoot的启动流程分析(1)
  14. openstack 王者归来学习笔记
  15. html5-css的引入
  16. dubbo rpc调用抛出的Exception处理
  17. jquery validation表单验证插件。
  18. winform登录代码
  19. Kafka:Consumer
  20. springmvc的json数据交互

热门文章

  1. Nginx通过CORS实现跨域
  2. SUSE Linux 多路径软件+LVM+裸设备的配置
  3. jsp页面数据分页模仿百度分页效果
  4. SpringBoot中使用AOP实现计算Service执行时间
  5. phpmailer配置163邮箱
  6. HUST软测1504班第2周作业成绩:WordCount
  7. Web测试实践--Rec 3
  8. [干货来袭]C#7.0新特性(VS2017可用)(转)
  9. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptDao_a' defined in class path resource [beansAndHibernate.xml]: Cannot resolve reference to bean 'sessionFact
  10. [LintCode笔记了解一下]41.Maximum Subarray