标签:outer lov nfs 服务器 keepaliv oda 变量 软件 email
一、集群介绍三、用keepalived配置高可用集群
本人两台机器IP说明,linux-01机器的IP:192.168.238.128作为master ,linux-02机器的IP:192.168.238.130作为backup
[root@linux-01 ~]# yum install -y keepalived
[root@linux-02 ~]# yum install -y keepalived
两台机器都安装nginx,其中128上已经编译安装过nginx,130上需要yum安装nginx
[root@linux-02 ~]# yum install -y nginx
[root@linux-01 ~]# ls /etc/keepalived/keepalived.conf
/etc/keepalived/keepalived.conf
[root@linux-01 ~]# vi !$ //编辑128上的keepalived.conf配置文件
vi /etc/keepalived/keepalived.conf
[root@linux-01 ~]# vi /etc/keepalived/keepalived.conf
[root@linux-01 ~]# > !$ //清空文件内容快捷键 >,清空keepalived.conf配置文件内容
/etc/keepalived/keepalived.conf
[root@linux-01 ~]# vim /etc/keepalived/keepalived.conf //在配置文件中新增加如下内容
global_defs {
notification_email {br/>aming@aminglinux.com
}
notification_email_from root@aminglinux.com
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 aminglinux>com
}
virtual_ipaddress {
168.238.100
}
track_script {
chk_nginx
}
}
[root@linux-01 ~]# vim /usr/local/sbin/check_ng.sh //写checkng.sh检测脚本,脚本路径是在keepalived.conf配置文件中定义的路径,添加如下内容
!/bin/bash
时间变量,用于记录日志
d=`date --date today +%Y%m%d%H:%M:%S<br/>计算nginx进程数量<br/>n=
ps -C nginx --no-heading|wc -l<br/>如果进程为0,则启动nginx,并且再次检测nginx进程数量,<br/>如果还为0,说明nginx无法启动,此时需要关闭keepalived<br/>if [ $n -eq "0" ]; then<br/>/etc/init.d/nginx start<br/>n2=
ps -C nginx --no-heading|wc -l<br/>if [ $n2 -eq "0" ]; then<br/>echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log<br/>systemctl stop keepalived<br/>fi<br/>fi<br/>//解释脚本:d=
date --date today +%Y%m%d_%H:%M:%S表示时间;n=
ps -C nginx --no-heading|wc -l是计算nginx进程数;if [ $n -eq "0" ]; then 如果进程数等于0,什么nginx没有启动,需要启动nginx服务;n2=
ps -C nginx --no-heading|wc -l`再去检测进程数; if [ $n2 -eq "0" ]; then如果进程数还是0;echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log写一个日志指定到一个文件里去方便排查问题;systemctl stop keepalived停止keepalived服务,nginx没有启动,需要杀掉keepalived进程,防止“脑裂”,杀掉主上的keepalived是因为要启动从的keepalived,从上的keepalived会自动启动,如果主和从的keepalived同时都启动着,两者之间会争抢资源,出现紊乱,这种情况叫做“脑裂”
脑裂
脑裂(split-brain),指在一个高可用(HA)系统中,当联系着的两个节点断开联系时,本来为一个整体的系统,分裂为两个独立节点,这时两个节点开始争抢共享资源,结果会导致系统混乱,数据损坏。
对于无状态服务的HA,无所谓脑裂不脑裂;但对有状态服务(比如MySQL)的HA,必须要严格防止脑裂。(但有些生产环境下的系统按照无状态服务HA的那一套去配置有状态服务,结果可想而知...)
[root@linux-01 ~]# chmod 755 /usr/local/sbin/check_ng.sh //给脚本755权限
[root@linux-01 ~]# systemctl start keepalived //启动keepalived服务
[root@linux-01 ~]# ps aux |grep keep //查看进程有没有启动
[root@linux-01 ~]# less /var/log/messages //查看日志
[root@linux-01 ~]# ip add //查看IP
[root@linux-01 ~]# iptables -nvL //配置从之前检查防火墙
[root@linux-01 ~]# getenforce //检查selinux,配置从之前也要关闭selinux和防火墙
[root@linux-02 ~]# iptables -nvL //检查从的防火墙
[root@linux-02 ~]# systemctl stop firewalld //关闭防火墙
[root@linux-02 ~]# getenforce
[root@linux-02 ~]# setenforce 0 //临时关闭selinux
[root@linux-02 ~]# > /etc/keepalived/keepalived.conf //清空配置文件内容
[root@linux-02 ~]# vim /etc/keepalived/keepalived.conf //添加如下内容
global_defs {
notification_email {br/>aming@aminglinux.com
}
notification_email_from root@aminglinux.com
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 aminglinux>com
}
virtual_ipaddress {
}
track_script {
chk_nginx
}
}
[root@linux-02 ~]# vim /usr/local/sbin/check_ng.sh //编写checkng.sh脚本
时间变量,用于记录日志
d=`date --date today +%Y%m%d%H:%M:%S<br/>计算nginx进程数量<br/>n=
ps -C nginx --no-heading|wc -l<br/>如果进程为0,则启动nginx,并且再次检测nginx进程数量,<br/>如果还为0,说明nginx无法启动,此时需要关闭keepalived<br/>if [ $n -eq "0" ]; then<br/>systemctl start nginx<br/>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
[root@linux-02 ~]# chmod 755 /usr/local/sbin/check_ng.sh //修改权限
[root@linux-02 ~]# systemctl start keepalived //启动服务
[root@linux-02 ~]# ps aux |grep keep
在浏览器上访问linux-01机器的IP地址
[root@linux-01 ~]# cat /usr/local/nginx/conf/vhost/aaa.com.conf //查看128机器上nginx配置文件默认访问到哪里
[root@linux-01 ~]# cat /data/wwwroot/default/index.html //默认页在这里修改
[root@linux-02 ~]# cat /usr/local/nginx/html/index.html //linux-02nginx配置默认页路径
小知识:开启keepalived服务nginx服务会自动加载起来
[root@linux-01 ~]# systemctl stop keepalived //停掉master上的keepalived服务
[root@linux-01 ~]# ip add //可以查看到vip地址 192.168.238.100没有被master绑定了
[root@linux-02 ~]# ip add //看到backup机器上绑定了vip地址192.168.238.100
有的企业配置高可用的时候使用两台或者三台backup机器,这时候配置backup配置文件时需要注意每个backup配置的权重要不一样,权重越高,master宕机时使用backup的优先级越高,权重配置在配置文件中的priority 这个参数。
除了nginx高可用之外,也可以做mysql的高可用,mysql高可用必须保持两边数据一致,如果主机宕机,从机起来了数据不一致肯定会出问题
集群介绍 keepalived介绍 用keepalived配置高可用集群
标签:outer lov nfs 服务器 keepaliv oda 变量 软件 email
原文地址:http://blog.51cto.com/13669226/2136521