第八周

1、DR模式下vip不在同一网段上实现过程(跨网段)

 第八周

 

 

 

 

准备环境:

1.准备客户端环境:

[root@cip ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=172.16.17.77
PREFIX=24
GATEWAY=172.16.17.17
ONBOOT=yes

2.准备router路由器环境:

#首先配置eth0是桥接模式 ip为172.16.17.17,eth1是NAT模式 ip为192.168.16.7
[root@router ~]# 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
  inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:b5:9d:3d brd ff:ff:ff:ff:ff:ff
  inet 172.16.17.17/24 brd 172.16.17.255 scope global noprefixroute eth0
      valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:feb5:9d3d/64 scope link tentative
      valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:b5:9d:47 brd ff:ff:ff:ff:ff:ff
  inet 192.168.16.7/24 brd 192.168.16.255 scope global noprefixroute eth1
      valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:feb5:9d47/64 scope link
      valid_lft forever preferred_lft forever
#在eth0上增加个vip 10.0.0.200
[root@router ~]# nmcli connection modify eth1 +ipv4.addresses 10.0.0.200/24 ifname eth1
[root@router ~]# nmcli c reload
[root@router ~]# nmcli c up eth1
#网卡配置
[root@router ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=172.16.17.17
PREFIX=24
ONBOOT=yes

[root@router ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
NAME=eth1
IPADDR=192.168.16.7
PREFIX=24
ONBOOT=yes

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR1=10.0.0.200
PREFIX1=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
UUID=9c92fad9-6ecb-3e6c-eb4d-8a47c6f50c04

[root@router ~]# 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
  inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:b5:9d:3d brd ff:ff:ff:ff:ff:ff
  inet 172.16.17.17/24 brd 172.16.17.255 scope global noprefixroute eth0
      valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:feb5:9d3d/64 scope link
      valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:b5:9d:47 brd ff:ff:ff:ff:ff:ff
  inet 192.168.16.7/24 brd 192.168.16.255 scope global noprefixroute eth1
      valid_lft forever preferred_lft forever
  inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute eth1
      valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:feb5:9d47/64 scope link
      valid_lft forever preferred_lft forever    
[root@router ~]# vim /etc/sysctl.conf
[root@router ~]# sysctl -p
net.ipv4.ip_forward = 1

3.使用脚本配置LVS环境

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

[root@lvs network-scripts]# cat lvs.sh
#!/bin/bash
#
#********************************************************************
#Author: yangxiaohao
#QQ: 1443964024
#Date: 2020-11-08
#FileName: lvs.sh
#URL:
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
vip='10.0.0.100'
iface='lo:1'
mask='255.255.255.255'
port='80'
rs1='192.168.16.17'
rs2='192.168.16.27'
scheduler='wrr'
type='-g'
rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null
case $1 in
start)
ifconfig $iface $vip netmask $mask #broadcast $vip up
iptables -F
ipvsadm -A -t ${vip}:${port} -s $scheduler
ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1
ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1
echo "The VS Server is Ready!"
;;
stop)
ipvsadm -C
ifconfig $iface down
echo "The VS Server is Canceled!"
;;
*)
echo "Usage: $(basename $0) start|stop"
exit 1
;;
esac
[root@lvs network-scripts]# bash lvs.sh start
The VS Server is Ready!
[root@lvs 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 10.0.0.100/32 scope global lo:1
      valid_lft forever preferred_lft forever
  inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:dc:d4:9d brd ff:ff:ff:ff:ff:ff
  inet 192.168.16.8/24 brd 192.168.16.255 scope global noprefixroute eth0
      valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:fedc:d49d/64 scope link
      valid_lft forever preferred_lft forever
     
[root@lvs network-scripts]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
 -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.100:80 wrr
 -> 192.168.16.17:80             Route   1      0          0        
 -> 192.168.16.27:80             Route   1      0          0      

4.准备后端服务器RS1,RS2

#准备RS1
[root@rs1 ~]# yum -y install httpd;systemctl enable --now httpd
[root@rs1 ~]# echo `hostname` > /var/www/html/index.html
[root@rs1 ~]# vim lvs.sh
#!/bin/bash
vip=10.0.0.100
mask='255.255.255.255'
dev=lo:1
#rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
#service httpd start &> /dev/null && echo "The httpd Server is Ready!"
#echo "`hostname -I`" > /var/www/html/index.html
case $1 in
start)
 echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
 echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
 echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
 echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $dev $vip netmask $mask #broadcast $vip up
 echo "The RS Server is Ready!"
;;
stop)
ifconfig $dev down
 echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
 echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
 echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
 echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
 echo "The RS Server is Canceled!"
;;
*)
 echo "Usage: $(basename $0) start|stop"
 exit 1
;;
esac

[root@rs1 ~]# bash lvs.sh start
The RS Server is Ready!
[root@rs1 ~]# 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 10.0.0.100/32 scope global lo:1
      valid_lft forever preferred_lft forever
  inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:ec:26:69 brd ff:ff:ff:ff:ff:ff
  inet 192.168.16.17/24 brd 192.168.16.255 scope global noprefixroute eth0
      valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:feec:2669/64 scope link
      valid_lft forever preferred_lft forever

#准备RS2
[root@rs2 ~]# yum -y install httpd;systemctl enable --now httpd;
[root@rs2 ~]#echo `hostname` > /var/www/html/index.html
[root@rs2 ~]# vim lvs.sh
#!/bin/bash
vip=10.0.0.100
mask='255.255.255.255'
dev=lo:1
#rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
#service httpd start &> /dev/null && echo "The httpd Server is Ready!"
#echo "`hostname -I`" > /var/www/html/index.html
case $1 in
start)
 echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
 echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
 echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
 echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $dev $vip netmask $mask #broadcast $vip up
 echo "The RS Server is Ready!"
;;
stop)
ifconfig $dev down
 echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
 echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
 echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
 echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
 echo "The RS Server is Canceled!"
;;
*)
 echo "Usage: $(basename $0) start|stop"
 exit 1
;;
esac
[root@rs2 ~]# bash lvs.sh start
The RS Server is Ready!
[root@rs2 ~]# 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 10.0.0.100/32 scope global lo:1
      valid_lft forever preferred_lft forever
  inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  link/ether 00:0c:29:95:6f:d4 brd ff:ff:ff:ff:ff:ff
  inet 192.168.16.27/24 brd 192.168.16.255 scope global noprefixroute eth0
      valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:fe95:6fd4/64 scope link
      valid_lft forever preferred_lft forever

5.客户端测试

[root@cip ~]#curl 10.0.0.100
rs1
[root@cip ~]#curl 10.0.0.100
rs2
[root@cip ~]#curl 10.0.0.100
rs1
[root@cip ~]#curl 10.0.0.100
rs2

2、CentOS7.6 中 nfs 客户端使用 /etc/fatab 实现开机自动挂载

环境:
10.0.0.7   服务器端
10.0.0.17   客户端
#服务器端配置
#安装包
[root@centos7 ~]#yum -y install nfs-utils
#开机自启
[root@centos7 ~]#systemctl enable --now nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@centos7 ~]#
#创建共享目录
[root@centos7 ~]# mkdir -pv /data/www
mkdir: created directory '/data/www'
[root@centos7 ~]# setfacl -m u:nobody:rwx /data/www
#实现共享
[root@centos7 ~]# vim /etc/exports
/data/www   *(rw,all_squash,async)
#重新加载
[root@centos7 ~]#exportfs -r
[root@centos7 ~]# exportfs -v
/data/www       <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,root_squash,no_all_squash)

#客户端配置
[root@centos17 ~]#yum -y install nfs-utils
[root@centos17 ~]#systemctl enable --now nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@centos17 ~]#showmount -e 10.0.0.7
Export list for 10.0.0.7:
/data/www *

#实现开机自动挂载
[root@backup ~]# cat /etc/fstab
10.0.0.7:/data/www     /data/backup                     nfs     _netdev,noexec        0 0

3、CentOS7.6 中 nfs 客户端使用 autofs 实现使用 nfs 时实时挂载

环境:
10.0.0.7   服务器端
10.0.0.17   客户端
#服务器端配置
#安装包
[root@centos7 ~]#yum -y install nfs-utils
#开机自启
[root@centos7 ~]#systemctl enable --now nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@centos7 ~]#
#创建共享目录
[root@centos7 ~]# mkdir -pv /data/www
mkdir: created directory ‘/data/www’
[root@centos7 ~]# setfacl -m u:nobody:rwx /data/www
#实现共享
[root@centos7 ~]# vim /etc/exports
/data/www   *(rw,all_squash,async)
#重新加载
[root@centos7 ~]#exportfs -r
[root@centos7 ~]# exportfs -v
/data/www       <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,root_squash,no_all_squash)

#客户端配置
[root@centos17 ~]#yum -y install nfs-utils
[root@centos17 ~]#systemctl enable --now nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@centos17 ~]#showmount -e 10.0.0.7
Export list for 10.0.0.7:
/data/www *

#相对路径
[root@centos17 ~]#yum -y install autofs
[root@centos17 ~]#vim /etc/auto.master
/data   /etc/data.nfs
[root@centos17 ~]#vim /etc/data.nfs
backup  10.0.0.7:/data/www
[root@centos17 ~]#systemctl restart autofs
[root@centos17 ~]#df /data/backup
Filesystem         1K-blocks Used Available Use% Mounted on
10.0.0.7:/data/www  52403200 32896  52370304   1% /data/backup

#绝对路径
[root@centos17 ~]# vim /etc/auto.master
/-   /etc/data.backup
[root@centos17 ~]# cat /etc/data.backup
/data/backup -ftype=nfs,vers=3 10.0.0.7:/data/www

[root@centos17 ~]# df /data/backup
Filesystem         1K-blocks Used Available Use% Mounted on
10.0.0.7:/data/www  52403200 32896  52370304   1% /data/backup
[root@centos17 ~]# df
Filesystem                     1K-blocks   Used Available Use% Mounted on
devtmpfs                           485816       0    485816   0% /dev
tmpfs                              497856       0    497856   0% /dev/shm
tmpfs                              497856    7824    490032   2% /run
tmpfs                              497856       0    497856   0% /sys/fs/cgroup
/dev/mapper/centos_centos7-root 104806400 1479884 103326516   2% /
/dev/sda1                         1038336  153532    884804  15% /boot
/dev/mapper/centos_centos7-data  52403200   32992  52370208   1% /data
tmpfs                               99572       0     99572   0% /run/user/0
10.0.0.7:/data/www               52403200   32896  52370304   1% /data/backup



上一篇:Java性能优化之编程技巧总结


下一篇:多线程-线程间通信-等待唤醒机制