haproxy代理 负载均衡

之前一直想做mysql的读写分离实验,发现haproxy更多的是负载均衡,针对的是多从库间的负载均衡(其实也可以做web端的负载均衡)
haproxy ip 192.168.3.3
slave1 192.168.3.8
slave2 192.168.3.5
centos6.7yum库里有haproxy在这里插入图片描述
yum安装完后

[root@mycat3 src]# find / -name haproxy
/usr/sbin/haproxy
/etc/rc.d/init.d/haproxy
/etc/haproxy
/var/lock/subsys/haproxy
/var/lib/haproxy
/root/haproxy-1.7.11/haproxy

主要 /etc/haproxy/haproxy.cfg

**********************************************************************
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) 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     4000
    user        haproxy
    group       haproxy
    daemon
 
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 30000 


 listen stats                            #关联前端和后端定义一个完整的代理
    mode http                           #设置代理协议
    bind 0.0.0.0:8080                   #绑定相应的端口
    stats enable                        #开启Haproxy统计状态
    stats refresh 3s             #统计页面自动刷新时间间隔
    stats hide-version                  #隐藏代理服务器版本
    stats uri     /haproxyadmin?stats   #访问的url
    stats realm   Haproxy\ Statistics   #统计页面认证时提示内容信息
    stats auth    admin:123456          #设置登录用户和密码     
    stats admin if TRUE                 #如果认证通过,则就可以打开stats
 
 
frontend http-in
    bind *:80
    mode http
    log global
    option httpclose
    option logasap
    option dontlognull
    capture request  header Host len 20
    capture request  header Referer len 60
    default_backend servers
 
frontend healthcheck
    bind :1099
    mode http
    option httpclose
    option forwardfor
    default_backend servers
 
backend servers
    balance roundrobin
    server websrv1 10.10.172.196:80 check maxconn 2000
    server websrv2 10.10.172.197:80 check maxconn 2000

数据库代理配置

[root@mycat3 haproxy]# vim haproxy.cfg 


global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats

defaults
        log     global
        log 127.0.0.1 local3
        mode    http
        option  tcplog
        option  dontlognull
        retries 10
        option redispatch
        maxconn         2000
        timeout connect         10s
        timeout client          1m
        timeout server          1m
        timeout http-keep-alive 10s
        timeout check           10s

listen  mysql
        bind 0.0.0.0:7306    监听所有IP的7306端口
        mode tcp
        balance roundrobin
        server mysql1 192.168.3.8:3306
        server mysql2 192.168.3.5:3306

listen stats                                  #这个是配置的web页面内容,通过web方式来查看haproxy的状态
        bind 0.0.0.0:1080
        mode http
        option httplog
        maxconn 10
        stats refresh 30s
        stats uri /stats
        stats realm XingCloud\ Haproxy
        stats auth admin:admin #用这个账号和密码登录,可以自己设置
        stats auth Frank:Frank
        stats hide-version
        stats admin if TRUE

Navicat for MySQL 进行连接 haproxy的端口 7306
在这里插入图片描述

可以看得出来轮询slave1和slave2的内容

web查看方式 192.168.3.3:1080/stats 之前配置文件中配置的账户是admin密码是admin

猜你喜欢

转载自blog.csdn.net/weixin_43945743/article/details/85115642