未加星标

基于VMware的虚拟Linux集群搭建-lvs+keepalived

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

通过 keepalived 实现 lvs 服务器的的双机热备和真实服务器之间的负载均衡 的 blog 挺多的,在搭建的时候也参考了相关博文,不同人搭建的实验环境不一样,本文是基于 VM 搭建的虚拟集群环境,全部虚拟机网卡模式为 NAT 方式相连,处于同一网段。

搭建环境:

使用 redhead 2.6.32-431.el6.x86_64 版本的 linux ,创建四台,配置相关网络信息,确保同一网段下。


基于VMware的虚拟Linux集群搭建-lvs+keepalived

配置好各自 ip 即可,虚拟 ip 在配置 lvs 时设定。

安装配置 LVS

1. 在主备Lvs上安装ipvsadm和keepalived

LVS是通过IPVS模块实现的,检查kernel是否支持LVS的IPVS模块,再安装IPVS管理软件ipvsadm

[[email protected] ~]# modprobe -l | grep ipvs

kernel/net/netfilter/ipvs/ip_vs.ko

kernel/net/netfilter/ipvs/ip_vs_rr.ko

kernel/net/netfilter/ipvs/ip_vs_wrr.ko

kernel/net/netfilter/ipvs/ip_vs_lc.ko

kernel/net/netfilter/ipvs/ip_vs_wlc.ko

kernel/net/netfilter/ipvs/ip_vs_lblc.ko

kernel/net/netfilter/ipvs/ip_vs_lblcr.ko

kernel/net/netfilter/ipvs/ip_vs_dh.ko

kernel/net/netfilter/ipvs/ip_vs_sh.ko

kernel/net/netfilter/ipvs/ip_vs_sed.ko

kernel/net/netfilter/ipvs/ip_vs_nq.ko

kernel/net/netfilter/ipvs/ip_vs_ftp.ko

kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko

安装ipvsadm

[[email protected] ~]# yum install ipvsadm

能通过 ipvsadm --help 查看相关信息即安装成功。

安装 keepalived

安装依赖库:yum install -y openssl openssl-devel (我是缺这两个,具体编译时候如果报错则安装相关提示补安装相关缺漏)

下载keepalived:

http://www.keepalived.org/software/keepalived-1.2.6.tar.gz

安装命令:

# tar zxvf keepalived-1.2.6.tar.gz

# cd keepalived-1.2.6

# ./configure --sysconf=/etc --with-kernel-dir=/lib/modules/2.6.32-431.el6.x86_64 #指定配置文件存放路径,指定使用内核源码中的头文件。

#make

#make install

#ln -s /usr/local/sbin/keepalived /sbin/ #把启动命令软连接到sbin,方便以后直接使用

能通过keepalived --help查看相关信息即安装成功。

配置 LVS :

主 LVS :

! Configuration File for keepalived

global_defs {

notification_email { #配置告警邮件发送

XXXX @qq.com

}

notification_email_from [email protected]

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER #主LVS标志

interface eth1 #设置对外服务口

virtual_router_id 51

priority 100 #设置优先级,优先级高的为主机

advert_int 1 #设置同步时间间隔

authentication { #设置主备LVS验证类型和密码,两边必须一致

auth_type PASS

auth_pass 1111

}

virtual_ipaddress { #设置虚拟服务IP

192.168.153.110

}

}

virtual_server 192.168.153.110 80 {

delay_loop 6 #健康检查时间间隔

lb_algo rr #负载均衡算法,此为轮询算法

lb_kind DR #负载均衡转发规则,也就是IP转发规则,DR转发方式最快,但是所有机器必须在一个网段内,还有NAT和TUNEL两种转发方式

protocol TCP

real_server 192.168.153.131 80 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.153.135 80 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

备用LVS配置:复制上面配置修改两处即可:1. state MASTER 改为 state BACKUP 2.将 priority 100 改低,这里改为80

配置Realserver

对所有 Realserver 重复做一下步骤

在/etc/init.d下创建脚本lvsrs

1 #!/bin/bash

2

3 VIP=192.168.153.110

4 ./etc/rc.d/init.d/functions

5 case "$1" in

6 start)

7 echo "Start LVS of Realserver!"

8 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up

9 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

10 echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce

11 echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

12 echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

13

14 ;;

15

16 stop)

17

18 /sbin/ifconfig lo:0 down

19 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

20 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce

21 echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore

22 echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

23 echo "RealServer Stoped"

24

25 ;;

26

27 *)

28

29 echo "Usage: $0 {start|stop}"

30

31 exit 1

32

33 esac

修改其执行权限:chmod 755 /etc/init.d/lvsrs

执行启动脚本:

#service lvsrs start

查看其ip:

lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

inet6 addr: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX packets:8 errors:0 dropped:0 overruns:0 frame:0

TX packets:8 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0

RX bytes:480 (480.0 b) TX bytes:480 (480.0 b)

lo:0 Link encap:Local Loopback inet addr:192.168.153.110 Mask:255.255.255.255 UP LOOPBACK RUNNING MTU:16436 Metric:1

在realserver上安装apache并且启动!

在 /var/www/hmtl/下添加index.html测试主页 ,不同 server用不同界面进行区分。

启动LVS并且测试:

主备LVS启动:/etc/init.d/keepalived start

查看启动状态:

主LVS:

Feb 4 20:11:41 rex Keepalived_vrrp[9522]: VRRP_Instance(VI_1) Transition to MASTER STATE Feb 4 20:11:42 rex Keepalived_vrrp[9522]: VRRP_Instance(VI_1) Entering MASTER STATE Feb 4 20:11:42 rex Keepalived_vrrp[9522]: VRRP_Instance(VI_1) setting protocol VIPs. Feb 4 20:11:42 rex Keepalived_vrrp[9522]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.153.110 Feb 4 20:11:42 rex Keepalived_healthcheckers[9521]: Netlink reflector reports IP 192.168.153.110 added Feb 4 20:11:47 rex Keepalived_vrrp[9522]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth2 for 192.168.153.110

备LVS:

Feb 4 20:18:16 rex Keepalived_vrrp[9522]: VRRP_Instance(VI_1) Received higher prio advert Feb 4 20:18:16 rex Keepalived_vrrp[9522]: VRRP_Instance(VI_1) Entering BACKUP STATE Feb 4 20:18:16 rex Keepalived_vrrp[9522]: VRRP_Instance(VI_1) removing protocol VIPs. Feb 4 20:18:16 rex Keepalived_healthcheckers[9521]: Netlink reflector reports IP 192.168.153.110 removed Feb 4 20:19:06 rex dhclient[1265]: DHCPREQUEST on eth2 to 192.168.153.254 port 67 (xid=0x6f9b7b38)

用ip addr和ipvsadm查看路由情况

主LVS# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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

inet6 ::1/128 scope host

valid_lft forever preferred_lft forever

2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:3e:ce:ce brd ff:ff:ff:ff:ff:ff

inet 192.168.153.133/2

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: Linux负载均衡服务器算法LVTI行权DHCDHCP
分页:12
转载请注明
本文标题:基于VMware的虚拟Linux集群搭建-lvs+keepalived
本站链接:http://www.codesec.net/view/529890.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(18)