Centos7搭建LVS負載均衡

2021-10-24 05:55:50 字數 4796 閱讀 6426

主機:192.168.111.51

備機:192.168.111.52

lvs vip: 192.168.111.50

yum -y install gcc gcc-c++ make popt popt-devel libnl libnl-devel popt-static openssl-devel kernel-devel

ln -s /usr/src/kernels/3.10.0-514.el7.x86_64 /usr/src/linux

yum -y install ipvsadm

ipvsadm -v

ipvsadm v1.

272008/5

/15(compiled with popt and ipvs v1.

2.1)

yum install keepalived

keepalived -v<

keepalived v1.

2.13(05

/25,2017

)

以下是在安裝keepalived時的依賴關係:

installed:

keepalived.x86_64 0:1.2.13-9.el7_3

dependency installed:

lm_sensors-libs.x86_64 0:3.4.0-4.20160601gitf9185e5.el7 net-snmp-agent-libs.x86_64 1:5.7.2-24.el7_3.2 net-snmp-libs.x86_64 1:5.7.2-24.el7_3.2

修改iptables配置,在主機上放開備機進來的流量,在備機也做相似設定:

firewall-cmd --direct --permanent --add-rule ipv4 filter input0--

in-inte***ce

enp0s3

--destination 224.0

.0.18

--protocol vrrp -j accept

firewall-cmd --reload

注:在備機上,該配置檔案僅有router_id和priority的值不同。其它值均一致。

以下為主機上的配置舉例:

global_defs 

notification_email_from [email protected]

smtp_server 127.0

.0.1

smtp_connect_timeout 30

router_id lvs_51

}vrrp_instance vi_1

virtual_ipaddress

}virtual_server 192.168

.111

.508888

} real_server 192.168

.111

.628888}}

virtual_server 192.168

.111

.508000

} real_server 192.168

.111

.628000

}}

啟動keepalived服務

# systemctl start keepalived
檢視keepalived服務狀態

# systemctl status keepalived
設定為隨系統自啟動

# systemctl enable keepalived
檢視系統網絡卡vip是否已經生效

# ip a
抓包觀察是否有主機定時發出的vrrp包

# tcpdump -p vrrp -n

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on enp0s3, link-type en10mb

(ethernet)

, capture size 65535 bytes10:

16:13.375399

ip192.168

.111

.51>

224.0

.0.18

: vrrpv2, advertisement, vrid 50

, prio 100

, authtype ******, intvl 1s, length 2010:

16:14.376542

ip192.168

.111

.51>

224.0

.0.18

: vrrpv2, advertisement, vrid 50

, prio 100

, authtype ******, intvl 1s, length 2010:

16:15.377596

ip192.168

.111

.51>

224.0

.0.18

: vrrpv2, advertisement, vrid 50

, prio 100

, authtype ******, intvl 1s, length 2010:

16:16.378590

ip192.168

.111

.51>

224.0

.0.18

: vrrpv2, advertisement, vrid 50

, prio 100

, authtype ******, intvl 1s, length 20

對keepalived的主備切換、vip位址漂移進行測試。

# systemctl stop keepalived

# systemctl start keepalived

注:為減少服務自動搶占對業務的影響,lvs服務是配置為執行在非搶占模式下。

chmod 700

/usr/local/src/lvs_real_server.sh

echo "/usr/local/src/lvs_real_server.sh start"

>>

/etc/rc.d/rc.local

/usr/local/src/lvs_real_server.sh start

以下為lvs_real_server.sh的內容,注意要更新vip參配置:

vi  lvs_real_server.sh

#!/bin/bash

vip=

192.168

.111

.50.

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

case

"$1"

instart)

ifconfig lo:

0 $vip netmask 255.255

.255

.255 broadcast $vip

route add -host $vip dev lo:

0 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

sysctl -p >

/dev/

null

2>&1

echo "real server start ok";;

stop)

ifconfig lo:

0 down

route del $vip

>

/dev/

null

2>&1

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

echo "real server stoped";;

*)echo "usage: $0 "

exit 1

esac

exit 0

在完成lvs部署後,後端提供服務的應用可能還沒有上線。這時需要預先測試lvs的負載均衡功能是否正常。方法是使用以下命令,在每個後端節點上臨時啟動乙個http server,服務埠設定為與lvs負載均衡服務相對應的埠號即可。使用後,停掉該命令即可。

centos7安裝nginx及簡單負載均衡配置

準備環境 centos7伺服器 1 安裝epel 源 更新源 yum update epel release 安裝epel源 yum install epel release y 2 安裝nginx 安裝nginx yum install nginx y 備註 安裝好的nginx已編譯好,無需mak...

Centos7搭建LAMP Typecho部落格

yum install httpd 安裝httpd服務 systemctl start httpd 啟動httpd服務 systemctl status httpd 檢視httpd服務狀態,看到active active running 說明httpd服務已啟動 systemctl enable h...

lvs 負載均衡環境搭建

記錄一下搭建lvs環境的步驟 其中master 10.0.0.11,backup 10.0.0.15,realserver1 10.0.0.119,realserver210.0.0.11 1.mkdir usr local src lvs 2.cd usr local src lvs 3.上傳ke...