基于 CentOS 7 构建 LVS-DR 群集。

1.准备实验环境

DS:DIP--192.168.163.138

        VIP--192.168.163.200

RS: RIP1(web1)--192.168.163.140

       RIP2(web2)--192.168.163.141

Client:user--192.168.163.142

2.配置服务器环境

1)搭建简易的web服务

RIP1

[root@localhost ~]# yum install httpd
[root@localhost ~]# echo "web1 test, ip is `hostname -I` ." > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd

RIP2

[root@localhost ~]# yum install httpd
[root@localhost ~]# echo "web2 test, ip is `hostname -I` ." > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd

使用Client验证

[root@localhost ~]# curl 192.168.163.140
web2 test ip is 192.168.163.140 .
[root@localhost ~]# curl 192.168.163.141
web1 test, ip is 192.168.163.141  .

2)配置VIP虚拟ip地址

[root@localhost ~]# nmcli connection show
NAME   UUID                                  TYPE      DEVICE 
ens33  5e4131fb-0601-422a-916f-9a0e8b16deb7  ethernet  ens33 
[root@localhost ~]# ifconfig ens33:200 192.168.163.200/24 
[root@localhost ~]# ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.163.138  netmask 255.255.255.0  broadcast 192.168.163.255
        inet6 fe80::83c2:22a3:b848:9285  prefixlen 64  scopeid 0x20
        inet6 fe80::3d2:78bf:c3a6:7bd7  prefixlen 64  scopeid 0x20
        inet6 fe80::5f89:8967:3a3a:a5f4  prefixlen 64  scopeid 0x20
        ether 00:0c:29:3b:1d:03  txqueuelen 1000  (Ethernet)
        RX packets 24394  bytes 25080376 (23.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8102  bytes 717188 (700.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:200: flags=4163  mtu 1500
        inet 192.168.163.200  netmask 255.255.255.0  broadcast 192.168.163.255
        ether 00:0c:29:3b:1d:03  txqueuelen 1000  (Ethernet)

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 77  bytes 9856 (9.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 77  bytes 9856 (9.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# nmcli connection up ens33
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/8)

3)手动绑定VIP和手写访问VIP的路由

RIP1:

[root@localhost ~]# ifconfig lo:200 192.168.163.200 netmask 255.255.255.255 up
[root@localhost ~]# ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.163.141  netmask 255.255.255.0  broadcast 192.168.163.255
        inet6 fe80::83c2:22a3:b848:9285  prefixlen 64  scopeid 0x20
        inet6 fe80::3d2:78bf:c3a6:7bd7  prefixlen 64  scopeid 0x20
        inet6 fe80::5f89:8967:3a3a:a5f4  prefixlen 64  scopeid 0x20
        ether 00:0c:29:d4:2c:1e  txqueuelen 1000  (Ethernet)
        RX packets 4113  bytes 317195 (309.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1935  bytes 218535 (213.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6  bytes 468 (468.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 468 (468.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:200: flags=73  mtu 65536
        inet 192.168.163.200  netmask 255.255.255.
[root@localhost ~]# route add -host 192.168.163.200 dev lo
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.163.2   0.0.0.0         UG    100    0        0 ens33
192.168.163.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.163.200 0.0.0.0         255.255.255.255 UH    0      0        0 lo

RIP2同上

4)在DS上配置LVS

下载ipvsadm

[root@localhost ~]# yum install ipvsadm -y

添加ipvsadm服务

[root@localhost ~]# ipvsadm -A -t 192.168.163.200:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.163.200:80 -r 192.168.163.141 -g -w 1
[root@localhost ~]# ipvsadm -a -t 192.168.163.200:80 -r 192.168.163.140 -g -w 1
[root@localhost ~]# 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.163.200:80 rr
  -> 192.168.163.140:80           Route   1      0          0         
  -> 192.168.163.141:80           Route   1      0          0         

5)在RS上配置ARP抑制

[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@localhost ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@localhost ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

RIP1和RIP2皆如此

6)结果测试

在Client上进行测试

[root@localhost ~]# for ((i=1;i<=10;i++)); do curl 192.168.163.200;doneweb1 test, ip is 192.168.163.141  .
web2 test ip is 192.168.163.140 .
web1 test, ip is 192.168.163.141  .
web2 test ip is 192.168.163.140 .
web1 test, ip is 192.168.163.141  .
web2 test ip is 192.168.163.140 .
web1 test, ip is 192.168.163.141  .
web2 test ip is 192.168.163.140 .
web1 test, ip is 192.168.163.141  .
web2 test ip is 192.168.163.140 .

成功访问到服务

你可能感兴趣的:(lvs,linux,运维)