Haproxy安装配置

安装

# tar -xvf haproxy-2.0.14.tar.gz

# uname -r

3.10.0-1062.18.1.el7.x86_64

# cd haproxy-2.0.14

#编译 uname -r #查看系统内核版本号

 

#make TARGET=linux3100 CPU=x86_64 PREFIX=/usr/local/haprpxy

#安装

# make install PREFIX=/usr/local/haproxy

 

初始化Haproxy

# mkdir -p /var/lib/haproxy/

#创建配置文件目录

mkdir -p /usr/local/haproxy/conf

#创建配置文件目录

mkdir -p /etc/haproxy

#创建配置文件

touch /usr/local/haproxy/conf/haproxy.cfg

#添加配置文件软连接

ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg

#创建日志文件目录

mkdir -p /usr/local/haproxy/log

#创建日志文件

touch /usr/local/haproxy/log/haproxy.log

#添加软连接

ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log

#拷贝开机启动文件

cp /opt/soft/haproxy-2.0.14/examples/haproxy.init /etc/rc.d/init.d/haproxy

#添加脚本执行权限

chmod +x /etc/rc.d/init.d/haproxy

#设置开机启动

chkconfig haproxy on

#添加软连接

ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin

 

 

 

配置haproxy.cnf参数(数据库读写分离)

 

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

# 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:

    #

    # 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      /usr/local/haproxy

    pidfile     /var/run/haproxy.pid

    maxconn     4000

    #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                    tcp

    log                     global

    option                  tcplog

    option                  dontlognull

    option http-server-close

#    option forwardfor       except 127.0.0.0/8

    option                  redispatch

    timeout connect         60s

    timeout client          60m

    timeout server          60m

    maxconn                 3000

 

listen MySQL_READ

    bind 0.0.0.0:3306

    mode tcp

    #option httpchk

    balance leastconn

    #server node145 172.16.208.145:3306 check port 3306 inter 2000 rise 3 fall 3

    server node147 172.16.208.147:3306 check port 3306 weight 1 inter 3000 rise 3 fall 3

    server node148 172.16.208.148:3306 check port 3306 weight 1 inter 3000 rise 3 fall 3

 

listen MySQL_WRITE

    bind 0.0.0.0:23306

    mode tcp

    #option httpchk

    balance leastconn

    server node145 172.16.208.145:3306 check port 3306 inter 2000 rise 3 fall 3

    server node147 172.16.208.147:3306 check port 3306 inter 3000 rise 3 fall 3 backup

    server node148 172.16.208.148:3306 check port 3306 inter 3000 rise 3 fall 3 backup

 

#listen MDB

#   bind 0.0.0.0:6379

#   option tcp-check

#   tcp-check connect

#   tcp-check send PING\r\n

#   tcp-check expect string +PONG

#   tcp-check send info\ replication\r\n

#   tcp-check expect string role:master

#   tcp-check send QUIT\r\n

#   tcp-check expect string +OK

#   server R10 10.118.36.10:6379 check inter 3s

#   server R74 10.118.36.74:6379 check inter 3s

#   server R161 10.118.5.161:6379 check inter 3s

 

 

listen stats

   mode http

   bind 0.0.0.0:80

   stats enable

   stats uri /dbs

   stats refresh 30s

   stats realm Global\ statistics

   stats auth admin:xxxx

 

 

启动服务

 

chmod 775 /etc/init.d/haproxy

 

# service haproxy start

 

 

# service haproxy reload

页面登录

http://47.114.43.240/dbs

 

 

帐号:admin

密码: xxxx

 

数据库连接测试(查看节点的信息)

 

 

[root@redis-server1 conf]# mysql -uroot -p -h 172.16.208.144

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 147

Server version: 10.4.12-MariaDB-log MariaDB Server

 

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show variables like 'wsrep_node_address';

+--------------------+----------------+

| Variable_name      | Value          |

+--------------------+----------------+

| wsrep_node_address | 172.16.208.145 |

 

 

 

 

参数优化

 

 

 

 

 

 

server web1 10.1.1.1:80 cookie server1 weight 6 check inter 2000 rise 2 fall 3 表示:

server用于定义多台后端真实服务器,不能用于frontend和listen段

格式如下:

server name address :[port] [param*]

name: 为后端真实服务器指定一个内部名称,随便定义一个即可

address:port : 指定后端服务器的IP地址及端口

param*参数

常用的参数:

check 表示启用对此后端服务器执行健康状态检查

inter 设置健康状态检查的时间间隔,单位是毫秒

rise 检查多少次认为服务器可用

fall 检查多少次认为服务器不可用

weight 设置服务器的权重,默认为1, 最大为256。 设置为0表示不参与负载均衡

backup 设置备份服务器,用于所有后端服务器全部不可用时

cookie 为指定的后端服务器设置cookie值,此处指定的值将在请求入站时被检查,第一次为此值挑选的后端服务器将在后续的请求中一直被选中,其目的在于实现持久连接的功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值