安装过程
查看是否有ipvsadm软件
1. 检查ipvsadm是否安装成功,可以直接输入,使用rpm包安装ipvsadm会比源码安装简单很多,
下载ipvsadm-1.24-13.el5.x86_64.rpm,执行rpm -ivh ipvsadm-1.24-13.el5.x86_64.rpm [root@localhost ~]# ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn 2. 检查模版是否加入内核 [root@localhost ~]# lsmod |grep ip_vs ip_vs 78081 0
如果没有显示,则说明没有加载,执行命令modprobe ip_vs就可以把ip_vs模块加载到内核
安装keepalived
1. keepalived-1.2.1.tar.gz # 解压这个文件并且安装 (tar -zxvf keepalived-1.2.1.tar.gz) 2. ./configure --prefix=/hnisi/local/keepalived/ --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686 3.make 4.make install 5.配置文件的路径 cp /hnisi/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ cp /hnisi/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /hnisi/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /hnisi/local/keepalived/sbin/keepalived /sbin/
配置主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_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 88 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 85574999 } virtual_ipaddress { 192.168.22.250 } } virtual_server 192.168.22.250 9082 { delay_loop 3 lb_algo rr lb_kind DR protocol TCP real_server 192.168.22.237 9082 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 9082 } } real_server 192.168.22.229 9082 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 9082 } } }
配置备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_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 30 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 85574999 } virtual_ipaddress { 192.168.22.250 } } virtual_server 192.168.22.250 9082 { delay_loop 3 lb_algo rr lb_kind DR protocol TCP real_server 192.168.22.237 9082 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 9082 } } real_server 192.168.22.229 9082 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 9082 } } }
分别在主备上启动keepalived
[root@ip_lb home]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[root@ip_lb home]# /etc/init.d/keepalived stop
Stoping keepalived: [ OK ]
[root@ip_lb home]# tail -f /var/log/messages
# PS:这里说的LVS,并不是用ipvsadm软件配置,而是直接用keepalived的virtual_server配置项控制的。
# 安装ipvsadm只是可以看到负载状况,其实只需要keepalived也可以实现负载均衡集群。
在web1服务器配置LVS-DR
vim /etc/init.d/lvsDR_node(web1主机)
#! /bin/sh # descript : start real server DR VIP=192.168.96.100 . /etc/rc.d/init.d/functions case "$1" in start) echo "start LVS of RealServer DR" /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce ;; stop) /sbin/ifconfig lo:0 down echo "close LVS of RealServer DR" echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce ;; *) echo "Usage : $0 {start|stop}" exit 1 esac
把这个文件复制到web2主机上
在web1和web2上分别启动这个脚本:
/etc/init.d/lvsDR_node start
测试是否可以负载均衡
#! /bin/sh for((i=1;i<=100;i++));do curl http://192.168.96.100:8099 >> /tmp/q; done
官方文档:http://www.keepalived.org/pdf/sery-lvs-cluster.pdf
相关阅读:http://www.cnblogs.com/liwei0526vip/p/6370103.html