Haproxy+Keepalived高可用负载均衡详细配置
本文所使用的环境:
10.6.2.128 centos6.5
10.6.2.129 centos6.5
VIP 为10.6.2.150
要实现的目标:
实现10.6.2.128和10.6.2.129的9998端口的服务通过haproxy负载,并通过keepalived实现高可用。
1、安装haproxy
yum install -y haproxy
2、配置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:
#
# ) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# ) 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 100000 #最大连接数
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/
option redispatch
retries 3 #定义连接后端服务器的失败重连次数,连接失败次数超过次值后就会将对应后端服务器标记为不可用
timeout http-request 10s #http请求超时时间
timeout queue 1m #一个请求在队列里的超时时间
timeout connect 10s #连接超时时间
timeout client 1m #客户端超时时间
timeout server 1m #服务器端超时时间
timeout http-keep-alive 10s #设置http-keep-alive的超时时间
timeout check 10s #检查超时的间隔
maxconn #每个进程可用的最大连接数 #---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend pay_test *:9999
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 pay_test #---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1: check #---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend pay_test
balance roundrobin #负载均衡的算法 roundrobin:轮询 source:根据请求源ip
fullconn 10000 #定义后端组的最大连接数
server pay_test_1 10.6.2.128:9998 inter 2000 rise 2 fall 3 check maxconn 5000 #inter 2000代表执行健康检查的间隔(ms),rise代表离线server转换到上线需要检查的次数,fall代表server从正常转到离线的检查次数
server pay_test_2 10.6.2.129:9998 inter 2000 rise 2 fall 3 check maxconn 5000 #check代表启动对此server执行健康检查,maxconn代表此服务器接受的最大并发连接数
listen stats
mode http
bind 0.0.0.0:9997
stats enable #开启监控页面
stats refresh 3s #页面刷新频率
stats hide-version #隐藏版本信息(为安全考虑)
stats uri /monitor #后台监控页面得uri
stats realm Haproxy\ monitor #提示信息
stats auth admin:admin #后台监控页面的用户名密码
stats admin if TRUE
3、服务启动
service haproxy start
4、将1-3步骤在10.6.2.129机器上也执行一次。
安装keepalived
1、下载安装keepalived
yum install -y keepalived
2、配置keepalived
vi /etc/keepalived/keepalived.conf
配置文件如下:
! Configuration File for keepalived global_defs {
notification_email {
bs_wjg@163.com #keepalived发生错误时候发送报警的邮箱 }
notification_email_from notify@163.com #发件人邮箱
smtp_server mail.163.com #发送email所使用的smtp服务器地址
smtp_connect_timeout 30 #连接stmp的超时时间
router_id LVS_DEVEL
}
#检查haproxy的进程状态,每1s执行一次
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval
weight
} vrrp_instance VI_1 {
state MASTER #主为MASTER 从为BACKUP
interface eth0 #实例绑定的网卡,视实际情况而定
virtual_router_id #这里设置vrid,如果两台机器属于同一组,设置为一样
priority #设置本节点的优先级,高的为master,不能超过255 一般master设置101 backup设置100
advert_int #组波信息发送间隔,默认为1s,同一本分组的两机器必须一样
authentication {
auth_type PASS
auth_pass 123456 } #验证密码,统一备份组的机器必须一致。
virtual_ipaddress {
10.6.2.150/ #虚拟IP的地址
}
track_interface {
eth0
}
track_script {
chk_haproxy
}
#状态通知
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault" }
notify.sh 脚本如下:
vip=10.6.2.150
contact='bs_wjg@163.com'
notify() {
mailsubject="`hostname` to be $1: $vip floating"
mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
} checkHA(){
counter=$(ps -C haproxy --no-heading|wc -l)
if [ "${counter}" = "" ]; then
/etc/rc.d/init.d/haproxy start
sleep
counter=$(ps -C haproxy --no-heading|wc -l)
if [ "${counter}" = "" ]; then
/etc/init.d/keepalived stop
fi
fi
} checkNG(){
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "" ]; then
/usr/local/bin/nginx
sleep
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "" ]; then
/etc/init.d/keepalived stop
fi
fi
} case "$1" in
master)
notify master
exit
;;
backup)
notify backup
exit
;;
fault)
notify fault
exit
;;
*)
echo 'Usage: `basename $0` {master|backup|fault}'
exit
;;
esac
3、服务启动
service keepalived start
4、10.6.2.129机器执行1-3步骤,根据从节点的配置进行配置。
到此配置结束,自行测试吧。
Haproxy+Keepalived高可用负载均衡详细配置的更多相关文章
- 案例一(haproxy+keepalived高可用负载均衡系统)【转】
1.搭建环境描述: 操作系统: [root@HA-1 ~]# cat /etc/redhat-release CentOS release 6.7 (Final) 地址规划: 主机名 IP地址 集群角 ...
- HAProxy+Keepalived 高可用负载均衡
转自 https://www.jianshu.com/p/95cc6e875456 Keepalived+haproxy实现高可用负载均衡 Master backup vip(虚拟IP) 192.16 ...
- 基于HAProxy+Keepalived高可用负载均衡web服务的搭建
一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web ...
- 005.HAProxy+Keepalived高可用负载均衡
一 基础准备 1.1 部署环境及说明 系统OS:CentOS 6.8 64位 HAProxy软件:HA-Proxy version 1.5.18 Keepalived软件:keepalived-1.3 ...
- HAProxy+Keepalived高可用负载均衡
一 基础准备 1.1 部署环境及说明 系统OS:CentOS 6.8 64位 HAProxy软件:HA-Proxy version 1.5.18 Keepalived软件:keepalived-1.3 ...
- Nginx keepalived实现高可用负载均衡详细配置步骤
Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing(负载均衡) ...
- 测试LVS+Keepalived高可用负载均衡集群
测试LVS+Keepalived高可用负载均衡集群 1. 启动LVS高可用集群服务 此时查看Keepalived服务的系统日志信息如下: [root@localhost ~]# tail -f /va ...
- LVS+Keepalived高可用负载均衡集群架构实验-01
一.为什么要使用负载均衡技术? 1.系统高可用性 2. 系统可扩展性 3. 负载均衡能力 LVS+keepalived能很好的实现以上的要求,LVS提供负载均衡,keepalived提供健康检查, ...
- 使用Ansible实现nginx+keepalived高可用负载均衡自动化部署
本篇文章记录通过Ansible自动化部署nginx的负载均衡高可用,前端代理使用nginx+keepalived,端web server使用3台nginx用于负载效果的体现,结构图如下: 部署前准备工 ...
随机推荐
- For Your Dream
队名:Braveheart 队员介绍: 队长:李洋洋 队员:姚欢,杨仁波,张波,乔闯 项目名称:数据沈航 总体任务: 收集整理学校的数据,为每个想要了解沈航的人展现一份我们收集来的信息 项目分组: ( ...
- opendove中的odgw所需要的内核模块
最近组里要做opendove相关的东西,需要odgw的一个kernel-module. 以前安装过,但备份不见了,在此做个链接备忘 : https://git.opendaylight.org/ger ...
- temp_web
使用vs2010创建.发布.部署.调用 WebService http://blog.sina.com.cn/s/blog_45eaa01a0102vp8z.html c#简易Http服务器 http ...
- OC Runtime
OC 是面向运行时的语言.Runtime就是系统在运行的时候的一些机制,其中最主要的是消息发送机制.OC语言与其他语言(如C语言)在函数(方法)的调用有很大的不同.C语言,函数的调用在编译的时候就已经 ...
- Composer实现PHP中类的自动加载
本篇博客承接上一篇,学习一下Composer实现的PHP的类的自动加载方式.首先说明一下,Composer是PHP针对PHP语言的第三方的依赖管理工具,将工程所用到的依赖文件包含在composer.j ...
- web前端开发资源分享:学习计划及资料推荐
HTML & CSS W3C官网 HTML学习 CSS学习 书籍: <Head First HTML与CSS.XHTML(中文版)><CSS禅意花园(修订版)> 视频 ...
- UNIX网络编程——getsockname和getpeername函数
UNIX网络编程--getsockname和getpeername函数 来源:网络转载 http://www.educity.cn/linux/1241293.html 这两个函数或者 ...
- Elasticsearch常用配置及性能参数
cluster.name: estest 集群名称node.name: “testanya” 节点名称 node.master: false 是否主节点node.data: true 是否 ...
- Python: 字典的基本操作
字典是Python里唯一的映射类型.字典是可变的.无序的.大小可变的键值映射,有时候也称为散列表或关联数组. 例子在下面: dic = {"apple":2, "oran ...
- java -json()
json-lib和org.json的使用几乎是相同的,我总结出的区别有两点: 两种包 1. List集合转换成json方法 List list = new ArrayList(); list.add( ...