目录
HAProxy是可提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,是免费、快速并且可靠的一种解决方案。HAProxy非常适用于并发大(并发达1w以上)web站点,这些站点通常又需要会话保持或七层处理。HAProxy的运行模式使得它可以很简单安全的整合至当前的架构中,同时可以保护web服务器不被暴露到网络上。
实验准备
systemctl stop firewalld
setenforce 0
Haproxy服务器:192.168.88.22
Nginx 服务器1:192.168.88.40
Nginx 服务器2:192.168.80.50
tomcat 服务器1:192.168.88.51:8080
tomcat 服务器2:192.168.88.51:8081
yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
yum install -y zlib-devel openssl-devel pcre-devel systemd-devel
cd /opt
tar xf haproxy-2.8.3.tar.gz
cd haproxy-2.8.3/
make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1
make install PREFIX=/usr/local/haproxy
mkdir /etc/haproxy
cp /opt/haproxy-2.8.3/examples/quick-test.cfg /etc/haproxy/haproxy.cfg
cp /opt/haproxy-2.8.3/examples/haproxy.init /etc/init.d/haproxy
vim /etc/init.d/haproxy
[ "${NETWORKING}" = "no" ] && exit 0 #26行添加引号
ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
haproxy -v
chmod +x /etc/init.d/haproxy
chkconfig --add /etc/init.d/haproxy
chkconfig --level 35 haproxy on
chkconfig --list haproxy
service haproxy start
netstat -lntp | grep haproxy
cd /etc/haproxy/
vim haproxy.cfg
global
log 127.0.0.1 local0 info
log 127.0.0.1 local1 warning
maxconn 30000
#chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
#nbproc 1
spread-checks 2
defaults
log global
mode http
option http-keep-alive
option forwardfor
option httplog
option dontlognull
option redispatch
option abortonclose
maxconn 20000
retries 3
#contimeout 5000
#clitimeout 50000
#srvtimeout 50000
timeout http-request 2s
timeout queue 3s
timeout connect 1s
timeout client 10s
timeout server 2s
timeout http-keep-alive 10s
timeout check 2
frontend http-in
bind *:80
maxconn 18000
acl url_static path_beg -i /static
acl url_dynamic path_end -i .jsp
use_backend yy if url_static
use_backend cc if url_dynamic
default_backend yy
backend yy
balance roundrobin
option httpchk GET /cc.html
server static_inst01 192.168.88.40:80 check maxconn 10000 inter 2000 rise 2 fall 3
server static_inst02 192.168.88.50:80 check maxconn 10000 inter 2000 rise 2 fall 3
backend cc
balance roundrobin
option http-server-close
cookie HA_STICKY_dy insert insert indirect nocache
server dynamic_inst01 192.168.88.51:8080 cookie appserver1 check
server dynamic_inst02 192.168.88.51:8081 cookie appserver1 check
listen stats
bind *:1080
stats enable
stats refresh 30s
stats uri /stats
stats realm HAProxy\ Stats
stats auth 11:11
haproxy -c -f haproxy.cfg
service haproxy restart
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
useradd -M -s /sbin/nologin nginx
cd /opt
tar -xf nginx-1.12.0.tar.gz
cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
##优化路径##
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
##添加 Nginx 系统服务##
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
cd /usr/local/nginx/html
vim cc.html
this is cc1
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
useradd -M -s /sbin/nologin nginx
cd /opt
tar -xf nginx-1.12.0.tar.gz
cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
##优化路径##
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
##添加 Nginx 系统服务##
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
cd /usr/local/nginx/html
vim cc.html
this is cc2
##jdk升级##
mkdir -p /usr/java
mv jdk1.8.0_361/ /usr/java
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_361
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
source /etc/profile.d/java.sh
java -version
##tomcat双实例部署##
cd /opt
tar xf apache-tomcat-9.0.16.tar.gz
mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1
cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2
vim /etc/profile.d/tomcat.sh
#tomcat1
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1
#tomcat2
export CATALINA_HOME2=/usr/local/tomcat/tomcat2
export CATALINA_BASE2=/usr/local/tomcat/tomcat2
export TOMCAT_HOME2=/usr/local/tomcat/tomcat2
source /etc/profile.d/tomcat.sh
vim /usr/local/tomcat/tomcat2/conf/server.xml
#22行,修改Server prot,默认为8005 -> 修改为8006
<Server port="8006" shutdown="SHUTDOWN">
#69行,修改Connector port,HTTP/1.1 默认为8080 -> 修改为8081
<Connector port="8081" protocol="HTTP/1.1"
#116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
vim /usr/local/tomcat/tomcat1/bin/startup.sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat/tomcat1/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat/tomcat2/bin/startup.sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
vim /usr/local/tomcat/tomcat2/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
/usr/local/tomcat/tomcat1/bin/startup.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
netstat -natp | grep java
cd /usr/local/tomcat/tomcat1/webapps/
mkdir yy
echo "this is yy1" > yy/yy.jsp
cd /usr/local/tomcat/tomcat2/webapps/
mkdir yy
echo "this is yy2" > yy/yy.jsp
##方法一##
vim /etc/haproxy/haproxy.cfg
global
log /dev/log local0 info
log /dev/log local0 notice
......
defaults
......
log global
......
#需要修改rsyslog配置,为了便于管理。将haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d/下,rsyslog启动时会自动加载此目录下的所有配置文件。
vim /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
&~
#这部分配置是将haproxy的info日志记录到/var/log/haproxy/haproxy-info.log下,将notice日志记录到/var/log/haproxy/haproxy-notice.log下。“&~”表示当日志写入到日志文件后,rsyslog停止处理这个信息。
service rsyslog restart
service haproxy restart
tail -f /var/log/haproxy/haproxy-info.log #查看haproxy的访问请求日志信息
##方法二##
#修改haproxy.cfg,将info及以上级别的日志发送到rsyslog的local0接口,将warning及以上级别的日志发送到rsyslog的local1接口
vim /etc/haproxy/haproxy.cfg
global
......
log 127.0.0.1 local0 info
log 127.0.0.1 local1 warning
......
defaults
......
log global
......
#注:信息级日志会打印HAProxy 的每一条请求处理,会占用大量的磁盘空间,在生产环境中,将日志级别调整为notice
#为 rsyslog 添加 haproxy 日志的配置
mkdir /var/log/haproxy
vim /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
$FileCreateMode 0644 #日志文件的权限
$FileOwner haproxy #日志文件的owner
local0.* /var/log/haproxy/haproxy.log #local0接口对应的日志输出文件
local1.* /var/log/haproxy/haproxy_warn.log #local1接口对应的日志输出文件
#修改 rsyslog 的启动参数
vim /etc/sysconfig/rsyslog
......
SYSLOGD_OPTIONS="-c 2 -r -m 0"
#重启 rsyslog 和 HAProxy
service rsyslog restart
service haproxy restart
tail -f /var/log/haproxy/haproxy.log