linux企业运维篇——keepalived实现LVS高可用

1.新建一个server4虚拟机

(不会的参照linux企业运维篇——环境配置 虚拟机封装)

linux企业运维篇——keepalived实现LVS高可用
配置yum源

linux企业运维篇——keepalived实现LVS高可用
linux企业运维篇——keepalived实现LVS高可用
安装openssh-clients
linux企业运维篇——keepalived实现LVS高可用

2.配置环境

在server1中下载keepalived安装包

tar zxf keepalive-2.0.6.tar.gz #解压keepalived

linux企业运维篇——keepalived实现LVS高可用
安装gcc
linux企业运维篇——keepalived实现LVS高可用
安装 openssl-devel

yum install -y openssl-devel

linux企业运维篇——keepalived实现LVS高可用
解析编译keepalived
linux企业运维篇——keepalived实现LVS高可用

./configure --prefix=/usr/local/keepalived --with-init=SYSV #当Use IPVS Framework 是yes时表示正确

linux企业运维篇——keepalived实现LVS高可用
linux企业运维篇——keepalived实现LVS高可用

make && make install 

linux企业运维篇——keepalived实现LVS高可用

scp -r keepalived server4:/usr/local/ #copy文件 keepalived到server4

linux企业运维篇——keepalived实现LVS高可用

chmod  +x /usr/local/keepalived/etc/rc.d/init.d/keepalived #给keepalived添加执行权限

linux企业运维篇——keepalived实现LVS高可用
做四个软连接

 ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
 ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf samples/
 ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
 ln -s /usr/local/keepalived/etc/keepalived/ /etc/
 ln -s /usr/local/keepalived/sbin/keepalived /sbin/

linux企业运维篇——keepalived实现LVS高可用
linux企业运维篇——keepalived实现LVS高可用
linux企业运维篇——keepalived实现LVS高可用
linux企业运维篇——keepalived实现LVS高可用

vim /etc/keepalived/keepalived.conf  #编辑配置文件

linux企业运维篇——keepalived实现LVS高可用

linux企业运维篇——keepalived实现LVS高可用

scp  /etc/keepalived/keepalivedconf server4:/etc/keepalived/

linux企业运维篇——keepalived实现LVS高可用

/etc/init.d/ldirectord stop    ## keepalived 可实现健康检查 
chkconfig ldirectord off  #关闭ldirectord开机自启,以免影响实验

linux企业运维篇——keepalived实现LVS高可用
删除eth0上的172.25.6.100/24,因为会自动加载172.25.6.100/24到eth0

linux企业运维篇——keepalived实现LVS高可用

在server4中修改配置文件
linux企业运维篇——keepalived实现LVS高可用
之前的在server1中已经配置好复制过来,只用修改18行和21行
18行修改MASTER为BACKUP
21行修改权重为50

vrrp_instance VI_1 {
    state BACKUP  #18行
    interface eth0
    virtual_router_id 51
    priority 50  #21行
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.6.100
    }
}

做四个软连接,和server1中的一样

linux企业运维篇——keepalived实现LVS高可用
linux企业运维篇——keepalived实现LVS高可用

chmod  +x /usr/local/keepalived/etc/rc.d/init.d/keepalived #给keepalived执行权限
/etc/initd/keepalived start #开启服务

linux企业运维篇——keepalived实现LVS高可用

3.测试:

在server1中

ip a #查看已经自动将172.25.6.100的ip加载到eth0

linux企业运维篇——keepalived实现LVS高可用

/etc/init.d/keepalived stop   #关闭server1的keepalived服务

linux企业运维篇——keepalived实现LVS高可用

在server4上:
ip a #可以看到172.25.6.100飘移到server4上了
linux企业运维篇——keepalived实现LVS高可用

4.邮件测试

server1和server4都下载安装malix
linux企业运维篇——keepalived实现LVS高可用
linux企业运维篇——keepalived实现LVS高可用
server1中:
打开keepalived服务并查看ip
linux企业运维篇——keepalived实现LVS高可用
关闭server2的httpd服务
linux企业运维篇——keepalived实现LVS高可用

在server1中mail查看发现一封邮件,显示172.25.6.2状态是DOWN
linux企业运维篇——keepalived实现LVS高可用
重新打开server2的httpd服务
linux企业运维篇——keepalived实现LVS高可用
在server1中mail查看

mail # 多了一封邮件,显示172.25.6.2状态是UP

linux企业运维篇——keepalived实现LVS高可用