二、应用场景:
1.2、keepalived常常需要调用脚本来完成功能的切换工作。并通过track_script进行追踪记录。
vrrp_script{
}
vrrp_instance{
track_script{
}
}
1.3、如何保障故障通知机制的实现?
通常使用一下方式,并定义带vrrp_instance中:
notify_master “ “
notify_backup “ "
Notify_fault “ "
vrrp_script chk_maintainace {
script "[[ -e /etc/keepalived/down]] && exit 1 || exit 0"
interval 1
weight -3
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123123
}
virtual_ipaddress {
10.40.0.227
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
track_script {
chk_maintainace}
}
1.4、如何配置ipvs,实现高可用的负载均衡效果。
这里所使用的负载均衡器是ipvs,这种结合方式(ipvs+keepalived)有以下特点:
完全可以不用安装ipvsadm服务,即可实现ipvs功能。
轻量级高可用
配置简单,系统资源消耗低
但是ipvs结构简单,功能单一,这里我们只是做了一个抛砖引玉的演示,在实际工作中完全可以和其他负载均衡器结合使用比如nginx。
virtual server
定义方式有三种:
1、virtual_server ip port的方式制定ipvs的vip地址及端口(具体提供服务的端口,比如80端口),需要定义协议,目前只支持tcp协议,protocol TCP。
2、virtual_server fwmark int 通过防火墙标记定义ipvs。
3、virtual_server group string
real__server配置:
real_server <ipaddr> <port>
{
Weight <int>
Notify_up
Notify_down
HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK
}
三、keepalived的安装
1、安装keepalived软件
[root@ms.dtedu.com~]$ansible all -m yum -a "name=keepalived state=present”
四、Keepalived应用
1、基本应用:
主节点和备用节点的配置:
global_defs {
notification_email {
gongbing_shenlan@163.com
}
notification_email_from root@dtedu.com.cn
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123123
}
virtual_ipaddress {
10.40.0.227
}
}
global_defs {
notification_email {
gongbing_shenlan@163.com
}
notification_email_from root@dtedu.com.cn
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 123123
}
virtual_ipaddress {
10.40.0.227
}
}
启动服务,查看状态:
[root@node5.dtedu.com ~]# service keepalived start
Starting keepalived: [ OK ]
[root@node5.dtedu.com ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fa:4c:6e brd ff:ff:ff:ff:ff:ff
inet 10.40.0.225/24 brd 10.40.0.255 scope global eth0
inet 10.40.0.227/32 scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fa:4c:78 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.205/24 brd 192.168.4.255 scope global eth1
2、ipvs+keepalived实现方式:
关闭iptables,setenforce、ip_forward=1
NAT模型,要求realserver使用DIP作为其网关,所以keepalived要有虚拟网关这样的设定:
vrrp_instance VI_GATEWAY {
state MASTER
interface eth1
lvs_sync_daemon_interface eth1
virtual_router_id 52
priority 100
advert_int 1
smtp_alter
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.240
}
}
3、keepalived+nginx高可用的实现
关闭keepalived配置文件关于virtual_server的内容,将nginx视为一种资源来进行定义这种资源的管理就是通过notify进行管理的。
马哥在这里做实验使用的脚本并不成功,需要更多的研究。
.keepalived还提供了vrrp_script脚本,可以扩展当前的监控,这样我们就可以让keepalived用在nginx和haproxy的高可用上了。vrrp_script的默认语法为:
vrrp_script check_name {
script "" 需要执行的脚本内容或脚本文件存放的位置
interval 2 每隔多少秒做一次检测
weight -2 如果失败了,降低多少权重
fail 2 检测多少次失败,才认为是真的失败
rise 1 检测一次成功,就认为在线了
}
vrrp_script脚本定义在实例之外。
上面只是定义了一个vrrp_script脚本,还需要执行它,执行的话可以通过track_script来执行。track_script需要放在实例中,让实例随时的去执行这个脚本,判断服务的状况。因此如果我们定义的是vrrp_script check_name{xxx},在vrrp_instance中需要相应的添加:
track_script{
check_name
}
下面实现keepalived+nginx的高可用功能脚本,这里的nginx服务器仍然做的是负载均衡服务器,MASTER上的脚本如下:
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_nginx {
script "killall -0 nginx"
interval 2
weight -2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass qiguo139
}
virtual_ipaddress {
192.168.1.200
}
track_script {
check_nginx
}
}
在BACKUP节点上只需要把priority 的值换成100,把state的状态换成BACKUP即可.
4、基于多虚拟路由的主主模型(互做备份)
此方式就是通过创建2个不同的实例instance,并通过给节点进行相反的设置完成的。
vrrp_instance VI_1 {实例一
state MASTER主节点
interface eth0
virtual_router_id 51实例id
priority 100优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 123123
}
virtual_ipaddress {虚拟ip
10.40.0.228
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
track_script {
chk_maintainace
}
}
vrrp_instance VI_2 {实例2
state BACKUP
interface eth1
lvs_sync_daemon_interface eth1
virtual_router_id 52实例id
priority 95优先级
advert_int 1
smtp_alter
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.240虚拟ip
}
}
两节点产生了不同的虚拟ip地址:
[root@node5.dtedu.com ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fa:4c:6e brd ff:ff:ff:ff:ff:ff
inet 10.40.0.225/24 brd 10.40.0.255 scope global eth0
inet 10.40.0.228/32 scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fa:4c:78 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.205/24 brd 192.168.4.255 scope global eth1
[root@node6.dtedu.com ~]# ip addr show
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9a:c8:b0 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.206/24 brd 192.168.4.255 scope global eth1
inet 192.168.4.240/32 scope global eth1
inet6 fe80::20c:29ff:fe9a:c8b0/64 scope link
valid_lft forever preferred_lft forever
keepalived高可用集群应用场景及配置: http://www.linuxidc.com/Linux/2017-03/141866.htm
keepalived配置详解:https://wenku.baidu.com/view/303cf1d583c4bb4cf6ecd17c.html
LVS+KEEPALIVE部署全解https://wenku.baidu.com/view/303cf1d583c4bb4cf6ecd17c.html
参考文献:Linux 高可用(HA)集群之keepalived详解
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是错误! 超链接引用无效。