ip划分:

RS1:192.168.223.135

RS2:192.168.223.137

节点node1和node2的keepalived(node1:192.168.223.136,node2:192.168.223.128
 
1、首先设置RS
[root@wadeson ~]# cat set_arp_args.sh
#!/bin/bash
case "$1" in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig lo:0 192.168.223.100/32 broadcast 192.168.223.100 up
route add -host 192.168.223.100 dev lo:0
;;
stop)
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
;;
esac
 
2、设置节点node1和node2的keepalived(node1:192.168.223.136,node2:192.168.223.128)
node1设置:
[root@node1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
json_hc@163.com
}
notification_email_from json_hc@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id node1
}
vrrp_script chk_keepalived_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -2
}
 
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.223.100/24 dev eth0 label eth0:0
}
 
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
 
track_script {
chk_keepalived_down
}
}
virtual_server 192.168.223.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
 
real_server 192.168.223.135 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.223.137 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
 
node2设置:
[root@node2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
json_hc@163.com
}
notification_email_from json_hc@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id node2
}
vrrp_script chk_keepalived_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.223.100/24 dev eth0 label eth0:0
}
 
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
 
track_script {
chk_keepalived_down
}
}
virtual_server 192.168.223.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
 
real_server 192.168.223.135 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.223.137 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
 
然后测试网页:由于现在vip在node1上面,需要在node2上面访问
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node2 ~]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
 
现在在node1上面创建down,使vip漂移到node2节点上:
[root@node1 keepalived]# touch down
观察日志信息可以看见vip已经飘到node2节点上了,而且我的163邮件已经收到了邮件
现在测试curl http://192.168.223.100只能在node1节点上测试
[root@node1 ~]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 ~]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
[root@node1 ~]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 ~]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
 
notify.sh脚本通知到邮箱:
[root@node1 keepalived]# cat notify.sh
#!/bin/bash
send_mail="json_hc@163.com"
HOSTNAME=$(hostname)
notify() {
mail_subject="$HOSTNAME to be $1:vip floating"
mail_body="$(date +'%Y-%m-%d %H:%M:%S'):vrrp transaction change to be $1"
echo ${mail_body}|mail -s "${mail_subject}" ${send_mail}
}
case "$1" in
master)
notify master
exit 0
;;
backup)
notify backup
exit 0
;;
fault)
notify fault
exit 0
;;
*)
echo "Usage:$(basename $0) {master|backup|fault}"
exit 1
;;
esac
 
现在将RS其中的某一台宕掉:
[root@wadeson script]# /usr/local/apache2.4/bin/apachectl stop
测试:
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
 
然后将该宕掉的RS恢复:可能需要稍等一下
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.135 centos7</h1>
[root@node1 keepalived]# curl http://192.168.223.100
<h1>test 192.168.223.137 html</h1>
 
当然如果后端的RS都宕机了那么可以设置keepalived自身具有web服务提供一个友好的页面提示:
配置操作如下:
virtual_server 192.168.223.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
只需在这后面加上:
sorry_server 127.0.0.1 80(两台keepalived节点都给加上)
那么RS都宕机后,本机会提供一个友好的web页面
 
除了HTTP_CHECK还可以TCP_CHECK:
TCP_CHECK {
    connect_timeout 3
}

keepalived结合lvs的更多相关文章

  1. MySQL HA方案之MySQL半复制+MHA+Keepalived+Atlas+LVS[转]

    MySQL HA方案之MySQL半复制+MHA+Keepalived+Atlas+LVS 简介 目前Mysql高可用的方案有好多,比如MMM,heartbeat+drbd,Cluster等,还有per ...

  2. 实战--Keepalived和LVS实现负载高可用

    显然,只有上一篇的操作,在WEB运维技术中,只能承担一半的角色. 想像一下,如何LVS本身倒了,肿么办?后端的NGINX再多,也只能是干着急,请求过来不呀! 所以,在本篇时,我们来实现LVS永不倒, ...

  3. 基于Keepalived实现LVS双主高可用集群

    Reference:  https://mp.weixin.qq.com/s?src=3&timestamp=1512896424&ver=1&signature=L1C7us ...

  4. Linux-利用keepalived实现lvs的高可用性

    单主模型IPVS示例 配置keepalive 高可用的ipvs集群示例:修改keepalived配置文件 修改主机:192.168.234.27的keepalived配置文件 [root@234c27 ...

  5. 使用Keepalived构建LVS高可用集群

    LVS的DR模型配置+Keepalive部署 介绍 下图为DR模型的通信过程,图中的IP不要被扑结构中的IP迷惑,图里只是为了说明DR的通信原理,应用到本例中的拓扑上其工作原理不变. 拓扑结构 服务器 ...

  6. keepalived给LVS带来了什么

    LVS+Keepalived 1>Keepalived简介  Keepalived是Linux下一个轻量级别的高可用解决方案,高可用(High Avalilability,HA),其实两种不同的 ...

  7. 003.Keepalived搭建LVS高可用集群

    一 基础环境 1.1 IP规划 OS:CentOS 6.8 64位 节点类型 IP规划 主机名 类型 主 Director Server eth0:172.24.8.10 DR1 公共IP eth1: ...

  8. keepalived heartbeat lvs haproxy

    一, keeplived @ 01,keeplived 是什么? Keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态,它根据TCP/IP参考模型的第三.第四层.第五层交 ...

  9. Linux keepalived与lvs的深入分析

    一)概述 在本篇文章里,我们会涉及两部份内容,一个是LVS,另一个则是keepalived. 即我们用LVS和keepalived实现了负载均衡及高可用的服务器.   LVS有实现三种IP负载均衡技术 ...

  10. [LVS] 用keepalived实现LVS NAT模式高可用性

    默认前提是LVS已经可以正常工作了. 因为是NAT模式,RS的路由要指向LVS的接口地址,所以需要一个统一的后台浮动地址,使得RS都指向这个浮动IP.否则在切换时,会导致RS回包到DOWN掉的LVS上 ...

随机推荐

  1. NIO概览

    NIO专题:http://developer.51cto.com/art/201112/307172.htm 一.新IO概述: 新IO和传统IO都是用于进行输入/输出,相比于传统IO面向流的处理方式, ...

  2. Ubuntu安装谷歌输入法或者搜狗

    1.进入系统先更软件更新器这个在系统计算机中搜索,点击就好了 2.先添加以下源sudo add-apt-repository ppa:fcitx-team/nightly 3.添加源之后需要更新一下系 ...

  3. Tortoise SVN 使用

    1.添加文件或文件夹 2.删除文件或文件夹 ①If you want to delete an item from the repository, but keep it locally as an ...

  4. 2017 Multi-University Training Contest - Team 2——HDU6050 Funny Function

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6050 题意:题目很短自己看吧, 就是这个递推式子,说的很清楚了,让你求F(m,1).题解里面分什么奇偶 ...

  5. Yii框架2.0的Gii

    Yii框架的Gii在我看来算是个快速创建器,当然对于学习来说意义不大,但对于已经懂得他的原理并用他开发的话,就是个快速开发的好工具. 他能快速的创建控制器,模块,crup,插件,Module. 打开g ...

  6. ubuntu安装mysql步骤

    https://dev.mysql.com/downloads/file/?id=477124 ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get inst ...

  7. 完全用nosql轻松打造千万级数据量的微博系统

    其实微博是一个结构相对简单,但数据量却是很庞大的一种产品.标题所说的是千万级数据量也并不是一千万条微博信息而已,而是千万级订阅关系之间发布.在看 我这篇文章之前,大多数人都看过sina的杨卫华大牛的微 ...

  8. redis实现cache系统实践(六)

    1. 介绍 rails中就自带有cache功能,不过它默认是用文件来存储数据的.我们要改为使用redis来存储.而且我们也需要把sessions也存放到redis中.关于rails实现cache功能的 ...

  9. SVM数学原理推导&鸢尾花实例

    //看了多少遍SVM的数学原理讲解,就是不懂,对偶形式推导也是不懂,看来我真的是不太适合学数学啊,这是面试前最后一次认真的看,并且使用了sklearn包中的SVM来进行实现了一个鸢尾花分类的实例,进行 ...

  10. genymotion——VT-x is not available (VERR_VMX_NO_VMX) 的解决方案

    进入虚拟机设置页面