Keepalived详解(三):Keepalived基础功能应用实例【转】

时间:2023-03-08 16:41:22
Keepalived详解(三):Keepalived基础功能应用实例【转】

Keepalived基础功能应用实例:

1.Keepalived基础HA功能演示:

在默认情况下,Keepalived可以实现对系统死机、网络异常及Keepalived本身进行监控,也就是说当系统出现死机、网络出现故障或Keepalived进程异常时,Keepalived会进行主备节点的切换。但这些还是不够的,因为集群中运行的服务也随时可能出现问题,因此,还需要对集群中运行服务的状态进行监控,当服务出现问题时也进行主备切换。Keepalived作为一个优秀的高可用集群软件,也考虑到了这一点,它提供了一个vrrp_script模块专门用来对集群中服务资源进行监控。

1. 配置Keepalived:

操作系统:CentOS release 6.7   版本:keepalived-1.2.12

主机名主机IP地址集群角色集群服务虚拟IP地址

keepalived-master  10.0.0.35            MASTER(主节点)          HTTPD        10.0.0.40

keepalived-backup10.0.0.36           BACKUP(备用节点)         HTTPD

keepalived-master节点的keepalived.conf文件的内容:

! Configuration File for keepalived

global_defs {

notification_email {

*********@qq.com

}

notification_email_from *********@163.com

smtp_server smtp.163.com

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script check_httpd {

script "killall -0 httpd"

interval 2

}

vrrp_instance HA_1 {

state MASTER

interface eth0

virtual_router_id 80

priority 100

advert_int 2

authentication {

auth_type PASS

auth_pass qwaszx

}

notify_master "/etc/keepalived/master.sh"

notify_backup "/etc/keepalived/backup.sh"

notify_fault "/etc/keepalived/fault.sh"

track_script {

check_httpd

}

virtual_ipaddress {

10.0.0.40/24 dev eth0

}

}

master.sh文件内容为:

#!/bin/bash

LOGFILE=/var/log/keepalived-mysql-state.log

echo "[Master]" >> $LOGFILE

date >> $LOGFILE

backup.sh文件内容为:

#!/bin/bash

LOGFILE=/var/log/keepalived-mysql-state.log

echo "[Backup]" >> $LOGFILE

date >> $LOGFILE

fault.sh文件内容为:

#!/bin/bash

LOGFILE=/var/log/keepalived-mysql-state.log

echo "[Fault]" >> $LOGFILE

date >> $LOGFILE

keepalived-backup节点上的keepalived.conf配置文件内容与keepalived-master节点上的基本相同,需要修改的地方有两个:

□ 将“state MASTER” 更改为 “state BACKUP”。

■ 将priority 100 更改为一个较小的值,这里改为“priority 80”

2.Keepalived启动过程分析:

keepalived-master节点启动keepalived服务,操作如下:

[root@keepalived-master ~]# /etc/init.d/httpd start

[root@keepalived-master ~]# /etc/init.d/keepalived start

keepalived-backup节点启动keepalived服务,操作如下:

[root@keepalived-backup ~]# /etc/init.d/httpd start

[root@keepalived-backup ~]# /etc/init.d/keepalived start

3.keepalived的故障切换过程分析:

在keepalived-master节点出现故障后,备用节点keepalived-backup立刻检测到,此时备用机变成MASTER角色,并且接管了keepalived-master主机的虚拟IP资源,最后将虚拟IP绑定在eth0设备上。

4.故障恢复切换分析:

keepalived-master节点通过vrrp_script模块检测到httpd服务已经恢复正常,然后自动切换到MASTER状态,同时也夺回了集群资源,将虚拟IP地址再次绑定在eth0设备上。

纵观Keepalived的整个运行过程和切换过程,看似合理,事实上并非如此:在一个高负载、高并发、追求稳定的业务系统中,执行一次主、备切换对业务系统影响很大,因此,不到万不得已,尽量不要进行主、备角色的切换,也就是说,在主节点发生故障后,必须切换到备用节点,而在主节点故障恢复后,不希望再次切回主节点,直到备用节点发生故障时才进行切换,这就是前面介绍的不抢占功能,可以通过Keepalived的nopreempt选项来实现。

配置:

master:

! Configuration File for keepalived

global_defs {

notification_email {

**********@qq.com

}

notification_email_from *******@163.com

smtp_server smtp.163.com

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script check_httpd {

script "killall -0 httpd"

interval 2

}

vrrp_instance HA_1 {

state BACKUP

interface eth0

virtual_router_id 80

priority 100

nopreempt

advert_int 2

authentication {

auth_type PASS

auth_pass qwaszx

}

notify_master "/etc/keepalived/master.sh"

notify_backup "/etc/keepalived/backup.sh"

notify_fault "/etc/keepalived/fault.sh"

track_script {

check_httpd

}

virtual_ipaddress {

10.0.0.40/24 dev eth0

}

}

backup:

! Configuration File for keepalived

global_defs {

notification_email {

********@qq.com

}

notification_email_from *******@163.com

smtp_server smtp.163.com

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script check_httpd {

script "killall -0 httpd"

interval 2

}

vrrp_instance HA_1 {

state BACKUP

interface eth0

virtual_router_id 80

priority 80

#nopreempt

advert_int 2

authentication {

auth_type PASS

auth_pass qwaszx

}

notify_master "/etc/keepalived/master.sh"

notify_backup "/etc/keepalived/backup.sh"

notify_fault "/etc/keepalived/fault.sh"

track_script {

check_httpd

}

virtual_ipaddress {

10.0.0.40/24 dev eth0

}

}

转自

Keepalived详解(三)-闫利朋的博客-51CTO博客
http://blog.51cto.com/6284444/2135196