mysql 和 HAProxy 配置请参考 上一篇随笔
keepalived 最简单的配置高可用
1.修改/etc/sysctl.conf
vi /etc/sysctl.conf
首先找到net.ipv4.ip_forward=1这个配置,并其注释去除,然后在下面插入net.ipv4.ip_nonlocal_bind=1这句配置
2.更新环境
sudo apt-get install libssl-dev openssl libpopt-dev daemon
3.下载安装包
wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz
4.解压
tar zxvf keepalived-1.2.18.tar.gz
5.切换到解压目录
cd keepalived-1.2.18
6.执行命令
./configure --prefix=/usr/local/keepalived
7.执行make命令
make
8. 执行make install
make install
9.将keepalived作为系统服务启动
# mkdir /etc/sysconfig # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ # cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ # cp /usr/local/keepalived/sbin/keepalived /sbin/
10.把keepalived 的配置文件放到/etc/keepalived 下,keepalived默认会读取这个文件。
# mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
11. 修改/etc/keepalived/keepalived.conf , (最好在配置文件中把里边的注释都删掉)
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node-00 vrrp_mcast_group4 224.0.38.12 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 38 //两台服务器的这个id不能一样 priority 100 advert_int 1 authentication { auth_type PASS auth_pass x1er44q } virtual_ipaddress { 192.168.117.111 dev eth0 } smtp_alert } }
- virtual_ipaddress :就是keepalived向外提供可访问的ip
-interface : 这个例子是eth0 ,根据自己机器的网卡确定
12.将keepalived作为系统启动服务:
mkdir -p /etc/rc.d/init.d
ln -s /lib/lsb/init-functions /etc/rc.d/init.d/functions
因为ubuntu 和centos 系统目录有差别,所以要创建上边的连接
13.创建subsys 目录
mkdir /var/lock/subsys
14.修改 /etc/rc.local
vi /etc/rc.local
在exit 0 前加入
mkdir /var/lock/subsys service keepalived start
15.启动keepalived:
service keepalived start
16.验证,执行
ip addr show
在eth0 中会显示192.168.117.111 ip
参考过的文章:
https://www.cnblogs.com/phpstudy2015-6/p/6706465.html
https://www.cnblogs.com/kevingrace/p/6710136.html
https://www.cnblogs.com/cutemsyu/p/6019420.html
https://www.cnblogs.com/linjiqin/p/8073468.html