LVS的DR模式和NAT模式配置http

LVS的DR模式和NAT模式配置http

文章目录

  • LVS的DR模式和NAT模式配置http
      • lvs 的 NET
      • lvs 的 DR

lvs 的 NET

LVS服务器(DR) DIP:192.168.240.60 VIP:192.168.240.250
apache服务器(RS1) IP:192.168.240.40 VIP:192.168.240.250
apache服务器(RS2) IP:192.168.240.50 VIP:192.168.240.250
##配置DR的IP(rs1的IP为:192.168.240.40,rs2的IP为192.168.240.50)
[root@localhost html]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=178e9602-d0df-4993-81de-230784e594d3
DEVICE=ens33
ONBOOT=yes
IPADDR0=192.168.240.60
IPADDR1=192.168.240.250
NATEMASK=255.255.255.0
GATEWAY=192.168.240.2
DNS1=114.114.114.114

##下载软件
[root@localhost]#yum install -y ipvsadm
[root@localhost ~]#yum -y install net-tools
[root@localhost ~]# systemctl enable --now  ipvsadm 
##开启转发功能
[root@localhost ~]# vim /etc/sysctl.conf 
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1

##添加规则调度器
[root@localhost ~]# ipvsadm -A -t 192.168.240.250:80 -s rr
[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.240.250:80 rr
[root@localhost ~]# ipvsadm -a -t 192.168.240.250:80 -r 192.168.240.40:80 -m
[root@localhost ~]# ipvsadm -a -t 192.168.240.250:80 -r 192.168.240.50:80 -m
[root@localhost ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
[root@localhost ~]# cat /etc/sysconfig/ipvsadm
-A -t 192.168.240.250:80 -s rr
-a -t 192.168.240.250:80 -r 192.168.240.40:80 -m -w 1
-a -t 192.168.240.250:80 -r 192.168.240.50:80 -m -w 1

##在192.168.240.40虚拟机httpd的index.html上追加es1
[root@localhost noindex]# echo 'esl' > index.html
[root@localhost noindex]# systemctl restart httpd

##在192.168.240.50虚拟机httpd的index.html上追加es1
[root@localhost noindex]# echo 'es2' > index.html
[root@localhost noindex]# systemctl restart httpd

##三台虚拟机都关闭防火墙跟selinx
[root@localhost html]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost html]# setenforce 0

##测试
[root@localhost html]# curl http://192.168.240.250
esl
[root@localhost html]# curl http://192.168.240.250
es2

lvs 的 DR

LVS服务器(DR) DIP:192.168.240.40 VIP:192.168.240.250
apache服务器(RS1) IP:192.168.240.50 VIP:192.168.240.250
apache服务器(RS2) IP:192.168.240.60 VIP:192.168.240.250
##关闭三台虚拟机的防火墙与selinux
[root@localhost ~]# systemctl disable --now  firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0

##DR配置VIP
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=6c962af3-5a66-4b53-a462-d14059ff98a8
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.240.40
IPADDR1=192.168.240.250
GATEWAY=192.168.240.2
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.8
[root@localhost ~]# ifdown ens33 ;ifup ens33 
成功停用连接 "ens33"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3)
[root@localhost ~]# ip a
1: lo:  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:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:cc:68:c5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.240.40/24 brd 192.168.240.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.240.250/24 brd 192.168.240.255 scope global secondary noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::b0aa:5d3b:1352:b7cb/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

##配置RS1与RS2的APR内核参数
[root@RS1 ~]# vim /etc/sysctl.conf 
[root@RS1 ~]# cat /etc/sysctl.conf 
net.ipv4.conf.all.arp_ignore = 1 
net.ipv4.conf.all.arp_announce = 2

[root@RS1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

[root@RS2 ~]# vim /etc/sysctl.conf 
[root@RS2 ~]# cat /etc/sysctl.conf 
net.ipv4.conf.all.arp_ignore = 1 
net.ipv4.conf.all.arp_announce = 2

[root@RS2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

##RS1与RS2上配置VIP(一定要先设置好内核参数在配置VIP,如果先配置VIP,VIP配置好后会立即通告给所有人,而修改内核参数就是为了不通告)
[root@RS1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
[root@RS1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=6ddb22bb-caad-4b3d-af34-4df850d4fedd
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.240.50
IPADDR1=192.168.240.250
NETMASK=255.255.255.0
GATEWAY=192.168.240.2
DNS1=114.114.114.114
[root@RS1 ~]# ifdown ens33 ;ifup ens33 
成功停用连接 "ens33"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3)
[root@RS1 ~]# ip a
1: lo:  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:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:00:ba:90 brd ff:ff:ff:ff:ff:ff
    inet 192.168.240.50/24 brd 192.168.240.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.240.250/24 brd 192.168.240.255 scope global secondary noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::66b2:7fb5:7e38:b7e3/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

[root@RS2 ~]# ip a
1: lo:  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:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:11:4d:9c brd ff:ff:ff:ff:ff:ff
    inet 192.168.240.60/24 brd 192.168.240.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.240.250/24 brd 192.168.240.255 scope global secondary noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::fabc:12fd:b92d:c0a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

##DR上安装ipvsadm并设置开机自启
[root@localhost ~]# yum -y install ipvsadm
[root@localhost ~]# systemctl enable --now  ipvsadm 
[root@localhost ~]# vim /opt/start.sh
[root@localhost ~]# cat /opt/start.sh
#!/bin/bash
ipvsadm -A -t 192.168.240.250:80 -s rr
ipvsadm -a -t 192.168.240.250:80 -r 192.168.240.50:80 -g -w 1
ipvsadm -a -t 192.168.240.250:80 -r 192.168.240.60:80 -g -w 1
ipvsadm -Sn > /etc/sysconfig/ipvsadm

[root@localhost ~]# chmod +x /opt/start.sh
[root@localhost ~]# vim /etc/rc.local
[root@localhost ~]# cat /etc/rc.local
#!/bin/bash
..........
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

source /opt/start.sh  


##ipvsadm配置
[root@localhost ~]# ipvsadm -A -t 192.168.240.250:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.240.250:80 -r 192.168.240.50:80 -g
[root@localhost ~]# ipvsadm -a -t 192.168.240.250:80 -r 192.168.240.60:80 -g
[root@localhost ~]# ipvsadm -Sn
-A -t 192.168.240.250:80 -s rr
-a -t 192.168.240.250:80 -r 192.168.240.50:80 -g -w 1
-a -t 192.168.240.250:80 -r 192.168.240.60:80 -g -w 1

##保存策略文件
[root@localhost ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
(-A:添加虚拟服务器
-t :指定vip及tcp端口
-s:指定算法 
rr:轮询
-a :添加节点 
-t :指定vip和端口
-r :指定节点ip及端口
-g:表示使用DR模式
-w:设置权重)

##给R1和R2http追加文件进行测试
[root@RS1 ~]# echo " 192.168.240.50" > /usr/share/httpd/noindex/index.html

[root@RS2 ~]# echo " 192.168.240.60" > /usr/share/httpd/noindex/index.html

LVS的DR模式和NAT模式配置http_第1张图片
LVS的DR模式和NAT模式配置http_第2张图片

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