Keepalived安装工具

时间:2023-07-08 23:24:49
装keepalived前,要先检查主机上是否已经安装,
ps -ef | grep keepalive
不检查的话。easy把前人装的东西覆盖掉,那么曾经弄的配置文件都没了比較麻烦。

下面都为root用户
root用户上传工具,压缩包就200多k,不大。
cd && mkdir fwy && chmod 777 fwy && cd fwy
scp padba@cnsz081003:/paic/dba/dbsoft/mysql/keepalived-1.1.19.tar.gz .
passwordMdCg2014
tar xvf keepalived-1.1.19.tar.gz
mv keepalived-1.1.19 ..

安装OpenSSL
[root@cnsh042942 fwy]# yum install openssl-devel
Loaded plugins: product-id, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
el6_u5_base | 1.2 kB 00:00
Setting up Install Process
Package openssl-devel-1.0.1e-15.el6.x86_64 already installed and latest version
Nothing to do

编译并安装
mv /root/fwy/keepalived-1.1.19 /root
cd /root/keepalived-1.1.19
./configure
make && make install

配置
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/
mkdir /opt/keepalived
touch /etc/keepalived/keepalived.conf

配置脚本
[root@cnsh042942 keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
    link/ether 00:50:56:aa:0a:89 brd ff:ff:ff:ff:ff:ff
3: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc
noqueue state UP
    link/ether 00:50:56:aa:0a:89 brd ff:ff:ff:ff:ff:ff
    inet 10.31.10.138/24 brd 10.31.10.255 scope global bond0
    inet6 fe80::250:56ff:feaa:a89/64 scope link
       valid_lft forever preferred_lft forever

我们的10.31.10.138是绑定在bond0上的,bond0应该算是虚拟网卡。
当中interface bond0的bond0。virtual_router_id 117的117。virtual_ipaddress中的地址。是写成自己想要的vip,也是DA先申请好一个vip以免冲突。

touch /opt/keepalived/keepalived_check_mysql.sh
touch /opt/keepalived/to_master.sh

keepalived.conf配置文件,该文件存在可是是空的。
ls -l /etc/keepalived/keepalived.conf

Master节点用例如以下脚本:
vrrp_script check_mysql {
  script "/opt/keepalived/keepalived_check_mysql.sh"
  weight -10
}
vrrp_instance KEEPALIVED_MYSQL {
        ##注意:两台MYSQLserver都设置为BACKUP
        state backup
        interface bond0
        virtual_router_id 117
        ##Master设置为101,Slave设置为100
        priority 101
        nopreempt
        track_script {
          check_mysql
        }
        virtual_ipaddress {
                10.31.10.152
        }
        notify_master /opt/keepalived/to_master.sh
}
vrrp_script check_mysql {
  script "/opt/keepalived/keepalived_check_mysql.sh"
  weight -10
}
主库、从库的virtual_router_id值要同样,mysql取100-149之间,假设同一网段已经有同样的virtual_router_id被用了,那么就会起来失败。

Slave节点用例如以下脚本,能够注意到,priority比上面小1。
vrrp_instance KEEPALIVED_MYSQL {
        ##注意:两台MYSQLserver都设置为BACKUP
        state backup
        interface bond0
        virtual_router_id 117
        ##Master设置为101,Slave设置为100
        priority 100
        ##nopreempt
        track_script {
          check_mysql
        }
        virtual_ipaddress {
                10.31.10.152
        }
        notify_master /opt/keepalived/to_master.sh
}


创建通道
通道就是,在mysql里面定义一些能够用密文登陆的用户。然后给一些工具直接用密文来登陆的,以保证安全。
切换到mysql用户:
mysql_config_editor set --login-path=keepalived_monitor --host=localhost --user=keepalived --password
--socket=${MYSQL_HOME}/var/mysql.sock
然后就会在mysql用户家文件夹下生成一下.mylogin.cnf文件。

root用户
cd /opt/keepalived/
keepalived_check_mysql.sh脚本例如以下
MYSQL=/usr/bin/mysql,要改动为/paic/t0gimp/rdbms/mysql/5.6/bin/mysql
LOG_FILE=/opt/keepalived/check_mysql.log 这里能够改动为更具识别名称的/opt/keepalived/check_t0gimp.log
export MYSQL_TEST_LOGIN_FILE=/paic/emmsq/data/mysqldata/emmsq /.mylogin.cnf ,这个.mylogin.cnf文件。在mysql用户的家文件夹下。是用mysql用户创建通道时自己主动建立的。
$MYSQL --login-path=keepalived_monitor -e 的”keepalived_monitor改动为实际的通道名“

#!/bin/bash
MYSQL=/paic/t0gimp/rdbms/mysql/5.6/bin/mysql
# 日志文件
LOG_FILE=/opt/keepalived/check_t0gimp.log
export MYSQL_TEST_LOGIN_FILE=/paic/t0gimp/rdbms/mt0gimp/.mylogin.cnf
# 检查次数
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
  $MYSQL --login-path=keepalived_monitor -e
"show status;" >/dev/null 2>&1
  if [ $? = 0 ] ;then
    MYSQL_OK=1
  else
    MYSQL_OK=0
  fi
  return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
  let "CHECK_TIME -= 1"
  check_mysql_helth
  if [ $MYSQL_OK = 1 ] ; then
    CHECK_TIME=0
    #echo `date --date=today +"%Y-%m-%d %H:%M:%S"` - [INFO] - mysql available: success[$MYSQL_OK] >> $LOG_FILE
    exit 0
  fi
  if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
  then
    echo `date --date=today +"%Y-%m-%d %H:%M:%S"` - [INFO] - mysql invaild. keepalived stop. >> $LOG_FILE
    exit 1
  fi
  sleep 1
done


to_master.sh  这是从库切换为主库之后执行的shell脚本

#!/bin/bash
MYSQL=/paic/t0gimp/rdbms/mysql/5.6/bin/mysql
VIP=10.31.10.152
GATEWAY=10.31.10.200
/sbin/arping -I bond0 -c
5 -s $VIP $GATEWAY &>/dev/null
$MYSQL --login-path=keepalived_switch -e "set
global read_only=OFF" >/dev/null 2>&1

sh脚本赋权
cd /opt/keepalived
chmod 700 *.sh

keepalived启停
[root@cnsh281018 ~]# /etc/init.d/keepalived start
[root@cnsh281018 ~]# /etc/init.d/keepalived stop

验证,
[root@cnsh042942 keepalived]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[root@cnsh042942 keepalived]# ps -ef | grep keepa
root 20353 1 0 13:53 ? 00:00:00 keepalived -D
root 20354 20353 0 13:53 ? 00:00:00 keepalived -D
root 20372 1 0 13:53 ? 00:00:00 /bin/bash /opt/keepalived/keepalived_check_mysql.sh
root 20382 20354 3 13:53 ?

00:00:00 keepalived -D

root 20383 20382 0 13:53 ?

00:00:00 /bin/bash /opt/keepalived/keepalived_check_mysql.sh

root 20394 17696 0 13:53 pts/2 00:00:00 grep keepa

版权声明:本文博客原创文章。博客,未经同意,不得转载。