基于CentOS 8 下的操作
需求: 有10台一下的服务器,需要做负载均衡。
LVS Linux Virtual System. 其大致可以理解为,一家电脑组装商铺,你是个黑心老板(DS),手下有几个组装工(RS),当然他们几个的技术相当(real-server 是一致的)。因为你是个黑心老板,所以你把组装工(RS)关到小黑屋,只能工作,他们除了和老板沟通,不能与外界联系。而老板负责订单的签订和装好机器的交付。
真实拓扑图:
在此解释下,因为鄙人设备有限,只能开3台虚拟设备。一台作为LVS 调度服务器,两台apache 服务器
按照上面的网络拓扑图,开始搭建
1、修改VMware real-server 设备的网络适配器,webserver1和2 都是该模式
DS(director-server) 这里我用的是桥接模式
新增DS 设备的第二块网卡
2、修改DS网络配置
centos8 设置新增网卡后直接就可以看到新增信息:
1)配置ens33网卡
vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.1.60
PREFIX=24
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=114.114.114.114
2)配置ens36网卡
因为该网卡是新增的,系统只是识别到了,并没有ifcfg-ens36的配置文件 配置方法:
- nmtui 图形化界面配置
- cp ifcfg-ens33 ifcfg-ens36 并修改
我这里使用第二种方法
[root@lvsserver network-scripts]
3dfe04c9-d31e-41a4-a636-dff0a4d35e77
[root@lvsserver network-scripts]
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens36
UUID=3dfe04c9-d31e-41a4-a636-dff0a4d35e77
DEVICE=ens36
ONBOOT=yes
IPADDR=192.168.2.60
PREFIX=24
GATEWAY=192.168.2.2
DNS1=8.8.8.8
DNS2=114.114.114.114
[root@lvsserver network-scripts]
[root@lvsserver network-scripts]
Connection 'ens36' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
[root@lvsserver network-scripts]
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.60 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 2409:8a44:8b3f:f10:20c:29ff:fee8:13d8 prefixlen 64 scopeid 0x0<global>
inet6 fe80::20c:29ff:fee8:13d8 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e8:13:d8 txqueuelen 1000 (Ethernet)
RX packets 1504 bytes 124607 (121.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1200 bytes 190355 (185.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.60 netmask 255.255.255.0 broadcast 192.168.2.255
inet6 fe80::20c:29ff:fee8:13e2 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e8:13:e2 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 56 bytes 7092 (6.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
就此,网卡配置成功
3)配置分发功能
a. 先查看目前是否支持分发功能:
[root@lvsserver network-scripts]
1
注意:centos8默认是开启的
如何手动开启:
[root@lvsserver network-scripts]
net.ipv4.ip_forward=1
[root@lvsserver network-scripts]
net.ipv4.ip_forward = 1
[root@lvsserver network-scripts]
1
b. 安装ipvsadm 软件
[root@lvsserver network-scripts]
ipvsadm -A -t 192.168.1.60:80 -s rr -A 新增规则 -a 新增分发 - t tcp IPADDR:PORT -s 设置算法
c. 设置分发
[root@lvsserver network-scripts]
[root@lvsserver network-scripts]
[root@lvsserver network-scripts]
[root@lvsserver network-scripts]
[root@lvsserver network-scripts]
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP localhost.localdomain:http rr
-> 192.168.1.61:http Masq 1 0 0
-> 192.168.1.62:http Masq 1 0 0
[root@lvsserver network-scripts]
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.60:80 rr
-> 192.168.1.61:80 Masq 1 0 0
-> 192.168.1.62:80 Masq 1 0 0
[root@lvsserver network-scripts]
就此 DS 设置完毕
3、修改RS网络配置和下载apache
1) 修改两台RS网卡配置
a. webserver1
[root@webserver1 network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens33
UUID=3dfe04c9-d31e-41a4-a636-dff0a4d35e77
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.2.61
PREFIX=24
GATEWAY=192.168.2.60
DNS1=8.8.8.8
DNS2=114.114.114.114
b. webserver2
[root@webserver2 network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens33
UUID=3dfe04c9-d31e-41a4-a636-dff0a4d35e77
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.2.62
PREFIX=24
GATEWAY=192.168.2.60
DNS1=8.8.8.8
DNS2=114.114.114.114
测试连通:
[root@lvsserver network-scripts]
PING 192.168.2.61 (192.168.2.61) 56(84) bytes of data.
64 bytes from 192.168.2.61: icmp_seq=1 ttl=64 time=0.660 ms
64 bytes from 192.168.2.61: icmp_seq=2 ttl=64 time=1.00 ms
^C
--- 192.168.2.61 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1056ms
rtt min/avg/max/mdev = 0.660/0.830/1.001/0.172 ms
[root@lvsserver network-scripts]
PING 192.168.2.62 (192.168.2.62) 56(84) bytes of data.
64 bytes from 192.168.2.62: icmp_seq=1 ttl=64 time=4.99 ms
64 bytes from 192.168.2.62: icmp_seq=2 ttl=64 time=0.932 ms
^C
--- 192.168.2.62 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.932/2.962/4.993/2.031 ms
2) 下载apache
a. webserver1 因为 webserver1 和 webserver1 与 DS 在一个局域网,webserver1 和 webserver1 不能连外网,这里通过本地yum源直接安装了
[root@lvsserver /]
[root@webserver1 ~]
[root@webserver1 ~]
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2022-05-13 16:17:02 CST; 5s ago
......
验证下:
就此 webserver1 安装完成
b. webserver1
[root@lvsserver /]
[root@webserver2 ~]
[root@webserver2 ~]
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2022-05-13 16:17:02 CST; 5s ago
......
验证下:
就此 webserver2 安装完成
外部wind10 访问192.168.1.60
|