一、下载安装
http://www.keepalived.org/software/keepalived-1.4.2.tar.gz
1、操作系统:CentOS 7.4,先确保安装了openssl,安装方法:
yum -y install openssl-devel
没有OpenSSL的话,configure会报错:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files.
2、解压编译
tar -zxf keepalived-1.4.2.tar.gz
chmod -R 777 keepalived-1.4.2
cd keepalived-1.4.2
./configure #默认安装
make
make install
会在以下路径生成:
/usr/local/etc/keepalived/keepalived.conf
/usr/local/etc/sysconfig/keepalived
/usr/local/sbin/keepalived
然后我们需要手动做以下事情:
1、mkdir /etc/keepalived #新建文件夹
再把/usr/local/etc/keepalived/keepalived.conf拷贝到/etc/keepalived/keepalived.conf,
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
内容修改为:
! Configuration File for keepalived global_defs { notification_email { #指定Keepalived在发生事情的时候,发送邮件通知,每行一个地址 acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc #指定发件人 smtp_server 192.168.200.1 #发送email的smtp地址 smtp_connect_timeout 30 #超时时间 router_id lb01 #运行Keepalived的机器标识号,主从机必须不同 ##vrrp_skip_check_adv_addr #注释掉vrrp_strict相关是为了解决虚拟ip,ping不通的问题 ##vrrp_strict ##vrrp_garp_interval 0 ##vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER interface ens33 #物理网卡名称,主节点和备节点需要相同 virtual_router_id 100 #唯一的id,主从机必须相同 priority 150 #优先级,主节点大于备节点,建议至少相差50 advert_int 1 #通信检查间隔时间1s authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.6.200 #虚拟ip,会绑定在ens33网卡 } }
注意:配置keepalived后发现虚拟ip ping不通的解决办法,vim /etc/keepalived/keepalived.conf。把vrrp_strict相关的语句注释就可以了。
2、在路径/etc/init.d/下新建keepalived.sh脚本,内容如下,重点关注exec和sysconfig的路径要对的上:
#!/bin/sh # # keepalived High Availability monitor built upon LVS and VRRP # # chkconfig: - 86 14 # description: Robust keepalive facility to the Linux Virtual Server project \ # with multilayer TCP/IP stack checks. ### BEGIN INIT INFO # Provides: keepalived # Required-Start: $local_fs $network $named $syslog # Required-Stop: $local_fs $network $named $syslog # Should-Start: smtpdaemon httpd # Should-Stop: smtpdaemon httpd # Default-Start: # Default-Stop: 0 1 2 3 4 5 6 # Short-Description: High Availability monitor built upon LVS and VRRP # Description: Robust keepalive facility to the Linux Virtual Server # project with multilayer TCP/IP stack checks. ### END INIT INFO # Source function library. . /etc/rc.d/init.d/functions exec="/usr/local/sbin/keepalived" prog="keepalived" config="/etc/keepalived/keepalived.conf" [ -e /usr/local/etc/sysconfig/$prog ] && /usr/local/etc/sysconfig/$prog lockfile=/var/lock/subsys/keepalived start() { [ -x $exec ] || exit 5 [ -e $config ] || exit 6 echo -n $"Starting $prog: " daemon $exec $KEEPALIVED_OPTIONS retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { echo -n $"Reloading $prog: " killproc $prog -1 retval=$? echo return $retval } force_reload() { restart } rh_status() { status $prog } rh_status_q() { rh_status &>/dev/null } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 restart ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac exit $?
二、运行测试
1、获得权限
chmod a+x /etc/init.d/keepalived
chmod a+x /usr/local/etc/sysconfig/keepalived
chmod a+x /usr/local/sbin/keepalived
2、启动
/etc/init.d/keepalived start
或者 service keepalived start
启动后,可以通过以下命令来查看是否安装正确,启动后有3个keepalive进程表示正确:
[root@localhost init.d]# ps -ef|grep keep|grep -v grep
root 20163 1 0 10:03 ? 00:00:00 /usr/local/sbin/keepalived -D
root 20164 20163 0 10:03 ? 00:00:00 /usr/local/sbin/keepalived -D
root 20165 20163 0 10:03 ? 00:00:00 /usr/local/sbin/keepalived -D
[root@localhost ~]# ip add|grep 172.16
inet 172.16.6.161/24 brd 172.16.6.255 scope global ens33
inet 172.16.6.200/32 scope global ens33