- 下载
yum install -y haproxy
- 配置
#---------------------------------------------------------------------
global
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
# haproxy管理页面端口
listen stats
bind 0.0.0.0:1080
mode http
option httplog
maxconn 5000
stats refresh 30s
stats uri /stats
# 代理tomcat访问的端口
listen web
bind 0.0.0.0:80 # 对外访问端口
mode http
option httplog
balance roundrobin
server bigdata2-test bigdata2-test:8080 check # 还可以定制其他参数
server bigdata3-test bigdata3-test:8080 check
server bigdata4-test bigdata4-test:8080 check
- 启动tomcat,
注意tomcat页面访问要关掉linux防火墙
systemctl stop firewalld
- 启动haproxy,查看ip:1080/stats
service haproxy start
service haproxy status
# 常见bug:“cannot bind socket”
# 执行命令
setsebool -P haproxy_connect_any=1
# 修改配置
vi /etc/sysctl.conf
添加:net.ipv4.ip_nonlocal_bind=1
sysctl -p
再重新启动haproxy
- 此时访问ip:80端口会自动分配到配置的三台服务器上,
一个小py脚本监听haproxy是否GG
主要是监听haproxy的ui界面是否还可以访问
// pip安装
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
// 安装python模块
pip install requests
// 监听脚本
#-*- coding:utf-8 -*-
import requests
import time
import json
url = 'http://bigdata2:1080/stats'
# 发送钉钉提醒
def dingDingMsg(text):
headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = 'https://oapi.dingtalk.com/robot/send?access_token=5e07a7ce2a75e7536e32a5782d0e040a85bb407c6946ed67d93e0e5f24012911'
json_text = {
"msgtype": "text",
"isAtAll": True,
"text": {
"content": text
}
}
post_return = requests.post(api_url,json.dumps(json_text),headers=headers)
print post_return.content
while True:
cnt = 0
try:
response = requests.get(url)
time.sleep(30)
except RuntimeError,e:
dingDingMsg('haproxy异常')
dingDingMsg('haproxy已重启')
cnt += 1
if cnt < 5:
continue
else:
e = raw_input('pauseing.........continue ?! (press enter or ctrl c)')
except Exception,e:
dingDingMsg('haproxy异常')
dingDingMsg('haproxy已重启')
cnt += 1
if cnt < 5:
continue
else:
e = raw_input('pauseing.........continue ?! (press enter or ctrl c)')
except KeyboardInterrupt:
exit(0)
except :
dingDingMsg('haproxy异常')
dingDingMsg('haproxy已重启')
cnt += 1
if cnt < 5:
continue
else:
e = raw_input('pauseing.........continue ?! (press enter or ctrl c)')
// python发布命令
nohup python /etc/haproxy/listen_haproxy.py > listen.log 2>&1 &
// shell定时任务
#删除脚本
#!/bin/bash
cat /dev/null > /etc/haproxy/listen.log
#脚本授权
chmod 777 clean_log.sh
#进入定时任务
crontab -e
0 */3 * * * /etc/haproxy/clean_log.sh
#查看任务
crontab -l
#定时任务日志
tail -500f /var/log/cron
网友评论