LVS 负载均衡配置
Posted 祝灵
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS 负载均衡配置相关的知识,希望对你有一定的参考价值。
LVS全称Linux Virtual Server,Linux虚拟服务器。现在LVS已经是Linux内核标准的一部分。使用LVS可以达到的技术目标是:通过LVS达到的负载均衡技术和Linux操作系统实现一个高性能高可用的Linux服务器群集,它具有良好的可靠性,可拓展性和可操作性。
注意:此次部署的三个节点采用系统为Centos 8-Stream
更改主机名,三个节点都要做
[root@localhost ~]# hostnamectl set-hostname lb
[root@localhost ~]# bash
[root@lb ~]#
[root@localhost ~]# hostnamectl set-hostname web1
[root@localhost ~]# bash
[root@web1 ~]#
[root@localhost ~]# hostnamectl set-hostname web2
[root@localhost ~]# bash
[root@web2 ~]#
关闭防火墙和selinux,三个节点都要做
systemctl disable --now firewalld
vi /etc/selinux/config
SELINUX=disabled
配置yum源,三个节点都要做
下面配置的是centos8的yum源,如果是centos7系统,自行到阿里云镜像站配置7的源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
sed -i -e \'/mirrors.cloud.aliyuncs.com/d\' -e \'/mirrors.aliyuncs.com/d\' /etc/yum.repos.d/CentOS-Base.repo
安装所需包,三个节点都要做
yum -y install net-tools network-scripts
配置lb节点
[root@lb ~]# ifconfig ens33:0 192.168.88.250/32 broadcast 192.168.88.250 up
[root@lb ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:20:b3:9e brd ff:ff:ff:ff:ff:ff
inet 192.168.88.128/24 brd 192.168.88.255 scope global dynamic noprefixroute ens33
valid_lft 1149sec preferred_lft 1149sec
inet 192.168.88.250/0 brd 192.168.88.250 scope global ens33:0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe20:b39e/64 scope link noprefixroute
valid_lft forever preferred_lft forever
配置web1节点
[root@web1 ~]# vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@web1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@web1 ~]# ifconfig lo:0 192.168.88.250/32 broadcast 192.168.88.250 up
[root@web1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 192.168.88.250/0 brd 192.168.88.250 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:5f:81:55 brd ff:ff:ff:ff:ff:ff
inet 192.168.88.129/24 brd 192.168.88.255 scope global dynamic noprefixroute ens33
valid_lft 1670sec preferred_lft 1670sec
inet6 fe80::20c:29ff:fe5f:8155/64 scope link noprefixroute
valid_lft forever preferred_lft forever
配置web2节点
[root@web2 ~]# vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@web2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@web2 ~]# ifconfig lo:0 192.168.88.250/32 broadcast 192.168.88.250 up
[root@web2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet 192.168.88.250/0 brd 192.168.88.250 scope global lo:0
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:94:2f:80 brd ff:ff:ff:ff:ff:ff
inet 192.168.88.130/24 brd 192.168.88.255 scope global dynamic noprefixroute ens33
valid_lft 1462sec preferred_lft 1462sec
inet6 fe80::20c:29ff:fe94:2f80/64 scope link noprefixroute
valid_lft forever preferred_lft forever
配置路由信息
[root@lb ~]# route add -host 192.168.88.250 dev ens33:0
[root@lb ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.88.2 0.0.0.0 UG 100 0 0 ens33
192.168.88.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.88.250 0.0.0.0 255.255.255.255 UH 0 0 0 ens33
[root@web1 ~]# route add -host 192.168.88.250 dev lo:0
[root@web1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.88.2 0.0.0.0 UG 100 0 0 ens33
192.168.88.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.88.250 0.0.0.0 255.255.255.255 UH 0 0 0 lo
[root@web2 ~]# route add -host 192.168.88.250 dev lo:0
[root@web2 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.88.2 0.0.0.0 UG 100 0 0 ens33
192.168.88.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.88.250 0.0.0.0 255.255.255.255 UH 0 0 0 lo
配置http
[root@web1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@web1 ~]# echo web1 > /var/www/html/index.html
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@web2 ~]# echo web2 > /var/www/html/index.html
添加规则
[root@lb ~]# yum -y install ipvsadm
[root@lb ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lb ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lb ~]# ipvsadm -A -t 192.168.88.250:80 -s rr
[root@lb ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.88.250:80 rr
[root@lb ~]# ipvsadm -a -t 192.168.88.250:80 -r 192.168.88.129:80 -g
[root@lb ~]# ipvsadm -a -t 192.168.88.250:80 -r 192.168.88.130:80 -g
[root@lb ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.88.250:80 rr
-> 192.168.88.129:80 Route 1 0 0
-> 192.168.88.130:80 Route 1 0 0
查看,打开本机终端使用curl命令
使用watch命令定住屏幕查看进程
[root@lb ~]# watch \'ipvsadm -Ln\'
以上是关于LVS 负载均衡配置的主要内容,如果未能解决你的问题,请参考以下文章