Haproxy

2018-12-12  本文已影响0人  soane1983

1. 两个节点:10.11.17.250,10.11.17.251 , centos7

2. yum install haproxy

3. haproxy -v

4. 修改10.11.17.250 /etc/haproxy/haproxy.cnf

#---------------------------------------------------------------------

# Global settings

#---------------------------------------------------------------------

global

log 127.0.0.1 local0

chroot /var/lib/haproxy              # 改变当前工作目录

pidfile /var/run/haproxy.pid        # haproxy的pid存放路径,启动进程的用户必须有权限访问此文件

maxconn 4000                        # 最大连接数,默认4000

user haproxy                        # 默认用户

group haproxy                        # 默认组

daemon                              # 创建1个进程进入deamon模式运行。此参数要求将运行模式设置为daemon

stats socket /var/lib/haproxy/stats  # 创建监控所用的套接字目录

#---------------------------------------------------------------------

# defaults settings

#---------------------------------------------------------------------

# 注意:因为要使用tcp的负载,屏蔽掉与http相关的默认配置

defaults

mode http                            # 默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK

log global

option dontlognull                  # 启用该项,日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器

option redispatch                    # serverId对应的服务器挂掉后,强制定向到其他健康的服务器

retries 3                            # 3次连接失败就认为服务不可用,也可以通过后面设置       

timeout queue 1m

timeout connect 10s                  # 连接超时时间

timeout client 1m                    # 客户端连接超时时间

timeout server 1m                    # 服务器端连接超时时间

timeout check 10s                   

maxconn 3000                        # 最大连接数

#---------------------------------------------------------------------

listen admin_stats                      #开启haproxy监控界面

bind *:80                            #绑定80端口

mode http

option httplog

stats enable                        #开启统计

stats refresh 30s

stats uri /haproxy?stats            #监控界面url为:http://ip:80/haproxy/stats

stats auth admin:admin

stats realm welcome\ Haproxy

stats admin if TRUE

#---------------------------------------------------------------------

listen rabbitmq_admin                    #代理rabbitmq集群的管理界面

bind *:15672                        #绑定15672端口

mode http

option httplog

server rabbit1 10.11.17.248:15672

server rabbit2 10.11.17.249:15672

server rabbit3 10.11.17.252:15672

#---------------------------------------------------------------------

listen rabbitmq_cluster                  #代理rabbitmq集群

bind *:5672                          #绑定5672端口

mode tcp

option tcplog

balance roundrobin

server rabbit1 10.11.17.248:5672 check

server rabbit2 10.11.17.249:5672 check

server rabbit3 10.11.17.252:5672 check

listen mysql_proxy

bind *:3306                          #绑定5672端口

mode tcp

option tcplog

balance source

server mysqlmaster 10.11.17.248:3306 check

server mysqlslave 10.11.17.249:3306 check

5. 修改10.11.17.251 /etc/haproxy/haproxy.cnf

#---------------------------------------------------------------------

# Global settings

#---------------------------------------------------------------------

global

log 127.0.0.1 local0

chroot /var/lib/haproxy              # 改变当前工作目录

pidfile /var/run/haproxy.pid        # haproxy的pid存放路径,启动进程的用户必须有权限访问此文件

maxconn 4000                        # 最大连接数,默认4000

user haproxy                        # 默认用户

group haproxy                        # 默认组

daemon                              # 创建1个进程进入deamon模式运行。此参数要求将运行模式设置为daemon

stats socket /var/lib/haproxy/stats  # 创建监控所用的套接字目录

#---------------------------------------------------------------------

# defaults settings

#---------------------------------------------------------------------

# 注意:因为要使用tcp的负载,屏蔽掉与http相关的默认配置

defaults

mode http                            # 默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK

log global

option dontlognull                  # 启用该项,日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器

option redispatch                    # serverId对应的服务器挂掉后,强制定向到其他健康的服务器

retries 3                            # 10次连接失败就认为服务不可用,也可以通过后面设置       

timeout queue 1m

timeout connect 10s                  # 连接超时时间

timeout client 1m                    # 客户端连接超时时间

timeout server 1m                    # 服务器端连接超时时间

timeout check 10s                   

maxconn 3000                        # 最大连接数

#---------------------------------------------------------------------

listen admin_stats                      #开启haproxy监控界面

bind *:80                            #绑定80端口

mode http

option httplog

stats enable                        #开启统计

stats refresh 30s

stats uri /haproxy?stats            #监控界面url为:http://ip:80/haproxy/stats

stats auth admin:admin

stats realm welcome\ Haproxy

stats admin if TRUE

#---------------------------------------------------------------------

listen rabbitmq_admin                    #代理rabbitmq集群的管理界面

bind *:15672                        #绑定15672端口

mode http

option httplog

server rabbit1 10.11.17.248:15672

server rabbit2 10.11.17.249:15672

server rabbit3 10.11.17.252:15672

#---------------------------------------------------------------------

listen rabbitmq_cluster                  #代理rabbitmq集群

bind *:5672                          #绑定5672端口

mode tcp

option tcplog

balance roundrobin

server rabbit1 10.11.17.248:5672 check

server rabbit2 10.11.17.249:5672 check

server rabbit3 10.11.17.252:5672 check

listen mysql_proxy

bind *:3306                          #绑定5672端口

mode tcp

option tcplog

balance source

server mysqlmaster 10.11.17.248:3306 check

server mysqlslave 10.11.17.249:3306 check

6. 启动haproxy

haproxy -f /etc/haproxy/haproxy.cnf

7.重启haproxy

service haproxy restart

上一篇 下一篇

猜你喜欢

热点阅读