码迷,mamicode.com
首页 > 其他好文 > 详细

lvs及lvs+keepalived负载均衡

时间:2017-03-24 20:51:59      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:lvs   lvs+keepalived   

################ lvs负载均衡 ####################
实验环境:Redhat6.5
ip:172.25.8.1,hostname:server1.example.com,lvs httpd
ip:172.25.8.3,hostname:server3.example.com,httpd
ip:172.25.8.4,hostname:server4.example.coms,httpd
关闭所有主机防火墙iptables

server1安装lvs作为调度器,后面的server3和server4是realserver,提供httpd服务

实验内容:
1.在server1上安装
yum install -y httpd
/etc/init.d/httpd start

ip addr add 172.25.8.100/24 dev eth1  ##添加虚拟ip到eth1
ip addr show

yum install -y ipvsadm
ipvsadm -A -t 172.25.8.100:80 -s rr   ##添加服务,算法为轮询
ipvsadm -a -t 172.25.8.100:80 -r 172.25.8.3:80 -g  
ipvsadm -a -t 172.25.8.100:80 -r 172.25.8.4:80 -g
ipvsadm -l

2.在server3和server4上安装
yum install httpd -y
/etc/init.d/httpd  start

cd /var/www/html/
vim index.html
server3.example.com    ##在server4中写server4.example.com

ip addr add 172.25.8.100/24 dev eth1
ip addr show
    
yum install arptables_jf -y
arptables -A IN -d 172.25.8.100 -j DROP
arptables -A OUT -s 172.25.8.100 -j mangle --mangle-ip-s 172.25.8.3 ##在server4,ip是172.25.8.4
/etc/init.d/arptables_jf save    ##开启arptables_jf
arptables -nL

3.测试
浏览器访问172.25.8.100测试.server3、server4可以做到简单负载均衡效果
【当删除server1上的ip:172.25.8.100,页面则是无法访问到的】


############## lvs+keepalived负载均衡 ###############
实验环境:Redhat6.5
ip:172.25.8.1,hostname:server1.example.com,lvs+keepalived httpd
ip:172.25.8.2,hostname:server2.example.com,lvs+keepalived httpd
ip:172.25.8.3,hostname:server3.example.com,httpd
ip:172.25.8.4,hostname:server4.example.coms,httpd
关闭所有主机防火墙iptables

实验内容:
###安装配置lvs的后端            #############理解!!!!
在server3和server4上安装
yum install httpd -y
/etc/init.d/httpd  start

cd /var/www/html/
vim index.html
 server3.example.com    ##在server4中写server4.example.com

ip addr add 172.25.8.100/24 dev eth1
ip addr show
    
yum install arptables_jf -y
arptables -A IN -d 172.25.8.100 -j DROP
arptables -A OUT -s 172.25.8.100 -j mangle --mangle-ip-s 172.25.8.3 ##在server4,ip是172.25.8.4
/etc/init.d/arptables_jf save    ##开启arptables_jf
arptables -nL

###安装keepalived
在server1和server2上装
安装包:
keepalived-1.2.24.tar.gz
libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm

1.安装httpd和libnfnetlink包
yum install -y libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm
yum install -y httpd        ##安装httpd并开启
/etc/init.d/httpd start

2.安装keepalived
tar  zxf keepalived-1.2.24.tar.gz
cd keepalived-1.2.24
./configure  --prefix=/usr/local/keepalived
yum  install -y libnl-devel
./configure  --prefix=/usr/local/keepalived
make;make install

3.scp -r /usr/local/keepalived/  172.25.8.2:/usr/local/    ##发送到server2

4.设置软链接
ln -s /usr/local/keepalived/etc/keepalived/ /etc/    ##配置文件
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ ##启动脚本
chmod +x /etc/init.d/keepalived        ##执行权限
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/  ##必要文件
ln -s /usr/local/keepalived/sbin/keepalived /sbin/    ##二进制命令文件
/etc/init.d/keepalived status    ##查看状态
返回内容:keepalived is stopped

5.编辑配置文件server1
vim /etc/keepalived/keepalived.conf
文件内容:
global_defs {
   notification_email {
        root@localhost    ##修改故障报警Email,可以多个分行写,在第5行
   }
   notification_email_from    keepalived@server1.example.com    ##指定发件人地址
   smtp_server            172.25.8.1
......    ##中间内容省略    
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 8  ##虚拟路由ID标示,为一个数字。MASTER和BACKUP配置中相同实例此数必须一致
    priority 100    ##设置优先级,MASTER优先级大于BACKUP
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.8.100/24
    }
}

virtual_server 172.25.8.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
   #persistence_timeout 50
    protocol TCP

    real_server 172.25.8.3 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }                ##注意“}”,别多别少
    real_server 172.25.8.4 80 {
    weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
  }                                                             

}
##将文件,此处以下全部删掉,否则会产生影响

6.发送keepalived.conf给server2
scp keepalived.conf  172.25.8.2:/etc/keepalived/
    
7.编辑server2的keepalived.conf
vim /etc/keepalived/keepalived.conf
文件内容:
......    ##省略
vrrp_instance VI_1 {
state BACKUP    ##修改为BACKUP
    interface eth1
    virtual_router_id 8
    priority 50  ##设置优先级,MASTER优先级大于BACKUP
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

8.测试keepalived            
/etc/init.d/keepalived  start    ##开启server1和server2中的keepalived,server1优先级高,先获得虚拟IP
ip addr show
返回内容:inet 172.25.8.100/24 scope global secondary eth1    ##自动添加了虚拟IP

ipvsadm -l    ##使用命令
返回内容:
-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.25.8.100:http rr
  -> 172.25.8.3:http              Route   1      0          0         
  -> 172.25.8.4:http            

tail -f /var/log/messages    ##可查看日志

浏览器访问http://172.25.8.100,可以看到server3和server4中的index.html中的网页内容


【keepalivd有健康检查,当关闭server1即MASTER时,server2即BACKUP会接管MASTER的资源,保证的提供的httpd服务的连续性】



本文出自 “12148275” 博客,转载请与作者联系!

lvs及lvs+keepalived负载均衡

标签:lvs   lvs+keepalived   

原文地址:http://12158275.blog.51cto.com/12148275/1910136

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!