1、使用虚拟机搭建环境
也是A、B 2台机器,使用一样的配置和软件。
环境:CentOS6.5 2台虚拟机
keepalived版本1.2.19
tengine版本2.1.2
节点A :
[root@A keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
admin@localhost.com #设置报警邮件地址,可以设置多个,每行一个。 需开启本机的sendmail服务
}
notification_email_from admin@lvtao.net #设置邮件的发送地址
smtp_server 127.0.0.1 #设置smtp server地址
smtp_connect_timeout 30 #设置连接smtp server的超时时间
router_id LVS_DEVEL #表示运行keepalived服务器的一个标识。发邮件时显示在邮件主题的信息
}
vrrp_script check_nginx {
script "/etc/keepalived/check_http_port"
interval 2 #检查间隔
weight 5 #权重
}
vrrp_instance VI_1 {
state BACKUP #指定keepalived的角色,MASTER表示此主机是主服务器,BACKUP表示此主机是备用服务器
interface eth0 #指定HA监测网络的接口
virtual_router_id 51 #虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。即同一vrrp_instance下,MASTER和BACKUP必须是一致的
priority 100 #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级
advert_int 1 #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
nopreempt #设置 nopreempt 防止抢占资源,只生效BACKUP节点
authentication { #设置验证类型和密码
auth_type PASS #设置验证类型,主要有PASS和AH两种
auth_pass 1111 #设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
}
virtual_ipaddress { #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个
10.0.0.15
}
track_script {
check_nginx
}
}
virtual_server 10.0.0.15 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.0.0.13 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 3
}
}
real_server 10.0.0.14 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 3
}
}
}
节点B:
[root@B keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
admin@localhost.com #设置报警邮件地址,可以设置多个,每行一个。 需开启本机的sendmail服务
}
notification_email_from admin@lvtao.net #设置邮件的发送地址
smtp_server 127.0.0.1 #设置smtp server地址
smtp_connect_timeout 30 #设置连接smtp server的超时时间
router_id LVS_DEVEL #表示运行keepalived服务器的一个标识。发邮件时显示在邮件主题的信息
}
vrrp_script check_nginx {
script "/etc/keepalived/check_http_port"
interval 2 #检查间隔
weight 5 #权重
}
vrrp_instance VI_1 {
state BACKUP #指定keepalived的角色,MASTER表示此主机是主服务器,BACKUP表示此主机是备用服务器
interface eth0 #指定HA监测网络的接口
virtual_router_id 51 #虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。即同一vrrp_instance下,MASTER和BACKUP必须是一致的
priority 99 #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级
advert_int 1 #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
nopreempt #设置 nopreempt 防止抢占资源,只生效BACKUP节点
authentication { #设置验证类型和密码
auth_type PASS #设置验证类型,主要有PASS和AH两种
auth_pass 1111 #设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
}
virtual_ipaddress { #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个
10.0.0.15
}
track_script {
check_nginx
}
}
virtual_server 10.0.0.15 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.0.0.13 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 3
}
}
real_server 10.0.0.14 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
delay_before_retry 3
}
}
}
[root@A keepalived]# cat> #!/bin/bash
#description: Config realserver
VIP=10.0.0.15
. /etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
[root@A keepalived]#
[root@A keepalived]# cat check_http_port
#!/bin/bash
#思路:1、使用curl检查本地nginx可用性
# 2、检查失败尝试启动nginx
# 3、仍失败,则关闭本地keepalived
NGINX=/usr/local/nginx/sbin/nginx
PORT="80"
#curl -v -I -m 10 -o /dev/null -s -w %{http_code}"\n" http://127.0.0.1/
curl http://127.0.0.1:$PORT
if [ $? -ne 0 ]; then
#重启nginx
/etc/init.d/nginx restart
# $NGINX -s stop
# $NGINX
sleep 3
curl http://127.0.0.1/
[ $? -ne 0 ] && /etc/init.d/keepalived stop
fi
exit 0
[root@A keepalived]#
2、3个客户端,依次访问。就能重现流量异常。
[root@tsm-test-centos6 ~]# curl 10.0.0.15
Welcome to tengine!
body {
35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
Welcome to tengine! B
If you see this page, the tengine web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
tengine.taobao.org.
Thank you for using tengine.
[root@tsm-test-centos6 ~]#
[root@tsm-app-dev1 ~]# curl 10.0.0.15
Welcome to tengine!
body {
35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
Welcome to tengine! A
If you see this page, the tengine web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
tengine.taobao.org.
Thank you for using tengine.
[root@tsm-app-dev1 ~]#
10.0.0.8(我的笔记本)
原因不得而知,在此记录。便日后寻得原因。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com