Haproxy (20.0.0.7)
nginx1 (20.0.0.18:80)
nginx2 (20.0.0.19:80)
tomcat123 (20.0.0.20:8080 20.0.0.20:8081 20.0.0.20:8082)
systemctl stop firewalld
setenforce 0
cd /opt
haproxy-1.5.19.tar.gz
yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
tar zxvf haproxy-1.5.19.tar.gz
cd haproxy-1.5.19/
make TARGET=linux2628 ARCH=x86_64
make install
使用uname -r查看内核,如:2.6.18-371.el5,此时该参数用TARGET=linux26;kernel大于2.6.28的用TARGET=linux2628
mkdir /etc/haproxy
cp examples/haproxy.cfg /etc/haproxy/
cd /etc/haproxy/
vim haproxy.cfg
global
log /dev/log local0 info
log /dev/log local0 notice
#log loghost local0 info
maxconn 4096 #最大连接数,需考虑ulimit-n限制
–8行–注释,chroot运行路径,为该服务自设置的根目录,一般需将此行注释掉
#chroot /usr/share/haproxy
uid 99 #用户UID
gid 99 #用户GID
daemon #守护进程模式
defaults
log global #定义日志为global配置中的日志定义
mode http #模式为http
option httplog #采用http日志格式记录日志
option dontlognull #不记录健康检查日志信息
retries 3 #检查节点服务器失败次数,连续达到三次失败,则认为节点不可用
redispatch #当服务器负载很高时,自动结束当前队列处理比较久的连接
maxconn 2000 #最大连接数
contimeout 5000 #连接超时时间
clitimeout 50000 #客户端超时时间
srvtimeout 50000 #服务器超时时间
删除下面所有listen项–,添加
listen webcluster 0.0.0.0:80
option httpchk GET /index.html #检查服务器的test.html文件
balance roundrobin #负载均衡调度算法使用轮询算法roundrobin
server inst1 20.0.0.18:8080 check inter 2000 fall 3 #定义在线节点 监听nginx监听的8080端口
server inst2 20.0.0.19:8080 check inter 2000 fall 3
cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy
chkconfig --add /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
service haproxy start 或 /etc/init.d/haproxy start
systemctl stop firewalld
setenforce 0
yum install -y pcre-devel zlib-devel gcc gcc-c++ make
useradd -M -s /sbin/nologin nginx
cd /opt
tar zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2/
./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 #启动nginx 服务
这次是在一台虚拟机上装 三个tomcat
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
cd /opt
rpm -qpl jdk-8u201-linux-x64.rpm #查询指定软件包的文件列表
rpm -ivh jdk-8u201-linux-x64.rpm #安装软件包,显示详细信息
java -version #查看java版本
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mkdir /usr/local/tomcat
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
#tomcat3
export CATALINA_HOME3=/usr/local/tomcat/tomcat3
export CATALINA_BASE3=/usr/local/tomcat/tomca3
export TOMCAT_HOME3=/usr/local/tomcat/tomca3
source /etc/profile.d/tomcat.sh
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile.d/java.sh
java -version
cp /usr/local/tomcat/tomcat2/conf/server.xml /usr/local/tomcat/tomc
at2/conf/server.xml.bak #备份
vim /usr/local/tomcat/tomcat2/conf/server.xml
#22行,修改Server prot,默认为8005 -> 修改为8006
修改为8081
#116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
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
vim /usr/local/tomcat/tomcat3/bin/startup.sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE3
export CATALINA_HOME=$CATALINA_HOME3
export TOMCAT_HOME=$TOMCAT_HOME3
vim /usr/local/tomcat/tomcat3/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE3
export CATALINA_HOME=$CATALINA_HOME3
export TOMCAT_HOME=$TOMCAT_HOME3
/usr/local/tomcat/tomcat1/bin/startup.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
/usr/local/tomcat/tomcat3/bin/startup.sh
netstat -natp | grep java
Tomcat 页面 配置
mkdir /usr/local/tomcat/tomcat1/webapps/sdh
mkdir /usr/local/tomcat/tomcat2/webapps/sdh
mkdir /usr/local/tomcat/tomcat3/webapps/sdh
vim /usr/local/tomcat/tomcat1/webapps/sdh/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
JSP test2 page
<% out.println("动态页面:这是tomcat1");%>
vim /usr/local/tomcat/tomcat2/webapps/sdh/index.jsp
vim //usr/local/tomcat/tomcat3/webapps/sdh/index.jsp
vim /usr/local/tomcat/tomcat1/conf/server.xml
注释148-149行,160-164行,删除前面的HOST配置
164行下插入:
vim /usr/local/tomcat/tomcat2/conf/server.xml
vim /usr/local/tomcat/tomcat3/conf/server.xml
/usr/local/tomcat/tomcat1/bin/shutdown.sh
/usr/local/tomcat/tomcat2/bin/shutdown.sh
/usr/local/tomcat/tomcat3/bin/shutdown.sh
/usr/local/tomcat/tomcat1/bin/startup.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
/usr/local/tomcat/tomcat3/bin/startup.sh
./configure --with-stream #不增加这个模块后面的反向代理配置识别不了
make && yum install
vim /usr/local/nginx/conf/nginx.conf
在http上面配置
stream {
upstream appserver {
server 20.0.0.20:8080 weight=1;
server 20.0.0.20:8081 weight=1;
server 20.0.0.20:8082 weight=1;
}
server {
listen 8080;
proxy_pass appserver;
}
}
默认haproxy的日志是输出到系统的syslog中,查看起来不是非常方便,为了更好的管理haproxy的日志,我们在生产环境中一般单独定义出来。需要将haproxy的info及notice日志分别记录到不同的日志文件中。
需要修改syslog配置,为了便于管理。将haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d/下,syslog启动时会自动加载此目录下的所有配置文件。
vim /etc/haproxy/haproxy.cfg
#更改日志文件
global
log /dev/log local0 info
log /dev/log local0 notice
#重启haproxy服务
service haproxy restart
#为了便于管理将haproxy相关的配置独立定义到haproxy.conf
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停止处理这个信息。
#重启服务
systemctl restart rsyslog.service
emerg 0 系统不可用
alert 1 必须马上采取行动的事件
crit 2 关键的事件
err 3 错误事件
warning 4 警告事件
notice 5 普通但重要的事件
info 6 有用的信息
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停止处理这个信息。
#重启服务
systemctl restart rsyslog.service
[外链图片转存中...(img-xxikge0B-1663748581970)]
emerg 0 系统不可用
alert 1 必须马上采取行动的事件
crit 2 关键的事件
err 3 错误事件
warning 4 警告事件
notice 5 普通但重要的事件
info 6 有用的信息
debug 7 调试信息