Keepalived

1.安装keepalived(ubuntu server)

首先安装依赖:

安装libssl-dev(sudo apt-get install libssl-dev);

        libpopt-dev(sudo apt-get install libpopt-dev);

        libnl libnl-devel(sudo apt-get install libnl libnl-devel)(IPV6);

        libnfnetlink-devel(sudo apt-get install libnfnetlink-devel)(IPV6);

下载keepalived-1.3.6.tar.gz安装包

并解压与~/keepalived-1.3.6/

编译:./configure --prefix=/usr/local/keepalived

            make && make install

2.keepalived配置

复制需要的文件

sudo cp ~/keepalived-1.3.6/keepalived/etc/init.d/keepalived /etc/init.d/keepalived

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

sudo cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig(/etc/sysconfig不存在,需提前创建)

sudo mkdir /etc/keepalived/

sudo cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf(配置信息可参照如下)

Master配置:

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server [email protected]
   smtp_connect_timeout 30
   router_id LVS_59
}

vrrp_script chk_haproxy {
   script "/usr/local/keepalived/shell/chk_haproxy.sh"    #服务探测,返回0说明服务是正常的
   interval 3                     #每隔3秒探测一次
   weight 5                       #haproxy上线,权重加5;下线,权重减5
   fall 2
   rise 2
}

vrrp_instance mysql_1 {
    state MASTER
    interface eth0
    virtual_router_id 50
    garp_master_delay 1
    priority 120
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass password1
    }
    virtual_ipaddress {
        192.168.56.101
    }
    track_interface {
        eth0
    }
    track_script {
        chk_haproxy
    }
}

Slave配置:

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server [email protected]
   smtp_connect_timeout 30
   router_id LVS_59
}

vrrp_script chk_haproxy {
   script "/usr/local/keepalived/shell/chk_haproxy.sh"    #服务探测,返回0说明服务是正常的
   interval 3                     #每隔3秒探测一次
   weight 5                       #haproxy上线,权重加5;下线,权重减5
   fall 2
   rise 2
}

vrrp_instance mysql_1 {
    state BACKUP
    interface eth0
    virtual_router_id 50
    garp_master_delay 1
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass password1
    }
    virtual_ipaddress {
        192.168.56.101
    }
    track_interface {
        eth0
    }
    track_script {
        chk_haproxy
    }
}


检测脚本chk_haproxy.sh:

#!/bin/bash

# 定时查看haproxy是否存在,如果不存在则启动haproxy,
# 如果启动失败,则停止keepalived
echo "[Keepalived]Check HAProxy Running..."

beforeStatus=$(ps aux|grep haproxy | grep -v grep | grep -v chk_haproxy.sh | grep -v bash | wc -l)
if [ $beforeStatus -le 0 ]; then
   echo "[Keepalived]Startup HAProxy Service"
   sudo sh /usr/local/haproxy/startup.sh
   sleep 2
   afterStatus=$(ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l)
   echo "After:$afterStatus"
   if [ $afterStatus -le 0  ]; then
      echo "[Keepalived]Shutdown Keepalived Service"
      sudo sh /usr/local/keepalived/shutdown.sh
   fi
fi

echo "[Keepalived]Check HAProxy Running Done."
curl -I -s --connect-timeout $timeout http://localhost:8080 -w %{http_code} | tail -n1

添加软连接:首先创建/etc/rc.d/init.d/目录

sudo ln -s /lib/lsb/init-functions /etc/rc.d/init.d/functions

3.启动keepalived

/etc/init.d/keepalived start|restart|stop

启动时需要输入当前用户密码


4.查看keepalived是否已启动

ps aux|grep keepalived

查看keepalived设置IP:ip a


5.配置keepalived日志

在/etc/sysconfig/keepalived文件中修改KEEPALIVED-OPTIONS="-D",修改为:KEEPALIVED-OPTIONS="-D -d -S 0"

在/etc/rsyslog.conf文件中添加:

#keepalived

local0.* /var/log/keepalived.log

在/etc/rsyslog.d/中添加48-keepalived.conf文件(sudo cp 50-default.conf 48-keepalived.conf)

删除原内容并添加如下内容:

local0.*     /var/log/keepalived.log

表示local0设备的日志信息记录于/var/log/keepalived.log里

然后重启rsyslog服务:sudo service rsyslog restart


6.启动停止脚本

启动:

#!/bin/sh 

HOME=/usr/local/keepalived/
DAEMON=/etc/init.d/keepalived
DAEMON_ARGS="start -f /etc/keepalived/keepalived.conf -d -D -S 0"
DESC="Keepalived Daemon"

echo "Starting $DESC, and use conf $DAEMON_ARGS"

$DAEMON $DAEMON_ARGS
停止:

#!/bin/sh 

HOME=/usr/local/keepalived/
DAEMON=/etc/init.d/keepalived
DAEMON_ARGS=" stop -f /etc/keepalived/keepalived.conf -d -D -S 0"
DESC="Keepalived Daemon"

echo "Stopping $DESC, and use conf $DAEMON_ARGS"

$DAEMON$DAEMON_ARGS

问题

问题1:Keepalived执行chk_haproxy.sh检测脚本时出现exited due to signal 15

解决1:vrrp_script{}中的interval时间需大于脚本中的sleep时间

问题2:通过查看日志发现slave机器启动后也进入master状态

解决2:主/备节点轮流在对外发布vrrp通告理论上备节点如果收到主节点的通告,通告中优先级高于自己,就不会主动对外发送通告查看iptables,默认没有允许vrrp或者组播流量,导致备节点收不到主节点的通告,认为主节点故障,切换状态,发布VIP

   看看是不是防火墙问题,

    防火墙iptables添加: -A INPUT -p vrrp -j ACCEPT

    重启防火墙:service iptables restart


你可能感兴趣的:(分布式)