构建高可用集群Keepalived+Haproxy负载均衡
重点概念
vrrp_script中节点权重改变算法
vrrp_script 里的script返回值为0时认为检测成功,其它值都会当成检测失败;
weight 为正时,脚本检测成功时此weight会加到priority上,检测失败时不加;
主失败:
主 priority < 从 priority + weight 时会切换。
主成功:
主 priority + weight > 从 priority + weight 时,主依然为主
weight 为负时,脚本检测成功时此weight不影响priority,检测失败时priority – abs(weight)
主失败:
主 priority – abs(weight) < 从priority 时会切换主从
主成功:
主 priority > 从priority 主依然为主
主要贴配置:VIP:10.16.37.198,10.16.37.199
web服务器IP:10.16.37.94,10.16.37.101
二台keepalived的IP:10.16.37.107,10.16.37.110
一台:
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
670196816@qq.com
}
notification_email_from admin@lnmp.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id Iptables
}
vrrp_script chk_maintaince_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight 2
}
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 1
weight 2
}
vrrp_instance VI_1 {
interface eth0
state MASTER
priority 100
virtual_router_id 125
garp_master_delay 1
authentication {
auth_type PASS
auth_pass 1e3459f77aba4ded
}
track_interface {
eth0
}
virtual_ipaddress {
10.16.37.198/22 dev eth0 label eth0:0
}
track_script {
chk_haproxy
}
notify_master "/etc/keepalived/notify.sh master 10.16.37.198"
notify_fault "/etc/keepalived/notify.sh fault 10.16.37.198"
}
vrrp_instance VI_2 {
interface eth0
state BACKUP
priority 99
virtual_router_id 126
grap_master_delay 1
authentication {
auth_type pass
auth_pass 7615c4b7f518cede
}
track_interface {
eth0
}
virtual_ipaddress {
10.16.37.199/22 dev eth0 label eth0:1
}
track_script {
chk_haproxy
chK_maintaince_down
}
notify_master "/etc/keepalived/notify.sh master 10.16.37.199"
notify_backup "/etc/keepalived/notify.sh backup 10.16.37.199"
notify_fault "/etc/keepalived/notify.sh fault 10.16.37.199"
}
另一台:
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
670196816@qq.com
}
notification_email_from admin@lnmp.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id Iptables
}
vrrp_script chk_maintaince_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight 2
}
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 1
weight 2
}
vrrp_instance VI_1 {
interface eth0
state BACKUP
priority 99
virtual_router_id 125
garp_master_delay 1
authentication {
auth_type PASS
auth_pass 1e3459f77aba4ded
}
track_interface {
eth0
}
virtual_ipaddress {
10.16.37.198/22 dev eth0 label eth0:1
}
track_script {
chk_haproxy
}
notify_master "/etc/keepalived/notify.sh master 10.16.37.198"
notify_fault "/etc/keepalived/notify.sh fault 10.16.37.198"
}
vrrp_instance VI_2 {
interface eth0
state MASTER
priority 100
virtual_router_id 126
grap_master_delay 1
authentication {
auth_type pass
auth_pass 7615c4b7f518cede
}
track_interface {
eth0
}
virtual_ipaddress {
10.16.37.199/22 dev eth0 label eth0:0
}
track_script {
chk_haproxy
chK_maintaince_down
}
notify_master "/etc/keepalived/notify.sh master 10.16.37.199"
notify_backup "/etc/keepalived/notify.sh backup 10.16.37.199"
notify_fault "/etc/keepalived/notify.sh fault 10.16.37.199"
}
脚本配置:
vi /etc/keepalived/notify.sh
#!/bin/bash
contact='root@localhost'
notify() {
mailsubject="'hostname' to be $1: $2 floating"
mailbody="'date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
master)
notify master $2
/etc/rc.d/init.d/haproxy restart
exit 0
;;
backup)
notify backup $2
exit 0
;;
fault)
notify fault $2
exit 0
;;
*)
echo 'Usage: 'basename $0' {master|backup|fault}'
exit 1
;;
Esac
Haproxy配置:
vi /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
#
# use_backend static if url_static
# default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
# balance roundrobin
# server app1 127.0.0.1:5001 check
# server app2 127.0.0.1:5002 check
# server app3 127.0.0.1:5003 check
# server app4 127.0.0.1:5004 check
listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats refresh 30s
maxconn 200
stats hide-version
stats uri /haproxy-stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
frontend http-in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .jpeg .gif .png .css .js .html
use_backend static_servers if url_static
default_backend dynamic_servers
backend static_servers
balance roundrobin
server imgsrv1 10.16.37.101:80 check maxconn 6000
server imgsrv2 10.16.37.94:80 check maxconn 6000
backend dynamic_servers
balance source
server websrv1 10.16.37.94:80 check maxconn 1000
server websrv2 10.16.37.101:80 check maxconn 1000
因为端口使用的是1080需要Iptables开启:
/sbin/iptables –I INPUT –p tcp –dport 1080 –j ACCEPT /etc/rc.d/init.d/iptables save Service iptables restart Vi /etc/selinux/config
关闭selinux然后呢重启!!
二个server web采用nginx+双主mysql数据库,保证了web服务器的高可用性能,一台服务器宕机,另外一台立马连接!!
参考:
http://www.it165.net/admin/html/201405/2957.html
构建高可用集群Keepalived+Haproxy负载均衡的更多相关文章
- Linux企业集群用商用硬件和免费软件构建高可用集群PDF
Linux企业集群:用商用硬件和免费软件构建高可用集群 目录: 译者序致谢前言绪论第一部分 集群资源 第1章 启动服务 第2章 处理数据包 第3章 编译内容 第二部分 高可用性 第4章 使用rsync ...
- Apache httpd和JBoss构建高可用集群环境
1. 前言 集群是指把不同的服务器集中在一起,组成一个服务器集合,这个集合给客户端提供一个虚拟的平台,使客户端在不知道服务器集合结构的情况下对这一服务器集合进行部署应用.获取服务等操作.集群是企业应用 ...
- lvs+keepalive构建高可用集群
大纲 一.前言 二.Keepalived 详解 三.环境准备 四.LVS+Keepalived 实现高可用的前端负载均衡器 一.前言 Keepalived使用的vrrp协议方式,虚拟路由 ...
- RabbitMQ集群使用Haproxy负载均衡
(1).下载 http://www.haproxy.org/#down (2).解压 tar -zxvf haproxy-1.5.18.tar.gz (3).安装 1).编译 make TARGET= ...
- 搭建 RabbitMQ Server 高可用集群
阅读目录: 准备工作 搭建 RabbitMQ Server 单机版 RabbitMQ Server 高可用集群相关概念 搭建 RabbitMQ Server 高可用集群 搭建 HAProxy 负载均衡 ...
- 搭建 RabbitMQ Server 高可用集群【转】
阅读目录: 准备工作 搭建 RabbitMQ Server 单机版 RabbitMQ Server 高可用集群相关概念 搭建 RabbitMQ Server 高可用集群 搭建 HAProxy 负载均衡 ...
- keepalived+nginx 高可用集群
一.什么是高可用? nginx做负载均衡,能达到分发请求的目的,但是不能很好的避免单点故障. 1.nginx集群单点问题 分发器宕机怎么处理? 假如nginx服务器挂掉了,那么所有的服务也会跟着瘫 ...
- MySQL8.0 MIC高可用集群搭建
mysql8.0带来的新特性,结合MySQLshell,不需要第三方中间件,自动构建高可用集群. mysql8.0作为一款新产品,其内置的mysq-innodb-cluster(MIC)高可用集群的技 ...
- 阿里云搭建k8s高可用集群(1.17.3)
首先准备5台centos7 ecs实例最低要求2c4G 开启SLB(私网) 这里我们采用堆叠拓扑的方式构建高可用集群,因为k8s 集群etcd采用了raft算法保证集群一致性,所以高可用必须保证至少3 ...
随机推荐
- win2012,oracle11g,sqlplus切换实例的方法
问题环境:windows 2012 r2 64位 ,oracle 11.2.0.4,多个实例. 在这种情况下, sqlplus "/as sysdba" 默认登录的是系统后面安装 ...
- 【《zw版·Halcon与delphi系列原创教程》 zw_halcon人脸识别
[<zw版·Halcon与delphi系列原创教程>zw_halcon人脸识别 经常有用户问,halcon人脸识别方面的问题. 可能是cv在人脸识别.车牌识别方面的投入太多了. 其实,人脸 ...
- N久没写过东西了..写个最近在研究的程序
import numpy as np import matplotlib.pyplot as plt #a = np.matrix([[1,1.15],[1,1.9],[1,3.06],[1,4.66 ...
- jqGrid APi 详解
jqGrid APi 详解 jqGrid皮肤 从3.5版本开始,jqGrid完全支持jquery UI的theme.我们可以从http://jqueryui.com/themeroller/下载我们所 ...
- Jquery remove 高级用法
Jquery remove 高级用法 html 代码 <div class="file-image">abc1111</div><div class= ...
- Azure Management API 之 利用 Windows Azure Management Libraries 来控制Azure platform
在此之前,我曾经发过一篇文章讲叙了如何利用Azure power shell team 提供的class library. 而就在这篇文章发布之后不久,我又发现微软发布了一个preview 版本的Wi ...
- 【译】Import Changes from Direct3D 11 to Direct3D 12
译者:林公子 出处:木木的二进制人生 转载请注明作者和出处,谢谢! 这是微软公布的Direct3D 12文档的其中一篇,此翻译留作学习记录备忘,水平有限,错漏难免,还望海涵. 原文链接是https:/ ...
- 非root用户 gcc安装
亲测 可以安装 过程并不复杂 但可能需要一些时间 认真一点 按照步骤 一定可以成功哒 其他版本可以将ftp.gnu.org/gnu/gcc/敲入浏览器,找到自己需要的文件:[安装过4.9.0:成功:用 ...
- iOS开发零碎知识点
记录一些常用和不常用的iOS知识点,防止遗忘丢失.(来源为收集自己项目中用到的或者整理看到博客中的知识点),如有错误,欢迎大家批评指正:如有好的知识点,也欢迎大家联系我,添加上去.谢谢! 一.调用代码 ...
- VS2010无法调试问题解决
最近,因为公司开发的需要,对开发环境进行全面的升级,在这其中也遇到了不少问题,在之后将陆续整理出来,以便以后查看. 之前开发环境:VS2008,ArcGIS9.3,ArcEngine9.3,Oracl ...