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. 20140207 - Java and Mac OS X Retina

    在Mac下使用文件管理工具类似Total Commander的muCommander,muCommander的编写语言是Java,打开后发现Java不兼容Mac Retina. muCommander ...

  2. matlab如何建立一个空矩阵,然后往里面赋值

    x=:; y=[]; :length(x) % y=[y;x(i)];%把每一个x都放到Y里,成为一列 y=[y,x(i)];%把每一个x都放到Y里,成为一行 end

  3. HTML问题集锦

    [1]HTML5怎么设置滚动字幕 <marquee direction=up behavior=scroll loop=3 scrollamount=1 scrolldelay=10 align ...

  4. windows设置开机启动项

    一.windows下设置开机启动有如下方法 1 注册表启动项目RUN 2 计划任务,在"windows管理">"计划任务管理器"中新建任务,在操作栏指定要 ...

  5. Ubuntu Terminal Shortcut

    Not all of the shortcuts are useful.Only remeber the most useful. 移动类Ctrl + a  - Jump to the start o ...

  6. Rest API 开发 学习笔记(转)

    Rest API 开发 学习笔记 概述 REST 从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表示方式.获得这些表徵致使这些应用程序转变了其状态.随着 ...

  7. ASP.NET利用WINRar实现在线解压缩文件

    一.肯定是服务器必须装了winrar这个软件了. 二.创建Helper类,如下: using System; using System.Collections.Generic; using Syste ...

  8. SpringMVC学习--拦截器

    简介 Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理. 拦截器定义 定义拦截器,实现HandlerInterceptor接 ...

  9. Shell命令_正则表达式

    正则表达式是包含匹配,通配符是完全匹配 基础正则表达式 test.txt示例文件 1 2 3 4 5 6 7 8 9 10 11 12 Mr. James said: he was the hones ...

  10. Entity Framework Code First (七)空间数据类型 Spatial Data Types

    声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...