配置文件说明

global_defs区域

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
  • notification_email 故障发生时给谁发邮件通知。

  • notification_email_from 通知邮件从哪个地址发出。

  • smpt_server 通知邮件的smtp地址。

  • smtp_connect_timeout 连接smtp服务器的超时时间。

  • enable_traps 开启SNMP陷阱(Simple Network Management Protocol)。

  • router_id 标识本节点的字条串,通常为hostname,但不一定非得是hostname。故障发生时,邮件通知会用到

vrrp_instance区域

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
}
}
  • state 可以是MASTER或BACKUP,不过当其他节点keepalived启动时会将priority比较大的节点选举为MASTER,因此该项其实没有实质用途。

  • interface 节点固有IP(非VIP)的网卡,用来发VRRP包。

  • virtual_router_id 取值在0-255之间,用来区分多个instance的VRRP组播。注意: 同一网段中virtual_router_id的值不能重复,否则会出错。

  • priority 用来选举master的,要成为master,那么这个选项的值最好高于其他机器50个点,该项取值范围是1-255(在此范围之外会被识别成默认值100)。

  • advert_int 发VRRP包的时间间隔,即多久进行一次master选举(可以认为是健康查检时间间隔)。

  • authentication 认证区域,认证类型有PASS和HA(IPSEC),推荐使用PASS(密码只识别前8位)。

  • virtual_ipaddress vip,不解释了。

virtual_server区域

virtual_server 10.10.10.2 1358 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP sorry_server 192.168.200.200 1358 real_server 192.168.200.2 1358 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
} real_server 192.168.200.3 1358 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
  • delay_loop 延迟轮询时间(单位秒)。

  • lb_algo 后端调试算法(load balancing algorithm)。

  • lb_kind LVS调度类型NAT/DR/TUN。

  • persistence_timeout:会话保持时间,单位是秒。这个选项对动态网站很有用处:当用户从远程用帐号进行登陆网站时,有了这个会话保持功能,就能把用户的请求转发给同一个应用服务器。在这里,我们来做一个假设,假定现在有一个lvs 环境,使用DR转发模式,真实服务器有3个,负载均衡器不启用会话保持功能。当用户第一次访问的时候,他的访问请求被负载均衡器转给某个真实服务器,这样他看到一个登陆页面,第一次访问完毕;接着他在登陆框填写用户名和密码,然后提交;这时候,问题就可能出现了—登陆不能成功。因为没有会话保持,负载均衡器可能会把第2次的请求转发到其他的服务器。

  • sorry_server 当所有real server宕掉时,sorry server顶替。

  • connect_port 健康检查,如果端口通则认为服务器正常。

  • connect_timeout,nb_get_retry,delay_before_retry分别表示超时时长、重试次数,下次重试的时间延迟。

keepalived+lvs环境搭建

环境说明

功能 IP 安装软件 系统
master 192.168.5.200 keepalived、ipvsadm CentOS 6
slave 192.168.5.228 keepalived、ipvsadm CentOS 6
node1 192.168.5.229 httpd CentOS 6
node2 192.168.5.230 httpd CentOS 6

rs提供测试页

# curl http://192.168.5.229
192.168.5.229
# curl http://192.168.5.230
192.168.5.230

rs节点配置

# cat rs.sh
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
. /etc/rc.d/init.d/functions
VIP=192.168.5.188 #修改你的VIP
host=`/bin/hostname`
case "$1" in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig lo down
/sbin/ifconfig lo up
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
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig lo:0 down
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
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep $VIP`
isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
if [ ! "$islothere" -o ! "isrothere" ];then
# Either the route or the lo:0 device
# not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac

node1、和node2上按上面的方式进行配置。

vs机器的keepalived配置

master机器配置:

global_defs {
notification_email {
xxxxxx@xxx.com
}
notification_email_from xxxxxx@xxx.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER_5.200
} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 188
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.188
}
} virtual_server 192.168.5.188 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.5.229 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.5.230 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
sorry_server 127.0.0.1 80
}

backup机器配置:

global_defs {
notification_email {
xxxxxx@xxx.com
}
notification_email_from xxxxxx@xxx.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_BACKUP_5.228
} vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.188
}
} virtual_server 192.168.5.188 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.5.229 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.5.230 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

注意:在keepalived的master和backup中的配置文件是有区别的。state和priority选项需要修改的。
除了使用HTTP_GET方式进行检测之外还可以使用TCP_CHECK等方式进行检测rs:

    real_server 192.168.5.229 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}

扩展学习:
http://cuchadanfan.blog.51cto.com/9940284/1696588
https://github.com/chenzhiwei/linux/tree/master/keepalived

keepalived+nginx

keepalived配置

master

global_defs {
notification_email {
xxxxxx@xxx.com
}
notification_email_from xxxxxx@xxx.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_BACKUP_5.228
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -5
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 146
mcast_src_ip 192.168.5.228
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.5.188
}
}

backup配置

global_defs {
notification_email {
xxxxxx@xxx.com
}
notification_email_from xxxxxx@xxx.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_BACKUP_5.200
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -5
fall 3
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 146
mcast_src_ip 192.168.5.200
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.5.188
}
}

nginx监控脚本

# cat /etc/keepalived/nginx_check.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/usr/sbin/nginx
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/etc/init.d/keepalived stop
fi
fi

参数说明:

  • mcast_src_ip : 发送多播数据包时的源IP地址,这里注意了,这里实际上就是在那个地址上发送VRRP通告,这个非常重要,一定要选择稳定的网卡端口来发送,这里相当于heartbeat的心跳端口,如果没有设置那么就用默认的绑定的网卡的IP,也就是interface指定的IP地址

  • virtual_ipaddress : 这里设置的就是VIP,也就是虚拟IP地址,他随着state的变化而增加删除,当state为master的时候就添加,当state为backup的时候删除,这里主要是有优先级来决定的,和state设置的值没有多大关系,这里可以设置多个IP地址

  • track_script : 引用VRRP脚本,即在 vrrp_script 部分指定的名字。定期运行它们来改变优先级,并最终引发主备切换。

  • script : 自己写的检测脚本。也可以是一行命令如killall -0 nginx

  • interval 2 : 每2s检测一次

  • weight -5 : 检测失败(脚本返回非0)则优先级 -5

  • fall 2 : 检测连续 2 次失败才算确定是真失败。会用weight减少优先级(1-255之间)

  • rise 1 : 检测 1 次成功就算成功。但不修改优先级

nginx配置

在两台192.168.5.200、192.168.5.228上分别安装nginx,然后在nginx上配置upstream并把前面的请求抛给后端的192.168.5.229、192.168.5.230。之后使用curl进行测试。具体使用这里不做过多介绍。

扩展学习
https://segmentfault.com/a/1190000002881132
http://noodle.blog.51cto.com/2925423/1794734

keepalived.md的更多相关文章

  1. MySQL keepalived 双主.md

    MySQL keepalived 双主搭建 环境说明 系统 IP 主机名 mysql keepalived VIP CentOS 6.8 192.168.197.61 C6-node1 5.6.36 ...

  2. Nginx反向代理,负载均衡,redis session共享,keepalived高可用

    相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此 ...

  3. Windows下PowerShell监控Keepalived

    一.背景 某数据库服务器为CentOS,想要监控Keepalived的VIP是否有问题,通过邮件进行报警,但这台机器不能上外网,现在只能在Windows下通过PowerShell来完成发邮件预警. 二 ...

  4. Keepalived+Redis高可用部署(第二版)

    更新 20150625 脚本由5个减少为4个,sh脚本指令做了精简. 修改了另外3个脚本,在日志里增加了日期显示. 新增redis数据类型,持久化,主从同步简介. 新增hiredis简介. 新增c语言 ...

  5. 采用Atlas+Keepalived实现MySQL读写分离、读负载均衡【转载】

      文章 原始出处 :http://sofar.blog.51cto.com/353572/1601552 ============================================== ...

  6. keepalived工作原理和配置说明 腾讯云VPC内通过keepalived搭建高可用主备集群

    keepalived工作原理和配置说明 腾讯云VPC内通过keepalived搭建高可用主备集群 内网路由都用mac地址 一个mac地址绑定多个ip一个网卡只能一个mac地址,而且mac地址无法改,但 ...

  7. 你需要了解的高可用方案之使用keepalived搭建双机热备一览

    在之前一篇使用nginx搭建高可用的解决方案的时候,很多同学会问,如果nginx挂掉怎么办,比如下面这张图: 你可以清楚的看到,如果192.168.2.100这台机器挂掉了,那么整个集群就下线了,这个 ...

  8. CentOS7下搭建FastDfs(V5.11)+Keepalived分布式集群部署

    FastDfs介绍 http://kb.cnblogs.com/page/82280/ 1.准备 系统 CentOS7 最小化安装. #VIP虚拟IP 10.1.6.218 #Keepalived 1 ...

  9. Atlas+Keepalived实现MySQL读写分离、读负载均衡

    一.基础介绍 ========================================================================================== 1. ...

随机推荐

  1. 关于PHP数据库mysql的一些案例

    案例1:查询select 使用php连接数据库class9, 获取数据库的表student中的信息, 然后输出到页面上(用表格套住) <?php header("Content-typ ...

  2. [转]IIS的各种身份验证详细测试

    本文转自:http://www.cnblogs.com/chnking/archive/2007/11/20/965553.html#_Toc183326163 一.    IIS的身份验证概述 1. ...

  3. JS日期、月份的加减

    JS日期.月份的加减 需要注意的是返回的月份是从0开始计算的,也就是说返回的月份要比实际月份少一个月,因此要相应的加上1 // 日期,在原有日期基础上,增加days天数,默认增加1天 function ...

  4. 基于ASP.NET Core 创建 Web API

    使用 Visual Studio 创建项目. 文件->新建->项目,选择创建 ASP.NET Core Web 应用程序. 基于 ASP.NET Core 2.0 ,选择API,身份验证选 ...

  5. MySQL---5、可视化工具Navicat for MySQL安装配置

    一.安装文件包下载 Navicat for MySQL 安装软件和破解补丁: 链接:https://pan.baidu.com/s/1oKcErok_Ijm0CY9UjNMrnA   密码:4xb1 ...

  6. oracle锁表问题解决

    select object_name,machine,s.sid,s.serial# from v$locked_object l,dba_objects o ,v$session s where l ...

  7. Restful架构思想

    java作为一门后端语言,其厉害之处在于web,大家比较熟知的各种网络应用,java都能做,那么在这个移动优先的时代,如何继续发挥java的强大呢.通常是让java作为一个app的服务端,为app客户 ...

  8. uestc 1709 Binary Operations 位运算的灵活运用

    Binary Operations Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674   Description   B ...

  9. 领域模型(DomainModel)与视图模型(ViewModel)

    Model-View-Controller(模型-视图-控制器,MVC)模式将你的软件组织并分解成三个截然不同的角色: Model 封装了你的应用数据.应用流程和业务逻辑. View 从 Model ...

  10. Chromium base库分割字符串SplitString

    前一段时间在工作过程中遇到一个场景需要将http response中的request header中的cookie字段取出并进行解析,但是手头没有解析cookie的工具类,同时cookie的表现就是个 ...