haproxy負載均衡 rabbitmq訊息佇列叢集
1 HAProxy的特點是:
HAProxy也是支援虛擬主機的。
HAProxy的優點能夠補充Nginx的一些缺點,比如支援Session的保持,Cookie的引導;同時支援通過獲取指定的url來檢測後端伺服器的狀態。
HAProxy跟LVS類似,本身就只是一款負載均衡軟體;單純從效率上來講HAProxy會比Nginx有更出色的負載均衡速度,在併發處理上也是優於Nginx的。
HAProxy支援TCP協議的負載均衡轉發,可以對MySQL讀進行負載均衡,對後端的MySQL節點進行檢測和負載均衡,大家可以用LVS+Keepalived對MySQL主從做負載均衡。
HAProxy負載均衡策略非常多
2 HAProxy安裝包下載
官網已經被和諧掉了 暫時登入不了,因此只能通過其他途徑獲取安裝包
本人使用的是 haproxy-1.7.0.tar.gz
3 先上完整自動化指令碼
#!/bin/bash -x nodeIp1=$1 nodeIp2=$2 HP_FILE_PATH="/app/rabbitmq/installFile" HP_INSTALL_PATH="/usr/local/haproxy" HP_CFG="/etc/haproxy/haproxy.cfg" HP_INIT="/etc/init.d/haproxy" HP_RSTSLOG="/etc/rsyslog.conf" #install basic function installBasic(){ yum install net-tools yum install -y iptables-services yum install -y wget tar lrzsz gzip gcc gcc-c++ yum install -y dos2unix } #install haproxy function installHp(){ cd $HP_FILE_PATH tar -zxvf haproxy-1.7.0.tar.gz cd haproxy-1.7.0 make MALLOC=libc TARGET=linux2628 PREFIX=$HP_INSTALL_PATH make install PREFIX=$HP_INSTALL_PATH } #copy file function copyFile(){ cp ${HP_INSTALL_PATH}/sbin/haproxy /usr/sbin/ cp ${HP_FILE_PATH}/haproxy-1.7.0/examples/haproxy.init $HP_INIT chmod 755 $HP_INIT useradd -r haproxy mkdir /etc/haproxy cp ${HP_FILE_PATH}/haproxy.cfg /etc/haproxy/ sed -i "s/nodeIp1/${nodeIp1}/" $HP_CFG sed -i "s/nodeIp2/${nodeIp2}/" $HP_CFG } #modify config file function modifyConfigFile(){ sed -i 's/#$ModLoad/$$ModLoad/' $HP_RSTSLOG sed -i 's/#$UDPServerRun/$UDPServerRun/' $HP_RSTSLOG echo "local3.* /var/log/haproxy.log" >> $HP_RSTSLOG } # start service function startService(){ systemctl daemon-reload systemctl restart rsyslog service haproxy start #Set the startup haproxy systemctl enable haproxy } # open port function openPort(){ systemctl start firewalld firewall-cmd --add-port=15672/tcp --permanent firewall-cmd --add-port=5672/tcp --permanent firewall-cmd --add-port=80/tcp --permanent firewall-cmd --reload } installBasic installHp copyFile modifyConfigFile startService openPort
4 安裝HAProxy(詳細介紹)
4.1 檢視核心版本
uname -r
3.2 解壓並安裝
tar -zxvf haproxy-1.7.0.tar.gz
cd haproxy-1.7.0
make TARGET=linux2628 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
安裝成功後,檢視版本
複製haproxy檔案到/usr/sbin下 因為下面的haproxy.init啟動指令碼預設會去/usr/sbin下找,當然你也可以修改,不過比較麻煩。
cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
複製haproxy指令碼,到/etc/init.d下
cp ./examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
建立系統賬號
useradd -r haproxy
建立配置檔案
mkdir /etc/haproxy
vi /etc/haproxy/haproxy.cfg
配置檔案內容如下:
global
log 127.0.0.1 local3 info
chroot /usr/local/haproxy
user haproxy
group haproxy
daemon
maxconn 4000
defaults
log global
mode http
option httplog
option dontlognull
option redispatch
retries 3
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http_front
bind *:80
stats realm Haproxy Manager
stats auth admin:admin
stats refresh 30s
stats uri /haproxy?stats
default_backend http_back
listen rabbitmq_admin
bind *:15672
server node1 nodeIp1:15672
server node2 nodeIp2:15672
listen rabbitmq_cluster
bind *:5672
option tcplog
mode tcp
timeout client 3h
timeout server 3h
option clitcpka
balance roundrobin
server node1 nodeIp2:5672 check inter 2000 rise 3 fall 3 weight 30
server node2 nodeIp2:5672 check inter 2000 rise 3 fall 3 weight 30
backend http_back
balance roundrobin
option httpchk GET /index.html
option forwardfor header X-Forwarded-For
server node1 nodeIp1:80 check inter 2000 rise 3 fall 3 weight 30
server node2 nodeIp2:80 check inter 2000 rise 3 fall 3 weight 30
開啟rsyslog配置:
vi /etc/rsyslog.conf
去掉下面兩行前面的#號
$ModLoad imudp
$UDPServerRun 514
並新增下面一行
local3.* /var/log/haproxy.log
重啟rsyslog
systemctl restart rsyslog
# centos 6 中木有systemctl命令,可以使用下面的命令
# service rsyslog restart
啟動haproxy
service haproxy start
驗證是否成功