haproxy基于一般的调度器设置(haproxy负载配置)
nanshan 2024-10-22 13:04 19 浏览 0 评论
基于一般的调度器设置(有listen,但无frontend和backend)
项目1:haproxy机器部署web集群(httpd是虚拟主机情况,只检查IP端口,不检查域名和站点网页)
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端能自动按照域名识别)
[root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg //只检查IP端口,不检查域名和站点网页
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen test
bind 172.10.10.5:80
mode http
stats enable
stats uri /admin?stats //web界面的uri,访问时格式:http://调度IP或域名/admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
option forwardfor //在web网站配置日志中能够看到客户端真实的IP,不好演示,做一个说明即可。
timeout server 15s
timeout connect 15s
option httpclose
cookie SERVERID rewrite
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
server web2 192.168.4.200:80 cookie B maxconn 4096 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP的设置
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common //记录客户端真实IP
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common //记录客户机真实IP
</VirtualHost>
wq
[root@localhost 桌面]# /etc/init.d/httpd restart
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo changyou1 > /changyou/index.html
[root@localhost 桌面]# echo tarena1 > /tarena/index.html
两台web服务器上配置:(192.168.4.200上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# /etc/init.d/httpd restart
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo changyou2 > /changyou/index.html
[root@localhost 桌面]# echo tarena2 > /tarena/index.html
客户端机器(172.10.10.6)
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena2
[root@localhost 桌面]# curl www.tarena.com
tarena2
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena2
做个说明:当后端的服务器的其中一个宕机后,会被haproxy自动剔除集群,不演示了,但看到效果了。
客户端访问调度器的web页面,查看后端web服务器是否正常:
输入:http://调度器IP或域名/admin?Stats如下图:
输入配置文件中设置的用户名和密码:admin,123456,如下图:(当后台两服务器都正常时)
当web1宕机后,如下图:
当web1恢复启动后,web界面又恢复正常,如下图:
项目2:haproxy机器部署web集群(httpd是虚拟主机情况,健康检查站点网页,当检查到站点有网页才认为正常)
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端能自动按照域名识别) [root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg //健康检查站点网页
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen test
bind 172.10.10.5:80
mode http
stats enable
stats uri /admin?stats //web界面的uri,访问时格式:http://调度IP或域名/admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
option forwardfor //在web网站配置日志中能够看到客户端真实的IP,不好演示,做一个说明即可。
option httpchk HEAD /check.html HTTP/1.0 //当站点目录有check.html才认为网站正常,HEAD也可换成GET
timeout server 15s
timeout connect 15s
option httpclose
cookie SERVERID rewrite
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
server web2 192.168.4.200:80 cookie B maxconn 4096 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上) //此时站点目录没有check.html,但两web服务都启动正常
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP的设置
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common //记录客户端真实IP
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common //记录客户端真实IP
</VirtualHost>
wq
[root@localhost 桌面]# /etc/init.d/httpd restart
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo changyou1 > /changyou/index.html
[root@localhost 桌面]# echo tarena1 > /tarena/index.html
两台web服务器上配置:(192.168.4.200上) //此时站点目录没有check.html,但两web服务都启动正常
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log //记录客户端真实IP
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# /etc/init.d/httpd restart
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo changyou2 > /changyou/index.html
[root@localhost 桌面]# echo tarena2 > /tarena/index.html
客户端机器(172.10.10.6)访问测试: //因为检查不到站点目录的check.html,所以认为不正常,访问不到
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com
[root@localhost 桌面]# curl www.changyou.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@localhost 桌面]# curl www.changyou.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@localhost 桌面]# curl www.tarena.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@localhost 桌面]# curl www.changyou.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
两台web服务器上配置:(192.168.4.100上) //此时站点目录有check.html
[root@localhost 桌面]# touch /changyou/check.html
[root@localhost 桌面]# touch /tarena/check.html
两台web服务器上配置:(192.168.4.200上) //此时站点目录有check.html
[root@localhost 桌面]# touch /changyou/check.html
[root@localhost 桌面]# touch /tarena/check.html
客户端机器(172.10.10.6)访问测试: //因为能检查到站点目录的check.html,所以认为正常,可以访问
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou1
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.changyou.com
changyou2
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena2
[root@localhost 桌面]# curl www.tarena.com
tarena1
[root@localhost 桌面]# curl www.tarena.com
tarena2
[root@localhost 桌面]# curl www.tarena.com
tarena2
做个说明:当后端的服务器的其中一个宕机后,会被haproxy自动剔除集群,不演示了,但看到效果了。
客户端访问调度器的web页面,查看后端web服务器是否正常:
输入:http://调度器IP或域名/admin?Stats如下图:
输入配置文件中设置的用户名和密码:admin,123456,如下图:(当后台两服务器都正常时)
当web1宕机后或者web1正常,但是站点目录无check.html,如下图:
当web1恢复启动且站点目录有check.html后,web界面又恢复正常,如下图:
基于acl控制的调度器设置(无listen,但有frontend和backend)
项目1:基于定义域名acl规则,然后根据不同域名抛给后面对应服务器池中或跳转。
要求:访问www.chagnyou.com时,抛给后面的chagnyou池,访问baidu.com时,跳转到真实的百度(无法测试),默认的IP或域名访问时,默认抛给后面的tarena池。
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端也能识别)
[root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
frontend zdyname
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
bind 172.10.10.5:80
acl bd_zdyname hdr(host) -i baidu.com
acl cy_zdyname hdr(host) -i www.changyou.com
redirect prefix http://www.baidu.com code 301 if bd_zdyname
use_backend changyou if cy_zdyname
default_backend tarena
backend changyou
balance roundrobin
option httpclose
option forwardfor
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
backend tarena
balance roundrobin
option httpclose
option forwardfor
server web2 192.168.4.200:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# echo changyou > /changyou/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
两台web服务器上配置:(192.168.4.200上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo tarena > /tarena/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
客户端访问调度器测试:(172.10.10.6)
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com www.test.com
[root@localhost 桌面]# curl baidu.com //无环境,无法测试。
curl: (6) Couldn't resolve host 'baidu.com'
[root@localhost 桌面]# curl www.changyou.com //当用changyou域名访问时,仅仅解析到changyou池
changyou
[root@localhost 桌面]# curl www.tarena.com
tarena
[root@localhost 桌面]# curl www.test.com //当默认访问时,仅仅解析到tarena池
tarena
[root@localhost 桌面]# curl 172.10.10.5 //当默认访问时,仅仅解析到tarena池
tarena
说明:客户端也可输入:http;//调度器IP或域名/admin?stats来查看后面各个节点是否处于正常状态,用户名和密码为:admin,123456,访问后如下图:
项目2:基于访问站点下的不同目录时,到后面对应池中找服务器的对应站点目录
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端也能识别)
[root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
frontend zdyname
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
bind 172.10.10.5:80
acl zdy_cy path_beg /changyou
acl zdy_tarena path_beg /tarena
use_backend changyou if zdy_cy
use_backend tarena if zdy_tarena
backend changyou
balance roundrobin
option httpclose
option forwardfor
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
backend tarena
balance roundrobin
option httpclose
option forwardfor
server web2 192.168.4.200:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /changyou/changyou -p
[root@localhost 桌面]# echo changyou > /changyou/changyou/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
两台web服务器上配置:(192.168.4.200上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /tarena/tarena -p
[root@localhost 桌面]# echo tarena > /tarena/tarena/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
客户端访问调度器测试:(172.10.10.6)
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com www.test.com
[root@localhost 桌面]# curl www.changyou.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@localhost 桌面]# curl www.tarena.com
<html><body><h1>503 Service Unavailable</h1>
No server is available to handle this request.
</body></html>
[root@localhost 桌面]# curl www.changyou.com/changyou/
changyou
[root@localhost 桌面]# curl www.tarena.com/tarena/
tarena
两台web把各自站点目录都删除:
192.168.4.100上:
[root@localhost 桌面]# rm -rf /changyou/changyou/
192.168.4.200上:
[root@localhost 桌面]# rm -rf /tarena/tarena/
客户端(172.10.10.6)访问调度器,客户端访问如下结果:
[root@localhost 桌面]# curl www.changyou.com/changyou/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /changyou/ was not found on this server.</p>
<hr>
<address>Apache/2.2.15 (Red Hat) Server at www.changyou.com Port 80</address>
</body></html>
[root@localhost 桌面]# curl www.tarena.com/tarena/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /tarena/ was not found on this server.</p>
<hr>
<address>Apache/2.2.15 (Red Hat) Server at www.tarena.com Port 80</address>
</body></html>
说明:客户端也可输入:http;//调度器IP或域名/admin?stats来查看后面各个节点是否处于正常状态,用户名和密码为:admin,123456,访问如下图:
项目3:基于访问站点目录下的扩展名访问资源时,抛给对应后端池服务器访问
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端也能识别)
[root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
frontend zdyname
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
bind 172.10.10.5:80
acl zdy_pic path_end .gif .png .jpg .css .js
acl zdy_static path_end .gif .png .jpg .css
use_backend static_changyou if zdy_pic or zdy_static
backend static_changyou
balance roundrobin
option httpclose
option forwardfor
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
backend tarena
balance roundrobin
option httpclose
option forwardfor
server web2 192.168.4.200:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# mv changyou.jpg /changyou/
[root@localhost 桌面]# ls /changyou/
changyou.jpg
[root@localhost 桌面]# /etc/init.d/httpd restart
两台web服务器上配置:(192.168.4.200上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# mv tarena.jpg /tarena/
[root@localhost 桌面]# ls /tarena/
tarena.jpg
[root@localhost 桌面]# /etc/init.d/httpd restart
客户端访问调度器测试:(172.10.10.6)
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com www.test.com
说明:客户端也可输入:http;//调度器IP或域名/admin?stats来查看后面各个节点是否处于正常状态,用户名和密码为:admin,123456,访问时如下图:
项目4:定义域名acl规则,根据不同域名抛给后面对应服务器池中,但基于端口和IP访问限制做控制
要求:访问www.chagnyou.com时,抛给后面的chagnyou池,默认的IP或域名访问时,默认抛给后面的tarena池。
haproxy机器:(双网卡:eth0:192.168.4.5 eth1:172.10.10.5)(不用配hosts解析,客户端也能识别)
[root@localhost 桌面]# ifconfig eth0 |head -2
eth0 Link encap:Ethernet HWaddr 00:0C:29:90:BC:2C
inet addr:192.168.4.5 Bcast:192.168.4.255 Mask:255.255.255.0
[root@localhost 桌面]# ifconfig eth1 |head -2
eth1 Link encap:Ethernet HWaddr 00:0C:29:90:BC:36
inet addr:172.10.10.5 Bcast:172.10.10.255 Mask:255.255.255.0
[root@localhost 桌面]# vim /etc/sysctl.conf //也可不开路由转发,养成习惯最好
net.ipv4.ip_forward = 1
wq
[root@localhost 桌面]# sysctl -p
[root@localhost 桌面]# yum -y install gcc gcc-c++
[root@localhost 桌面]# ls
haproxy-1.4.24.tar.gz
[root@localhost 桌面]# tar -zxf haproxy-1.4.24.tar.gz
[root@localhost 桌面]# ls
haproxy-1.4.24 haproxy-1.4.24.tar.gz
[root@localhost 桌面]# cd haproxy-1.4.24
[root@localhost haproxy-1.4.24]# ls
CHANGELOG ebtree LICENSE Makefile.osx src TODO
contrib examples Makefile README SUBVERS VERDATE
doc include Makefile.bsd ROADMAP tests VERSION
[root@localhost haproxy-1.4.24]# make TARGET=linux2628 ARCH=x86_64 //编译,target指定内核,架构选择
[root@localhost haproxy-1.4.24]# make PREFIX=/usr/local/haproxy install
[root@localhost haproxy-1.4.24]# cd /usr/local/haproxy/
[root@localhost haproxy]# ls
doc sbin share
[root@localhost haproxy]# mkdir conf logs var/run var/chroot -p //优化目录结构,也可不做,根据需要
[root@localhost haproxy]# ls
conf doc logs sbin share var
安装后链接变量路径和cp配置文件和启动脚本:
[root@localhost haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@localhost haproxy]# mkdir /etc/haproxy
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy]# cp /root/桌面/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy]# /etc/init.d/haproxy start //第一次启动会报错,配置文件模板需改
[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1:514 local0 warning
log 127.0.0.1 local1 notice
chroot /usr/local/haproxy/var/chroot
pidfile /usr/local/haproxy/var/run/haproxy.pid
maxconn 20480
nbproc 8
daemon
group haproxy
user haproxy
spread-checks 3
defaults
log global
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
balance roundrobin
frontend zdyname
mode http
stats enable
stats uri /admin?stats
stats hide-version
stats auth admin:123456
bind 172.10.10.5:80
#acl zdy_valid_ip src 192.168.4.0/24 //也可对网段进行限制,这里是合法的网段
acl zdy_valid_ip src 172.10.10.6 #//这里是合法的IP
block if !zdy_valid_ip #//如果不是合法的IP,则丢弃,不给代理
acl cy_zdyname hdr(host) -i www.changyou.com
use_backend changyou if cy_zdyname
default_backend tarena
backend changyou
balance roundrobin
option httpclose
option forwardfor
server web1 192.168.4.100:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
backend tarena
balance roundrobin
option httpclose
option forwardfor
server web2 192.168.4.200:80 cookie A maxconn 2048 weight 1 check port 80 inter 2000 rise 2 fall 5
wq
[root@localhost haproxy]# useradd -s /sbin/nologin haproxy -M
利用rsyslog配置haproxy记录日志功能: //当看/usr/local/haproxy/logs/haproxy.log可看haproxy日志,只做下说明
[root@localhost haproxy]# echo "local0.* /usr/local/haproxy/logs/haproxy.log" >> /etc/rsyslog.conf
[root@localhost haproxy]# tail -1 /etc/rsyslog.conf
local0.* /usr/local/haproxy/logs/haproxy.log //因为haproxy.cfg中是local0,用这个设备写日志
[root@localhost haproxy]# tail -1 /etc/sysconfig/rsyslog //这个不用动
SYSLOGD_OPTIONS="-c 5" //6.0版以上不用动,5.8版应写为:SYSLOGD_OPTIONS="-c 2 -m 0 -r -x",兼容2版本
[root@localhost haproxy]# /etc/init.d/rsyslog restart //启动记录haproxy日志服务,6.4版启动无端口
[root@localhost haproxy]# vim /etc/rsyslog.conf //解决rsyslog启动服务后无端口问题
$ModLoad imudp //注释取消
$UDPServerRun 514 //注释取消
wq
[root@localhost haproxy]# /etc/init.d/rsyslog restart
[root@localhost haproxy]# netstat -anptu |grep 514
udp 0 0 0.0.0.0:514 0.0.0.0:* 27715/rsyslogd
udp 0 0 :::514 :::* 27715/rsyslogd
[root@localhost haproxy]# /etc/init.d/haproxy start //启动haproxy服务
[root@localhost haproxy]# ps -elf |grep haproxy //查看进程
[root@localhost haproxy]# /etc/init.d/haproxy stop //停止haproxy
//也可以用源码启动,如下:
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c //检查语法
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -D //启动 pkill haproxy //停止
-f指定配置文件,-D后台启动,-q安静的执行,-c检查语法,-n并发连接数,-m使用内存多少M限制,-p指定把pid号写到哪个文件去,-sf平滑重启参数。
两台web服务器上配置:(192.168.4.100上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 1441107787@qq.com
DocumentRoot /changyou
ServerName www.changyou.com
ErrorLog logs/changyou-error_log
CustomLog logs/changyou-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /changyou
[root@localhost 桌面]# echo changyou > /changyou/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
两台web服务器上配置:(192.168.4.200上)
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# /etc/init.d/httpd start
[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b" common //默认如此,修改为下面,把%h修改为:\"%{X-Forwarded-For}i\"
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b" common //记录客户端访问的真实IP
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 14411077@qq.com
DocumentRoot /tarena
ServerName www.tarena.com
ErrorLog logs/tarena-error_log
CustomLog logs/tarena-access_log common
</VirtualHost>
wq
[root@localhost 桌面]# mkdir /tarena
[root@localhost 桌面]# echo tarena > /tarena/index.html
[root@localhost 桌面]# /etc/init.d/httpd restart
客户端访问调度器测试:(172.10.10.6) //成功给代理
[root@localhost 桌面]# vim /etc/hosts
172.10.10.5 www.changyou.com www.tarena.com www.test.com
[root@localhost 桌面]# curl www.changyou.com //当用changyou域名访问时,仅仅解析到changyou池
changyou
[root@localhost 桌面]# curl www.tarena.com
tarena
[root@localhost 桌面]# curl www.test.com //当默认访问时,仅仅解析到tarena池
tarena
[root@localhost 桌面]# curl 172.10.10.5 //当默认访问时,仅仅解析到tarena池
tarena
客户端访问调度器测试:(172.10.10.8) //不给代理,访问不到
[root@localhost 桌面]# curl www.changyou.com
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
[root@localhost 桌面]# curl www.tarena.com
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
[root@localhost 桌面]# curl www.test.com
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
[root@localhost 桌面]# curl 172.10.10.5
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
说明:客户端也可输入:http;//调度器IP或域名/admin?stats来查看后面各个节点是否处于正常状态,用户名和密码为:admin,123456,访问后如下图:
相关推荐
- 提升网络安全 cisco asa5512-k8防火墙促
-
(中关村在线网络安全行情)ciscoasa5512-k8为标准下一代防火墙,能够为中小型网络提供出色的安全防护和流量管控服务。最近这款防火墙设备在京东商城的促销价格为14299元,有需要的用户可以关注...
- 太一星晨:负载均衡性能参数如何测评?
-
海外网2014-08-0816:03:568月6日消息,当前,在云计算和大数据为主导的需求环境下,负载均衡和应用交付已为大型企业数据中心“保驾护航”的必备品。不过,负载均衡作为一种比较“新奇”的设备...
- Nginx架构揭秘:如何用5大核心机制扛住百万级并发
-
1.灵魂拷问:为什么全球Top1000网站中65%选择Nginx?17Nginx不仅是Web服务器,更是高并发架构的核武器。其单机支持10万+并发连接的秘密,源于三大设计哲学:事件驱动模型:非阻塞...
- 高并发场景下,Nginx性能如何提升10倍?
-
大家好,我是mikechen。在高并发场景,Nginx是流量入口的第一道防线,如果想拦截亿级流量,需要Nginx合理调优才能应对@mikechen。本文作者:陈睿|mikechen文章来源:mike...
- 紧急避坑!数据库突现数十GB临时文件?原因与根治方案揭秘
-
引言:某天深夜,运维小王突然收到磁盘爆满的告警,追踪发现Kingbase数据库的syssql_tmp目录竟堆积了数十GB的临时文件!这些神秘文件为何产生?会引发哪些风险?如何彻底根治?本文将带你深入探...
- 互联网大厂后端必看!3 步搞定 Nginx IP 限流,服务器扛住百万流量
-
作为互联网大厂的后端开发人员,你是否曾遇到过这样的场景:服务器突然涌入大量请求,服务响应速度急剧下降,甚至出现崩溃?这时候,Nginx的IP访问限流策略就显得尤为重要。然而,不少开发者在配置N...
- MySQL max_connections 达到最大值 – 我们如何解决它
-
您的网站是否显示MySQLmax_connections达到最大限制错误?通常,当我们尝试连接到MySQL服务器时,MySQLmax_connections值不足会导致“Tooma...
- Nginx百万并发背后技术揭秘!(nginx并发能力是多少)
-
在互联网业务高速发展的今天,用户访问量呈指数级增长,服务器面临的并发压力也越来越大。一个高并发的网站,如果处理不当,可能会出现请求超时、服务器宕机、用户体验下降等问题。Nginx作为当前最流行的高性...
- Nginx底层原理:一文解析Nginx为什么并发数可以达到3w!
-
Nginx以其高性能,稳定性,丰富的功能,简单的配置和低资源消耗而闻名。本文从底层原理分析Nginx为什么这么快!Nginx的进程模型Nginx服务器,正常运行过程中:多进程:一个Mast...
- Nginx合集-并发连接能力优化(nginx高并发调优)
-
一、前言nginx服务器老是报告TIME_WAIT告警,ESTABLISHED告警,检查nginx配置和系统网络配置发现现有的配置并发能力太弱,无法满足现有的并发请求的需求。二、解决方法改进方法...
- 开源OS上安装Gnome Flashback经典桌面
-
1安装GnomeFlashback对于用户来说,相比Unity桌面,GnomeFlashback桌面环境是一个简单的并且不错的选择,可以让你找回过去经典的桌面。GnomeFlashback基于G...
- 新手篇 — 虚拟机系统的使用与常见问题
-
本文章会详细介绍虚拟机系统的使用与常见问题,有很多读者都会遇到这样的情况,软件装不上,自己的电脑中软件很多,又不想换电脑系统,那么虚拟机可以帮你解决这个烦恼,由于文章内容写的比较详细,内容会比较多,可...
- VMware虚拟机与主机之间无法复制粘贴解决
-
问题:VMware安装系统后发现无法直接与主机之间进行复制粘贴了,怎么办?解决办法:按照以下3步进行1、设置中客户机隔离检查2、重新安装VMwareTools3、重启电脑...
- 实现VMware虚拟机与物理主机共享文件夹
-
在安装虚拟机之后,难免会遇到需要将文件从主机拷到虚拟机当中,但是很尴尬的事情就是不能直接将文件从主机拖到虚拟机中,所以只能借助U盘,但是频繁的插拔U盘非常的繁琐。为了解决这一需求,就可以将物理主机和...
- 在 Windows 11 或 10 上安装 Virt-viewer 的单行命令
-
Virt-Viewer(或RemoteViewer)是Redhat提供的一个开源程序,允许用户控制和查看运行在本地或远程服务器上的虚拟机。它体积轻巧,并提供了一个简单的图形用户界面来访问由L...
你 发表评论:
欢迎- 一周热门
-
-
如何在安装前及安装后修改黑群晖的Mac地址和Sn系列号
-
爱折腾的特斯拉车主必看!手把手教你TESLAMATE的备份和恢复
-
[常用工具] OpenCV_contrib库在windows下编译使用指南
-
Ubuntu系统Daphne + Nginx + supervisor部署Django项目
-
WindowsServer2022|配置NTP服务器的命令
-
WIN11 安装配置 linux 子系统 Ubuntu 图形界面 桌面系统
-
极空间如何无损移机,新Z4 Pro又有哪些升级?极空间Z4 Pro深度体验
-
解决Linux终端中“-bash: nano: command not found”问题
-
NBA 2K25虚拟内存不足/爆内存/内存占用100% 一文速解
-
Linux 中的文件描述符是什么?(linux 打开文件表 文件描述符)
-
- 最近发表
- 标签列表
-
- linux 查询端口号 (58)
- docker映射容器目录到宿主机 (66)
- 杀端口 (60)
- yum更换阿里源 (62)
- internet explorer 增强的安全配置已启用 (65)
- linux自动挂载 (56)
- 禁用selinux (55)
- sysv-rc-conf (69)
- ubuntu防火墙状态查看 (64)
- windows server 2022激活密钥 (56)
- 无法与服务器建立安全连接是什么意思 (74)
- 443/80端口被占用怎么解决 (56)
- ping无法访问目标主机怎么解决 (58)
- fdatasync (59)
- 405 not allowed (56)
- 免备案虚拟主机zxhost (55)
- linux根据pid查看进程 (60)
- dhcp工具 (62)
- mysql 1045 (57)
- 宝塔远程工具 (56)
- ssh服务器拒绝了密码 请再试一次 (56)
- ubuntu卸载docker (56)
- linux查看nginx状态 (63)
- tomcat 乱码 (76)
- 2008r2激活序列号 (65)