lvs+keepalived配置

一、环境

lvs-keep-m: 192.168.3.24

lvs-keep-s: 192.168.3.25

RS1: 192.168.3.26

RS2: 192.168.3.27

VIP: 192.168.3.28

二、在所有节点都配置ntp同步时间

[[email protected] ~]# ntpdate asia.pool.ntp.org
[[email protected] ~]# hwclock -w

三、RS1和RS2配置httpd服务,以及VIP

RS1操作

[[email protected] ~]# yum -y install httpd
[[email protected] ~]# echo "<h1>RS1.com</h1>" >/var/www/html/index.html
[[email protected] ~]# service iptables stop
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[[email protected] ~]# service httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for RS1
httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]
[[email protected] ~]# curl http://127.0.0.1
<h1>RS1.com</h1>
#以上是配置httpd服务

在RS1上配置VIP,使用脚本配置,脚本内容如下

[[email protected] ~]# cat realserver.sh 
#!/bin/bash  
#   
# Script to start LVS DR real server.   
# description: LVS DR real server   
#   
.  /etc/rc.d/init.d/functions

#在上文中我们设置的VIP是192.168.3.28
VIP=192.168.3.28 
host=`/bin/hostname`
case "$1" in  
start)   
       # Start LVS-DR real server on this machine.   
        /sbin/ifconfig lo down   
        /sbin/ifconfig lo up   
        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
        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up  
        /sbin/route add -host $VIP dev lo:0
;;  
stop)
        # Stop LVS-DR real server loopback device(s).  
        /sbin/ifconfig lo:0 down   
        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
;;  
status)
        # Status of LVS-DR real server.  
        islothere=`/sbin/ifconfig lo:0 | grep $VIP`   
        isrothere=`netstat -rn | grep "lo:0" | grep $VIP`   
        if [ ! "$islothere" -o ! "isrothere" ];then   
            # Either the route or the lo:0 device   
            # not found.   
            echo "LVS-DR real server Stopped."   
        else   
            echo "LVS-DR real server Running."   
        fi   
;;   
*)   
            # Invalid entry.   
            echo "$0: Usage: $0 {start|status|stop}"   
            exit 1   
;;   
esac   

#赋予执行权限
[[email protected] ~]# chmod +x realserver.sh 
[[email protected] ~]# sh realserver.sh start
[[email protected] ~]# ip a |grep "192.168.3.28"
    inet 192.168.3.28/32 brd 192.168.3.28 scope global lo:0

RS2操作

[[email protected] ~]# yum -y install httpd
[[email protected] ~]# echo "<h1>RS2.com</h1>" >/var/www/html/index.html
[[email protected] ~]# service iptables stop
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[[email protected] ~]# service httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for RS2
httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]
[[email protected] ~]# curl http://127.0.0.1
<h1>RS2.com</h1>

在RS1上配置VIP,使用脚本配置,脚本内容同上

[[email protected] ~]# sh realserver.sh start
[[email protected] ~]# ip a |grep "192.168.3.28"
    inet 192.168.3.28/32 brd 192.168.3.28 scope global lo:0

四、lvs-keep-m和lvs-keep-s上安装lvs

lvs-keep-m操作

[[email protected] ~]# yum install make popt  libnl  libnl-devel  popt-static gcc* -y
[[email protected] ~]# wget  http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
[[email protected] ~]# tar xf ipvsadm-1.26.tar.gz 
[[email protected] ~]# cd ipvsadm-1.26
[[email protected] ipvsadm-1.26]# make
[[email protected] ipvsadm-1.26]# make install

lvs-keep-s操作

[[email protected] ~]# yum install make popt  libnl  libnl-devel  popt-static gcc* -y
[[email protected] ~]# wget  http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
[[email protected] ~]# tar xf ipvsadm-1.26.tar.gz 
[[email protected] ~]# cd ipvsadm-1.26
[[email protected] ipvsadm-1.26]# make
[[email protected] ipvsadm-1.26]# make install

五、lvs-keep-m和lvs-keep-s上安装keepalived

在lvs-keep-m上安装keepalived

[[email protected] ~]# yum install openssl openssl-devel -y
[[email protected] ~]# wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
[[email protected] ~]# tar xf keepalived-1.2.13.tar.gz 
[[email protected] ~]# cd keepalived-1.2.13
[[email protected] keepalived-1.2.13]# ./configure 
[[email protected] keepalived-1.2.13]# make && make install
#将keepalived配置成开机启动
[[email protected] keepalived-1.2.13]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[[email protected] keepalived-1.2.13]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[[email protected] keepalived-1.2.13]# mkdir  /etc/keepalived
[[email protected] keepalived-1.2.13]# ln -s /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[[email protected] keepalived-1.2.13]# ln -s /usr/local/sbin/keepalived  /usr/sbin/

#备份keepalived.conf文件
[[email protected] keepalived-1.2.13]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[[email protected] ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {  
   notification_email {   
	[email protected]               #配置管理员邮箱
   }   
   notification_email_from root      #配置发件人   
   smtp_server 127.0.0.1             #配置邮件服务器   
   smtp_connect_timeout 30   
   router_id LVS_M                   #标识当前节点
}
vrrp_instance VI_1 {  
    state MASTER                     #配置keepalived的模式   
    interface eth0                   #将VIP绑定在哪个网卡上,这里设置成eth0
    virtual_router_id 99             #VRRP组名,两个节点设置必须一样,表明属于同一个VRRP组
    priority 101                     #配置优先级   
    advert_int 1                     #组播信息发送间隔
    authentication {   
        auth_type PASS               #设置验证方式
        auth_pass 1111               #验证密码
    }   
    virtual_ipaddress {   
        192.168.3.28                 #配置虚拟IP地址,这里可以配置多个地址,每个VIP下对应不同的realserver   
    }   
}
virtual_server 192.168.3.28 80 {  
    delay_loop 6   
    lb_algo rr                       #配置lvs算法,这里是rr,即轮询
    lb_kind DR                       #配置lvs模型,这里是DR,即直接路由模式
    nat_mask 255.255.255.0   
    #persistence_timeout 50          #会话保持时间
    protocol TCP
    real_server 192.168.3.26 80 {    #配置realaserver地址  
        weight 1   
        HTTP_GET {                   #监控配置   
            url {   
              path /                 #监控网站的/路径
          status_code 200   
            }   
            connect_timeout 2        #连接超时时间
            nb_get_retry 3           #重连次数
            delay_before_retry 1     #重连时间间隔
        }   
    }   
    real_server 192.168.3.27 80 {   
        weight 1   
        HTTP_GET {   
            url {   
              path /   
              status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
}

#启动keepalived服务
[[email protected] ~]# chkconfig --add keepalived
[[email protected] ~]# chkconfig keepalived on
[[email protected] ~]# chkconfig |grep keep
keepalived     	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[[email protected] ~]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]
[[email protected] ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.3.28:80 rr
  -> 192.168.3.26:80              Route   1      0          0         
  -> 192.168.3.27:80              Route   1      0          0    
  
#到此RS1的keepalived已配置好
[[email protected] ~]# service iptables stop

在lvs-keep-m上安装keepalived

[[email protected] ~]# yum install openssl openssl-devel -y
[[email protected] ~]# wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
[[email protected] ~]# tar xf keepalived-1.2.13.tar.gz 
[[email protected] ~]# cd keepalived-1.2.13
[[email protected] keepalived-1.2.13]# ./configure
[[email protected] keepalived-1.2.13]# make && make install
[[email protected] keepalived-1.2.13]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[[email protected] keepalived-1.2.13]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[[email protected] keepalived-1.2.13]# mkdir  /etc/keepalived
[[email protected] keepalived-1.2.13]# ln -s /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[[email protected] keepalived-1.2.13]# ln -s /usr/local/sbin/keepalived  /usr/sbin/
[[email protected] keepalived-1.2.13]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

#keepalived的配置文件我们从lvs-keep-m上复制过来修改即可
[[email protected] keepalived-1.2.13]# cp ~/keepalived.conf /etc/keepalived/
[[email protected] keepalived-1.2.13]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {  
   notification_email {   
	[email protected]                 #配置管理员邮箱
   }   
   notification_email_from root        #配置发件人   
   smtp_server 127.0.0.1               #配置邮件服务器   
   smtp_connect_timeout 30   
   router_id LVS_S                     #修改这里,表示节点标示符
}
vrrp_instance VI_1 {  
    state BACKUP                       #修改lvs的模式为BACKUP  
    interface eth0   
    virtual_router_id 99   
    priority 99                        #修改优先级,要比master的低   
    advert_int 1   
    authentication {   
        auth_type PASS   
        auth_pass 1111   
    }   
    virtual_ipaddress {   
        192.168.3.28  #配置虚拟IP地址   
    }   
}
virtual_server 192.168.3.28 80 {  
    delay_loop 6   
    lb_algo rr   
    lb_kind DR   
    nat_mask 255.255.255.0   
    #persistence_timeout 50   
    protocol TCP
    real_server 192.168.3.26 80 { #配置realaserver  
        weight 1   
        HTTP_GET { #监控配置   
            url {   
              path /   
          status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
    real_server 192.168.3.27 80 {   
        weight 1   
        HTTP_GET {   
            url {   
              path /   
              status_code 200   
            }   
            connect_timeout 2   
            nb_get_retry 3   
            delay_before_retry 1   
        }   
    }   
}

#添加到开机自动启动
[[email protected] ~]# chkconfig --add keepalived
[[email protected] ~]# chkconfig keepalived on
[[email protected] ~]# chkconfig |grep keep
keepalived     	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[[email protected] ~]# service keepalived start
Starting keepalived:                                       [  OK  ]
[[email protected] ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.3.28:80 rr
  -> 192.168.3.26:80              Route   1      0          0         
  -> 192.168.3.27:80              Route   1      0          0   
[[email protected] ~]# service iptables stop
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]

#正常情况下,这里是查询不到VIP地址的
[[email protected] ~]# ip a |grep "192.168.3.28"

六、测试结果

#结果显示访问http://192.168.3.28,后端服务正常
[[email protected] ~]# curl http://192.168.3.28
<h1>RS1.com</h1>
[[email protected] ~]# curl http://192.168.3.28
<h1>RS2.com</h1>
[[email protected] ~]# curl http://192.168.3.28
<h1>RS1.com</h1>
[[email protected] ~]# curl http://192.168.3.28
<h1>RS2.com</h1>
[[email protected] ~]# curl http://192.168.3.28
<h1>RS1.com</h1>
[[email protected] ~]# curl http://192.168.3.28
<h1>RS2.com</h1>
时间: 2024-10-14 15:15:14

lvs+keepalived配置的相关文章

CentOS Linux 负载均衡高可用WEB集群之LVS+Keepalived配置

CentOS Linux 负载均衡高可用WEB集群之LVS+Keepalived配置 LB集群是locd balance集群的简称.翻译成中文是:负载均衡集群的意思:集群是一组相互独立的.通过高速网络互联的计算机相互之间构成一个组合,并以单一的系统的模式加以管理.LVS是Linux Virtual Server的简写,翻译中文是Linux虚拟服务器,是一个虚拟的服务器集群系统. 负载均衡集群:是为了企业提供更为实用,性价比更高的系统机构解决方案.负载均衡集群把用户的请求尽可能的平均分发到集群的各

实例:LVS+Keepalived配置LVS的高可用

LVS+Keepalived配置LVS的高可用 我们这里LVS-DR模型的高可用集群: 实验环境:     vm1 LVS-DR1:              eth0 172.16.3.2/16              VIP :eth0:0 172.16.3.88              vm2 LVS-DR2:             eth0 172.16.3.3/16     vm3 Server-web1             RS1: eth0 172.16.3.1/16  

LVS + Keepalived 配置详解

LVS+Keepalived 配置 我们通过LVS-DR-MASTER,LVS-DR-BACKUP作为LVS负载均衡调度器,并且两者之间通过keepalived来两者之间的HA.keepalived本身就是为了LVS为开发的,所以说我们通过keepalived来进行LVS的配置就显得十分的方便.而且keepalived是直接操作ip_vs不用通过ipvsadm,所以更加方便. 实验拓扑图 LVS节点1配置: # yum install popt-devel openssl openssl-dev

利用lvs keepalived配置redis高可用及负载均衡

需求 我们用ES做日志服务,架构是 上游数据来源=>redis=>logstash=>ES redis目前还是单点, 没有做高可用, 现在数据量越来越多, 如果下游消费不出问题还好, redis里面数据来了就走,但是下游一旦出问题, 分给redis的内存半小时就撑满了. 看到redis3.0 beta版本已经提供了集群功能, 但是需要client以集群模式接入, 我们这么多上游用户, 不太可能统一要求他们改造. 公司也有硬件的LB, 同事在E公司的时候就是用的硬件LB. 但接入还要申请,

LVS + Keepalived 配置

#keepalived 配置 ! Configuration File for keepalived global_defs { router_id LVS_DEVEL } ########VRRP Instance######## vrrp_instance VI_1 { state MASTER #指定Keepalived的角色,MASTER为主机服务器,BACKUP为备用服务器 interface eth2 #BACKUP为备用服务器 virtual_router_id 31 priori

lvs+keepalived配置过程

---恢复内容开始--- ###lb01 操作 ####1.关闭keepalived iptables selinux ####2.手工添加LVS转发 #####1)配置lvs 添加vip ip addr add 10.0.0.3/24 dev eth0 label eth0:0 #####2)配置lvs规则 ipvsadm-save   -n ipvsadm -C ipvsadm --set 30 5 60 ipvsadm -A -t 10.0.0.3:80 -s wrr -p 20 ipvs

LVS + KEEPAlived 配置 DIR模式

1 .1 Lvs LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一.目前有三种IP负载均衡技术(VS/NAT.VS/TUN和VS/DR) 本文主要介绍DR模式运用 1 .2 DR模式      DR模式是什么?撇开lvs调度算法部分,DR模式的原理就是,将报文直接路由给目标服务器(Direct Routing),其实这里没有routing啥事情,叫做Direc

lvs+keepalived配置高可用集群

说明: 上周使用keepalived实现了mysql主主同步的高可用,借着刚研究了keepalived,顺便把lvs集群搞一下 参考文档: lvs集群简介:http://732233048.blog.51cto.com/9323668/1617201 LVS.Nginx和HAProxy负载均衡区别:http://732233048.blog.51cto.com/9323668/1623375 环境: 前段LB 后端web 底端共享存储 vip (主)192.168.186.132 (R1)192

MySQL 主主复制 + LVS + Keepalived 实现 MySQL 高可用性

MySQL复制能够保证数据的冗余的同时可以做读写分离来分担系统压力,如果是主主复制还可以很好的避免主节点的单点故障.但是MySQL主主复制存在一些问题无法满足我们的实际需要:未提供统一访问入口来实现负载均衡,如果其中master宕掉的话需要手动切换到另外一个master,而不能自动进行切换. 这篇文章下面要介绍如何通过LVS+Keepalived的方式来是实现MySQL的高可用性,同时解决以上问题. Keepalived和LVS介绍 Keepalived是一个基于VRRP(虚拟路由冗余协议)可用