时间同步:
yum -y install ntpdate
yum -y install ntpntpdate cn.ntp.org.cn
systemctl start ntpdsystemctl enable ntpd
安装ipvsadm
安装 ipvsadm
[root@nat ~] # yum -y install ipvsadm
清空以往的规则
[root@nat ~] # ipvsadm -C
查看规则
[root@nat ~] # ipvsadm -L -n
新增规则
[root@nat ~] # ipvsadm -A -t 192.168.0.100:80 -s rr
添加主机
[root@nat ~] # ipvsadm -a -t 192.168.0.100:80 -r
10.1.1.200:80 -m
[root@nat ~] # ipvsadm -a -t 192.168.0.100:80 -r
10.1.1.201:80 -m设置 ip 转发
[root@nat ~] # vim /etc/sysctl.conf
net.ipv4 .ip_forward = 1
设置生效
[root@nat ~] # sysctl -p
net.ipv4.ip_forward = 1
web服务器网关配置:
[root@web01 ~] # route del default
[root@web01 ~] # route add default gw 10.1.1.100
[root@web02 ~] # route del default
[root@web02 ~] # route add default gw 10.1.1.100
添加规则:
# 配置ipvs规则
[root@ds01 ~] # ipvsadm -A -t 192.168.10.100:80 -s rr
[root@ds01 ~] # ipvsadm -Ln
IP Virtual Server version 1 .2.1 (size = 4096 )
Prot LocalAddress:Port Scheduler Flags
- > RemoteAddress:Port Forward Weight ActiveConn InActConnTCP 192 .168.10.100:80 rr
# 添加rs web01 web02 添加规则
[root@ds01 ~] # ipvsadm -a -t 192.168.10.100:80 -r
10.1.1.200:80 -m
[root@ds01 ~] # ipvsadm -a -t 192.168.10.100:80 -r
10.1.1.201:80 -m
[root@ds01 ~] # 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.10.100:80 rr- > 10 .1.1.200:80 Masq 1 00
- > 10 .1.1.201:80 Masq 1 00
ip****转发:
[root@ds01 ~] # vim /etc/sysctl.conf
##########################################
net.ipv4 .ip_forward = 1
#############################################
[root@ds01 ~] # sysctl -p
net.ipv4.ip_forward = 1
临时修改web01和web02的网关,网关必须指向dip**(调度服务器的对内的ip****)**
[root@web01 ~] # route del default
[root@web01 ~] # route add default gw 10.1.1.100
[root@web01 ~] # # 临时修改网关这也要求了 rs ip 和 dip 要在同一个网段,因为 dip 是要作为网关存在的
NAT****脚本:
ds 脚本
#!/bin/bash
配置网卡
echo TYPE = "Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-ens36
echo BOOTPROTO = "none" >> /etc/sysconfig/network-scripts/ifcfg-ens36
read -p "router name:" router_name
echo NAME = '"$rount_name"' >> /etc/sysconfig/network-scripts/ifcfg-ens36
uuidkey = ( uuidgen ) echo UUID = '"uuidkey"' >> /etc/sysconfig/network-scripts/ifcfg-ens36 >> /etc/sysconfig/network-scripts/ifcfg-ens36
echo DEVICE = '"$rount_name"' >> /etc/sysconfig/network-scripts/ifcfg-ens36
echo ONBOOT = "yes" >> /etc/sysconfig/network-scripts/ifcfg-ens36
echo IPADDR = 192 .168.10.100 >> /etc/sysconfig/network-scripts/ifcfg-ens36
systemctl restart network安装 ipvsadm
yum list installed|grep ipvsadm
if [ $? -ne 0 ];then
yum -y install ipvsadm
fi配置规则
read -p "vip:" vip
read -p "port:" port
read -p "rule:" s
ipvsadm -A -t $vip : $port -s $sip forward
echo "net.ipv4.ip_forward=1" >/etc/sysctl.conf
sysctl -prs 脚本
#!/bin/bash
read -p "dip:" dip设置网关
route del default
route add defualt gw $dipDR模式:
- 性能更优,回路不再经过 ds
2.ds 和 rs 为了保证用户响应,都要求配置统一的 vip 3. 由于 rs 是直接响应 client ,网关一定不能设置为 ds 的 dip- 对 rs 的 vip 进行抑制,让 ds 的 vip 接收请求, rs 的 vip 不接受请求
5.rs 的 vip 绑定点 lo 回路网卡上
在ds的ens33上挂一个vip 10.1.1.102
ifconfig ens33:0 10.1.1.102 broadcast 10.1.1.102 netmask 255.255.255.255 up
route add -host 10.1.1.102 dev ens33:0
设置规范:
安装 ipvsadm
yum -y install ipvsadm
$ 设置规则
ipvsadm -A -t 10 .1.1.102:80 -s rr
ipvsadm -a -t 10 .1.1.102:80 -r 10 .1.1.200 -g
ipvsadm -a -t 10 .1.1.102:80 -r 10 .1.1.201 -grs 不在需要指定端口, dr 不支持端口映射, vip 上是 80 端口,最终就是 80 端口
-m nat -g gateway
设置rs主机:
1.在lo接口上绑定vip
[root@web01 ~] # ifconfig lo:0 10.1.1.102 broadcast10.1.1.102 netmask 255.255.255.255 up
2.设置主机路由
[root@web01 ~] # route add -host 10.1.1.102 dev lo:0
3.抑制rs接收请求
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
4.生成脚本,对web02使用
ifconfig lo:0 10 .1.1.102 broadcast 10 .1.1.102 netmask
255 .255.255.255 up
route add -host 10 .1.1.102 dev lo:0
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
5.测试使用,查看状态
[root@dr03 ~] # ipvsadm -Ln --stats
IP Virtual Server version 1 .2.1 (size = 4096 )
Prot LocalAddress:Port Conns InPkts
OutPkts InBytes OutBytes
- > RemoteAddress:PortTCP 10 .1.1.102:80 1 60 360 0
- > 10 .1.1.200:80 0 00 0 0
- > 10 .1.1.201:80 1 60 360 0
[root@dr03 ~] #
dr****模式的脚本:
#!/bin/bash
在 ens33 上挂载一个 ip 地址
read -p "vip:" vip
read -p "mac:" mac
read -p "num" num
ifconfig $mac : $num $vip broadcast $vip netmask 255 .255.255.255主机路由
route add -host $vip dev $mac : $num
安装 ipvsadm
yum list installed|grep ipvsadm
if [ $? -ne 0 ] ; then
yum -y install ipvsadm
fi配置规则(不需要设置 ip_forword )
ipvsadm -C
read -p "rule:" rule
read -p "port:" port
ipvsadm -A -t $vip : $port -s $rule
read -p "rip1:" rip1
ipvsadm -a -t $vip : $port -r $rip1 -g
read -p "rip2:" rip2
ipvsadm -a -t $vip : $port -r $rip2 -g
rs脚本:
#!/bin/bash
在 ens33 上挂载一个 ip 地址
read -p "vip:" vip
read -p "mac:" mac
read -p "num" num
ifconfig $mac : $num $vip broadcast $vip netmask 255 .255.255.255主机路由
route add -host $vip dev $mac : $num
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce