CentOS7编译安装keepalived实现自动漂移、高可用及开机启动

CentOS7编译安装keepalived实现自动漂移、高可用及开机启动

一、先安装依赖包
[root@haoding205-jq1 hd205]# yum install -y epel-release
[root@haoding205-jq1 hd205]# yum install -y gcc gcc-c++ openssl openssl-devel popt-devel kernel-devel libnfnetlink-devel

二、安装keepalived
[root@haoding205-jq1 hd205]# ll
总用量 7796
-rw-rw-r-- 1 haoding haoding 289380 9月 13 11:28 keepalived-1.2.24.tar.gz
[root@haoding205-jq1 hd205]# cp keepalived-1.2.24.tar.gz /usr/local/src/
[root@haoding205-jq1 hd205]# cd /usr/local/src/
[root@haoding205-jq1 src]# ll
总用量 284
-rw-r–r-- 1 root root 289380 9月 13 11:34 keepalived-1.2.24.tar.gz
[root@haoding205-jq1 src]# gzip -cdf keepalived-1.2.24.tar.gz | tar -xvf -
[root@haoding205-jq1 src]# ll
总用量 288
drwxrwxr-x 7 haoding haoding 4096 8月 29 2012 keepalived-1.2.24
-rw-r–r-- 1 root root 289380 9月 13 11:34 keepalived-1.2.24.tar.gz
[root@haoding205-jq1 src]# cd keepalived-1.2.24
[root@haoding205-jq1 keepalived-1.2.24]# ll
总用量 356
-rw-rw-r-- 1 haoding haoding 41 7月 19 2012 AUTHOR
drwxrwxr-x 2 haoding haoding 23 8月 29 2012 bin
-rw-rw-r-- 1 haoding haoding 107055 8月 29 2012 ChangeLog
-rwxrwxr-x 1 haoding haoding 165821 8月 14 2012 configure
-rw-rw-r-- 1 haoding haoding 9818 8月 14 2012 configure.in
-rw-rw-r-- 1 haoding haoding 830 7月 19 2012 CONTRIBUTORS
-rw-rw-r-- 1 haoding haoding 17982 7月 19 2012 COPYING
drwxrwxr-x 4 haoding haoding 107 8月 29 2012 doc
drwxrwxr-x 2 haoding haoding 4096 8月 29 2012 genhash
-rw-rw-r-- 1 haoding haoding 1601 7月 19 2012 INSTALL
-rwxrwxr-x 1 haoding haoding 5598 7月 19 2012 install-sh
drwxrwxr-x 9 haoding haoding 118 8月 29 2012 keepalived
-rw-rw-r-- 1 haoding haoding 5229 7月 19 2012 keepalived.spec.in
drwxrwxr-x 2 haoding haoding 4096 8月 29 2012 lib
-rw-rw-r-- 1 haoding haoding 1259 8月 14 2012 Makefile.in
-rw-rw-r-- 1 haoding haoding 1007 7月 19 2012 README
-rw-rw-r-- 1 haoding haoding 20 8月 29 2012 TODO
-rw-rw-r-- 1 haoding haoding 6 8月 29 2012 VERSION

[root@haoding205-jq1 keepalived-1.2.24]# pwd
/usr/local/src/keepalived-1.2.24
[root@haoding205-jq1 keepalived-1.2.24]# ./configure --prefix=/usr/local/keepalived

Use IPVS Framework : Yes
IPVS sync daemon support : Yes
Use VRRP Framework : Yes
# 必须看到如上的提示,说明配置正确,才能继续后面的安装

[root@haoding205-jq1 keepalived-1.2.24]# make

[root@haoding205-jq1 keepalived-1.2.24]# make install

[root@haoding205-jq1 keepalived-1.2.24]# find / -name keepalived.conf
/usr/local/src/keepalived-1.2.24/keepalived/etc/keepalived/keepalived.conf
/usr/local/keepalived/etc/keepalived/keepalived.conf
[root@haoding205-jq1 keepalived-1.2.24]# cat /usr/local/keepalived/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.16
        192.168.200.17
        192.168.200.18
    }
}

virtual_server 192.168.200.100 443 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 192.168.201.100 443 {
        weight 1
        SSL_GET {
            url {
              path /
              digest ff20ad2481f97b1754ef3e12ecd3a9cc
            }
            url {
              path /mrtg/
              digest 9b3a0c85a887a256d6939da88aabd8cd
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

virtual_server 10.10.10.2 1358 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    sorry_server 192.168.200.200 1358

    real_server 192.168.200.2 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.200.3 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334c
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

virtual_server 10.10.10.3 1358 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 192.168.200.4 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.200.5 1358 {
        weight 1
        HTTP_GET {
            url {
              path /testurl/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl2/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            url {
              path /testurl3/test.jsp
              digest 640205b7b0fc66c1ea91c463fac6334d
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

[root@haoding205-jq1 keepalived-1.2.24]#

下面开始修改:
需要修改个keepalived.conf成自己想要的效果
[root@haoding205-jq2 keepalived]# vim /usr/local/keepalived/etc/keepalived/keepalived.conf

[root@haoding205-jq2 keepalived]# cat keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eno16456436984
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.256.233
    }
}

cp -R /usr/local/keepalived/etc/keepalived /etc/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

将keepalived命令软连接到/usr/sbin下
ln -svf /usr/local/keepalived/sbin/keepalived /usr/sbin/

修改/usr/local/keepalived/etc/rc.d/init.d/keepalived中的程序路径
. /etc/sysconfig/keepalived
改为:
. /usr/local/keepalived/etc/sysconfig/keepalived

添加启动脚本且方便用service keepalived start/stop/restart管理

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

添加执行权限
chmod +x /etc/init.d/keepalived

开机启动
chkconfig keepalived on

[root@haoding205-jq2 keepalived]# find / -name keepalived.conf
/etc/keepalived/keepalived.conf
/usr/local/src/keepalived-1.2.24/keepalived/etc/keepalived/keepalived.conf
/usr/local/keepalived/etc/keepalived/keepalived.conf

启动服务
service keepalived start

OK了,只要haoding-jq1与haoding-jq2相互能ping通,且没有防火墙阻挡,相互是可以切换的。

上述keepalived安装包获取路径如下:
网盘下载地址:
https://pan.baidu.com/s/1COzCRL4wS6xJeTRCE_E9VA
这里写图片描述

我是昊鼎王五,我为自己带盐,有兴趣的朋友们可以扫码关注公众号,更多惊喜等待着你哦
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值