高可用负载均衡集群Keepalived+lvs-DR模式的搭建

实验环境:redhat6.5
server1:172.25.20.1 server2:172.25.20.2
server3:172.25.20.3 server4:172.25.20.4

又一个避免单点故障的方法:将DR添加到HA的keepalived中。keepalived对后端有健康检查,则在安装好keepalived之后可直接添加DR。

1.源码安装keepalived

[root@server1 ~]# tar zxf keepalived-1.3.5.tar.gz
cd keepalived-1.2.24
[root@server1 keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived --with-init=SYSV

首次编码出错, yum installopenssl-devel.x86_64 gcc -y

[root@server1 keepalived-1.3.5]# make && make install
cd /usr/local
scp -r keepalived 172.25.20.4:/usr/local
[root@server1 ~]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/keepalived #二进制文件的软链接
[root@server1 ~]# ln -s /usr/local/keepalived/etc/keepalived/ /etc/  #主配置文件的软链接
[root@server1 ~]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/#配置文件的软链接
[root@server1 ~]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/#启动脚本的软链接
[root@server1 ~]# chmod +x /usr/local/keepalived/etc/rc.d/init.d/keepalived

2.将DR添加到keepalived及测试

[root@server1 ~]# vim /etc/keepalived/keepalived.conf
  1 ! Configuration Filefor keepalived
  2
  3 global_defs {
  4    notification_email {
  5         root@localhost      #邮件接受端
  6    }
  7    notification_email_from keepalived@server1#邮件发送端
  8    smtp_server 127.0.0.1      #本地回环
  9    smtp_connect_timeout 30    #连接超时
 10    router_id LVS_DEVEL
 11    vrrp_skip_check_adv_addr
 12    vrrp_strict
 13    vrrp_garp_interval 0
 14    vrrp_gna_interval 0
 15 }
 16
 17 vrrp_instance VI_1 {
 18     state MASTER        #server1是master
 19     interface eth0      #进入接口eth0
 20     virtual_router_id 20    #虚拟路由id(1-254之间)
 21     priority 100            #在启动keepalived服务时,系统会比较priority的值,哪个值大哪个就是master
 22     advert_int 1
 23     authentication {
 24         auth_type PASS
 25         auth_pass 1111
 26     }
 27     virtual_ipaddress {
 28         172.25.20.100   #虚拟ip
 29     }
 30
 31
 32 virtual_server172.25.20.100 80 {  #虚拟服务
 33     delay_loop 6
 34     lb_algo rr
 35     lb_kind DR          #DR
 36 #    persistence_timeout 50    #超时
 37     protocol TCP
 38
 39     real_server 172.25.20.2 80 {      #真正的服务端
 40         weight 1        #权重
 41         TCP_CHECK {
 42             connect_timeout 3
 43             nb_get_retry 3
 44             delay_before_retry 3
 45         }
 46     }
 47      real_server 172.25.20.3 80 {     #真正的服务端
 48         weight 1
 49         TCP_CHECK {
 50             connect_timeout 3
 51             nb_get_retry 3
 52             delay_before_retry 3
 53         }
 54     }
 55
 56 }

[root@server1 ~]# /etc/init.d/keepalived start
[root@server1 ~]# scp /etc/keepalived/keepalived.conf172.25.20.4:/etc/keepalived/ #将其传到server4
[root@server4 ~]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/keepalived 
[root@server4 ~]# ln -s /usr/local/keepalived/etc/keepalived/ /etc/  
[root@server4 ~]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@server4 ~]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@server4 ~]# chmod +x /usr/local/keepalived/etc/rc.d/init.d/keepalived
[root@server4 ~]# vim /etc/keepalived/keepalived.conf
 7    notification_email_from keepalived@server4#邮件发送段,本机
18     state BACKUP     #备服务器
21     priority 50      #优先级
[root@server2 local]# /etc/init.d/keepalived star

测试:
当主机keepalived停掉,系统会将服务转移到备机上;当主机重新打开时,系统会重新读配置文件,来确定主机和备机,将服务开启到主机上
[root@server1 ~]# /etc/init.d/keepalived stop
[root@server1 ~]# ip addr show #查看,虚拟ip不在server1
[root@server4 ~]# iptables -F
[root@server4 ~]# ip addr show
curl 172.25.20.100 ##物理机访问
[root@server2 ~]# /etc/init.d/httpd stop
[root@server3 ~]# /etc/init.d/httpd stop
server2 server3相继关闭http服务,本地回环

你可能感兴趣的:(linux运维企业部分)