#################################    1.安装兼容包  ###########################

yum -y install openssl openssl-devel ipvsadm

################################  2.下载并安装popt和keepalived  ####################

wget http://rpm5.org/files/popt/popt-1.14.tar.gz

tar popt-1.14.tar.gz

cd popt-1.14

./configuration

make && make install

wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz

tar zxvf keepalived-1.2.20.tar.gz

cd keepalived-1.2.20 

./configuration

make && make install

###############################3.redis_master keepalived配置##################

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

cp /usr/local/sbin/keepalived /usr/sbin/

cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

chkconfig --add keepalived

vi /etc/keepalived/keepalived.conf #redis master配置

配置MASTER 配置如下:

Configuration File for keepalived

global_defs {

   notification_email {##指定keepalived在发生事件(如切换)需要发送Email的对象,多个写多行##

     [email protected]

   }

   notification_email_from [email protected] ##发送人

   smtp_server smtp.126.com ##SMTP服务器##

   smtp_connect_timeout 30  ##链接超时时间##

   router_id LVS_DEVEL       ##路由标识,这里用主机名##

}

vrrp_instance VI_1 {   ##虚拟路由标识##

state MASTER           ##初始状态,默认,选举产生后才可以升级为Master ,这里明确定义其为Master##

interface eno16777736         ##选举通过那个网卡接口## 通过ifconfig查看

virtual_router_id 51   ##虚拟路由的ID号,一般不大于255,可选IP最后一段使用##

priority 100           ##初始优先级,选举过程中判断的依据,和路由的概念一样##

advert_int 1           ##检查间隔,默认1s##

authentication {       ##认证机制##

auth_type PASS         ##认证方式,PASS为明文##

auth_pass ipython      ##认证密码##

}

virtual_ipaddress {    ##虚拟地址池##

192.168.230.190

}

}

###后面的配置参数 先删删掉吧###

 

配置Backup 配置如下:

 

global_defs {

   notification_email {##指定keepalived在发生事件(如切换)需要发送Email的对象,多个写多行##

     [email protected]

   }

   notification_email_from [email protected] ##发送人

   smtp_server smtp.126.com ##SMTP服务器##

   smtp_connect_timeout 30  ##链接超时时间##

   router_id LVS_DEVEL       ##路由标识,这里用主机名##

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eno16777736

    virtual_router_id 51

    priority 50

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass ipython

    }

    virtual_ipaddress {

        192.168.230.190

    }

}

 

###其他配置:####

nopreempt 设置为不抢占,这个配置只能设置在state为BACKUP的节点上,并且这个机器的优先级必须比另一台高

preempt_delay 抢占延迟,默认5分钟

debug debug级别

notify_master 切换到Master时执行的脚本

##start##

service keepalived start

Starting keepalived:   [  OK  ]

 

###观察其日志文件###

tail -f /var/log/messages 

Aug  3 00:02:12 Nginx-one Keepalived[8177]: Starting Keepalived v1.2.13 (08/03,2014)

Aug  3 00:02:12 Nginx-one Keepalived[8178]: Starting Healthcheck child process, pid=8180

Aug  3 00:02:12 Nginx-one Keepalived[8178]: Starting VRRP child process, pid=8181

####当前的IP地址####

Aug  3 00:02:13 Nginx-one Keepalived_vrrp[8181]: Netlink reflector reports IP 192.168.230.128 added

Aug  3 00:02:13 Nginx-one Keepalived_vrrp[8181]: Netlink reflector reports IP fe80::20c:29ff:fecb:90a2 added

Aug  3 00:02:13 Nginx-one Keepalived_vrrp[8181]: Registering Kernel netlink reflector

Aug  3 00:02:13 Nginx-one Keepalived_vrrp[8181]: Registering Kernel netlink command channel

Aug  3 00:02:13 Nginx-one Keepalived_healthcheckers[8180]: Netlink reflector reports IP 1.1.1.10 added

Aug  3 00:02:13 Nginx-one Keepalived_healthcheckers[8180]: Netlink reflector reports IP fe80::20c:29ff:fecb:90a2 added

Aug  3 00:02:13 Nginx-one Keepalived_healthcheckers[8180]: Registering Kernel netlink reflector

Aug  3 00:02:13 Nginx-one Keepalived_vrrp[8181]: Registering gratuitous ARP shared channel

Aug  3 00:02:13 Nginx-one Keepalived_healthcheckers[8180]: Registering Kernel netlink command channel

Aug  3 00:02:13 Nginx-one Keepalived_vrrp[8181]: Opening file '/etc/keepalived/keepalived.conf'.

Aug  3 00:02:13 Nginx-one Keepalived_vrrp[8181]: Configuration is using : 62834 Bytes

Aug  3 00:02:13 Nginx-one Keepalived_vrrp[8181]: Using LinkWatch kernel netlink reflector...

Aug  3 00:02:13 Nginx-one Keepalived_vrrp[8181]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]

###打开并加载配置文件####

Aug  3 00:02:13 Nginx-one Keepalived_healthcheckers[8180]: Opening file '/etc/keepalived/keepalived.conf'.

Aug  3 00:02:13 Nginx-one Keepalived_healthcheckers[8180]: Configuration is using : 7377 Bytes

Aug  3 00:02:13 Nginx-one Keepalived_healthcheckers[8180]: Using LinkWatch kernel netlink reflector...

####切换为Master 状态####

Aug  3 00:02:14 Nginx-one Keepalived_vrrp[8181]: VRRP_Instance(VI_1) Transition to MASTER STATE

Aug  3 00:02:15 Nginx-one Keepalived_vrrp[8181]: VRRP_Instance(VI_1) Entering MASTER STATE

Aug  3 00:02:15 Nginx-one Keepalived_vrrp[8181]: VRRP_Instance(VI_1) setting protocol VIPs.

####在接口上添加VIP###

Aug  3 00:02:15 Nginx-one Keepalived_vrrp[8181]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.230.190

Aug  3 00:02:15 Nginx-one Keepalived_healthcheckers[8180]: Netlink reflector reports IP 1.1.1.100 added

Aug  3 00:02:20 Nginx-one Keepalived_vrrp[8181]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 192.168.230.190

 

###查看是否添加VIP###

ip a show|awk '/inet\ /'

    inet 127.0.0.1/8 scope host lo

    inet 1.1.1.10/8 brd 1.255.255.255 scope global eth1

    inet 1.1.1.100/32 scope global eth1

 

停止MASTER,查看BACKUP的状态转移

service keepalived stop

Stopping keepalived:                                       [  OK  ]

 

####和路由协议一样,当MASTER上线被检测到会抢占VIP,可以想象的到,Keepalived也支持非抢占模式,只有BACKUP在变成MASTER后宕机了,才会转移VIP,说起来怎么这么绕口####

######################4.启动keepalived,并查看###########################################

service keepalived start

[root@redis_master ~]# ip a|grep inet

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

    inet 192.168.230.128/24 brd 192.168.240.255 scope global eth0

    inet 192.168.230.190/32 scope global eth0

inet6 fe80::250:56ff:fe98:6a56/64 scope link