当前位置:网站首页>LVS+Keepalived实现四层负载及高可用
LVS+Keepalived实现四层负载及高可用
2022-07-04 10:56:00 【星哥玩云】
最近要上线一个新项目,需要软负载,本来决定用nginx做反向代理,但装完nginx以后,发现nginx无法显示css、js,目前还在研究中,但由于项目较急,所以依然采用了经典架构LVS+Keepalived来实现。
环境:
LVS-01 192.168.199.117 MASTER
LVS-02 192.168.199.118 BACKUP
WEB-01 192.168.199.113
WEB-02 192.168.199.114
VIP 192.168.199.119
编译安装LVS
ln -s/usr/src/kernels/2.6.18-308.el5-x86_64/ /usr/src/linux
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
tar zxf ipvsadm-1.26.tar.gz
cd ipvsadm-1.26
make
发现报错,报错结果如下:
make[1]: *** [libipvs.o] Error1
make[1]: Leaving directory`/root/tools/ipvsadm-1.26/libipvs'
make: *** [libs] Error 2
经查询,有好多种说法,均没有解决我的问题:
1、 有的说是需要做内核软链接 ln-s /usr/src/kernels/2.6.18-308.el5-x86_64/ /usr/src/linux
2、 有的说是要yuminstall gcc-*
3、还有的说是yum installlibnl* popt*
以上方法可能适用于其实环境,但是在我的机器上面均无效。折腾了好久,请教高手,又是查看http://www.linuxvirtualserver.org/Documents.html官方说明文档,最终认为版本与内核不匹配,总结为以下2点:
1、 ipvsadm-1.26版本适合kernel 2.6.28-rc3及以后
2、 选择好相应的ipvsadm后,还要选择相对应的keepalived,否则会出现启动keepalived后,没有realserver列表。大致显示就是这样:
IP Virtual Server version 1.2.1(size=4096)
Prot LocalAddress:PortScheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
好了,问题找到了,重新操作,下载低版本的ipvsadm,我的内核版本为2.6.18-308.el5-x86_64,用ipvsadm-1.24测试正常,keepalived的版本为keepalived-1.1.15,具体操作如下:
安装LVS
ln -s/usr/src/kernels/2.6.18-308.el5-x86_64/ /usr/src/linux
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make
make install
cd ..
ipvsadm –version
安装keepalived
wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
tar zxfkeepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure
make
make install
cp/usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp/usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived/
cp/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived/usr/sbin/
/etc/init.d/keepalived start
至此,安装完毕,下面配置文件。
LVS-01上面keepalived的配置文件,主与备基本一致,不同的为红色部分
vim /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs {
router_id LVS-01 #routeid
}
vrrp_instance LVS {
state MASTER #备上为BACKUP
interface eth0
virtual_router_id 251
priority 100 #备节点上比主的数值要小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.199.119
}
}
virtual_server 192.168.199.11980 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60 #会话保持
protocol TCP
real_server 192.168.199.113 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.168.199.114 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
启动keealived
/etc/init.d/keepalived restart
检查是否有VIP
[[email protected] ipvsadm-1.26]#ip add
1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd00: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: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:70:47:f4 brdff:ff:ff:ff:ff:ff
inet 192.168.199.117/24 brd 192.168.199.255scope global eth0
inet 192.168.199.119/32 scope global eth0
inet6 fe80::20c:29ff:fe70:47f4/64 scopelink
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
检查是否有realserver列表
[[email protected] ipvsadm-1.26]#ipvsadm -Ln
IP Virtual Server version 1.2.1(size=4096)
Prot LocalAddress:PortScheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.199.119:80 wrr
-> 192.168.199.113:80 Route 1 0 0
-> 192.168.199.114:80 Route 1 0 0
最后别忘了,有realserver上面配置VIP和ARP抑制,这个用脚本就好啦,具体如下:
[[email protected] tools]# cat realserver.sh
#!/bin/sh
[ -f"/etc/init.d/functions" ] && . /etc/init.d/functions
VIP=(
192.168.199.119
)
start(){
#bind VIP
for ((i=0;i<${#VIP[*]};i++))
do
/sbin/ifconfig lo:i {VIP[
done
#ARP restrain
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
}
stop(){
for ((i=0;i<${#VIP[*]};i++))
do
/sbin/ifconfig lo:i {VIP[
done
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
}
case "$1" in
start)
start
if [ $? -eq 0 ];then
action "ipvs start sucess" /bin/true
else
action "ipvs start failed "/bin/false
fi
;;
stop)
stop
if [ $? -eq 0 ];then
action "ipvs stop sucess" /bin/true
else
action "ipvs stop failed "/bin/false
fi
;;
*)
echo "USAGE:$0{start|stop}"
esac
执行完成后,查看VIP
[[email protected] tools]# ip add
1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet 192.168.199.119/32 brd 192.168.199.119scope global lo:0
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:41:7c:7e brdff:ff:ff:ff:ff:ff
inet 192.168.199.114/24 brd 192.168.199.255scope global eth0
inet6 fe80::20c:29ff:fe41:7c7e/64 scopelink
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
剩下的就是故障模拟与测试了,在此就不多写了。
边栏推荐
- [Galaxy Kirin V10] [server] soft RAID configuration
- Regular expression
- What if the book written is too popular? Author of "deep reinforcement learning" at Peking University: then open the download
- The bamboo shadow sweeps the steps, the dust does not move, and the moon passes through the marsh without trace -- in-depth understanding of the pointer
- From programmers to large-scale distributed architects, where are you (2)
- DCL statement of MySQL Foundation
- The most ideal automated testing model, how to achieve layering of automated testing
- Discussion | has large AI become autonomous? Lecun, chief scientist of openai
- C language structure to realize simple address book
- Network connection (II) three handshakes, four waves, socket essence, packaging of network packets, TCP header, IP header, ACK confirmation, sliding window, results of network packets, working mode of
猜你喜欢
VLAN part of switching technology
[Galaxy Kirin V10] [server] KVM create Bridge
MPLS: multi protocol label switching
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
[Galaxy Kirin V10] [desktop] printer
Software sharing: the best PDF document conversion tool and PDF Suite Enterprise version sharing | with sharing
Function introduction of canbedded component
Sword finger offer 05 (implemented in C language)
Appscan installation error: unable to install from Net runtime security policy logout appscan solution
Learning XML DOM -- a typical model for parsing XML documents
随机推荐
Unittest+airtest+beatiulreport combine the three to make a beautiful test report
Write a program that uses pointers to set all elements of an int array to 4.18: 0.
/*Write a function to open the file for input, read the contents of the file into the vector container of string class 8.9: type, and store each line as an element of the container object*/
MBG combat zero basis
Performance features focus & JMeter & LoadRunner advantages and disadvantages
[Galaxy Kirin V10] [server] failed to start the network
DML statement of MySQL Foundation
[Galaxy Kirin V10] [server] iSCSI deployment
Remove linked list elements
[untitled]
Elevator dispatching (pairing project) ①
Seven examples to understand the storage rules of shaped data on each bit
VI text editor and user rights management, group management and time management
2022 ape circle recruitment project (software development)
Fundamentals of database operation
Summary of several job scheduling problems
Oracle11g | getting started with database. It's enough to read this 10000 word analysis
DCL statement of MySQL Foundation
How to quickly parse XML documents through C (in fact, other languages also have corresponding interfaces or libraries to call)
R built in data set