目录
实验准备
主DR 服务器:192.168.88.22
备DR 服务器:192.168.88.40
Web 服务器1:192.168.88.50
Web 服务器2:192.168.88.51
vip:192.168.88.250
systemctl stop firewalld.service
setenforce 0
yum -y install ipvsadm keepalived
modprobe ip_vs
cat /proc/net/ip_vs
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
vim keepalived.conf
mtp_server 127.0.0.1 #10行修改,邮件服务指向本地
router_id LVS_01 #12行修改,指定服务器(路由器)的名称,主01,备02
#vrrp_strict #14行注释掉
state MASTER #20行修改,指定热备状态,主为MASTER,备为BACKUP
interface ens33 #21行修改,指定承载vip地址的物理接口
priority 100 #23行修改,指定优先级,主为100,备为90
virtual_ipaddress { #指定群集vip地址
192.168.88.250
virtual_server 192.168.88.250 80 #36行修改,指定虚拟服务器地址(VIP)、端口
delay_loop 6 #健康检查的间隔时间
lb_algo rr #指定调度算法,轮询(rr)
lb_kind DR #39行修改,指定群集工作模式,直接路由(DR)
persistence_timeout 0 #连接保持时间(秒)
real_server 192.168.88.50 80 #43行修改,指定第一个Web节点的地址、端口
##45行删除,添加以下健康检查方式
TCP_CHECK {
connect_port 80 #添加检查的目标端口
connect_timeout 3 #添加连接超时(秒)
nb_get_retry 3 #添加重试次数
delay_before_retry 3 #添加重试间隔
}
}
##添加第二个 Web节点的地址、端口
real_server 192.168.88.51 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
} #删除后面多余的配置
systemctl start keepalived
ip addr #查看虚拟网卡vip
##启动 ipvsadm 服务##
--192.168.88.22---
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
ipvsadm -ln
#如没有VIP 的分发策略,则重启 keepalived 服务,systemctl restart keepalived
--192.168.88.40---
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
ipvsadm -ln
##调整 proc 响应参数,关闭Linux 内核的重定向参数响应##
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
yum -y install httpd
systemctl start httpd
--192.168.88.50---
echo 'this is yy web!' > /var/www/html/index.html
--192.168.88.51---
echo 'this is cc web!' > /var/www/html/index.html
cd /etc/sysconfig/network-scripts
cp ifcfg-lo ifcfg-lo:0
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.88.250
NETMASK=255.255.255.255
ifup lo:0
ifconfig lo:0
route add -host 192.168.88.250 dev lo:0
vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
sysctl -p
systemctl start httpd
实验准备
主DR 服务器:192.168.88.22
备DR 服务器:192.168.88.40
Web 服务器1:192.168.88.50
Web 服务器2:192.168.88.51
vip:192.168.88.250
systemctl stop firewalld.service
setenforce 0
yum -y install httpd
systemctl start httpd
--192.168.88.50---
echo 'this is yy web!' > /var/www/html/index.html
--192.168.88.51---
echo 'this is cc web!' > /var/www/html/index.html
cd /etc/sysconfig/network-scripts
cp ifcfg-lo ifcfg-lo:0
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.88.250
NETMASK=255.255.255.255
ifup lo:0
ifconfig lo:0
route add -host 192.168.88.250 dev lo:0
vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
sysctl -p
systemctl start httpd
yum install -y keepalived
yum install -y nginx
vim /usr/local/nginx/conf/nginx.conf
stream{
upstream backends{
server 192.168.88.50:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.88.51:80 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 9527;
proxy_pass backends;
}
}
nginx -t
systemctl restart nginx.service
cd /etc/keepalived/
cp keepalived.conf{,.bak}
vim check_nginx.sh #编写监控脚本
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
systemctl restart nginx
B=`ps -C nginx --no-header |wc -l`
if [ $B -eq 0 ];then
systemctl stop keepalived
fi
fi
chmod +x check_nginx.sh
vim keepalived.conf
smtp_server 127.0.0.1 #10行修改
router_id NGINX_01 #12行修改
vrrp_script check_nginx {
cript "/etc/keepalived/check_nginx.sh"
interval 2
weight 1
} #14行添加
state MASTER #20行修改
interface ens33 #21行修改
virtual_ipaddress {
192.168.88.250
}
track_script {
check_nginx
}
} #29行添加
systemctl restart keepalived.service
?
yum install -y keepalived
yum install -y nginx
vim /usr/local/nginx/conf/nginx.conf
stream{
upstream backends{
server 192.168.88.50:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.88.51:80 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 9527;
proxy_pass backends;
}
}
nginx -t
systemctl restart nginx.service
cd /etc/keepalived/
cp keepalived.conf{,.bak}
vim check_nginx.sh #编写监控脚本
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
systemctl restart nginx
B=`ps -C nginx --no-header |wc -l`
if [ $B -eq 0 ];then
systemctl stop keepalived
fi
fi
chmod +x check_nginx.sh
vim keepalived.conf
smtp_server 127.0.0.1 #10行修改
router_id NGINX_02 #12行修改
vrrp_script check_nginx {
cript "/etc/keepalived/check_nginx.sh"
interval 2
weight 1
} #14行添加
state BACKUP #20行修改
interface ens33 #21行修改
virtual_ipaddress {
192.168.88.250
}
track_script {
check_nginx
}
} #29行添加
systemctl restart keepalived.service
ping -c 4 -i 0.5 -w 2 192.168.88.41 #ping41段看能否成功
if [ $? -ne 0 ];then #若不成功则远程连接到节点服务器ping备
ssh 192.168.88.50 ping -c 4 -i 0.5 -w 2 -I 192.168.88.50 192.168.88.41
if [ $? -eq 0 ];then #若成功成功则关闭keepalived服务
systemctl stop keepalived
else
exit #若不成功则退出
fi
fi