部署web服务器环境
java环境
各服务器准备tomcat运行环境:
# useradd -u 2020 www
# mkdir /apps && cd /apps
# tar xvf jdk-8u181-linux-x64.tar.gz
# ln -sv /apps/jdk1.8.0_181/ /apps/jdk
# vim /etc/profile
export HISTTIMEFORMAT="%F %T whoami
"
export JAVA_HOME=/apps/jdk
export CLASSPATH=.:JAVA_HOME/jre/lib/rt.jar:JAVA_HOME/lib/dt.jar:JAVA_HOME/lib/tools.jar
export PATH=PATH:$JAVA_HOME/bin
# source /etc/profile && java -version
# tar xvf apache-tomcat-8.5.34.tar.gz
# ln -sv /apps/apache-tomcat-8.5.34 /apps/tomcat
准备tomcat启动脚本
# cp /root/tomcatd /etc/init.d/
web部署
部署web服务器并确认各web服务器访问正常:
# useradd -m www -u 2020 -s /bin/bash #ubuntu 系统上创建账户
# useradd www -u 2020 #centos 系统上创建账户
# mkdir /data/tomcat/tomcat_appdir -p #保存web压缩包
# mkdir /data/tomcat/tomcat_webdir #保存解压后的web目录
# mkdir /data/tomcat/tomcat_webapps #tomcat工具目录,存放上面目录中子目录的软链接
# mkdir /data/tomcat/tomcat_webdir/myapp #Java 代码目录
# ln -s /data/tomcat/tomcat_webdir/myapp/ /data/tomcat/tomcat_webapps/myapp
# vim /apps/tomcat/conf/server.xml
# echo 'myapp on <SERVER_IP>' > /data/tomcat/tomcat_webdir/myapp/index.html
# chown -R www.www /apps/jdk/ /apps/tomcat/ /data/tomcat/
# tree /data/
/data/
├── lost+found
└── tomcat
├── tomcat_appdir
├── tomcat_webapps
│ └── myapp -> /data/tomcat/tomcat_webdir/myapp/
└── tomcat_webdir
└── myapp
└── index.html
7 directories, 1 file
配置tomcat配置文件
[root@tomcat ~]#vim /apps/tomcat/conf/server.xml
<Host name="localhost" appBase="/data/tomcat/tomcat_webapps/"
unpackWARs="false" autoDeploy="false">
启动tomcat
cat > /apps/tomcat/conf/tomcat.conf <<EOF
JAVA_HOME=/apps/jdk
JRE_HOME=/apps/jdk/jre
EOF
chown -R www.www /apps/tomcat/
cat > /lib/systemd/system/tomcat.service <<EOF
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/apps/tomcat/conf/tomcat.conf
ExecStart=/apps/tomcat/bin/startup.sh
ExecStop=/apps/tomcat/bin/shutdown.sh
PrivateTmp=true
User=www
Group=www
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now tomcat.service
# /etc/init.d/tomcat start
确认后端各tomcat的web服务器访问正常
部署keepalived
# yum install libnfnetlink-devel libnfnetlink ipvsadm libnl libnl-devel libnl3 libnl3-devel lm_sensors-libs net-snmp-agent-libs net-snmp-libs open server openssh-clients openssl openssl-devel automake iproute
# cd keepalived-2.0.7 && ./configure --prefix=/usr/local/keepalived --disable-fwmark
# make && amke install
# # mkdir /usr/local/keepalived/etc/sysconfig –p && cp keepalived/etc/init.d/keepalived.rh.init /usr/local/keepalived/etc/sysconfig/keepalived
# cp keepalived/keepalived.service /usr/lib/systemd/system/
# mkdir /usr/local/keepalived/sbin && cp bin/keepalived /usr/local/keepalived/sbin/keepalived
# mkdir /etc/keepalived
# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 80
priority 100
advert_int 1
unicast_src_ip 192.168.7.103
unicast_peer {
192.167.7.104
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.7.100 dev eth0 label eth0:0
}
}
部署haproxy
# tar xvf haproxy-1.8.13.tar.gz
# cd haproxy-1.8.13
# make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy
# make install PREFIX=/usr/local/haproxy
# vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
#支持多配置文件读取,类似于从侧面是实现配置文件的include功能。
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -p /run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
# mkdir /etc/haproxy
测试访问
测试haproxy反向代理web服务器:
编辑本机hosts文件,将myapp.web.com解析到对应的IP负载IP:
C:/Windows/System32/drivers/etc/hosts
192.168.7.100 myapp.web.com
记录HAProxy访问日志:
# vim /etc/rsyslog.conf:
14 # Provides UDP syslog reception
15 ModLoad imudp #去掉注释
16UDPServerRun 514 #去掉注释
18 # Provides TCP syslog reception
19 ModLoad imtcp #去掉注释
20InputTCPServerRun 514 #去掉注释
93 local3.* /var/log/haproxy.log
# systemctl restart rsyslog
log 127.0.0.1 local3 info #global部分
listen web_port
bind 0.0.0.0:80
mode http
log global
option httplog
server 192.168.7.103 192.168.7.103:8080 check inter 3000 fall 2 rise 5
server 192.168.7.104 192.168.7.104:8080 check inter 3000 fall 2 rise 5
重启rsyslog和haproxy服务,验证/var/log/haproxy.log可以记录日志:
验证HAProxy统计页面
http://myapp.web.com:9009/haproxy-status
验证haproxy代理web服务器
本文链接:http://www.yunweipai.com/35711.html
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/52605.html