lvs基础配置

发布时间 2023-07-06 18:59:32作者: 雙_木

基础配置

[root@localhost ~]# systemctl disable --now firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.servic
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.servic
[root@localhost ~]# vi /etc/selinux/config 
[root@localhost ~]# setenforce  0
[root@localhost ~]# hostnamectl set-hostname LB
[root@localhost ~]# bash
[root@lb ~]# yum -y install net-tools network-scripts
[root@lb ~]# cd /etc/sysconfig/network-scripts/
[root@lb network-scripts]# vi ifcfg-ens33 
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.10
PREFIX=24
GATEWAY=192.168.100.2
DNS1=114.114.114.114
[root@lb network-scripts]# cd
[root@lb ~]# ifconfig ens33:0 192.168.100.250/32 broadcast 192.168.100.250 up

[root@slave1 network-scripts]# vi ifcfg-ens33 
[root@slave1 network-scripts]# systemctl restart network
[root@slave1 network-scripts]# cd
[root@slave1 ~]# vi /etc/sysctl.conf 
[root@slave1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@slave1 ~]# ifconfig lo:0 192.168.100.250/32 broadcast 192.168.100.250 up
[root@slave1 ~]# 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.100.250/0 brd 192.168.100.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 pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:e8:24:18 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.20/24 brd 192.168.100.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
       
[root@slave2 ~]# vi /etc/sysctl.conf 
[root@slave2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@slave2 ~]# cd /etc/sysconfig/network-scripts/
[root@slave2 network-scripts]# vi ifcfg-ens33 
[root@slave2 network-scripts]# vi ifcfg-ens33 
[root@slave2 network-scripts]# systemctl restart network
[root@slave2 network-scripts]# ifconfig lo:0 192.168.100.250/32 broadcast 192.168.100.250 up
[root@slave2 network-scripts]# 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.100.250/0 brd 192.168.100.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 pfifo_fast state UP group default qlen 1000
    link/ether 00:50:56:20:36:a0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.30/24 brd 192.168.100.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:fe20:36a0/64 scope link 
       valid_lft forever preferred_lft forever

配置路由信息

[root@lb ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.2   0.0.0.0         UG    100    0        0 ens33
192.168.100.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@lb ~]# route add -host 192.168.100.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.100.2   0.0.0.0         UG    100    0        0 ens33
192.168.100.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.100.250 0.0.0.0         255.255.255.255 UH    0      0        0 ens33

[root@slave1 ~]# route add -host 192.168.100.250 dev lo:0
[root@slave1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.2   0.0.0.0         UG    100    0        0 ens33
192.168.100.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.100.250 0.0.0.0         255.255.255.255 UH    0      0        0 lo

[root@slave2 network-scripts]# route add -host 192.168.100.250 dev lo:0
[root@slave2 network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.2   0.0.0.0         UG    0      0        0 ens33
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 ens33
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 ens33
192.168.100.250 0.0.0.0         255.255.255.255 UH    0      0        0 lo

在rs1,rs2安装httpd服务

[root@slave1 ~]# yum -y install httpd
[root@slave1 ~]# systemctl enable --now httpd
[root@slave2 ~]# yum -y install httpd
[root@slave2 ~]# systemctl enable --now httpd
#将slav1,slave2写入
[root@slave1 ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128        *:80                     *:*                  
LISTEN     0      128        *:22                     *:*                  
LISTEN     0      100    127.0.0.1:25                     *:*                  
LISTEN     0      128       :::22                    :::*                  
LISTEN     0      100      ::1:25                    :::*                  
[root@slave1 ~]# echo slave1 > /var/www/html/index.html
[root@slave2 ~]# echo slave2 > /var/www/html/index.html

在lb上添加保护规则

[root@lb ~]# yum -y install ipvsadm
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package ipvsadm.x86_64 0:1.27-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================
 Package          Arch            Version               Repository     Size
============================================================================
Installing:
 ipvsadm          x86_64          1.27-8.el7            base           45 k

Transaction Summary
============================================================================
Install  1 Package

Total download size: 45 k
Installed size: 75 k
Downloading packages:
ipvsadm-1.27-8.el7.x86_64.rpm                          |  45 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ipvsadm-1.27-8.el7.x86_64                                1/1 
  Verifying  : ipvsadm-1.27-8.el7.x86_64                                1/1 

Installed:
  ipvsadm.x86_64 0:1.27-8.el7                                               

Complete!
[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 -A -t 192.168.100.250:80 -s rr
[root@lb ~]# ipvsadm -a -t 192.168.100.250:80 -r 192.168.100.20:80 -g
[root@lb ~]# ipvsadm -a -t 192.168.100.250:80 -r 192.168.100.30: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.100.250:80 wlc
  -> 192.168.100.20:80            Route   1      0          0         
  -> 192.168.100.30:80            Route   1      0          0