#1:java 环境: 各 wenb 服务器准备 tomcat 运行环境:
# useradd www -u 2000
#mkdir/apps&&cd /apps
# tar xvfjdk-8u181-linux-x64.tar.gz
# ln -sv /apps/jdk1.8.0_181//apps/jdk
# vim/etc/profile
export HISTTIMEFORMAT="%F %T `whoami` "
export export LANG="en_US.utf-8"
exportJAVA_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
#2:准备 tomcat 启动脚本:
# cp /root/tomcatd/etc/init.d/
#3:web 部署:
#部署 web 服务器并确认各 web 服务器访问正常:
# useradd -m www -u 2019 -s /bin/bash #创建账户
# useradd www -u 2019 #centos 创建账户
# mkdir /data/tomcat/tomcat_appdir -p #保存 web 压缩包
# mkdir /data/tomcat/tomcat_webdir #保存解压后的 web 目录
# mkdir /data/tomcat/tomcat_webdir/myapp
# echo SERVER_IP > /data/tomcat/tomcat_webdir/myapp/index.html
#4:配置 tomcat 配置文件:
appBase="/data/tomcat_webdir/"
#5:启动 tomcat:
# /etc/init.d/tomcat start
#6:确认各 web 服务器访问正常:
#7:部署 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 openssldevel 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
}
}
#8:部署 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
#9:测试访问:
#测试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 #去掉注释
16 $UDPServerRun 514 #去掉注释
18 # Provides TCP syslog reception
19 $ModLoad imtcp #去掉注释
20 $InputTCPServerRun 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 可以记录日志:
#10:验证 HAProxy 统计页面:
http://myapp.web.com:9009/haproxy-status
#11:验证 haproxy 代理 web 服务器:
http://myapp.web.com/myapp/