我是靠谱客的博主 知性棒球,最近开发中收集的这篇文章主要介绍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 PINGrn

#   tcp-check expect string +PONG

#   tcp-check send info replicationrn

#   tcp-check expect string role:master

#   tcp-check send QUITrn

#   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值,此处指定的值将在请求入站时被检查,第一次为此值挑选的后端服务器将在后续的请求中一直被选中,其目的在于实现持久连接的功能

最后

以上就是知性棒球为你收集整理的Haproxy安装配置的全部内容,希望文章能够帮你解决Haproxy安装配置所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部