http+mysql结合keepalived做热备
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做热备的更多相关文章
- MySQL高可用性之Keepalived+MySQL(双主热备)
环境描述:OS:CentOS6.5_X64MASTER:192.168.0.202BACKUP:192.168.0.203VIP:192.168.0.204 1.配置两台Mysql主主同步[root@ ...
- 【Nginx】如何基于主从模式搭建Nginx+Keepalived双机热备环境?这是最全的一篇了!!
写在前面 最近出版了<海量数据处理与大数据技术实战>,详情可以关注 冰河技术 微信公众号,查看<我的<海量数据处理与大数据技术实战>出版啦!>一文. 也有不少小伙伴 ...
- Keepalived双机热备
一,Keepalived双机热备的应用场景 1,网站流量不高,压力不大,但是对服务器的可靠性要求极其高,例如实时在线OA系统,政府部门网站系统,医院实时报医系统,公安局在线报案系统,股市后台网站系统等 ...
- keepalived双机热备nginx
nginx目前是我最常用的反向代理服务,线上环境为了能更好的应对突发情况,一般会使用keepalived双机热备nginx或者使用docker跑nginx集群,keepalived是比较传统的方式,虽 ...
- Mycat+Mysql主从复制实现双机热备
Mycat+Mysql主从复制实现双机热备 一.mysql主从配置原理 双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据 ...
- MySQL 5.6 双机热备windows7
MySQL 5.6 双机热备 目录: 1.说明 2.数据手工同步 3.修改主数据库配置文件 4.修改从数据库配置文件 5.主数据库添加备份用户 6.从数据库设置为Slave 7.验证 1.说明 1)数 ...
- Nginx+keepalived双机热备(主主模式)
之前已经介绍了Nginx+Keepalived双机热备的主从模式,今天在此基础上说下主主模式的配置. 由之前的配置信息可知:master机器(master-node):103.110.98.14/19 ...
- Nginx+keepalived 双机热备(主主模式)
之前已经介绍了Nginx+Keepalived双机热备的主从模式,今天在此基础上说下主主模式的配置. 由之前的配置信息可知:master机器(master-node):103.110.98.14/19 ...
- keepalived双机热备,安装部署文档
keepalived双击热备,安装部署文档: 下载目录:/apps/keepalived-1.2.7.tar.gz 1:---> yum install -y make wget 2:---&g ...
随机推荐
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Docker总结(图片打开略慢请知晓)
- Java--剑指offer(3)
11.输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. a)使用Integer.toBinaryString(n);来计算得出二进制的字符串,然后使用for循环截取字符串是否为1 pu ...
- Android-动画简介
Android中动画分为3种: ween Animation:通过对场景里的对象不断做图像变换(平移.缩放.旋转)产生动画效果,即是一种渐变动画: 也称View动画:也叫渐变动画,针对View的动画, ...
- java-汉字转化拼音(纯java)
1.转换所有的拼音 import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Set; public cl ...
- 国外有哪些比较好的IT社区
作者:匿名用户链接:https://www.zhihu.com/question/35373320/answer/114468522来源:知乎著作权归作者所有,转载请联系作者获得授权. Github ...
- HTTP协议学习---(十)拓展-HTTPS协议
HTTPS(Hypertext Transfer Protocol over Secure Socket Layer,基于SSL的HTTP协议)使用了HTTP协议,但HTTPS使用不同于HTTP协议的 ...
- 【CodeForces 699D】Fix a Tree
dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1 ...
- bzoj4415&&bzoj4416&&bzoj4417:SHOI2013Day1题解
这场题好弱啊qwq 先发代码再填坑 坑已填qwq T1 bzoj4415 题目大意就是,有一个环,编号1-N,一开始指针在1,有一个长度为n的序列p,每次指针向后移pi个,然后把那个点删掉. 问所有点 ...
- python编程关键字
1.常见关键字 (带有操作含义的关键字) for:循环 in:成员比较运算符 if :如果分支 elif:如果分支 else:其他分支 while:循环 def:定义函数 class:定义类 glob ...