【LVS-NAT配置】

配置

node1:128(客户端)
node2:135(调度器)
RS:
node3:130
node4:132

node2添加网络适配器(仅主机模式)
【LVS-NAT配置】_第1张图片

[root@node2 ~]# nmtui

【LVS-NAT配置】_第2张图片

【LVS-NAT配置】_第3张图片
【LVS-NAT配置】_第4张图片

[root@node2 ~]# nmcli connection up ens36
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)

#在虚拟机node3、node4上
[root@node3 ~]# yum install httpd -y
[root@node4 ~]# yum install httpd -y

将node3、node4适配器改为仅主机模式
#此时在虚拟机上
网关是node2的内网接口

node3
在这里插入图片描述
node4
在这里插入图片描述

#通过node2连node3
[root@node2 ~]# ssh 10.0.0.13
[email protected]'s password: 
Last login: Sun Aug 13 18:06:01 2023
[root@node3 ~]# 
#连接node4
[root@node2 ~]# ssh 10.0.0.14
[email protected]'s password: 
Last login: Sun Aug 13 18:13:23 2023 from gateway
[root@node4 ~]# 

[root@node3 ~]# echo "web test page,ip is `hostname -I`." > /var/www/html/index.html
[root@node3 ~]# systemctl start httpd
[root@node4 ~]# echo "web test page,ip is `hostname -I`." > /var/www/html/index.html
[root@node4 ~]# systemctl start httpd

#开启路由转发功能(任意一种即可)
#一、临时开启
#1、
[root@node2 ~]# cat /proc/sys/net/ipv4/ip_forward
0
[root@node2 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@node2 ~]# cat /proc/sys/net/ipv4/ip_forward
1
#2、
[root@node2 ~]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
[root@node2 ~]# cat /proc/sys/net/ipv4/ip_forward
1
#二、永久开启
[root@node2 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1  #添加
#立即生效
[root@node2 ~]# sysctl -p
net.ipv4.ip_forward = 1

[root@node2 ~]# yum install ipvsadm -y
[root@node2 ~]# ipvsadm -At 192.168.40.135:80 -s rr
[root@node2 ~]# ipvsadm -at 192.168.40.135:80 -r 10.0.0.13:80 -m
[root@node2 ~]# ipvsadm -at 192.168.40.135:80 -r 10.0.0.14:80 -m

[root@node2 ~]# 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.40.135:80 rr
  -> 10.0.0.13:80                 Masq    1      0          0         
  -> 10.0.0.14:80                 Masq    1      0          0  

#node1 curl
[root@node1 ~]# for ((i=1;i<=8;i++));do curl 192.168.40.135;done
web test page,ip is 10.0.0.14 .
web test page,ip is 10.0.0.13 .
web test page,ip is 10.0.0.14 .
web test page,ip is 10.0.0.13 .
web test page,ip is 10.0.0.14 .
web test page,ip is 10.0.0.13 .
web test page,ip is 10.0.0.14 .
web test page,ip is 10.0.0.13 .

设置权重的配置

[root@node2 ~]# ipvsadm -Et 192.168.40.135:80 -s wrr
[root@node2 ~]# ipvsadm -et 192.168.40.135:80 -r 10.0.0.13:80 -m -w 1
[root@node2 ~]# ipvsadm -et 192.168.40.135:80 -r 10.0.0.14:80 -m -w 2
[root@node1 ~]# for ((i=1;i<=8;i++));do curl 192.168.40.135;done
web test page,ip is 10.0.0.14 .
web test page,ip is 10.0.0.14 .
web test page,ip is 10.0.0.13 .
web test page,ip is 10.0.0.14 .
web test page,ip is 10.0.0.14 .
web test page,ip is 10.0.0.13 .
web test page,ip is 10.0.0.14 .
web test page,ip is 10.0.0.14 .

需要node3、4可以上网

在这里插入图片描述

#node2上
[root@node2 ~]# iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o ens33 -j SNAT --to-source 192.168.40.135

【LVS-NAT配置】_第5张图片

保存恢复

[root@node2 ~]# ipvsadm -S
-A -t node2:http -s wrr
-a -t node2:http -r 10.0.0.13:http -m -w 1
-a -t node2:http -r 10.0.0.14:http -m -w 2
#保存到ipvs.txt
[root@node2 ~]# ipvsadm -S > ipvs.txt
[root@node2 ~]# ls
anaconda-ks.cfg  ipvs.txt

#删除完成
[root@node2 ~]# ipvsadm -C
[root@node2 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  
#恢复
[root@node2 ~]# ipvsadm -R < ipvs.txt 
[root@node2 ~]# 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.40.135:80 wrr
  -> 10.0.0.13:80                 Masq    1      0          0         
  -> 10.0.0.14:80                 Masq    2      0          0

你可能感兴趣的:(负载均衡集群,lvs)