龚哥哥 - 山里男儿 爱生活、做自己!
CentOS使用Keepalived搭建双机热备
发表于 2016-8-23 | 服务器

1、环境

CentOS6.5
keepalived-1.2.23

2、准备两台服务器和一个VIP IP

Master      192.168.0.109
Backup      192.168.0.110
VIP         192.168.0.200

3、安装gcc编译器,openssl,wget,如果已经安装则跳过

yum -y install openssl-devel ncurses-devel gcc gcc-c++ make rpm-build wget

4、目录创建

mkdir /data/soft
mkdir /data/nginx
mkdir /data/www

6、安装keepalived

cd /data/soft
wget http://www.keepalived.org/software/keepalived-1.2.23.tar.gz
tar -zxvf keepalived-1.2.23.tar.gz
cd keepalived-1.2.23
./configure --prefix=/usr/local/keepalived
make
make install

7、将keepalived做成启动脚务

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
chmod +x /etc/init.d/keepalived

8.1、MASTER配置文件,位置 /etc/keepalived/keepalived.conf

global_defs {
    notification_email
    {
        fuxiang.gong@qq.com
    }
    notification_email_from 17091959688@163.com
    smtp_server smtp.163.com
    stmp_connect_timeout 30
    router_id lnmp_node1
}

# 服务
vrrp_instance LNMP {
    state MASTER    #设置为主服务器
    interface eth0  #监测网络接口
    virtual_router_id 51  #主、备必须一样
    priority 100   #主机级别,值越大优先级越高
    advert_int 1   #VRRP Multicast广播周期秒数

    authentication {
        auth_type PASS  #VRRP认证方式,主备必须一致
        auth_pass 1111  #密码
    }
    virtual_ipaddress {
        192.168.0.200  #VRRP HA虚拟地址
    }
}

8.2、BACKUP配置文件

global_defs { 
    notification_email
    {
        fuxiang.gong@qq.com
    }
    notification_email_from 17091959688@163.com
    smtp_server smtp.163.com
    stmp_connect_timeout 30
    router_id lnmp_node2
}

# 服务
vrrp_instance LNMP {
    state BACKUP    #设置为备用服务器
    interface eth0  #监测网络接口
    virtual_router_id 51  #主、备必须一样
    priority 90   #主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高 
    advert_int 1   #VRRP Multicast广播周期秒数
    authentication {
        auth_type PASS  #VRRP认证方式,主备必须一致
        auth_pass 1111  #密码
    }
    virtual_ipaddress {
        192.168.0.200  #VRRP HA虚拟地址
    }
}

8.3、允许两台服务器vrrp包通过防火墙,如果关闭防火墙则跳过(两台服务器上都配置)

MASTER
  vim /etc/sysconfig/iptables
  -A INPUT -i eth0 -p vrrp -s 192.168.0.111 -j ACCEPT

BACKUP
  vim /etc/sysconfig/iptables
  -A INPUT -i eth0 -p vrrp -s 192.168.0.110 -j ACCEPT

重启防火墙
service iptables restart

9、keepalived操作,这里做启动操作

service keepalived start      启动
service keepalived stop       停止
service keepalived restart    重启

10、查看已经成功添加虚拟网卡

主服务器  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,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d9:a8:bd brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.109/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.200/32 scope global eth0
    inet6 fe80::20c:29ff:fed9:a8bd/64 scope link 
       valid_lft forever preferred_lft forever

BACKUP服务器  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,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d9:8f:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.110/24 brd 192.168.0.255 scope global eth0
    inet6 fe80::20c:29ff:fed9:8f72/64 scope link 
       valid_lft forever preferred_lft forever

11、安装nginx,详细步骤查看博客中其它nginx安装方法文章

12、把nginx访问指向 /data/www目录,在 /data/www 目录中创建index.html内容分别写 MASTER, BACKUP(通过浏览器访问 192.168.0.200 ),默认MASTER,如果关闭MASTER keepalived服务,BACKUP服务器将自动升级为MASTER提供服务。开启MASTER keepalived服务,自动切换MASTER服务器来服务,BACKUP降级为备用服务。

13、查看日志

tail -f /var/log/messages

发表评论:

TOP