公司发展的需求,先在本地做测试,把测试步骤贴出来。

------------------------------------------------

环境说明:

VirtualBOX 

ubuntu-real1  192.168.1.201

ubuntu-real2  192.168.1.202

Centos        192.168.1.202

VIP           192.168.1.200

-----------------------------------------------------

# 安装Ubuntu 10 Server  Centos 

# 克隆Ubuntu     ubuntu-real1   ---ubuntu-real2

# 克隆完后 会出现网络无法启动

sudo mv /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.old

Reboot

#设置静态IP

vim /etc/network/interfaces

auto eth0
iface eth0 inet static    
address 192.168.1.201
netmask 255.255.255.0
gateway 192.168.1.1

/etc/init.d/networking restart

#结束个用户 

pkill -9 -t pts/0

# 更换源  (不一定需要操作 )

apt-key adv --keyserver keyserver.Ubuntu.com --recv-keys C300EE8C

echo "deb lucid main" >> /etc/apt/sources.list
apt-get update

# 源安装 

apt-get install  ipvsadm  keepalived 

cd /etc/keepalived

vim  keepalived.conf

---------------Master配置----------------

global_defs {

   notification_email {
    
   }
   notification_email_from 
    
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER    # keepalived2 机器为 BAUCK
    interface eth0   
    virtual_router_id 50   #VRID标示
    priority 102        # 优先级
    advert_int 1        #检测间隔
    authentication {
        auth_type PASS
        auth_pass 1987
    }
    virtual_ipaddress {
       192.168.1.200
    }
}
virtual_server 192.168.1.200 80 {
    delay_loop 10   #每10秒检测次RealServer
    lb_algo rr      #轮训 rr|wrr|lc|lblc|sh|dh
    lb_kind DR      ##lvs使用Direct Route NAT|DR|TUN
    persistence_timeout 50   # 默认50秒钟都访问同一台机器(会话保持)
    protocol TCP
   
  real_server 192.168.1.201 80 {
        weight 1
        TCP_CHECK {
        connect_port 80
        connect_timeout 30
        }
    }
    real_server 192.168.1.202 80 {
        weight 1
        TCP_CHECK {
        connect_port 80
        connect_timeout 30
        }
    }

    real_server 192.168.1.203 80 {

        weight 1
        TCP_CHECK {
        connect_port 80
        connect_timeout 30
        }
    }
}
-----------------------------------------------------

/etc/init.d/keepalived start 

# 查看状态

ip a

---------------BACKUP配置----------------

global_defs {

   notification_email {
    
   }
   notification_email_from 
    
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP     # BACKUP
    interface eth0   
    virtual_router_id 50   #VRID标示
    priority 101        # 优先级
    advert_int 1        #检测间隔
    authentication {
        auth_type PASS
        auth_pass 1987
    }
    virtual_ipaddress {
       192.168.1.200
    }
}
virtual_server 192.168.1.200 80 {
    delay_loop 10   #每10秒检测次RealServer
    lb_algo rr      #轮训 rr|wrr|lc|lblc|sh|dh
    lb_kind DR      ##lvs使用Direct Route NAT|DR|TUN
    persistence_timeout 50   # 默认50秒钟都访问同一台机器(会话保持)
    protocol TCP
   
  real_server 192.168.1.201 80 {
        weight 1
        TCP_CHECK {
        connect_port 80
        connect_timeout 30
        }
    }
    real_server 192.168.1.202 80 {
        weight 1
        TCP_CHECK {
        connect_port 80
        connect_timeout 30
        }
    }

    real_server 192.168.1.203 80 {

        weight 1
        TCP_CHECK {
        connect_port 80
        connect_timeout 30
        }
    }
}
-----------------------------------------------------

 ifconfig eth0 down  /ifconfig eth0 up  

查看ip a   

负载器 

----------------------------------开启路由转发

echo "1" > /proc/sys/net/ipv4/ip_forward

改/etc/sysctl.conf文件,让包转发功能在系统启动时自动生效:

# Controls IP packet forwarding
net.ipv4.ip_forward = 1
sysctl -p

WEB服务器

ifconfig lo:0 192.168.1.200  broadcast 192.168.1.200 netmask 255.255.255.255

ifconfig lo:0 up
/sbin/route add -host 192.168.1.200 dev lo:0
echo "1">/proc/sys/net/ipv4/conf/default/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/default/arp_announce
echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p