Keepalived搭建和使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived搭建和使用相关的知识,希望对你有一定的参考价值。

Keepalived主备
??这里我们仅仅只利用Keepalived做双机热备,也就是保证服务器的高可用性,其他的不用管。

一、Keepalived双机热备的应用场景
二、Keepalived双机热备的特性以及优缺点
??特性:
??1,至少需要两台服务器,其中一台为master始终提供服务,另外一台作为backup始终处于空闲状态,只有在主服务器挂掉的时候他就来帮忙了,这是典型的双机热备
??2,能根据需求判断服务是否可用,在不可用的时候要即使切换 ??优缺点:
??优点:数据同步非常简单,不像负载均衡对数据一致性要求非常高,实现起来相对复杂维护也颇为不便,双机热备用rsync就可以实现了操作和维护非常简单
??缺点:服务器有点浪费,始终有一台处于空闲状态

三、Keepalived双机热备的配置
??首先画个双机热备拓扑图吧:
技术分享图片

Keepalived安装
??操作系统:Linux6.3 2.6.32

??下载软件
??keepalived官方地址:http://www.keepalived.org/download.html

大家可以到这里下载最新版本的keepalived(如果已经提供用提供的安装包keepalived-1.2.15.tar.gz)
??创建目录、压缩文件上传、解压(主备机进行相同操作)

mkdir -p /usr/local/HAsoft
cd /usr/local/HAsoft
tar -zxvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
??预编译

./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64/
??(kernels后面的版本“2.6.32-279.el6.x86_64”以实际安装linux版本为准,不要复制直接按Tab键自动补全即可) 结果如下:(出现如下类似结果就行)

Keepalived configuration

Keepalived version : 1.2.15
Compiler : gcc
Compiler flags : -g -O2 -DETHERTYPE_IPV6=0x86dd
Extra Lib : -lssl -lcrypto -lcryptUse IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : No
Use VRRP Framework : Yes
Use Debug flags : No
??编译

make && make install
??整理管理文件:

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
??建立配置文件目录

mkdir -p /etc/keepalived/
??注意:keepalived的配置文件默认在/etc/keepalived/目录 两台服务器(两个节点)都这样安装即可

配置 打开配置文件:(该文件不存在,需手动创建)

vim /etc/keepalived/keepalived.conf
??按i键将下面主机配置的内容复制粘贴到keepalived.conf中,并按时间环境修改标红的配置项。

??主机配置

global_defs
{
}x
vrrp_instance N1 { //定义一个vrrp组
state MASTER //指定哪个为master(主机),哪个为backup(备机)。
mcast_src_ip 192.166.66.80 //此处ip填写本机ip即可(非必要设置)
interface eth2 //设置实例绑定的网卡
virtual_router_id 51 // VPID标记 (主机备机要一致)
priority 100 //定义优先级,数字越大,优先级越高
authentication { //验证类型和密码
auth_type PASS
auth_pass 123456
}
virtual_ipaddress { //虚拟IP
192.166.66.85
}
}
??备机配置

vim /etc/keepalived/keepalived.conf
??将下面内容复制到keepalived.cong中,并按实际环境修改标红配置项。

global_defs
{
}
vrrp_instance N1 {
state BACKUP
interface eth2
mcast_src_ip 192.166.66.81
virtual_router_id 51
priority 88
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.166.66.85
}
}
四、启动调试
??在节点A即主机上启动keepalive

/etc/init.d/keepalived start
??启动日志:(/var/log/message)

tail -f /var/log/messages
Sep 8 18:26:02 centosaKeepalived_vrrp: Registering Kernel netlink reflector
Sep 8 18:26:02 centosaKeepalived_vrrp: Registering Kernel netlink command channel
Sep 8 18:26:02 centosaKeepalived_vrrp: Registering gratutious ARP shared channel
Sep 8 18:26:02 centosaKeepalived_vrrp: Opening file ‘/etc/keepalived/keepalived.conf‘.
Sep 8 18:26:02 centosaKeepalived_vrrp: Configuration is using : 36076 Bytes
Sep 8 18:26:02 centosaKeepalived_vrrp: Using LinkWatch kernel netlink reflector...
Sep 8 18:26:02 centosa Keepalived: Starting VRRP child process, pid=5606
Sep 8 18:26:07 centosaKeepalived_vrrp: VRRP_Instance(lnmp) Transition to MASTER STATE
Sep 8 18:26:12 centosaKeepalived_vrrp: VRRP_Instance(lnmp) Entering MASTER STATE
Sep 8 18:26:12 centosaavahi-daemon[2528]: Registering new address record for 192.168.17.200 on eth0.
??在节点B即备机上上启动

/etc/init.d/keepalived start
??启动日志:

tail -f /var/log/messages
Sep 8 18:30:02 centosb Keepalived: Starting Keepalived v1.2.2 (09/08,2011)
Sep 8 18:30:02 centosb Keepalived: Starting Healthcheck child process, pid=5837
Sep 8 18:30:02 centosbKeepalived_vrrp: Registering Kernel netlink reflector
Sep 8 18:30:02 centosbKeepalived_vrrp: Registering Kernel netlink command channel
Sep 8 18:30:02 centosbKeepalived_vrrp: Registering gratutious ARP shared channel
Sep 8 18:30:02 centosb Keepalived: Starting VRRP child process, pid=5839
Sep 8 18:30:02 centosb kernel: IPVS: Registered protocols (TCP, UDP, AH, ESP)
Sep 8 18:30:02 centosb kernel: IPVS: Connection hash table configured (size=4096, memory=32Kbytes)
Sep 8 18:30:02 centosb kernel: IPVS: ipvs loaded.
Sep 8 18:30:02 centosbKeepalived_healthcheckers: Registering Kernel netlink reflector
Sep 8 18:30:02 centosbKeepalived_healthcheckers: Registering Kernel netlink command channel
Sep 8 18:30:02 centosbKeepalived_healthcheckers: Opening file ‘/etc/keepalived/keepalived.conf‘.
Sep 8 18:30:02 centosbKeepalived_vrrp: Opening file ‘/etc/keepalived/keepalived.conf‘.
Sep 8 18:30:02 centosbKeepalived_vrrp: Configuration is using : 36252 Bytes
Sep 8 18:30:02 centosbKeepalived_vrrp: Using LinkWatch kernel netlink reflector...
Sep 8 18:30:02 centosbKeepalived_healthcheckers: Configuration is using : 6271 Bytes
Sep 8 18:30:02 centosbKeepalived_healthcheckers: Using LinkWatch kernel netlink reflector...
Sep 8 18:30:02 centosbKeepalived_vrrp: VRRP_Instance(lnmp) Entering BACKUP STATE
??从日志可以看出,启动都没有问题,并且安装我给的优先级完成了竞选,各自成就了各自的状态
??在本机运行cmd命令 Ping 192.168.17.200 看能否正常ping通。
??关闭节点A主机的服务测试切换是否正常

/etc/init.d/keepalived stop
??观察节点B的日志:

Sep 8 18:32:55 centosbKeepalived_vrrp: VRRP_Instance(lnmp) Transition to MASTER STATE
Sep 8 18:33:00 centosbKeepalived_vrrp: VRRP_Instance(lnmp) Entering MASTER STATE
Sep 8 18:33:00 centosbavahi-daemon[2531]: Registering new address record for 192.168.17.200 on eth0.
??在本机运行cmd命令 Ping 192.168.17.200 看能否正常ping通。
??启动节点A的服务测试切换是否正常

/etc/init.d/keepalived start
??观察节点B的日志:

Sep 8 18:33:31 centosbKeepalived_vrrp: VRRP_Instance(lnmp) Received higher prio advert
Sep 8 18:33:31 centosbKeepalived_vrrp: VRRP_Instance(lnmp) Entering BACKUP STATE
Sep 8 18:33:31 centosbavahi-daemon[2531]: Withdrawing address record for 192.168.17.200 on eth0.
??在本机运行cmd命令 Ping 192.168.17.200 看能否正常ping通。
??注意把keepalived写在开机启动里面 准备机同时设置

vim /etc/rc.local
??输入下面内容保存

service keepalived start

以上是关于Keepalived搭建和使用的主要内容,如果未能解决你的问题,请参考以下文章

keepalived高可用搭建

keepalived的搭建

基于keepalived搭建MySQL的高可用集群

keepalived高可用haproxy+varnish+lnmp实现站点搭建与ansible实现

Keepalived+Nginx搭建主从高可用并带nginx检测

keepalived服务的搭建