LVS+keepalived实战配置

LVS+Keepalived负载均衡高可用集群架构适用于千万级并发网站,在互联网企业得到大力的应用,如下为完整LVS+Keepalived企业级配置方法和步骤:

(1)Ipvsadm编译安装方法如下:

wget  -c  
http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz 
ln -s /usr/src/kernels/2.6.*  /usr/src/linux
tar xzvf ipvsadm-1.24.tar.gz 
cd ipvsadm-1.24 
make 
make install

(2)Keepalived安装配置:

cd  /usr/src
wget  -c  http://www.keepalived.org/software/keepalived-1.1.15.tar.gz 
tar  -xzvf  keepalived-1.1.15.tar.gz 
cd  keepalived-1.1.15 
./configure
make && make install
DIR=/usr/local/ 
cp  $DIR/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp  $DIR/etc/sysconfig/keepalived  /etc/sysconfig/
mkdir  -p  /etc/keepalived
cp  $DIR/sbin/keepalived /usr/sbin/

(3)Master上keepalived.conf配置代码:

! Configuration File for keepalived
global_defs {
   notification_email {
      wgkgood@163.com
   }
   notification_email_from wgkgood@163.com
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
    state  MASTER  
    interface  eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 51
    priority 100 
    advert_int 5
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.33.188   
    }
}
virtual_server 192.168.33.188 80 {
    delay_loop 6    
    lb_algo wrr    
    lb_kind DR   
persistence_timeout  60   
    protocol TCP     
    real_server 192.168.33.12 80 {
        weight 100        
        TCP_CHECK {
        connect_timeout 10 
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80	
        }
    }
    real_server 192.168.33.13 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
}

(4)Backup上keepalived.conf配置代码:

! Configuration File for keepalived
global_defs {
   notification_email {
      wgkgood@163.com
   }
   notification_email_from wgkgood@163.com
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
    state  BACKUP  
    interface  eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 51
    priority  90 
    advert_int 5
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.33.188   
    }
}
virtual_server 192.168.33.188 80 {
    delay_loop 6    
    lb_algo wrr    
    lb_kind DR   
persistence_timeout  60   
    protocol TCP     
    real_server 192.168.33.12 80 {
        weight 100        
        TCP_CHECK {
        connect_timeout 10 
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.33.13 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
}

Master Keepalived配置state状态为MASTER,Priority设置100,Backup Keepalived配置state状态为BACKUP,Priority设置90,转发方式为DR直连路由模式,算采用wrr模式,在LVS BACKUP服务器写入如下配置,需要注意的是客户端的配置要修改优先级及状态:

LVS+keepalived负载均衡主备配置完毕,由于LVS采用DR模式,根据DR模式转发原理,需在客户端realserver绑定VIP。

发表评论

后才能评论