本文所使用的环境:

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. IS A 和 HAS A的区别

    IS A:一般是用作继承或者接口类.比如:中国人和外国人都是人,重点在:是一个... . HAS A:一般用作对象和他的成员的从属关系.比如:中国人和外国人都有眼睛.耳朵.嘴巴.重点在:有一个... ...

  2. QNDataSet打印预览自动关闭问题

    问题:打印预览后,数据集自动关闭 解决: TQNDataSet = class(TFDMemTable) private protected procedure PSReset; override; ...

  3. python文件操作实例

    把目录 E:\ 下面所有 后缀名为 .py 的 文件复制到 E:\PyLearn #coding:utf-8 import os import shutil def getfile(srcDir,ds ...

  4. Request Entity Too Large for Self Hosted ASP.Net Web API在Selfhost的api后台怎么解决Request Entity Too Large问题

    Request Entity Too Large for Self Hosted ASP.Net Web API在Selfhost的api后台怎么解决Request Entity Too Large问 ...

  5. Tomcat的SSL证书配置以及Tomcat+Nginx实现SSL配置

    把jks上传到java容器在的服务器上,路径只要不是webapps下就可以,然后到conf目录下server.xml里配置 <Connector port=" protocol=&qu ...

  6. PHP防SQL注入不要再用addslashes和mysql_real_escape_string

    PHP防SQL注入不要再用addslashes和mysql_real_escape_string了,有需要的朋友可以参考下. 博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助 ...

  7. python操作mysql数据库

    连接数据库 输入值 存入数据库 关闭 import string import mysql.connector conn=mysql.connector.connect(user='root',pas ...

  8. Zepto API 学习

    1.  after, before, append, prepend 的区别 A.after(B) ==== B.insertAfter(A) // B 放在 A 的后面 A.before(B) == ...

  9. win7 下加载MSCOMCTL.OCX

    cd C:\Windows\System32"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regtlibv12.exe" msd ...

  10. 如何在cluster上跑R脚本

    R 是一个比较不错但是有时候操蛋的语言,不错是因为用着爽的时候真的很爽,操蛋是因为这种爽不是什么时候都可以的,比如说在cluster上批处理跑R脚本. 当然说这话有些在上面跑过的各种不服气,你丫傻逼吧 ...