五台虚拟机,全部禁用selinux 关闭防火墙
两台lvs调度服务器
192.168.38.3 keepalived的主设备master
192.168.38.6 keepalived的从设备backup
两台web服务器
192.168.38.20
192.168.38.30
一台nfs服务器
192.168.38.60
虚拟地址192.168.38.66
一、配置两台lvs调度器
两台调度服务器相同配置如下
1, 关闭 arp 内核参数
vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
sysctl -p
2, yum 安装 keepalived ipvsadm
3, 修改 keepalived 配置文件
modprobe ip_vs //加载ipvsadm模块
vim /etc/keepalived/keepalived.conf //进入修改配置文件
第一台lvs
global_defs { router_id keep_20 //修改名称,两台lvs名称不能一样 } vrrp_instance VI_1 { state MASTER interface ens33 //改为ens33网卡 virtual_router_id 51 priority 100 //优先级 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.38.66 //虚拟地址 } } virtual_server 192.168.38.66 80 { //虚拟地址 80端口 delay_loop 6 lb_algo rr lb_kind DR //改为DR模式 # persistence_timeout 50 //加#注释掉 protocol TCP real_server 192.168.38.20 80 { web1的地址 80端口 weight 1 TCP_CHECK { //改为TCP_CHECK connect_port 80 //添加这条命令,其余删除 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.38.30 80 { //复制下来改为 web2地址 weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } |
第二台lvs配置 ,重复以上操作命令再修改配置文件
global_defs { router_id LVS_30 //修改名称 } vrrp_instance VI_1 { state BACKUP //改为BACKUP interface ens33 virtual_router_id 51 priority 90 //优先级改为比第一台低的数字 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.38.66 //虚拟地址 } } virtual_server 192.168.38.66 80 { delay_loop 6 lb_algo rr lb_kind DR #persistence_timeout 50 protocol TCP real_server 192.168.38.20 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.38.30 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } |
两台lvs启动服务
systemctl start keepalived
systemctl enable keepalived
ipvsadm -ln //查看节点
二、配置两台web服务器
1, 修改内核转发参数
vim /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
sysctl -p //开启服务
2,安装 httpd 服务 并修改配置文件 添加web 首页 启动服务
yum install -y httpd
echo "666" >/var/www/html/index.html //第一台web1
echo "8888" >/var/www/html/index.html //第二台web2
systemctl start httpd //启动服务
3,添加lo:0 回环地址 ,两台web服务器都添加
cd /etc/sysconfig/network-scripts/
cp ifcfg-lo ifcfg-lo:0
DEVICE=lo:0 IPADDR=192.168.10.222 NETMASK=255.255.255.255 ONBOOT=yes NAME=loopback:0 |
ifup lo:0 //启动lo:0网卡
ifconfig lo:0 //查看lo:0地址
4,为两台web 服务器添加回环路由
route add -host 192.168.10.222/32 dev lo:0
vim /etc/rc.local //进入配置开机启动路由
route -n //查看路由
5,用测试机测试网页 访问地址 192.168.38.66 //访问虚拟地址
测试机使用 nfs 服务器
三、配置nfs服务器
mount /dev/cdrom /media
安装软件 yum -y install nfs-utils rpcbind
mkdir -p /share/web1
mkdir -p /share/web2
vim /etc/exports
/share 192.168.38.0/24(rw) /share/web1 192.168.38.20(rw) /share/web2 192.168.38.30(rw) |
systemctl start nfs
systemctl start rpcbind
systemctl enable nfs
systemctl enable rpcbind
测试
showmount -e 192.168.38.60 //检测nfs本机地址
在两台web服务器里挂载,如果是最小化需要安装nfs-utils
showmount -e 192.168.38.60 //检测nfs地址
mount -t nfs 192.168.38.100:/share/web1 /var/www/html/ //web1挂载
mount -t nfs 192.168.38.100:/share/web2 /var/www/html/ //web2挂载
df -h //查看挂载点
返回nfs服务器里
cd /share
echo "web11111111" >web1/index.html
echo "web22222222" >web2/index.html
最后curl 192.168.38.66测试