linux / Nginx · 2021年7月21日 0

Centos keepalive+apache高可用集群 实例


简单讲解实现web服务器高可用集群 不间断服务
仅供学习与交流,请勿用于商业用途

设备环境

虚拟路由:
网关: 192.168.123.2
DNS: 192.168.123.2
虚拟服务器
web服务器1:192.168.123.20/24
web服务器2:192.168.123.21/24
NFS网络文件系统存储服务器:192.168.123.24/24
每个服务器按需设定网络地址,主机名

NFS存储环境部署

配置网络,固定网络地址
(省略)
安装nfs

$ yum install nfs -y

配置nfs

$ mkdir /nfs
$ echo '某龙兄牛逼'>/nfs/index.html
$ echo '/nfs 192.168.123.*(rw,sync)'>/etc/exports

配置防火墙,放行nfs

$ firewall-cmd --permanent --add-service=rpc-bind --add-service=mountd --add-service=nfs

web服务器环境部署

web服务器 centos最小化安装
配置网络,固定网络地址
(省略几十行代码,同上)

安装vim,httpd,keepalive,net-tools,bash-completion

$ yum install vim httpd keepalive net-tools -y

防火墙放行httpd vrrp
VRRP放行规则详见红帽官方说明

$ firewall-cmd --permanent --add-service=http
$ firewall-cmd --reload
$ firewall-cmd --add-protocol=vrrp --permanent
$ #或者使用富规则限制ip源192.168.123.*使服务器更加安全
$ firewall-cmd --add-rich-rule='rule protocol value="vrrp" accept' --permanent

设置selinux布尔值使前端文件允许挂载到nfs存储上

$ setsebool -P httpd_use _nfs=on

挂载NFS到httpd默认目录

$ mount 192.168.123.24:/nfs /var/www/html

配置keepalive

cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak               #备用
scp keepalived.conf.bak root@192.168.123.21:/etc/keepalived/
vim keepalived.conf
! Configuration File for keepalived
#全局设置
global_defs {
#通告邮箱
notification_email {
root@localhost
}
script_user root
enable_script_security
notification_email_from root@loacalhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#可增加Shell监控脚本监控Apache服务
#否则只有机器断电才会触发备用机器,若服务器只是apache服务进程死亡,无法调用备用机
vrrp_script chk_apache_keepalived {
script "/etc/keepalived/chk_apache_keepalived.sh"
interval 2
weight 2
}

#VRRP
vrrp_instance VI_1 {
state MASTER                     
#主机写MASTER备用机写BACKUP
interface ens35                
#监听端口 不是链接名称
virtual_router_id 51         
#虚拟路由id 数字相同
priority 100               
#优先级 备用机写90
advert_int 1
##vrrp协议验证 保证两虚拟机一致
authentication {                
auth_type PASS
auth_pass long123
}
track_script {
chk_apache_keepalived
}
virtual_ipaddress {
192.168.123.19                #虚拟ip
}
}

主配置文件权限需设为644
编写监控脚本

vim /etc/keepalived/chk_apache_keepalived.sh
#!/bin/bash
# wdnmd
# power by moulongxiong
# the script for cheak apache service
systemctl is-active apache2 &>/dev/null
if [ $? -ne 0 ];then
systemctl stop keepalived
fi
#    systemctl restart apache2 
#    sleep 5
#    systemctl is-active apache2 &>/dev/null
#    if [ $? -ne 0 ];then
#        systemctl stop keepalived
#    fi

启动服务,并开机自启

systemctl enable nfs keepalive
systemctl start nfs keepalive

尝试访问服务

curl 192.168.123.19
某龙兄牛逼