集群介绍keepalived介绍用keepalived配置高可用集群
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集群介绍keepalived介绍用keepalived配置高可用集群相关的知识,希望对你有一定的参考价值。
集群介绍
keepalived介绍
用keepalived配置高可用集群
keepalived他需要包含一个服务,比如这个服务用来实现高可用,最早讲述集群的时候一个角色可能存在单点故障,如果这个服务当机就有问题了,现在想让这个服务高可用就得用一个keepalived或者heartbeat等高可用的软件,让他实现高可用
搭建了高可用的集群首先要有个keepalived的服务然后还要有让那个服务去实现高可用,这里用nginx做高可用的实验
为什么用nginx做实验对象,因为在工作中或在生产环境中好多企业把nginx作为负载均衡器,后面讲负载均衡集群的时候会介绍到nginx他可以实现负载均衡
准备两台机器192.168.63.100和192.168.63.101,192.168.63.100作master,192.168.63.101作为backup
两台机器都执行yum install -y keepalived
两台机器都安装nginx,其中100上已经编译安装过nginx,101上需要yum安装nginx: yum install -y nginx
在101上安装nginx的时候提示没有可用软件包 nginx。错误:无须任何处理,这个因为centos7系统库中默认是没有nginx的rpm包的,所以我们自己需要先更新下rpm依赖库
[[email protected] ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm ##更新rpm依赖库
以上操作完成后修改100上keepalived的配置文件,配置文件内容去https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/master_keepalived.conf获取
[[email protected] ~]# ls /etc/keepalived/keepalived.conf ##配置文件路径
/etc/keepalived/keepalived.conf
[[email protected] ~]# > !$ ##清空配置文件的原始内容
> /etc/keepalived/keepalived.conf
[[email protected] ~]# vim /etc/keepalived/keepalived.conf ##进入配置文件编辑,把以下内容拷贝到这个配置文件里
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
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 {
192.168.188.100
}
track_script {
chk_nginx
}
}
130上定义检查nginx是否正常的脚本,内容从https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/master_check_ng.sh获取
[[email protected] ~]# vim /usr/local/sbin/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,并且再次检测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
写完脚本做权限如果不去改这个他就没有权限自动加载这个脚本
[[email protected] ~]# chmod 755 /usr/local/sbin/check_ng.sh
100上启动服务
[[email protected] ~]# systemctl start keepalived
关闭nginx服务自动起来keep成功
查看日志
[[email protected] ~]# less /var/log/messages
开始配置从,在配置从之前先看防火墙
从上修改配置文件内容从https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/backup_keepalived.conf获取
[[email protected] ~]# vim /etc/keepalived/keepalived.conf ##写入一下内容
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
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 {
192.168.188.100
}
track_script {
chk_nginx
}
}
定义检查nginx是否正常的脚本内容从https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/backup_check_ng.sh获取
[[email protected] ~]# vim /usr/local/sbin/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,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
systemctl start nginx
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
给脚本755权限
[[email protected] ~]# chmod 755 /usr/local/sbin/check_ng.sh
启动keepalived服务
[[email protected] ~]# systemctl start keepalived
删除nginx自动启动成功
区分主从的nginx
主默认页
从,是yum安装的默认页
访问63.200的默认页
下面做测试高可用的时候访问的是vip的ip是192.168.63.200
停掉Keepalived查看是否高可用成功
以前主没事的时候vip显示主的页面,主出现问题后跳转到从的页面设置高可用设置成功
主上恢复后,从上不运行vip的ip了一切恢复正常
高可用集群测试完毕
以上是关于集群介绍keepalived介绍用keepalived配置高可用集群的主要内容,如果未能解决你的问题,请参考以下文章
集群介绍,keepalived介绍,使用keepalived配置高可用集群
六十集群介绍keepalived介绍用keepaliver配置高可用集群
集群介绍keepalived介绍用keepalived配置高可用集群
18.1集群介绍18.2keepalived介绍18.3-5用keepalived配置高可用集群