本文所使用的环境:

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高可用负载均衡详细配置的更多相关文章

  1. 案例一(haproxy+keepalived高可用负载均衡系统)【转】

    1.搭建环境描述: 操作系统: [root@HA-1 ~]# cat /etc/redhat-release CentOS release 6.7 (Final) 地址规划: 主机名 IP地址 集群角 ...

  2. HAProxy+Keepalived 高可用负载均衡

    转自 https://www.jianshu.com/p/95cc6e875456 Keepalived+haproxy实现高可用负载均衡 Master backup vip(虚拟IP) 192.16 ...

  3. 基于HAProxy+Keepalived高可用负载均衡web服务的搭建

    一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web ...

  4. 005.HAProxy+Keepalived高可用负载均衡

    一 基础准备 1.1 部署环境及说明 系统OS:CentOS 6.8 64位 HAProxy软件:HA-Proxy version 1.5.18 Keepalived软件:keepalived-1.3 ...

  5. HAProxy+Keepalived高可用负载均衡

    一 基础准备 1.1 部署环境及说明 系统OS:CentOS 6.8 64位 HAProxy软件:HA-Proxy version 1.5.18 Keepalived软件:keepalived-1.3 ...

  6. Nginx keepalived实现高可用负载均衡详细配置步骤

    Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing(负载均衡) ...

  7. 测试LVS+Keepalived高可用负载均衡集群

    测试LVS+Keepalived高可用负载均衡集群 1. 启动LVS高可用集群服务 此时查看Keepalived服务的系统日志信息如下: [root@localhost ~]# tail -f /va ...

  8. LVS+Keepalived高可用负载均衡集群架构实验-01

    一.为什么要使用负载均衡技术? 1.系统高可用性 2.  系统可扩展性 3.  负载均衡能力 LVS+keepalived能很好的实现以上的要求,LVS提供负载均衡,keepalived提供健康检查, ...

  9. 使用Ansible实现nginx+keepalived高可用负载均衡自动化部署

    本篇文章记录通过Ansible自动化部署nginx的负载均衡高可用,前端代理使用nginx+keepalived,端web server使用3台nginx用于负载效果的体现,结构图如下: 部署前准备工 ...

随机推荐

  1. php 判断文件或目录是否存在

    判断文件或目录是否存在有自带的函数 file_exists:文件是否存在 $file = "check.txt"; if(file_exists($file)) {     ech ...

  2. 移动web app 中的meta 标签

    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scal ...

  3. spring 注入静态变量

    import java.util.Iterator; import java.util.LinkedList; import javax.annotation.PostConstruct; impor ...

  4. SQL Server提高事务复制效率优化(一)总体概述

      随着公司业务的发展,数据量增长迅速,在解决Scale Out的同时,还要考虑到主从的复制延迟问题,尽量降到1s以内满足线上业务,如果不调整,SQL Server默认的配置可能平均要3s左右.生产的 ...

  5. ARCGIS多种影像裁剪

    在互联网上下载的遥感影像都进行过分幅处理,下载下来的影像多是规则的四方形,而在进行遥感影像研究时,多是针对特定区域来进行,比如研究北京市的遥感影像,不在北京市范围内的影像对于研究者就没有利用意义,如果 ...

  6. mysql error: (2006, 'MySQL server has gone away')

    max_allowed_packet=16M wait_timeout= interactive_timeout = vim /etc/my.cnf  mysqld 中加入上面的内容.

  7. zend framework2 下载及安装

    1.安装XAMPP 2.安装zend studio 3.在GITHUB上下载一个zendframework模板,插入到IDE中 4.将下载的zend framework2文件夹解压放在vendor文件 ...

  8. 代理模式(Proxy pattern)

    代理模式(proxy pattern):作用:为其他对象提供一种代理,以控制对这个对象的访问.代理对象在客户端对象和目标对象之间起中介的作用. 代理模式涉及到的角色: 抽象角色:声明真实对象和代理对象 ...

  9. java 在linux环境下写入 syslog 问题研究

    1.Syslog 在Unix类操作系统上,syslog广泛应用于系统日志.syslog日志消息既可以记录在本地文件中,也可以通过网络发送到接收syslog的服务器.接收syslog的服务器可以对多个设 ...

  10. delphi 类方法、类变量、类常量、类属性的研究,自己的研究

    群里我师傅给我的答案: unit Unit4; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Vari ...