一:ubuntu安装haproxy
1、sudo apt-get update
2、sudo apt-get -y install haproxy
3、dpkg -L haproxy //查看haproxy安装的文件
4、安装完毕后,haproxy默认已经启动。如下:
ps -ef |grep haproxy
5、查看haproxy版本,如下:
haproxy -v
注意:apt-get方式安装haproxy,如果版本为1.4.24的话。我们一定要修改/etc/default/haproxy,如下:
sudo vi /etc/default/haproxy
ENABLED=1
6、修改配置文件
/etc/haproxy
端口和用户名密码配置
7、重启服务
service haproxy restart
8、打开浏览器
http://172.16.202.5:1080/stats
ubuntu安装keepalived:
apt-get install keepalived
二、CentOS安装haproxy
1、安装配置haproxy之前需要关闭系统selinux,将SELINUX设置位disabled
2、步骤2需要重启才能生效,所以我们这里先手动设置SELINUX
[root@test-10-226 ~]# getenforce
Disabled
3、我们这里不采用复杂的源代码编译的方式安装,而是使用yum install haproxy命令安装haproxy,可以很好的解决各种依赖关系
4、设置haproxy的自启动并启动haproxy服务,启动完成使用ps -ef查看进程
[root@test-10-226 ~]# systemctl enable haproxy
[root@test-10-226 ~]# systemctl start haproxy
[root@test-10-226 ~]# ps -ef|grep haproxy
三、haproxy代理rabbitMQ
在/etc/haproxy/haproxy.cfg的配置文件中填写:
重启haproxy命令:
systemctl restart haproxy
登录:
例:172.16.201.115是haproxy
172.16.201.116 rabbit1
172.16.201.117 rabbit2
172.16.201.118 rabbit3 端口都是15672
在监听中添加:
listen Rabbit_Mq
bind 0.0.0.0:5672
mode tcp
balance roundrobin
option tcpka
server rabbit1 172.16.201.116:15672 check inter 2000 rise 2 fall 3
server rabbit2 172.16.201.117:15672 check inter 2000 rise 2 fall 3
server rabbit3 172.16.201.118:15672 check inter 2000 rise 2 fall 3
备注:
在haproxy代理登录rabbitMQ的账号用rabbitMQ本身的账号即可。
并在访问8888端口时:
能够出rabbitMQ的监控状态。
备注:上面的两个自定义端口:8888和5672尽量不同,要不然会有问题
四、haproxy代理mariadb
在没有代理mariadb集群时配置和三没有代理rabbitMQ集群配置一样即可
代理时配置如下:
在控制台上能够出现mariadb的运行状态:
打开navicat可以连接haproxy代理的DB:
给与haproxy地址访问权限并提供用户名和密码:
GRANT ALL ON . TO ‘root’@‘172.16.201.58’ IDENTIFIED BY ‘opark@123!’;
root // 用户名
172.16.201.58 //haproxy的服务器地址
opark@123!’ //密码
附录一:以前成功代理rabbitMQ的配置文件
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
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/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 3000
listen stats
bind 0.0.0.0:8888
mode http
option httplog
stats refresh 30s
stats uri /
stats realm Haproxy Manager
stats auth admin:admin
listen Rabbit_Mq
bind 0.0.0.0:5672
mode tcp
balance roundrobin
option tcpka
server rabbit1 172.16.201.116:15672 check inter 2000 rise 2 fall 3
server rabbit2 172.16.201.117:15672 check inter 2000 rise 2 fall 3
server rabbit3 172.16.201.118:15672 check inter 2000 rise 2 fall 3
#listen rabbitmq_admin
bind 0.0.0.0:8004
server node1 172.16.201.116:15672
server node2 172.16.201.117:15672
server node3 172.16.201.118:15672
#listen rabbitmq_cluster
bind 0.0.0.0:5672
option tcplog
mode tcp
timeout client 3h
timeout server 3h
option clitcpka
balance roundrobin
server node1 172.16.201.116:5672 check inter 5s rise 2 fall 3
server node2 172.16.201.117:5672 check inter 5s rise 2 fall 3
server node3 172.16.201.118:5672 check inter 5s rise 2 fall 3
#---------------------------------------------------------------------
main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000
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 app
#---------------------------------------------------------------------
static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 127.0.0.1:5001 check
server app2 127.0.0.1:5002 check
server app3 127.0.0.1:5003 check
server app4 127.0.0.1:5004 check
附录二:以前成功代理mariadb的配置文件
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
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/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 3000
listen stats
bind 0.0.0.0:1080
mode http
option httplog
#stats refresh 30s
stats uri /stats
stats realm Haproxy Manager
stats auth admin:admin
listen mariadb 0.0.0.0:3306
mode tcp
balance roundrobin
option tcpka
server mariadb1 172.16.201.116:3306 check maxconn 2000 weight 1
server mariadb2 172.16.201.117:3306 check maxconn 2000 weight 1
server mariadb3 172.16.201.118:3306 check maxconn 2000 weight 1
#---------------------------------------------------------------------
main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000
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 app
#---------------------------------------------------------------------
static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 127.0.0.1:5001 check
server app2 127.0.0.1:5002 check
server app3 127.0.0.1:5003 check
server app4 127.0.0.1:5004 check