1:编译安装nginx服务器(两个nginx网站的步骤完全一样,只有测试页面不同)
[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc*
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar zxvf nginx-1.12.0.tar.gz
[root@localhost ~]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make && make install
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/html/
[root@localhost html]# echo "test web01" > test.html
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# nginx -t
[root@localhost ~]# nginx ##开启nginx进程
[root@localhost ~]# netstat -anpt | grep nginx
2:编译安装Haproxy
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc*
[root@localhost ~]# tar zxvf haproxy-1.5.19.tar.gz
[root@localhost ~]# cd haproxy-1.5.19/
[root@localhost haproxy-1.5.19]# make TARGET=linux2628
[root@localhost haproxy-1.5.19]# make install
3:Haproxy服务器配置
(1)建立haproxy配置文件
[root@localhost haproxy-1.5.19]# mkdir /etc/haproxy
[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
(2)Haproxy配置介绍
[root@localhost haproxy-1.5.19]# vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
# redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen webcluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server inst1 192.168.1.61:80 check inter 2000 fall 3
server inst2 192.168.1.62:80 check inter 2000 fall 3
4:创建自启动脚本
[root@localhost haproxy-1.5.19]# cp examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.5.19]# chmod +x /etc/init.d/haproxy
[root@localhost haproxy-1.5.19]# /etc/init.d/haproxy start
5:测试web群集
http://192.168.1.60/test.html
刷新页面进行测试
或使用脚本测试
[root@localhost ~]# for i in $(seq 10); do curl http://192.168.1.60/test.html ;done
6:Haproxy的日志
haproxy在默认情况不会记录日志,除了在haproxy.conf中的global段指定日志的输出外,还需要配置系统日志的配置文件。
简单方法:
修改日志配置文件:
vim /etc/rsyslog.conf
$ModLoad imudp ##取消注释 15行
$UDPServerRun 514 ##取消注释 16行,接收haproxy日志
local0.* /var/log/haproxy.log ##添加,指定local0设备日志存放位置
重启
systemctl restart rsyslog
/etc/init.d/haproxy restart
访问验证
tail -f /var/log/haproxy.log
方法一:
[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
log /dev/log local0 info
log /dev/log local0 notice
[root@localhost haproxy-1.4.24]# touch /etc/rsyslog.d/haproxy.conf
[root@localhost haproxy-1.4.24]# vi /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~
[root@localhost haproxy-1.4.24]# service rsyslog restart
[root@localhost ~]#/etc/init.d/haproxy restart
[root@localhost ~]# cat /var/log/haproxy/haproxy-info.log
方法二:
(1)编辑/etc/haproxy/haproxy.conf
[root@localhost ~]# vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local3
local3是设备,对应于 /etc/rsyslog.conf中的配置,默认回收info的日志级别
(2)编写haproxy日志文件
[root@localhost ~]# vim /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
&~
注释:
$ModLoad imudp 采集日志的协议UDP
$UDPServerRun 514 指定日志采集使用的端口号
local3.* /var/log/haproxy.log 指定日志存放位置
(3)配置rsyslog的主配置文件,开启远程日志(可以不配)
[root@localhost ~]# vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS=”-c 2 -r -m 0″
-c 2 使用兼容模式,默认是 -c 5
-r 开启远程日志
-m 0 标记时间戳。单位是分钟,为0时,表示禁用该功能
(4)重启haproxy和rsyslog服务
[root@localhost ~]# systemctl restart rsyslog
[root@localhost ~]# systemctl restart haproxy
(5)访问网站后查看日志
[root@localhost ~]# cat /var/log/haproxy.log
web:apache、nginx、tomcat
LB:lvs、haproxy、nginx
高可用:
keepalived+lvs
keepalived+haproxy
keepalived+nfs