NFS共享存储配置
—————————————————————————————————————华丽分割线———————————————————————————————————— NFS简介: NFS即Network File System,网络文件系统 安装使用前提:(1)依赖于RPC(远程过程调用) (2)需要安装nfs-utils(NFS共享发布和访问)、rpcbind(rpc支持)软件包 (3)系统服务:nfs、rpcbind (4)共享配置文件:/etc/exports —————————————————————————————————————————————————————————————————————————————————— ******(1)使用yum安装nfs-utils、rpcbind软件包 [root@nfs ~]# mount /dev/cdrom /media/cdrom/ mount: /dev/sr0 写保护,将以只读方式挂载 [root@nfs ~]# yum -y install nfs-utils rpcbind 。。。。。。 完毕! ******(2)设置为开机自启 [root@nfs ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. [root@nfs ~]# systemctl enable rpcbind ******(3)设置共享目录(配置文件为/etc/exports) [root@nfs ~]# mkdir -p /opt/web1 /opt/web2(创建共享目录) [root@nfs ~]# vim /etc/exports (编写配置文件) ———————————————————————————————————————— 语法结构为:目录位置 客户机地址(权限操作) 权限操作有:(1)rw 读写 (2)sync 同步写入 (3)no_用户名_squash 客户机以指定用户访问时赋予本地root权限 ———————————————————————————————————————— 写入: /opt/web1 192.168.100.2(rw,sync,no_root_squash) /opt/web2 192.168.100.3(rw,sync,no_root_squash) 保存退出 ******(4)启动nfs和rpcbind服务(要注意要先启动rpcbind在启动nfs) [root@nfs ~]# systemctl start rpcbind [root@nfs ~]# systemctl start nfs [root@nfs ~]# netstat -anpt | grep rpcbind (查看端口) tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 16313/rpcbind tcp6 0 0 :::111 :::* LISTEN 16313/rpcbind ******(5)查看本机发布的NFS共享目录 [root@nfs ~]# showmount -e (查看本机发布的共享目录) Export list for nfs: /opt/web2 192.168.100.3 /opt/web1 192.168.100.2 ———————————————————————————————————————— showmount -e 服务器ip地址 (从客户机上查看NFS服务器的共享情况,是从客户机上打的) ————————————————————————————————————————
Web1配置
******(1)安装httpd、nfs、rpcbind [root@web1 ~]# mount /dev/cdrom /media/cdrom/ mount: /dev/sr0 写保护,将以只读方式挂载 [root@web1 ~]# yum -y install httpd nfs-utils rpcbind 。。。。。。 完毕! ******(2)查看nfs服务器的共享情况 [root@web1 ~]# systemctl enable rpcbind (设置为开机自启) [root@web1 ~]# systemctl start rpcbind (开启rpcbind服务) [root@web1 ~]# showmount -e 192.168.100.4 (查看nfs共享服务器的共享情况) Export list for 192.168.100.4: /opt/web2 192.168.100.3 /opt/web1 192.168.100.2 ******(3)挂载nfs服务器的共享目录到本地的网页目录 [root@web1 ~]# mount 192.168.100.4:/opt/web1 /var/www/html/ [root@web1 ~]# df -hT (查看挂载情况) 文件系统 类型 容量 已用 可用 已用% 挂载点 devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 487M 0 487M 0% /dev/shm tmpfs tmpfs 487M 7.7M 479M 2% /run tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 17G 1.7G 16G 10% / /dev/sda1 xfs 1014M 137M 878M 14% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 /dev/sr0 iso9660 4.5G 4.5G 0 100% /media/cdrom 192.168.100.4:/opt/web1 nfs4 17G 1.7G 16G 10% /var/www/html ******(4)编写网页、配置自动挂载 [root@web1 ~]# echo "11111111111111111111" > /var/www/html/index.html (编写网页) [root@web1 ~]# echo "192.168.100.4:/opt/web1 /var/www/html nfs defaults,_netdev 0 0" >> /etc/fstab (配置自动挂载) ******(5)配置httpd开机自启并启动 [root@web1 ~]# systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. [root@web1 ~]# systemctl start httpd [root@web1 ~]# curl 127.0.0.1 (本地测试) 11111111111111111111
Web2配置
与Web1相同,网页编写不同即可,这里就不写步骤了 [root@web2 ~]# mount /dev/cdrom /media/cdrom/ mount: /dev/sr0 写保护,将以只读方式挂载 [root@web2 ~]# yum -y install httpd nfs-utils rpcbind 。。。。。。 完毕! [root@web2 ~]# systemctl enable rpcbind [root@web2 ~]# systemctl start rpcbind [root@web2 ~]# showmount -e 192.168.100.4 Export list for 192.168.100.4: /opt/web2 192.168.100.3 /opt/web1 192.168.100.2 [root@web2 ~]# mount 192.168.100.4:/opt/web2 /var/www/html/ [root@web2 ~]# df -hT 文件系统 类型 容量 已用 可用 已用% 挂载点 devtmpfs devtmpfs 475M 0 475M 0% /dev tmpfs tmpfs 487M 0 487M 0% /dev/shm tmpfs tmpfs 487M 7.7M 479M 2% /run tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 17G 1.7G 16G 10% / /dev/sda1 xfs 1014M 137M 878M 14% /boot tmpfs tmpfs 98M 0 98M 0% /run/user/0 /dev/sr0 iso9660 4.5G 4.5G 0 100% /media/cdrom 192.168.100.4:/opt/web2 nfs4 17G 1.7G 16G 10% /var/www/html [root@web2 ~]# echo "2222222222222222" > /var/www/html/index.html [root@web2 ~]# echo "192.168.100.4:/opt/web2 /var/www/html nfs defaults,_netdev 0 0" >> /etc/fstab [root@web2 ~]# systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. [root@web2 ~]# systemctl start httpd [root@web2 ~]# curl 127.0.0.1 2222222222222222
测试
这个时候可以先看一下nfs共享存储中是否共享成功 [root@nfs ~]# cat /opt/web1/index.html 11111111111111111111 [root@nfs ~]# cat /opt/web2/index.html 2222222222222222 发现已经共享成功
打开客户机win7,访问200.0.0.1调度器的外网网址
如果做到最后,win7测试无法访问,那么要看一下调度器的配置是不是错了,使用ipvsadm -ln 查看TCP的那一行是否是外网地址,我的外网地址是200.0.0.1:80
三、搭建LVS负载均衡群集(DR模式)
因为net模式是把调度器当作网关,所以当数据量大的时候容易造成网络瓶颈,而TUN模式浪费了太多的公网ip资源,工作中很少使用TUN模式的,所以在工作中最常用的是DR模式
这里的基础配置和NAT模式是相同的,所以就不做解释了
(1)环境修改
名称 | ip地址 | 扮演角色 | 网卡 |
LVS | 192.168.100.1 200.0.0.1 | LVS负载调度器 | ens33(VM1网卡) ens37(VM2网卡) |
Web1 | 192.168.100.2 200.0.0.10 | web服务器 | ens33(VM1网卡)ens37(VM2网卡) |
Web2 | 192.168.100.3 200.0.0.20 | web服务器 | ens33(VM1网卡)ens37(VM2网卡) |
NFS | 192.168.100.4 | nfs共享存储 | ens33(VM1网卡) |
win7 | 200.0.0.2 | 客户机 | VM2网卡 |
Web群集ip为200.0.0.100,可以多加几台节点服务器,但是都必须是双网卡
(2)实验步骤(NAT模式的基础上)
LVS调度器配置
做完NAT模式的配置即可,然后配置群集接口(即安装ipvsadm等软件) ******(1)配置群集接口(虚拟接口) [root@lvs ~]# cd /etc/sysconfig/network-scripts/ [root@lvs network-scripts]# cp ifcfg-ens37 ifcfg-ens37:0 (复制外网网卡配置到群集接口) [root@lvs network-scripts]# vim ifcfg-ens37:0 修改为: 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=ens37:0 DEVICE=ens37:0 ONBOOT=yes IPADDR=200.0.0.100 PREFIX=24 IPV6_PRIVACY=no 保存退出 [root@lvs network-scripts]# ifup ens37:0 (开启新编写的群集接口) [root@lvs network-scripts]# ifconfig ens37:0 (查看是否配置成功) ens37:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 200.0.0.100 netmask 255.255.255.0 broadcast 200.0.0.255 ether 00:0c:29:44:ad:e5 txqueuelen 1000 (Ethernet) [root@lvs network-scripts]# cd ******(2)调整/proc内核参数,关闭重定向响应 [root@lvs ~]# vim /etc/sysctl.conf 修改为: # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.ens37.send_redirects = 0 保存退出 [root@lvs ~]# sysctl -p (立即应用策略) net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.ens37.send_redirects = 0 ******(3)配置LVS-DR的配置策略 [root@lvs ~]# ipvsadm -C (清空策略) [root@lvs ~]# ipvsadm -ln (查看策略,确认已经成功清空) IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn [root@lvs ~]# ipvsadm -A -t 200.0.0.100:80 -s rr (编写策略) [root@lvs ~]# ipvsadm -a -t 200.0.0.100:80 -r 200.0.0.10:80 -g -w 1[root@lvs ~]# ipvsadm -a -t 200.0.0.100:80 -r 200.0.0.20:80 -g -w 1 [root@lvs ~]# ipvsadm-save >/etc/sysconfig/ipvsadm (保存策略) [root@lvs ~]# cat /etc/sysconfig/ipvsadm (确认无误) -A -t lvs:http -s rr -a -t lvs:http -r 200.0.0.10:http -g -w 1 -a -t lvs:http -r 200.0.0.20:http -g -w 1 [root@lvs ~]# systemctl enable ipvsadm [root@lvs ~]# ipvsadm -ln (再次查看策略) IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 200.0.0.100:80 rr -> 200.0.0.10:80 Route 1 0 0 -> 200.0.0.20:80 Route 1 0 0
Web1配置
******(1)配置新添加的网卡 [root@web1 ~]# cd /etc/sysconfig/network-scripts/ [root@web1 network-scripts]# cp ifcfg-ens33 ifcfg-ens37 [root@web1 network-scripts]# vim ifcfg-ens37 [root@web1 network-scripts]# systemctl restart network [root@web1 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:bc:67:07 brd ff:ff:ff:ff:ff:ff inet 192.168.100.2/24 brd 192.168.100.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::2ff4:55fa:6c3d:65e0/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:bc:67:11 brd ff:ff:ff:ff:ff:ff inet 200.0.0.10/24 brd 200.0.0.255 scope global noprefixroute ens37 valid_lft forever preferred_lft forever inet6 fe80::b4fc:9eac:9686:e7e9/64 scope link noprefixroute valid_lft forever preferred_lft forever ******(2)配置群集接口 [root@web1 network-scripts]# vim ifcfg-lo:0 (配置群集接口) 写入: DEVICE=lo:0 ONBOOT=yes IPADDR=200.0.0.100 (地址填群集地址,即调度器ens37:0的地址) NETMASK=255.255.255.255 (这里的掩码必须全为255) 保存退出 [root@web1 network-scripts]# ifup ifcfg-lo:0 (启用网卡) [root@web1 network-scripts]# ifconfig lo:0 (查看是否配置成功) lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 200.0.0.100 netmask 255.255.255.255 loop txqueuelen 1000 (Local Loopback) [root@web1 network-scripts]# cd ******(3)调整/proc内核参数,关闭ARP响应 [root@web1 ~]# vim /etc/sysctl.conf 修改: # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.default.arp_ignore = 1 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 保存退出 [root@web1 ~]# sysctl -p (立即生效) net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.default.arp_ignore = 1 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 ******(4)添加到群集ip地址的本地路由记录 [root@web1 ~]# vim /etc/rc.local 修改: #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # 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 /sbin/route add -host 200.0.0.100 dev lo:0 保存退出 [root@web1 ~]# route add -host 200.0.0.100 dev lo:0
Web2配置(与Web1相同)
[root@web2 ~]# cd /etc/sysconfig/network-scripts/ [root@web2 network-scripts]# cp ifcfg-ens33 ifcfg-ens37 [root@web2 network-scripts]# vim ifcfg-ens37 修改为: 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=ens37 DEVICE=ens37 ONBOOT=yes IPADDR=200.0.0.20 PREFIX=24 IPV6_PRIVACY=no 保存退出 [root@web2 network-scripts]# systemctl restart network [root@web2 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:ab:11:5b brd ff:ff:ff:ff:ff:ff inet 192.168.100.3/24 brd 192.168.100.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::32ab:5b77:3fc:bf01/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:ab:11:65 brd ff:ff:ff:ff:ff:ff inet 200.0.0.20/24 brd 200.0.0.255 scope global noprefixroute ens37 valid_lft forever preferred_lft forever inet6 fe80::ccb4:e801:7f4d:af22/64 scope link noprefixroute valid_lft forever preferred_lft forever [root@web2 network-scripts]# cat <<aa> ifcfg-lo:0 > DEVICE=lo:0 > ONBOOT=yes > IPADDR=200.0.0.100 > NETMASK=255.255.255.255 > aa [root@web2 network-scripts]# ifup ifcfg-lo:0 [root@web2 network-scripts]# ifconfig lo:0 lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 200.0.0.100 netmask 255.255.255.255 loop txqueuelen 1000 (Local Loopback) [root@web2 network-scripts]# cat <<aa>> /etc/sysctl.conf > net.ipv4.conf.all.arp_ignore = 1 > net.ipv4.conf.all.arp_announce = 2 > net.ipv4.conf.default.arp_ignore = 1 > net.ipv4.conf.default.arp_announce = 2 > net.ipv4.conf.lo.arp_ignore = 1 > net.ipv4.conf.lo.arp_announce = 2 > aa [root@web2 network-scripts]# sysctl -p net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.default.arp_ignore = 1 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 [root@web2 network-scripts]# echo "/sbin/route add -host 200.0.0.100 dev lo:0" >> /etc/rc.local [root@web2 network-scripts]# route add -host 200.0.0.100 dev lo:0
NFS共享存储无需修改
测试
使用客户机Win7测试,查看是否来回切换
第一次访问
第二次访问
至此LVS负载均衡群集的DR模式搭建完成!!!!