preface

公司要求http+mysql+redis+二次开发的ldap要求做高可用,所以此处写写keepalived在这种

环境下的高可用。keepalived这个软件我就不啰嗦了,众所周知,基于VRRP协议做的高可用,VRRP就是virtual route protocol。把这个协议了解透了,keepalived自然也就明白怎么回事了。

实践

环境如下

ip 角色
172.16.160.189 master
172.16.160.179 backup

两台服务器都安装好了http,mysql,redis,以及ldap,确保两台服务器的http,mysql,redis以及ldap服务正常运行。

接下来重点讲解keepalived的配置文件

master 上配置keepalived

配置文件如下:

[root@localhost script]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
router_id LVS_DEVEL
} vrrp_script monitor_server { # monitor_server是自定义名字
script "/etc/keepalived/script/check_server.sh" # 设定的脚本,脚本内容下面会贴出来
interval 1 #探测间隔,实际也是脚本的执行间隔
weight 0 # 权重,脚本执行后的返回值会与这个做加减法,然后再把值与下面vrrp_instance的权重值做加减。
} vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51 # 与备份机的id值必须一样的。
priority 100 # 权重值
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.160.250
}
track_script {
monitor_server # 调用脚本模块
}
}
virtual_server 172.16.160.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol http # 因为这里要访问http协议,所以protocol写成http,这样浏览器才能访问,不然写成TCP的话,浏览器没法访问。 # real_server 172.16.160.179 80 {
# weight 100
# HTTP_CHECK {
# connect_timeout 2 #(10秒无响应超时)
# nb_get_retry 1
# delay_before_retry 1
# connect_port 80
# }
# }
# real_server 172.16.160.189 80 {
# weight 100
# HTTP_CHECK {
# connect_timeout 2 #(10秒无响应超时)
# nb_get_retry 1
# delay_before_retry 1
# connect_port 80
# }
# }
}
backup上的配置
[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
router_id LVS_DEVEL1 # 本机id
} vrrp_script monitor_server{
script "/etc/keepalived/script/check_server.sh" # 同上
interval 1
weight 0
} vrrp_instance VI_1 {
state BACKUP #角色改为backup
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 90 # 权重值调小即可
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.160.250
}
track_script {
monitor_server
}
}
virtual_server 172.16.160.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol HTTP # real_server 172.16.160.179 80 {
# weight 100
# TCP_CHECK {
# connect_timeout 2 #(10秒无响应超时)
# nb_get_retry 1
# delay_before_retry 1
# connect_port 80
# }
# }
# real_server 172.16.160.189 80 {
# weight 100
# TCP_CHECK {
# connect_timeout 2 #(10秒无响应超时)
# nb_get_retry 1
# delay_before_retry 1
# connect_port 80
# }
# }
}
脚本内容

脚本编写思路很简单,检测每个服务是否在,如果不在,尝试脚本启动,启动不了的话,停止keepalived,这样vip就切换走了。

function judge_ge_1 {
if [ "$1" -ge "1" ];then
export result="true"
else
export result="false"
fi
} function stop_keepalived {
if [ "$1" == "false" ];then
service keepalived stop
fi
} ################# check redis
exist_port=`netstat -lnpt |grep -v grep | grep 6379 |wc -l`
judge_ge_1 $exist_port
if [ "$result" == "false" ];then
/etc/init.d/redis start
fi
exist_port=`netstat -lnpt |grep -v grep | grep 6379 |wc -l`
judge_ge_1 $exist_port
stop_keepalived $result ###################### check mysql
exist_port=`netstat -lnpt |grep -v greo | grep 3306 |wc -l`
judge_ge_1 $exist_port
if [ "$result" == "false" ];then
service mysqld restart
fi
exist_port=`netstat -lnpt |grep -v greo | grep 3306 |wc -l`
judge_ge_1 $exist_port
stop_keepalived $result ################### check http
exist_port=`netstat -lnpt |grep -v grep | grep httpd |wc -l`
judge_ge_1 $exist_port
if [ "$result" == "false" ];then
service httpd restart
fi
exist_port=`netstat -lnpt |grep -v grep | grep httpd |wc -l`
judge_ge_1 $exist_port
stop_keepalived $result [root@localhost script]# chmod 777 /etc/keepalived/script/check_server.sh # 最后不要忘记添加777的权限,保证具有可执行权限

有问题请随时与我联系,18500777133@sina.cn

http+mysql结合keepalived做热备的更多相关文章

  1. MySQL高可用性之Keepalived+MySQL(双主热备)

    环境描述:OS:CentOS6.5_X64MASTER:192.168.0.202BACKUP:192.168.0.203VIP:192.168.0.204 1.配置两台Mysql主主同步[root@ ...

  2. 【Nginx】如何基于主从模式搭建Nginx+Keepalived双机热备环境?这是最全的一篇了!!

    写在前面 最近出版了<海量数据处理与大数据技术实战>,详情可以关注 冰河技术 微信公众号,查看<我的<海量数据处理与大数据技术实战>出版啦!>一文. 也有不少小伙伴 ...

  3. Keepalived双机热备

    一,Keepalived双机热备的应用场景 1,网站流量不高,压力不大,但是对服务器的可靠性要求极其高,例如实时在线OA系统,政府部门网站系统,医院实时报医系统,公安局在线报案系统,股市后台网站系统等 ...

  4. keepalived双机热备nginx

    nginx目前是我最常用的反向代理服务,线上环境为了能更好的应对突发情况,一般会使用keepalived双机热备nginx或者使用docker跑nginx集群,keepalived是比较传统的方式,虽 ...

  5. Mycat+Mysql主从复制实现双机热备

    Mycat+Mysql主从复制实现双机热备 一.mysql主从配置原理 双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据 ...

  6. MySQL 5.6 双机热备windows7

    MySQL 5.6 双机热备 目录: 1.说明 2.数据手工同步 3.修改主数据库配置文件 4.修改从数据库配置文件 5.主数据库添加备份用户 6.从数据库设置为Slave 7.验证 1.说明 1)数 ...

  7. Nginx+keepalived双机热备(主主模式)

    之前已经介绍了Nginx+Keepalived双机热备的主从模式,今天在此基础上说下主主模式的配置. 由之前的配置信息可知:master机器(master-node):103.110.98.14/19 ...

  8. Nginx+keepalived 双机热备(主主模式)

    之前已经介绍了Nginx+Keepalived双机热备的主从模式,今天在此基础上说下主主模式的配置. 由之前的配置信息可知:master机器(master-node):103.110.98.14/19 ...

  9. keepalived双机热备,安装部署文档

    keepalived双击热备,安装部署文档: 下载目录:/apps/keepalived-1.2.7.tar.gz 1:---> yum install -y make wget 2:---&g ...

随机推荐

  1. Adobe Reader & PDF 护眼设置

    1.首先选择“编辑”--->“首选项” 选择其他颜色,把RGB如下设置

  2. SQL Server output子句用法 output inserted.id 获取刚插入数据的id

    --插入数据,并返回刚刚插入的数据id INSERT INTO [soloreztest] ([name]) output inserted.id VALUES ('solorez') --执行结果: ...

  3. [HDU5904]LCIS(DP)

    题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 n,m<=1e5,a[i],b[i]<=1e6分析:dp[i]表示以数字i结尾的序列最长长度 dp[a ...

  4. [CF #236 (Div. 2) E] Strictly Positive Matrix(强联通分量)

    题目:http://codeforces.com/contest/402/problem/E 题意:给你一个矩阵a,判断是否存在k,使得a^k这个矩阵全部元素都大于0 分析:把矩阵当作01矩阵,超过1 ...

  5. C# WinForm捕获全局异常

    网上找的C# WinForm全局异常捕获方法,代码如下: static class Program { /// <summary> /// 应用程序的主入口点. /// </summ ...

  6. 发短信的简单实现——C#版

    为了验证操作人的身份,界面中通常会有获取验证码的功能.及点击获取验证码就会往你输入的手机号里面发送一条短信进行验证. 最近公司给我的任务中也包含这个功能,那么接下来就让我讲解下. ---------- ...

  7. IOS自学

    初识IOS 开发工具:xcode , 第一步学习c 打开xcode 新建一个object #include<stdio.h>//引入一个库,支持pringf输出功能 /* this is ...

  8. 【CSS】 background

    background: #22b4ff //背景色 url("http://images.cnblogs.com/cnblogs_com/oiliu/529256/o_titleIMG.jp ...

  9. 【原创】你知道OneNote的OCR功能吗?office lens为其增大威力,中文也识别

    OneNote提供了强大的从图片中取出文字的功能,大家只要装上了桌面版OneNote(本人用的2013版和win8.1版测试的,其他版本为测),将图片放在OneNote笔记中,右键图片即可把图片中的文 ...

  10. SQL SERVER 2008向ORACLE 11G迁移示例

    来源于:http://www.cnblogs.com/hiizsk/ 由SQL SERVER 2008向ORACLE 11G迁移过程记录之一-表 使用Oracle Sql Developer将SQL ...