Haproxy部署
Haproxy服务器:192.168.237.129
web服务器1:192.168.237.138
web服务器2:192.168.237.148
客户端:192.168.237.160
前提准备:haproxy-1.5.19.tar
web1服务器
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost opt]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost opt]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost opt]# make && make install
[root@localhost opt]# echo "this is wpc" > /usr/local/nginx/html/test.html
[root@localhost opt]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost opt]# nginx #启动nginx 服务
web2服务器
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost opt]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost opt]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@localhost opt]# make && make install
[root@localhost opt]# echo "this is zc" > /usr/local/nginx/html/test.html
[root@localhost opt]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost opt]# nginx #启动nginx 服务
Haproxy服务器
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y pcre-devel bzip2-devel gcc gcc-c++ make #安装依赖环境
[root@localhost ~]# tar zxvf haproxy-1.5.19.tar.gz -C /opt #解压到opt
[root@localhost ~]# cd /opt/haproxy-1.5.19/
[root@localhost haproxy-1.5.19]# make TARGET=linux2628 ARCH=x86_64 #编译安装(TARGET=linux26 #内核版本,使用uname -r查看内核,如:2.6.18-371.el5,此时该参数用TARGET=linux26;kernel大于2.6.28的用TARGET=linux2628
ARCH=x86_64 系统位数,64位系统)
[root@localhost haproxy-1.5.19]# make install
install -d "/usr/local/sbin"
install haproxy "/usr/local/sbin"
install -d "/usr/local/share/man"/man1
install -m 644 doc/haproxy.1 "/usr/local/share/man"/man1
install -d "/usr/local/doc/haproxy"
for x in configuration architecture haproxy-en haproxy-fr; do \
install -m 644 doc/$x.txt "/usr/local/doc/haproxy" ; \
done
[root@localhost haproxy-1.5.19]# mkdir /etc/haproxy #创建目录
[root@localhost haproxy-1.5.19]# cp haproxy.cfg /etc/haproxy
[root@localhost haproxy-1.5.19]# cd /etc/haproxy/
[root@localhost haproxy-1.5.19]# vim haproxy.cfg
#this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log /dev/log local0 #修改,配置日志记录,local0为日志设备,默认存放到系统日志
log /dev/log local1 notice #修改,配置日志记录,local0为日志设备,默认存放到系统日志
#log loghost local0 info
maxconn 4096
#chroot /usr/share/haproxy #注释,chroot运行路径,为该服务自设置的根目录,需注释掉
uid 99 #用户UID
gid 99 #用户GID
daemon
#debug
#quiet
defaults
log global #定义日志为global配置中的日志定义
mode http #模式为http
option httplog #采用http日志格式记录日志
option dontlognull #不记录健康检查日志信息
retries 3 #检查节点服务器失败次数,连续达到三次失败,则认为节点不可用
redispatch #当服务器负载很高时,自动结束当前队列处理比较久的连接
maxconn 2000 #最大连接数
contimeout 5000 #连接超时时间
clitimeout 50000 #客户端超时时间
srvtimeout 50000 #服务器超时时间
listen webcluster 0.0.0.0:80 #定义一个名为webcluster的应用
option httpchk GET /test.html #检查服务器的test.html文件
balance roundrobin #负载均衡调度算法使用轮询算法roundrobin
server inst1 192.168.237.138:80 check inter 2000 fall 3 #定义在线节点
server inst2 192.168.237.148:80 check inter 2000 fall 3
:wq
[root@localhost haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# cd /etc/init.d
[root@localhost init.d]# chmod +x haproxy #赋予权限
[root@localhost init.d]# chkconfig --add /etc/init.d/haproxy #添加haproxy 系统服务
[root@localhost init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy #软链接
[root@localhost init.d]# service haproxy start #开启服务
Starting haproxy (via systemctl): [ 确定 ]
测试
在客户端使用浏览器打开 http://192.168.237.129/test.html ,刷新浏览器测试负载均衡效果
Haproxy集群的日志重新定义
[root@localhost haproxy-1.5.19]# vim haproxy.cfg
#this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log /dev/log local0 #修改,配置日志记录,local0为日志设备,默认存放到系统日志
log /dev/log local1 notice #修改,配置日志记录,local0为日志设备,默认存放到系统日志
[root@localhost init.d]# vim /etc/rsyslog.d/haproxy.conf
#将haproxy的info日志记录到/var/log/haproxy/haproxy-info.log下,将notice日志记录到/var/log/haproxy/haproxy-notice.log下。“&~”表示当日志写入到日志文件后,rsyslog停止处理这个信息。
if ($programname == ‘haproxy’ and KaTeX parse error: Expected 'EOF', got '&' at position 72: …proxy-info.log &̲~ if (programname == ‘haproxy’ and $syslogseverity-text == ‘notice’)
then -/var/log/haproxy/haproxy-notice.log
&~
:wq
[root@localhost init.d]# mkdir /var/log/haproxy/
[root@localhost init.d]# systemctl restart rsyslog.service
重新访问http://192.168.237.129/test.html ,刷新浏览器测试
[root@localhost init.d]# tail -f /var/log/haproxy/haproxy-info.log #查看日志
Jun 7 16:47:43 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:43.485] webcluster webcluster/inst1 0/0/0/1/1 200 247 - - ---- 1/1/0/1/0 0/0 “GET /test.html HTTP/1.1”
Jun 7 16:47:43 localhost haproxy[41248]: 192.168.237.160:50030 [07/Jun/2022:16:47:43.533] webcluster webcluster/inst2 0/0/0/1/1 404 726 - - ---- 2/2/0/1/0 0/0 “GET /favicon.ico HTTP/1.1”
Jun 7 16:47:45 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:43.487] webcluster webcluster/inst1 2173/0/0/1/2174 304 178 - - ---- 2/2/0/1/0 0/0 “GET /test.html HTTP/1.1”
Jun 7 16:47:45 localhost haproxy[41248]: 192.168.237.160:50030 [07/Jun/2022:16:47:43.534] webcluster webcluster/inst2 2136/0/0/0/2136 404 726 - - ---- 2/2/0/1/0 0/0 “GET /favicon.ico HTTP/1.1”
Jun 7 16:47:46 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:45.661] webcluster webcluster/inst1 800/0/0/0/800 304 178 - - ---- 2/2/0/1/0 0/0 “GET /test.html HTTP/1.1”
Jun 7 16:47:46 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:46.461] webcluster webcluster/inst2 122/0/0/1/123 200 246 - - ---- 2/2/0/1/0 0/0 “GET /test.html HTTP/1.1”
Jun 7 16:47:46 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:46.584] webcluster webcluster/inst1 148/0/1/0/150 200 247 - - ---- 2/2/0/1/0 0/0 “GET /test.html HTTP/1.1”
Jun 7 16:47:47 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:46.734] webcluster webcluster/inst2 633/0/0/1/634 200 246 - - ---- 2/2/0/1/0 0/0 “GET /test.html HTTP/1.1”
Jun 7 16:47:48 localhost haproxy[41248]: 192.168.237.160:50029 [07/Jun/2022:16:47:47.368] webcluster webcluster/inst1 758/0/1/0/759 200 247 - - ---- 2/2/0/1/0 0/0 “GET /test.html HTTP/1.1”
[root@localhost init.d]# cd /var/log/haproxy/
[root@localhost haproxy]# ll
总用量 4
-rw-------. 1 root root 1710 6月 7 16:47 haproxy-info.log