【2017/8/15】Linux7 lvs+keepalived+tomcat负载均衡配置

参考:http://www.keepalived.org/pdf/sery-lvs-cluster.pdf
参考:http://blog.csdn.net/m582445672/article/details/7670015
参考:http://blog.csdn.net/caoshuming_500/article/details/8291940
参考:http://blog.csdn.net/tengyuantuohai/article/details/19639671
参考:http://www.cnblogs.com/edisonchou/p/4281978.html
参考:http://blog.csdn.net/u012852986/article/details/52412174
参考:http://blog.csdn.net/zmj_88888888/article/details/8819105
参考:http://blog.csdn.net/qq_21835703/article/details/51922603?locationNum=2&fps=1




主机环境如下:



192.168.158.100  LVS_VIP
192.168.158.140  LVS_Master    
192.168.158.141  LVS_Backup
192.168.158.142  WEB1_RealServer
192.168.158.143  WEB2_RealServer
192.168.158.1  测试client

拓扑:


【2017/8/15】Linux7 lvs+keepalived+tomcat负载均衡配置_第1张图片

LVS_MASTER服务器



安装依赖包
yum -y install kernel-devel make gcc openssl-devel libnl* popt*  






安装IPVSADM
yum -y install ipvsadm


防火墙
systemctl stop firewalld.service




KeepAlived 的安装
安装keepalived
tar -zxvf keepalived-1.2.18.tar.gz
cd keepalived-1.2.18/
./configure --prefix=/usr/local/keepalived
make && make install




设置开机启动
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/








ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
若存在
[root@111 sbin]# cd /sbin
[root@111 sbin]# rm -rf keepalived 
[root@111 sbin]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/
[root@111 sbin]# 








chkconfig keepalived on










KeepAlivde的配置
打开IP Forward 功能
vim /etc/sysctl.conf    
修改里面“net.ipv4.ip_forward = 1”
sysctl -p 






vi /etc/keepalived/keepalived.conf 


! Configuration File for keepalived  
global_defs {  
    notification_email {   
        [email protected]   
    }   
    notification_email_from [email protected]  
    smtp_server 127.0.0.1  
    smtp_connect_timeout 30  
    router_id LVS_MASTER  
}  
  
vrrp_instance VI_1 {  
    state MASTER  
    interface eno16777736  
    virtual_router_id 60  
    priority 100  
    advert_int 1  
    authentication {  
        auth_type PASS  
        auth_pass 1111  
    }  
    virtual_ipaddress {  
        192.168.158.100  
    }  
}  
  
virtual_server 192.168.158.100 8080 {  
    delay_loop 6  
    lb_algo rr   
    lb_kind DR  
    nat_mask 255.255.255.0  
    persistence_timeout 50  
    protocol TCP  
  
    real_server 192.168.158.142 8080 {  
        weight 1  
        TCP_CHECK {  
            connect_timeout 3  
            nb_get_retry 3  
            delay_before_retry 3  
        }  
    }  
  
    real_server 192.168.158.143 8080 {  
        weight 1  
        TCP_CHECK {  
            connect_timeout 3  
            nb_get_retry 3  
            delay_before_retry 3  
        }  
    }  
}  


启动keepalive
[root@111 ~]# systemctl restart keepalived.service 
[root@111 ~]# ps -ef | grep keepalived-1.2.18
root      49513   4123  0 14:30 pts/3    00:00:00 grep --color=auto keepalived-1.2.18
[root@111 ~]# ps -ef | grep keepalived
root      49501      1  0 14:30 ?        00:00:00 keepalived -D
root      49502  49501  0 14:30 ?        00:00:00 keepalived -D
root      49504  49501  0 14:30 ?        00:00:00 keepalived -D
root      49516   4123  0 14:30 pts/3    00:00:00 grep --color=auto keepalived
[root@111 ~]# 




查看下虚拟IP是否已经加上(重要)
ip a




还有3个命令在先列示下,并不用执行


显示集群中服务器ip信息:ipvsadm -ln


查看日志:tail -f /var/log/messages


查看请求转发情况:ipvsadm -lcn | grep 虚拟IP


至此,LVS_MASTER服务器已经配置好并启动了,接下来我们配置web服务器。






WEB1_RealServer的配置

vim /etc/init.d/realserver.sh  


SNS_VIP=192.168.158.100  
/etc/rc.d/init.d/functions  
  
case "$1" in  
start)  
       /sbin/ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP  
       /sbin/route add -host $SNS_VIP dev eth0: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  
       sysctl -p >/dev/null 2>&1  
       echo "RealServer Start OK"  
  
       ;;  
stop)  
       /sbin/ifconfig lo:0 down  
       /sbin/route del $SNS_VIP >/dev/null 2>&1  
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore  
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce  
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore  
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce  
       echo "RealServer Stoped"  
       ;;  
*)  
       echo "Usage: $0 {start|stop}"  
       exit 1  
esac  
  
exit 0   


chmod +x /etc/init.d/realserver.sh 
安装配置TOMCAT
yum -y install tomcat6 tomcat6-webapps tomcat6-admin-webapps  
chkconfig tomcat6 on  
vi /usr/share/tomcat6/webapps/ROOT/index.html
rs1 192.168.158.142

service tomcat6 start  


有了一个虚拟IP
sh /etc/init.d/realserver.sh start  
ifconfig




LVS_MASTER服务器的终端查看下ipvsadm,查看已经连接上了WEB1服务器


ipvsadm -ln  


ipvsadm -lcn | grep 192.168.158.100  













LVS_BACKUP服务器



! Configuration File for keepalived  
global_defs {  
    notification_email {   
        [email protected]   
    }   
    notification_email_from [email protected]  
    smtp_server 127.0.0.1  
    smtp_connect_timeout 30  
    router_id LVS_BACKUP  
}  
  
vrrp_instance VI_1 {  
    state BACKUP  
    interface eno16777728  
    virtual_router_id 60  
    priority 90  
    advert_int 1  
    authentication {  
        auth_type PASS  
        auth_pass 1111  
    }  
    virtual_ipaddress {  
        192.168.158.100  
    }  
}  
  
virtual_server 192.168.158.100 8080 {  
    delay_loop 6  
    lb_algo rr   
    lb_kind DR  
    nat_mask 255.255.255.0  
    persistence_timeout 50  
    protocol TCP  
  
    real_server 192.168.158.142 8080 {  
        weight 1  
        TCP_CHECK {  
            connect_timeout 3  
            nb_get_retry 3  
            delay_before_retry 3  
        }  
    }  
  
    real_server 192.168.158.143 8080 {  
        weight 1  
        TCP_CHECK {  
            connect_timeout 3  
            nb_get_retry 3  
            delay_before_retry 3  
        }  
    }  
}  


WEB2_RealServer的配置

vi /usr/share/tomcat6/webapps/ROOT/index.html
rs2 192.168.158.143



测试

首先通过测试client访问192.168.158.100:8080
获得的页面内容:rs1 192.168.158.142 
此时LVS_Master&LVS_BACKUP配置如下:
[root@LVS_Master140 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.158.100:8080 rr persistent 50
  -> 192.168.158.142:8080         Route   1      2          3         
  -> 192.168.158.143:8080         Route   1      0          0         
[root@LVS_Master140 ~]# ip a
1: lo: mtu 65536 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
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:61:50:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.158.140/24 brd 192.168.158.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet 192.168.158.100/32 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe61:5007/64 scope link 
       valid_lft forever preferred_lft forever
[root@LVS_Master140 ~]# 




[root@LVS_Backup ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.158.100:8080 rr persistent 50
  -> 192.168.158.142:8080         Route   1      0          0         
  -> 192.168.158.143:8080         Route   1      0          0         
[root@LVS_Backup ~]# ip a
1: lo: mtu 65536 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
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777728: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:c9:e8:1b brd ff:ff:ff:ff:ff:ff
    inet 192.168.158.141/24 brd 192.168.158.255 scope global eno16777728
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fec9:e81b/64 scope link 
       valid_lft forever preferred_lft forever
[root@LVS_Backup ~]# 


模拟LVS_Master故障:
[root@LVS_Master140 ~]# systemctl stop keepalived
此时LVS_Master&LVS_BACKUP配置如下:
[root@LVS_Master140 ~]# ip a
1: lo: mtu 65536 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
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:61:50:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.158.140/24 brd 192.168.158.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe61:5007/64 scope link 
       valid_lft forever preferred_lft forever
[root@LVS_Master140 ~]# 




[root@LVS_Backup ~]# 
[root@LVS_Backup ~]# ip a
1: lo: mtu 65536 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
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777728: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:c9:e8:1b brd ff:ff:ff:ff:ff:ff
    inet 192.168.158.141/24 brd 192.168.158.255 scope global eno16777728
       valid_lft forever preferred_lft forever
    inet 192.168.158.100/32 scope global eno16777728
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fec9:e81b/64 scope link 
       valid_lft forever preferred_lft forever
[root@LVS_Backup ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.158.100:8080 rr persistent 50
  -> 192.168.158.142:8080         Route   1      0          0         
  -> 192.168.158.143:8080         Route   1      0          2         
[root@LVS_Backup ~]# 


通过测试client访问192.168.158.100:8080
获得的页面内容:rs2 192.168.158.143 




模拟192.168.158.143这台服务器故障:
[root@WEB2_RealServer ~]# systemctl stop tomcat.service
通过测试client访问192.168.158.100:8080
获得的页面内容:rs1 192.168.158.142 

















你可能感兴趣的:(linux)