当前位置:网站首页>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] KVM create Bridge
- PHP programming language (1) - operators
- Canoe - the second simulation project -xvihicle1 bus database design (operation)
- Unittest+airtest+beatiulreport combine the three to make a beautiful test report
- Canoe - the third simulation project - bus simulation - 3-2 project implementation
- Remove linked list elements
- Read a piece of text into the vector object, and each word is stored as an element in the vector. Convert each word in the vector object to uppercase letters. Output the converted elements in the vect
- JMeter Foundation
- The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
猜你喜欢

Elevator dispatching (pairing project) ②

The most detailed teaching -- realize win10 multi-user remote login to intranet machine at the same time -- win10+frp+rdpwrap+ Alibaba cloud server

Huge number (C language)

Canoe - description of common database attributes

Appscan installation error: unable to install from Net runtime security policy logout appscan solution

Linked list operation can never change without its roots

From programmers to large-scale distributed architects, where are you (I)

Using SA token to solve websocket handshake authentication

Elevator dispatching (pairing project) ④

183 sets of free resume templates to help everyone find a good job
随机推荐
Add t more space to your computer (no need to add hard disk)
[untitled]
From programmers to large-scale distributed architects, where are you (I)
2、 Operators and branches
Canoe the second simulation engineering xvehicle 3 CAPL programming (operation)
Canoe - the second simulation project -xvihicle1 bus database design (operation)
Performance test method
Pod management
Iterator generators and modules
Oracle11g | getting started with database. It's enough to read this 10000 word analysis
51 data analysis post
[Galaxy Kirin V10] [desktop] can't be started or the screen is black
[Galaxy Kirin V10] [server] failed to start the network
Aike AI frontier promotion (2.14)
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
[Galaxy Kirin V10] [server] grub default password
XMIND installation
Canoe - the third simulation project - bus simulation - 3-1 project implementation
20 minutes to learn what XML is_ XML learning notes_ What is an XML file_ Basic grammatical rules_ How to parse
Write a program to define an array with 10 int elements, and take its position in the array as the initial value of each element.