Lvs Keepalive DR模式高可用配置

Lvs Keepalive DR模式配置

一.环境

#DIP# eth0:192.168.233.145
#VIP# eth0:0 192.168.233.250/32

#RIP1:192.168.233.146
#RIP2:192.168.233.147
#VIP# 192.168.233.250/32


#DIP-Backup:eth0 192.168.233.150

二.安装

yum install -y ipvsadm gcc make openssl openssl-devel
curl -O http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
tar zxvf keepalived-1.2.12.tar.gz
cd keepalived-1.2.12
./configure --prefix=/usr/local/keepalived
make && make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp -rf /usr/local/keepalived/etc/keepalived /etc/

三.配置

这种方式,当出现故障,恢复后,自动会切回来,如果希望希望下次出故障转回需要配置nopreempt,并将两个都state都配置为BACKUP

Master:

[root@DIP ~]# more /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
         caoguo@admin.com
   }
   notification_email_from sns-lvs@gmail.com
   smtp_server 127.0.0.1
  # smtp_connect_timeout 30
   router_id LVS_DEVEL
}

# 20081013 written by :netseek
# VIP1
vrrp_instance VI_1 {
    state MASTER             #备份服务器上将MASTER改为BACKUP   
    interface eth0
    virtual_router_id 51
    priority 100    # 备份服务上将100改为99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 7777
    }
    virtual_ipaddress {
        192.168.233.250 
        #(如果有多个VIP,继续换行填写.)
    }
}

virtual_server 192.168.233.250 80 {
    delay_loop 6                  #(每隔10秒查询realserver状态)
    lb_algo wrr                  #(lvs 算法)
    lb_kind DR                  #(Direct Route)
    persistence_timeout 0        #(同一IP的连接60秒内被分配到同一台realserver)
    protocol TCP                #(用TCP协议检查realserver状态)

    real_server 192.168.233.146 80 {
        weight 3               #(权重)
        TCP_CHECK {
        connect_timeout 10       #(10秒无响应超时)
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.233.147 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

Backup:

[root@DIP-slave ~]# more /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
         caoguo@admin.com
   }
   notification_email_from sns-lvs@gmail.com
   smtp_server 127.0.0.1
  # smtp_connect_timeout 30
   router_id LVS_DEVEL
}

# 20081013 written by :netseek
# VIP1
vrrp_instance VI_1 {
    state BACKUP             
    interface eth0
    virtual_router_id 51
    priority 99    
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 7777
    }
    virtual_ipaddress {
        192.168.233.250 
        #(如果有多个VIP,继续换行填写.)
    }
}

virtual_server 192.168.233.250 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    persistence_timeout 0        
    protocol TCP

    real_server 192.168.233.146 80 {
        weight 3               #(权重)
        TCP_CHECK {
        connect_timeout 10       #(10秒无响应超时)
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.233.147 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

四.启动

/etc/init.d/keepalived start
chkconfig keepalived on

五.检查

[root@DIP ~]# ip address show
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:29:92:35 brd ff:ff:ff:ff:ff:ff
    inet 192.168.233.145/24 brd 192.168.233.255 scope global eth0
    inet 192.168.233.250/32 scope global eth0
    inet6 fe80::20c:29ff:fe29:9235/64 scope link 
       valid_lft forever preferred_lft forever
[root@DIP ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.233.250:80 wrr persistent 60
  -> 192.168.233.146:80           Route   3      0          0         
  -> 192.168.233.147:80           Route   3      0          0  
原文地址:https://www.cnblogs.com/caoguo/p/4976057.html