[转] Haproxy、Keepalived双主高可用负载均衡
http://blog.chinaunix.net/uid-25266990-id-3989321.html
在测试了Nginx+Keepalived的负载均衡后,也对Haproxy+Keepalived双主模式做了测试,双主的模式充分利用了服务器资源,这样不会导致服务器浪费。 这里举例说明:
默认情况下,第一台负载均衡器主要分发www.zhirs.com的请求,第二台负载均衡器主要分发img.zhirs.com的请求。
任意一台宕机都不会影响网站分发。
实验环境
VIP1=192.168.137.200 (www.zhirs.com)
VIP2=192.168.137.100 (img.zhirs.com)
haproxy01=192.168.137.254
haproxy02=192.168.137.253
web1=192.168.137.201
web2=192.168.137.202
img1=192.168.158.101
img2=192.168.158.102
架构图
安装配置过程
1、安装配置过程,(两台Haproxy的安装配置步骤一样)
haproxy下载地址:http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
#cd /usr/local/src
#wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
#tar xf haproxy-1.4..tar.gz
#cd haproxy-1.4.
#make TARGET=linux26 ARCH=x86_64
#TARGET是指定内核版本,ARCH指定CPU架构,我使用的是64bit系统
#make install
2、安装完毕后,创建配置文件和启动文件。
#mkdir /etc/haproxy
#cp examples/haproxy.cfg /etc/haproxy
#cp examples/haproxy.init /etc/init.d/haproxy
#chmod +x /etc/init.d/haproxy
#ln -s /usr/local/sbin/haproxy /usr/sbin/
#mkdir /usr/share/haproxy
3、编辑配置文件(两台Haproxy配置文件相同)
#vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1. or haproxy-1.2.
global
log 127.0.0.1 local0 #日志输出配置,所有日志都记录在本机,通过local0输出
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn #最大连接数
chroot /usr/share/haproxy #改变当前工作目录。
uid #所属用户的uid
gid #所属运行的gid
daemon #以后台形式运行haproxy
#debug
#quiet defaults
log global
mode http
#默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
option httplog
option dontlognull
option redispatch
#当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose
#当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
retries #两次连接失败就认为是服务器不可用
maxconn #默认的最大连接数
#timeout http-keep-alive 10s
# timeout queue 1m
contimeout #连接超时
clitimeout #客户端超时
srvtimeout #服务器超时
timeout check 5s #心跳检测超时
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm baison-test-Haproxy #统计页面密码框上提示文本
stats auth admin:admin123 #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息
frontend www
bind *:
#这里建议使用bind *:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。
acl web hdr(host) -i www.zhirs.com
#acl后面是规则名称,-i是要访问的域名,如果访问www.zhirs.com这个域名就分发到下面的webserver 的作用域。
acl img hdr(host) -i img.zhirs.com
#如果访问img.baison.com.cn就分发到imgserver这个作用域。
use_backend webserver if web
use_backend imgserver if img backend webserver #webserver作用域
mode http
balance roundrobin
#banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数
option httpchk /index.html
#检测文件,如果分发到后台index.html访问不到就不再分发给它
server web01 192.168.137.201: check inter fall weight
server web01 192.168.137.202: check inter fall weight
server web01 192.168.137.203: check inter fall weight backend imgserver
mode http
option httpchk /index.php
balance roundrobin
server img01 192.168.137.101: check inter fall
server img02 192.168.137.102: check inter fall
4、启动Haproxy服务,查看状态。
|
1
|
#service haproxy start |
5、安装配置keepalived(两台keepalived安装步骤一样)
#cd /root/software
#wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
#tar -zxvf pcre-8.33.tar.gz
#cd pcre-8.33
#./configure
#make && make install
#wget http://nginx.org/download/nginx-1.2.9.tar.gz
#tar -zxvf nginx-1.2..tar.gz
#cd nginx-1.2.
#./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module
#make && make install
6、第一台keepalived配置文件。第一台做192.168.137.200vip的master,192.168.137.100的backup。
#vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
admin@centos.bz
}
notification_email_from keepalived@domain.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
#script "/opt/nginx_pid.sh"
script "/opt/check_haproxy.sh"
interval
weight
}
vrrp_instance VI_1 {
state MASTER ############ 辅机为 BACKUP
interface eth0
virtual_router_id
mcast_src_ip 192.168.137.254
priority ########### 权值要比 back 高
advert_int
authentication {
auth_type PASS
auth_pass
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress {
192.168.137.200
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.137.100
}
}
7、第二台keepalived的配置文件。
#vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
admin@centos.bz
}
notification_email_from keepalived@domain.com
smtp_server 127.0.0.1
smtp_connect_timeout
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
#script "/opt/nginx_pid.sh"
script "/opt/check_haproxy.sh"
interval
weight
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id
mcast_src_ip 192.168.137.253
priority ##########权值 要比master低
advert_int
authentication {
auth_type PASS
auth_pass
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.137.200
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.137.100
}
}
8、检测脚本,为了防止haproxy服务关闭导致keepalived不自动切换。
#vim /opt/check_haproxy.sh
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq ]; then
/etc/init.d/haproxy start
fi
sleep
if [ $(ps -C haproxy --no-header | wc -l) -eq ]; then
/etc/init.d/keepalived stop
fi
9、启动两台keepalived和Haproxy服务。
#servcie haproxy start
#service keepalived start
10、查看两台服务器的VIP 查看
查看192.168.137.254:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:dc:01:3e brd ff:ff:ff:ff:ff:ff
inet 192.168.137.254/24 brd 192.168.137.255 scope global eth0
inet 192.168.137.200/32 scope global eth0
查看192.168.137.253:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:d5:e7:3d brd ff:ff:ff:ff:ff:ff
inet 192.168.137.253/24 brd 192.168.137.255 scope global eth0
inet 192.168.137.100/32 scope global eth0
inet6 fe80::20c:29ff:fed5:e73d/64 scope link
valid_lft forever preferred_lft forever
11、测试,关闭任意一台keepalived,img和www域名访问正常。
后续会对Nginx+Keepalived 双主结果做测试。
我的VM测试
# 机器A keepalived.conf
global_defs {
router_id NodeA
} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 55
priority 150
advert_int 1
nopreempt authentication {
auth_type PASS
auth_pass 1111
} virtual_ipaddress {
192.168.28.188/24
}
} # 机器B
global_defs {
router_id NodeB
} vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 55
priority 149
advert_int 1
nopreempt authentication {
auth_type PASS
auth_pass 1111
} virtual_ipaddress {
192.168.28.188/24
}
} # 查看当前IP使用机器
# ip a
[转] Haproxy、Keepalived双主高可用负载均衡的更多相关文章
- nginx+keepalived双主高可用负载均衡
实验环境及软件版本:CentOS版本: 6.6(2.6.32.-504.el6.x86_64)nginx版本: nginx-1.6.3keepalived版本:keepalived-1.2.7 主LB ...
- Nginx(haproxy)+keepalived+Tomcat双主高可用负载均衡
周末的时候一个正在学Linux的朋友问我,高可用怎么玩?我和他微信了将近三个小时,把Nginx和haproxy双主高可用教给他了,今天突然想把这个给写进博客里,供给那些正在学习Linux系统的朋友们, ...
- Haproxy+Keepalived搭建Weblogic高可用负载均衡集群
配置环境说明: KVM虚拟机配置 用途 数量 IP地址 机器名 虚拟IP地址 硬件 内存3G 系统盘20G cpu 4核 Haproxy keepalived 2台 192.168.1.10 192 ...
- Keepalived+Nginx实现高可用负载均衡集群
一 环境介绍 1.操作系统CentOS Linux release 7.2.1511 (Core) 2.服务keepalived+nginx双主高可用负载均衡集群及LAMP应用keepalived-1 ...
- Keepalived+LVS(dr)高可用负载均衡集群的实现
一 环境介绍 1.操作系统CentOS Linux release 7.2.1511 (Core) 2.服务keepalived+lvs双主高可用负载均衡集群及LAMP应用keepalived-1.2 ...
- Dubbo入门到精通学习笔记(二十):MyCat在MySQL主从复制的基础上实现读写分离、MyCat 集群部署(HAProxy + MyCat)、MyCat 高可用负载均衡集群Keepalived
文章目录 MyCat在MySQL主从复制的基础上实现读写分离 一.环境 二.依赖课程 三.MyCat 介绍 ( MyCat 官网:http://mycat.org.cn/ ) 四.MyCat 的安装 ...
- MariaDB+Keepalived双主高可用配置MySQL-HA
利用keepalived构建高可用MySQL-HA,保证两台MySQL数据的一致性,然后用keepalived实现虚拟VIP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换. ...
- LVS+Keepalived搭建MyCAT高可用负载均衡集群
LVS+Keepalived 介绍 LVS LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国 ...
- RHEL 5.4下部署LVS(DR)+keepalived实现高性能高可用负载均衡
原文地址:http://www.cnblogs.com/mchina/archive/2012/05/23/2514728.html 一.简介 LVS是Linux Virtual Server的简写, ...
随机推荐
- tar打包时的排除选项
1. 在命令行排除文件时,用 --exclude, 可以用多个--exclude . 可以用=号,也可以不用 如 tar -cvf ./aaa.tar ./tvc --exclude ...
- oracle in 函数
IN操作符 select * from scott.emp where empno=7369 or empno=7566 or empno=7788 or empno=9999: ...
- (Nginx反向代理+NFS共享网页根目录)自动部署及可用性检测
1.nginx反向代理安装配置 #!/usr/bin/bash if [ -e /etc/nginx/nginx.conf ] then echo 'Already installed' exit e ...
- BFPRT 算法 (TOP-K 问题)——本质就是在利用分组中位数的中位数来找到较快排更合适的pivot元素
先说快排最坏情况下的时间复杂度为n^2. 正常情况: 最坏的情况下,待排序的记录序列正序或逆序,每次划分只能得到一个比上一次划分少一个记录的子序列,(另一个子序列为空).此时,必须经过n-1次递归 ...
- forget sus,syn sym semi word out~s
1★ sus 在~下面 2★ syn 3★ sym 共同 4★ semi 半
- zabbix3.4.7主动模式监控日志(多关键字)
日志监控原理 1.Zabbix Server和Zabbix Agent会追踪日志文件的大小和最后修改时间,并且分别记录在字节计数器和最新的时间计数器中. 2.Agent会从上次读取日志的地方开始读取日 ...
- Laravel 更新数据时在表单请求验证中排除自己,检查指定字段唯一性
原文地址:https://moell.cn/article/24 不错的laravel网站 需求场景 修改用户信息时,在表单请求验证中排除当前邮箱所在的记录行,并检查邮箱的唯一性. Laravel版本 ...
- 批量设置样式json版
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- 阿里云服务器上安装mysql的详细步骤
阿里云安装mysql (1)下载mysql安装包,去mysql官网下载对应的包 mysql数据库官方下载网址: https://downloads.mysql.com/archives/commu ...
- HanLP自然语言处理包介绍
支持中文分词(N-最短路分词.CRF分词.索引分词.用户自定义词典.词性标注),命名实体识别(中国人名.音译人名.日本人名.地名.实体机构名识别),关键词提取,自动摘要,短语提取,拼音转换,简繁转换, ...

