`
lionlx
  • 浏览: 282426 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

(转)nginx+keepalived配置双主高可用负载均衡

阅读更多
源地址:http://www.linuxmr.com/2012/nginx_keepalived_0629/207.html

nginx+keepalived配置高可用性负载均衡--nginx+keepalived配置双主高可用负载均衡

一、nginx双主高可用试验环境
1、操作系统redhat X86-64
2、使用的软件nginx-1.2.1,keepalived-1.2.1
3、两台主机,一台ip是192.168.1.2,另一台ip是192.168.1.3
4、虚拟ip是192.168.1.4,192.168.1.5

二、nginx双主高可用目的
nginx单主高可用,虽然实现了高可用,但是总是有一台机器是空闲的,为了合理使用资源,做成nginx双主高可用是很有必要的

三、安装nginx
这个nginx配置安装相当简单,不做说明
安装完成之后开启nginx
用命令ps -C nginx --no-heading,有类似下面的输出,即表示nginx正常启动

四、安装keepalived
参考http://www.linuxmr.com/2012/nginx_keepalived_0628/203.html,获得keepalived安装方法。
这里主要用到keepalived的故障切换功能。

五、nginx双主高可用具体配置
具体细节参考keepalived配置文件详解
这里直接上实现nginx单主高可用的配置文件内容
主机192.168.1.2上的keepalived的配置是
global_defs { #全局定义
notification_email { #定义报警邮件相关,这些可以忽略
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.1.3 #smtp服务器地址
smtp_connect_timeout 30
router_id LVS_DEVEL #标示虚拟路由的id
}

vrrp_script check_nginx_alive { #定义实例使用的检测脚本
scritp /root/check_nginx.sh #定义检测脚本路径
interval 2 #定义检测时间间隔
}

vrrp_instance VI_1 { #定义实例
state MASTER #该主机初始状态
interface eth0 #使用的网卡
virtual_router_id 51 #虚拟路由,互为高可用的主机之间的虚拟路由必须相同
priority 100 #优先级,这个是MASTER,应该比SLAVE的高
advert_int 1 #VRRP发送advertisment数据包的间隔时间
authentication { #互为高可用的主机之间通信的认证信息
auth_type PASS #类型是明文密码,官方推荐使用明文密码
auth_pass 1111 #明文密码内容,互为高可用的主机之间的密码也应该相同
}

track_script { #该实例使用的检测脚本
check_nginx_alive #该脚本在keepalived中定义的名称
}

virtual_ipaddress { #绑定的虚拟ip地址
192.168.1.4
}
}

vrrp_instance VI_2 { #再为该主机定义一个实例
state SLAVE #该主机初始状态
interface eth0 #使用的网卡
virtual_router_id 52 #虚拟路由,同一台主机之间不同实例路由必须不一样
priority 95 #优先级,这个是SLAVE,应该比MASTER的低
advert_int 1 #VRRP发送advertisment数据包的间隔时间
authentication { #互为高可用的主机之间通信的认证信息
auth_type PASS #类型是明文密码,官方推荐使用明文密码
auth_pass 1111 #明文密码内容,互为高可用的主机之间的密码也应该相同
}

virtual_ipaddress { #绑定的虚拟ip地址
192.168.1.5
}
}

主机192.168.1.3上的keepalived的配置是
global_defs { #全局定义
notification_email { #定义报警邮件相关,这些可以忽略
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.1.3 #smtp服务器地址
smtp_connect_timeout 30
router_id LVS_DEVEL #标示虚拟路由的id
}

vrrp_script check_nginx_alive { #定义实例使用的检测脚本
scritp /root/check_nginx.sh #定义检测脚本路径
interval 2 #定义检测时间间隔
}

vrrp_instance VI_1 { #定义实例
state SLAVE #该主机初始状态
interface eth0 #使用的网卡
virtual_router_id 51 #虚拟路由,互为高可用的主机之间的虚拟路由必须相同
priority 95 #优先级,这个是SLAVE,应该比MASTER的高
advert_int 1 #VRRP发送advertisment数据包的间隔时间
authentication { #互为高可用的主机之间通信的认证信息
auth_type PASS #类型是明文密码,官方推荐使用明文密码
auth_pass 1111 #明文密码内容,互为高可用的主机之间的密码也应该相同
}

virtual_ipaddress { #绑定的虚拟ip地址
192.168.1.4
}
}

vrrp_instance VI_2 { #为主机192.168.1.3定义另一个实例,对应192.168.1.2中的那个实例
state MASTER #该主机初始状态
interface eth0 #使用的网卡
virtual_router_id 52 #虚拟路由,同一主机不同实例之间的虚拟路由必须不同,这里对应192.168.1.2里面的配置
priority 100 #优先级,这个是MASTER,应该比SLAVE的高
advert_int 1 #VRRP发送advertisment数据包的间隔时间
authentication { #互为高可用的主机之间通信的认证信息
auth_type PASS #类型是明文密码,官方推荐使用明文密码
auth_pass 1111 #明文密码内容,互为高可用的主机之间的密码也应该相同
}

track_script { #该实例使用的检测脚本
check_nginx_alive #该脚本在keepalived中定义的名称
}

virtual_ipaddress { #绑定的虚拟ip地址
192.168.1.5
}
}

/root/check_nginx.sh的内容是:
nginx_state=`ps -C nginx --no-heading | wc -l`
if [ ${nginx_state} == 0 ];then
#/usr/local/nginx/sbin/nginx
#sleep 3
#nginx_state=`ps -C nginx --no-heading | wc -l`
[ ${nginx_state} == 0 ] && service keepalived stop
fi

注释掉的那段代码,是为了防止nginx意外停止而家的设置,用处是,当检测到nginx停止时,尝试重新启动nginx,如果重新启动nginx失败, 则停止keepalived,让主机192.168.1.4自动切换到MASTER状态,从而实现高可用。这里注释掉是为了测试方便。

现在重启两台机器上的keepalived,
在192.168.1.2上运行代码
shell> ip a | grep eth0 -A5
输出的结果类似
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue
link/ether 00:ea:01:16:87:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.4/32 scope global eth0
inet6 fe80::2ea:1ff:fe16:87a1/64 scope link

在192.168.1.3上运行代码
shell> ip a | grep eth0 -A5
输出的结果类似
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue
link/ether 00:ea:01:16:87:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.5/32 scope global eth0
inet6 fe80::2ea:1ff:fe16:87a1/64 scope link

看,虚拟ip192.168.1.4、192.168.1.5已经分别绑定在192.168.1.2与192.168.1.3的网卡eth0上。

在命令行输入命令
shell> curl -s http://192.168.1.4/ --ignore-content-length
shell> curl -s http://192.168.1.5/ --ignore-content-length
如果是nginx网页文件没有改变,则两次输出内容都类似下面:
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>

这说明已经可以通过虚拟IP来访问网站了,那到底能不能实现nginx双主高可用呢?

测试如下:

停掉192.168.1.2上的nginx,到192.168.1.3上运行命令:
shell>ip a | grep eth0 -A5
输出的结果类似
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue
link/ether 00:16:3e:31:cd:56 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.3/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.4/32 scope global eth0
inet 192.168.1.5/32 scope global eth0

inet6 fe80::2ea:1ff:fe16:87a1/64 scope link
现在两个虚拟ip已经绑定到192.168.1.3的网卡eth0上了,
在命令行输入命令
shell> curl -s http://192.168.1.4/ --ignore-content-length
shell> curl -s http://192.168.1.5/ --ignore-content-length
如果是nginx网页文件没有改变,则输出内容也是类似下面:
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
由此可见,nginx双主高可用配置成功
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics