keepalived配置

淺唱寂寞╮ 提交于 2019-12-18 13:20:31

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

1、下载地址

https://www.keepalived.org/software/keepalived-2.0.19.tar.gz

2、编译安装

centos7环境

1、安装依赖库

yum install openssl-devel libnl3-devel ipset-devel iptables-devel make autoconf automake file-devel net-snmp-devel glib2-devel json-c-devel pcre2-devel libnftnl-devel libmnl-devel

2、解压缩

tar -xvf keepalived-2.0.19.tar.gz

3、进入目录

cd keepalived-2.0.19

4、编译安装

./configure

make

make install

5、配置主节点

keepalived在启动时,会自动去/etc/keepalived下面找配置文件

mkdir /etc/keepalived

vi /etc/keepalived/ keepalived.conf

! Configuration File for keepalived

global_defs {
#   notification_email {
#     lipengfei@topnet.net.cn
#   }
#   notification_email_from  lipengfei@topnet.net.cn
#   smtp_server smtp.exmail.qq.com
#   smtp_connect_timeout 30
#   router_id LVS_DEVEL
#   vrrp_skip_check_adv_addr
#   vrrp_strict
#   vrrp_garp_interval 0
#   vrrp_gna_interval 0
}

vrrp_script chk_mysql {  #定义脚本并设置规则
          script "</dev/tcp/127.0.0.1/3306"
          interval 1
          weight -2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens192
    virtual_router_id 51
    priority 100
#    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.3.247
    }
    track_script {
      chk_mysql
    }
}
~                                                                                                                                                                                                                  
~                                                                                                                                                                                                                  
~                        

6、配置从节点

mkdir /etc/keepalived

vi /etc/keepalived/ keepalived.conf

! Configuration File for keepalived

global_defs {
#   notification_email {
#     lipengfei@topnet.net.cn
#   }
#   notification_email_from  lipengfei@topnet.net.cn
#   smtp_server smtp.exmail.qq.com
#   smtp_connect_timeout 30
#   router_id LVS_DEVEL
#   vrrp_skip_check_adv_addr
#   vrrp_strict
#   vrrp_garp_interval 0
#   vrrp_gna_interval 0
}

vrrp_script chk_mysql {  #定义脚本并设置规则
          script "</dev/tcp/127.0.0.1/3306"
          interval 1
          weight -2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens192
    virtual_router_id 51
    priority 99
   # nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.3.247
    }
    track_script {
      chk_mysql
    }
}
~                    

7、验证

在主节点上创建文件

echo ‘master’ >a

在从节点上创建文件

echo ‘salve’ >a

停止主节点的mysql

登录 192.168.3.247 cat a 结果是 salve

启动主节点的mysql

登录 192.168.3.247 cat a 结果是 master

则keepaliven安装成功。

8、注意事项

当mysql主库挂了以后,修复主库时,先把keepalived服务停止,然后启动mysql进行数据回复。回复成功后,再启动keepalived

9、常用命令

启动keepalived

systemctl start keepalived

停止keepalived

systemctl stop keepalived

查看状态keepalived

systemctl status keepalived

启动 mysql

service mysql start

停止 mysql

service mysql stop

查看状态 mysql

service mysql status

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!