环境
效果
LVS负载均衡高可用防止单点故障
keepalived负责管理lvs,检查lvs节点,实验高可用
步骤
安装
LVS+keepalived master和backup端
安装ipvsadm
分支一如果内核不支持ipvsadm wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz # <===适合5.x系统 wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz # <===适合6.x系统 tar xf ipvsadm-1.26.tar.gz -C /usr/src/ yum -y install kernel-devel gcc gcc-c++ #安装管理ipvsadm内核的包 ls -ld /usr/src/kernels/2.6.32-431.el6.x86_64/ #出现此目录表示成功 drwxr-xr-x 22 root root 4096 8月 16 17:34 /usr/src/kernels/2.6.32-431.el6.x86_64/ ln -s /usr/src/kernels/2.6.32-431.el6.x86_64/ /usr/src/linux #做一个软连接 yum -y install libnl* popt* #需要通过公网源安装 cd /usr/src/ipvsadm-1.26/ make;make install #直接编译不需要./configure lsmod | grep ip_vs #执行完/sbin/ipvsadm就会有信息 ip_vs 125220 0 libcrc32c 1246 1 ip_vs ipv6 317340 270 ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6 #==>出现这个内容就表示LVS已经安装好,并加载到了内核 分支二 modprobe ip_vs #加载ip_vs模块 cat /proc/net/ip_vs IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn yum -y install ipvsadm #安装ipvsadm工具
安装keepalived
yum -y install keepalived #光盘安装即可
两台Web服务器安装Web服务
yum -y install httpd
/etc/init.d/httpd start
配置
LVS负载均衡器主的keepalived配置文件
! 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_1 #keepalived的唯一标识 } vrrp_instance VI_1 { state MASTER #主master interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.10 #虚拟vip } } virtual_server 192.168.2.10 80 { #虚拟vip服务器设置 delay_loop 6 lb_algo rr #rr调度算法 lb_kind DR #DR工作模式 nat_mask 255.255.255.0 #persistence_timeout 1 #不注释掉会出现负载不均衡的状况 protocol TCP real_server 192.168.2.134 80 { #管理节点1设置 weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.2.135 80 { #管理节点2设置 weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
配置完后可以直接scp给备服务端修改3条配置就可以用了
yum -y install openssh-clients #主备都要装 scp /etc/keepalived/keepalived.conf root@192.168.2.137:/etc/keepalived/
LVS负载均衡器备的keepalived配置文件
vim /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_2 #与主不同 } vrrp_instance VI_1 { state BACKUP #与主不同 interface eth0 virtual_router_id 51 priority 90 #与主不同 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.10 } } virtual_server 192.168.2.10 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 #persistence_timeout 1 protocol TCP real_server 192.168.2.134 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.2.135 80 { weight 1 TCP_CHECK { connect_timeout 8 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
WEB端配置(都配)
ifconfig lo:0 192.168.2.10 broadcast 192.168.2.10 netmask 255.255.255.255 route add -host 192.168.2.10 dev lo:0
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
启动httpd 和keepalivd 就完成高可用负载均衡了
详细的keepalived和lvs配置使用可参考前边博文