标签:emc usr head 地址 配置文件 rac 根据 global keepalive
集群从功能实现上分高可用和负载均衡:
高可用集群,即“HA"集群,也常称作“双机热备”。
当提供服务的机器宕机,备胎将接替继续提供服务;
实现高可用的开源软件有:heartbeat、keepalived。,keepalived还有负载均衡的功能。
核心原理都是通过心跳线连接两台服务器,一台用于提供服务,当这台机子宕机了,另外一台服务器顶替。;
keepalivied工作原理:
keepalived通过VRRP(Virtual Router Redundancy Protocl,虚拟路由冗余协议)来实现高可用。
环境: master:192.168.123.128 keepalive+Nginx
backup:192.168.123.129 keepalive+Nginx
VIP(Virtual IP):192.168.123.100 虚拟IP;
nginx的安装 在这里就不说了,这里主要说的不是这个;
安装keepalive:
yum install -y keepalived
修改配置文件:
vim /etc/keepalived/keepalived.conf
keepalived.conf内容如下:
global_defs { notification_email { 1012447978@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx{ script "/usr/local/sbin/check_ng.sh" interval 3 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass gys } virtual_ipaddress { 192.168.123.100 } track_script{ chk_nginx } }
自定义脚本,监控Nginx服务:
vim /usr/local/sbin/check_ng.sh
check_ng.sh脚本内容如下
#!/bin/bash # 时间变量 用于记录日志 d=`date --date today +%Y%m%d_%H:%M:%S` # 计算Nginx进程数量 n=`ps -C nginx --no-heading|wc -l` # 如果进程为0,则启动Nginx,并且再次检测进程数量 # 如果还为0,说明Nginx无法启动,此时需要关闭keepalived if [ $n -eq "0" ]; then /etc/init.d/nginx start n2=`ps -C nginx --no-heading|wc -l` if [ $n2 -eq "0" ];then echo "$d nginx down,keepalived will stop">> /var/log/check_ng.log systemctl stop keepalived fi fi
附权,否则keepalived无法调用
chmod 777 /usr/local/sbin/check_ng.sh
启动keepalived
systemctl start keepalived
根据上面的配置文件;如果本机的Nginx没有启动,keepalived启动的时候会把Nginx拉起来。
查看nginx是否起成功
ps -ef|grep nginx
查看keepalived是否起成功
ps -ef|grep keepalived
Master配置完成后,129上配置backup;
vim /etc/keepalived/keepalived.conf
内容如下:
global_defs { notification_email { gys@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx{ script "/usr/local/sbin/check_ng.sh" interval 3 } vrrp_instance VI_1 { state backup interface ens33 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass gys } virtual_ipaddress { 192.168.123.100 } track_script{ chk_nginx } }
监控脚本内容如下
#!/bin/bash # 时间变量 用于记录日志 d=`date --date today +%Y%m%d_%H:%M:%S` # 计算Nginx进程数量 n=`ps -C nginx --no-heading|wc -l` # 如果进程为0,则启动Nginx,并且再次检测进程数量 # 如果还为0,说明Nginx无法启动,此时需要关闭keepalived if [ $n -eq "0" ]; then /etc/init.d/nginx start n2=`ps -C nginx --no-heading|wc -l` if [ $n2 -eq "0" ];then echo "$d nginx down,keepalived will stop">> /var/log/check_ng.log systemctl stop keepalived fi fi
附权:
chmod 777 /usr/local/sbin/check_ng.sh
启动keepalived
systemctl start keepalived
本实例中,可以模拟出master宕机后,访问vip地址,可以过度到backup上,但是无法通过keepalived来拉起Nginx。
如果有人知道怎么回事,麻烦告诉我一声;
标签:emc usr head 地址 配置文件 rac 根据 global keepalive
原文地址:https://www.cnblogs.com/guoyansi19900907/p/9898756.html