系统:centos7
Jdk:1.8
Tomcat:8
Ngxin+Keepalived
关闭防火墙或者放行端口
#停止firewall
systemctl stop firewalld.service
#禁止firewall开机启动
systemctl disable firewalld.service
#查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
firewall-cmd --state
#添加放行端口(–permanent永久生效,没有此参数重启后失效)
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
#刷新防火墙 使其生效
firewall-cmd --reload
#查看开发的端口
firewall-cmd --zone=public --list-ports
#查看java列表
yum -y list java*
#安装1.8.0 openjdk(或者官网下载jdk)
yum -y install java-1.8.0-openjdk
#JDK默认安装路径/usr/lib/jvm
ls /usr/lib/jvm
#环境变量配置
vi /etc/profile
#将如下配置添加至文件中,然后保存退出。
# set java environment
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME CLASSPATH PATH
#保存关闭profile文件,执行如下命令生效
source /etc/profile
#使用如下命令,查看JDK变量
echo $JAVA_HOME
echo $PATH
echo $CLASSPATH
yum -y install wget
cd /usr/local
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47.tar.gz
#解压
tar -zxvf apache-tomcat-8.5.47.tar.gz
#移动重命名
mv apache-tomcat-8.5.47 /usr/local/tomcat
#修改配置文件
vi /usr/local/tomcat/bin/catalina.sh
#在 OS specific support. 前面加入如下代码
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
JRE_HOME=$JAVA_HOME/jre
#创建tomcat.service文件
vi /usr/lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=oneshot
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
#允许tomcat开机启动
systemctl enable tomcat
#启动tomcat
systemctl start tomcat.service
#关闭tomcat
systemctl stop tomcat.service
#重启tomcat
systemctl restart tomcat.service
#查看状态tomcat
systemctl status tomcat.service
备注:nginx 负载均衡参数ip_hash:实现同一ip地址下访问项目为同一个服务器,实现session共享机制,缺点:当服务器挂了,已在访问该服务器的用户请求会失效,需要重新访问项目。
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
cd /usr/local/
wget https://nginx.org/download/nginx-1.13.4.tar.gz
tar -zxvf nginx-1.13.4.tar.gz
cd nginx-1.13.4
./configure --prefix=/etc/nginx
make
make install
#创建日志目录
mkdir -p /home/bdxhdata/nginx/logs
#修改配置
whereis nginx
cd /etc/nginx/conf
> nginx.conf
vi nginx.conf
nginx.conf:(配置信息)
worker_processes 1;
error_log /home/bdxhdata/nginx/logs/nginx-error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /home/bdxhdata/nginx/logs/nginx-access.log main;
sendfile on;
keepalive_timeout 65;
upstream bdxh{
ip_hash;
server 192.168.100.11:8080;
server 192.168.100.12:8080;
server 192.168.100.13:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /rsv {
proxy_pass http://bdxh;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
}
}
#启动nginx
/etc/nginx/sbin/nginx
#重启nginx
/etc/nginx/sbin/nginx -s stop
/etc/nginx/sbin/nginx
#查看ip占用
yum -y install net-tools
netstat -tunlp
yum -y install keepalived
> /etc/keepalived/keepalived.conf
vi /etc/keepalived/keepalived.conf
#keepalived.conf 配置:(主服务器配置:state MASTER)
! Configuration File for keepalived
global_defs {
router_id jackfang
}
vrrp_script check_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 100
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.100.10
}
}
#keepalived.conf 配置:(备服务器配置)
state MASTER 改为state BACKUP
priority 100 改小 priority 99
vi /etc/keepalived/nginx_check.sh
#!/bin/bash
A=`netstat -tunlp|grep 80|wc -l`
if [ $A -eq 0 ];then
/etc/nginx/sbin/nginx
sleep 2
if [ `netstat -tunlp|grep 80|wc -l` -eq 0 ];then
killall keepalived
fi
fi
chmod +x /etc/keepalived/nginx_check.sh
service keepalived start
service keepalived restart
service keepalived stop
#查看keepalived运行状态
service keepalived status
mkdir -p /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
#启动keepalived
/usr/local/keepalived/sbin/keepalived
#重启tomcat服务
service tomcat restart
#测试当停止11、12 其中一台服务器时,仍然能访问成功表示实现了高可用配置。