1.准备5台虚拟机,2台做LVS主备调度器,2台做web服务器,1台做存储,主机机验证
2.LVS主备调度器 master(192.168.1.127)backup(192.168.1.106)
3.web1(192.168.1.121)web2(192.168.1.118)
4.存储(192.168.1.15)
5.虚拟ip(192.168.1.111)
关闭防火墙
————————————————
开始
[root@localhost ~]# vim /etc/sysctl.conf
[root@localhost ~]# 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
yum -y install httpd
web1与web2分别写一个index.html用于测试(/var/www/html/ )httpd的网站根目录
echo "web1" >/var/www/html/index.html
echo "web1" >/var/www/html/index.html
web1与web2创建虚拟ip
cd /etc/sysconfig/network-scripts/
cp ifcfg-lo ifcfg-lo:0
修改配置文件创建虚拟IP
vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.1.111
NETMASK=255.255.255.255
NETWORK=127.0.0.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback:0
重启网卡
systemctl restart network
添加回环路由
[root@localhost network-scripts]# route add -host 192.168.1.111/32 dev lo:0
重启服务
systemctl start httpd
相同操作都是
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
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
安装
yum install -y keepalived ipvsadm
配置keepalived master
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL1 这里不一样
}
vrrp_instance master {
state MASTER 这里不一样
interface ens33
virtual_router_id 51
priority 100 这里不一样
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.111
}
}
virtual_server 192.168.1.111 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 192.168.1.121 80 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.118 80 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
bal_defs {
router_id LVS_DEVEL2
}
vrrp_instance backup {
state BACKUP
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.111
}
}
virtual_server 192.168.1.111 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 192.168.1.121 80 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.118 80 {
weight 1
HTTP_GET {
url {
path /
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
创建文件夹这是在Linux系统中创建一个名为ipvsadm的文件。该文件是用于配置IPVS(IP Virtual Server)的工具,IPVS是一个Linux内核中的负载均衡器。在该文件中,可以设置IPVS的参数和规则,以便实现负载均衡和高可用性。
touch /etc/sysconfig/ipvsadm
keepalived与ipvsadm
[root@localhost ~]# systemctl restart keepalived
[root@localhost ~]# systemctl restart ipvsadm
加载
ip_vs
模块modprobe ip_vs
ipvsadm -ln
[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.1.111:80 rr
-> 192.168.1.118:80 Route 1 0 0
-> 192.168.1.121:80 Route 1 0 0
测试(下面可以看到测试成功,自动切换web1与web2)
[root@localhost ~]# curl 192.168.1.111
web1
[root@localhost ~]# curl 192.168.1.111
web2
两个库测试阶段只有一个可以用
关闭主库
[root@localhost ~]# systemctl stop keepalived
[root@localhost ~]# systemctl stop ipvsadm
开启备库
[root@localhost ~]# systemctl restart keepalived
[root@localhost ~]# curl 192.168.1.111
web2
[root@localhost ~]# yum -y install nfs-utils 下载
[root@localhost ~]# vim /etc/exports 进入
/data/www/html/ 192.168.1.0/24(rw) 指定共享目录具备可读写权限
echo "web 1 2" >/data/www/html/index.html
将字符串 "web 1 2" 写入到 /data/www/html/index.html 文件中。这个文件将成为一个网站的主页,当用户访问该网站时,将会看到 "web 1 2" 这个字符串。
重启服务
systemctl start nfs
查看
[root@localhost network-scripts]# showmount -e 192.168.1.15
Export list for 192.168.1.15:
/data/www/html 192.168.1.0/24
[root@localhost ~]# mount 192.168.1.15:/data/www/html /var/www/html/
[root@localhost network-scripts]# curl 192.168.1.111
web 1 2