nginx+keepalived+tomcat
第一部分 环境准备
nginx+keepalived服务器两台(调度器,双机热备)
IP地址192.168.80.100(lvs01 主机名zlf1)
192.168.80.101(lvs02 主机名zlf2)
软件需求:nginx安装包(nginx-1.13.5.tar.gz)keepalived安装包(keepalived-2.0.7.tar.gz)
第二部分部署调度器搭建nginx+keepalived(双机热备)
以下在两台nginx调度服务器上操作
第一步:配置主服务器(192.168.80.100 )
-------安装nginx服务-------
yum install -y gcc gcc-c++ make openssl-devel zlib-devel pcre-devel
//安装编译工具及插件
useradd -s /sbin/nologin -M nginx
//创建程序用户
tar xf nginx-1.13.5.tar.gz -C /opt/
cd /opt/nginx-1.13.5/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
--with-file-aio --with-http_flv_module --with-http_stub_status_module
--with-http_ssl_module --with-http_gzip_static_module
--with-http_realip_module
make && make install
//编译安装
以下编译nginx主配置文件
vi /usr/local/nginx/conf/nginx.conf
//删除内容添加以下的内容
user nginx nginx;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;pid logs/nginx.pid;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 logs/access.log main;add_header X-Server $hostname;server_names_hash_bucket_size 128;server_name_in_redirect off;sendfile on;tcp_nopush on;tcp_nodelay on;#keepalive_timeout 0;keepalive_timeout 65;client_header_buffer_size 32k;large_client_header_buffers 4 128k;client_max_body_size 512m;open_file_cache max=65535 inactive=20s;open_file_cache_valid 30s;open_file_cache_min_uses 1;gzip on;gzip_static on;gzip_http_version 1.1;gzip_comp_level 2;gzip_min_length 1024;gzip_vary on;gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;server_tokens off;fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 512k;fastcgi_buffers 6 512k;fastcgi_busy_buffers_size 512k;fastcgi_temp_file_write_size 512k;fastcgi_intercept_errors on;client_body_buffer_size 128k;proxy_connect_timeout 600;proxy_read_timeout 600;proxy_send_timeout 600;proxy_buffer_size 32k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 2m;proxy_ignore_client_abort on;proxy_cache_path /usr/local/nginx/cache_temp levels=2:2keys_zone=cache_temp:128m inactive=30m max_size=2g;proxy_cache_valid 200 302 10m;include /usr/local/nginx/conf/conf.d/*.conf;server {listen 80;server_name localhost;#charset koi8-r;charset UTF-8;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
cd /usr/local/nginx/conf/
//以下编辑子配置文件
mkdir conf.d
//创建子配置项
cd conf.d/
//切换工作目录
vi lvs01.conf
//新建子配置文件
server {listen 80;server_name lvs01 192.168.80.100; //服务器名称与IP地址index index.html index.jsp;root /usr/local/nginx/html;access_log /usr/local/nginx/logs/tomcat.aa.com_access.log main;location ~ .*\.jsp$ {index index.jsp;proxy_set_header HOST $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header Client-IP $remote_addr;proxy_set_header X-For $proxy_add_x_forwarded_for;proxy_pass http://center_pool;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {expires 30d;proxy_pass http://center_pool;}location ~ .*\.(js|css)?$ {expires 1h;proxy_pass http://center_pool;}
}
vi pool.conf
//创建服务器池
upstream center_pool {server 192.168.80.102:8080;server 192.168.80.103:8080;
}
vi /etc/init.d/nginx
//制作启动脚本
#!/bin/bash
# chkconfig: 35 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" instart)$PROG;;stop)kill -s QUIT $(cat $PIDF);;restart)$0 stop$0 start;;reload)kill -s HUP $(cat $PIDF);;*)echo "Usage: $0 {start|stop|restart|reload}"exit 1
esac
exit 0
chmod +x /etc/init.d/nginx
//增加执行权限
chkconfig --add nginx
//加入系统管理服务
service nginx start
//启动nginx服务
netstat -anpt | grep nginx
//查看监听端口
部署keepalived
yum -y install popt-devel kernel-devel openssl-devel
//安装前的环境库
tar xf keepalived-2.0.7.tar.gz -C /opt
//解压
cd /opt/keepalived-2.0.7/
//切换工作目录
./configure --prefix=/
//指定安装到根目录下
make && make install
//编译与安装
cp keepalived/etc/init.d/keepalived /etc/init.d/
//将keepalived脚本放在service启动程序中
systemctl enable keepalived && systemctl start keepalived
//设置开机自启
//以下编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf
进入删除里面插入下面内容 lvs2 对应BACKUP
! Configuration File for keepalived
global_defs {route_id NGINX-01}
vrrp_script nginx {script "/opt/nginx.sh"interval 2weight -10
}
vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 51priority 150advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {nginx}virtual_ipaddress {192.168.80.188}
}
vi /opt/nginx.sh
//判断keepalived进程是否存在,在就启动nginx不在就关闭
#!/bin/bash
#Filename:nginx.sh
A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $A -gt 0 ]; thenservice nginx start
elseservice nginx stop
fi
chmod +x /opt/nginx.sh
//添加执行权限
systemctl restart keepalived
//重启keepalived服务
ip addr show dev ens33
//查看漂移地址是否生成
---------------测试验证------------
systemctl stop keepalived
//关闭keepalived服务
yum install psmisc -y
//安装killall命令
killall -9 nginx
//关闭nignx服务
netstat -anpt | grep nginx
//80端口已停止运行
systemctl start keepalived
//开启keepalived服务
netstat -anpt | grep nginx
//nginx随keepalived启动
cat /var/log/messages
//查看日志
验证成功
第二步:配置从服务器(192.168.80.101)
安装nginx服务与主服务器一样省略过程
vi /usr/local/nginx/conf/nginx.conf
//以下编译nginx主配置文件(和主服务器配置一样)
以下编辑子配置文件(注意与主服务器不同项)
cd /usr/local/nginx/conf/
mkdir conf.d
cd conf.d/
vi lvs02.conf
//新建子配置文件
server {listen 80;server_name lvs02 192.168.80.101;index index.html index.jsp;root /usr/local/nginx/html;access_log /usr/local/nginx/logs/tomcat.aa.com_access.log main;location ~ .*\.jsp$ {index index.jsp;proxy_set_header HOST $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header Client-IP $remote_addr;proxy_set_header X-For $proxy_add_x_forwarded_for;proxy_pass http://center_pool;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {expires 30d;proxy_pass http://center_pool;}location ~ .*\.(js|css)?$ {expires 1h;proxy_pass http://center_pool;}
}
vi pool.conf
//创建服务器池
upstream center_pool {server 192.168.80.102:8080;server 192.168.80.103:8080;
}
vi /etc/init.d/nginx
//制作启动脚本
#!/bin/bash
# chkconfig: 35 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" instart)$PROG;;stop)kill -s QUIT $(cat $PIDF);;restart)$0 stop$0 start;;reload)kill -s HUP $(cat $PIDF);;*)echo "Usage: $0 {start|stop|restart|reload}"exit 1
esac
exit 0
chmod +x /etc/init.d/nginx
//增加执行权限
chkconfig --add nginx
//加入系统管理服务
service nginx start
//启动nginx服务
netstat -anpt | grep nginx
//查看服务监听端口
部署keepalived
测试验证
查看从服务器状态
yum -y install popt-devel kernel-devel openssl-devel
tar xf keepalived-2.0.7.tar.gz -C /opt/
cd /opt/keepalived-2.0.7/
./configure --prefix=/
make && make install
cp keepalived/etc/init.d/keepalived /etc/init.d/
vi /etc/keepalived/keepalived.conf
//进入删除里面插入下面内容
! Configuration File for keepalived
global_defs {route_id NGINX-02}
vrrp_script nginx {script "/opt/nginx.sh"interval 2weight -10
}
vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 140advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {nginx}virtual_ipaddress {192.168.80.188}
}
vi /opt/nginx.sh
//创建nginx启动脚本
#!/bin/bash
A=$(ip addr | grep 192.168.80.188/32 | grep -v grep | wc -l)
if [ $A -gt 0 ]; then/etc/init.d/nginx startelse/etc/init.d/nginx stop
fi
chmod +x /opt/nginx.sh
//添加权限
systemctl start keepalived
//开启服务
[root@lvs02 ~]# ip addr show dev ens33
//查看漂移地址
由于主服务器在运行,漂移地址并未同步过来
当主服务器在运行的时候,从服务器虚拟地址并未生成,nginx服务并未随keepalived启动
二:模拟主服务故障
- 主服务器
service keepalived stop
//关闭服务
killall -9 nginx
//杀死nginx服务
netstat -anpt | grep 80
//80端口已经不再运行了
[root@lvs01 ~]#ip addr show dev ens33
漂移地址消失,不工作
2. 从服务器
[root@lvs02 ~]# ip addr show dev ens33 //查看漂移地址
//漂移地址自动生成
netstat -anpt | grep nginx
//服务器检测到虚拟地址,nginx服务自动启动开始工作
三:模拟主服务器恢复工作
- 主服务器
service keepalived start
ip addr show dev ens33
netstat -anpt | grep nginx
//主服务器已恢复工作
2. 从服务器
ip addr show dev ens33
//查看地址
//漂移地址自动移除
netstat -anpt | grep 80
//nginx自动停止服务
//双机热备验证成功
第三部分 部署服务器池—搭建Tomcat
//以下在两台tomcat服务器上操作
第一步:部署第一个节点服务器TM01(192.168.80.30)
----------部署java环境,jdk---------
在这里插入图片描述
tar xf jdk-8u144-linux-x64.tar.gz -C /opt/
//解压jdk
cd /opt
//切换工作目录
cp -rf jdk1.8.0_144/ /usr/local/java
//创建java源目录
vi /etc/profile
//进入环境配置文件中
最下面添加以下内容:
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
source /etc/profile
//立即生效
java -version
//查看版本
java环境部署完成
----------部署tomcat----------
tar xf apache-tomcat-8.5.34.tar.gz -C /opt/
//解压apache-tomcat
cd /opt
//切换工作目录
cp -rf apache-tomcat-8.5.34/ /usr/local/tomcat8
//创建tomcat源目录
//做个软链接,使tomcat开启与关闭更加方便
ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
//开启
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
//关闭
tomcatup
//开启tomcat
netstat -anpt | grep 8080
//查看监听端口
-----------验证------------
- 浏览器访问默认主页:http://192.168.80.102:8080
tomcat部署成功
2. 服务器池中有两台tomcat服务器,为了便于识别,主页上添加点标记
vi /usr/local/tomcat8/webapps/ROOT/index.jsp
//进入tomcat的jsp页面中
添加一行内容:
35gg找到body在下面插入<h1>service AA</h1>
再次访问默认主页http://192.168.80.102:8080
第二步:部署第二个节点服务器TM02(192.168.80.103:8080)
第三步:验证nginx调度器漂移地址轮询点击刷新实现轮询效果
网页访问http://192.168.80.188/index.jsp
到此nginx+keepalived+tomcat搭建完成
nginx+keepalived+tomcat相关推荐
- Nginx+Keepalived+Tomcat之动静分离的web集群
为小公司提供大概一天持续在100万/日之间访问的高性能.高可用.高并发访问及动静分离的web集群方案 Nginx+Keepalived 高可用.反向代理 ...
- nginx+keepalived+tomcat+memcache负载均衡搭建小集群
最近一段时间一直在研究高可用高并发负载均衡分布式集群等技术,先前发布了lvs基于网络第四次协议搭建的小集群,现在用空刚好搭建了一个基于nginx搭建的小集群. 我准备了四台机器,情况如下 机器名称 机 ...
- Nginx+keepalived 实现高可用,防盗链及动静分离配置
一.Nginx Rewrite 规则 1. Nginx rewrite规则 Rewrite规则含义就是某个URL重写成特定的URL(类似于Redirect),从某种意义上说为了美观或者对搜索引擎友好, ...
- nginx+keepalived安装配置(整理中)
线上环境安装测试说明 两台测试机 nginx+keepalived做后端LNMP及tomcat+jdk+mysql的负载高可用 测试环境 centos 6.2 x86_64 IP:192.168.10 ...
- Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)...
一.Nginx Rewrite 规则 1. Nginx rewrite规则 Rewrite规则含义就是某个URL重写成特定的URL(类似于Redirect),从某种意义上说为了美观或者对搜索引擎友好, ...
- Nginx+keepalived 实现高可用,防盗链及动静分离配置详解(值得收藏)
作者:小不点啊 www.cnblogs.com/leeSmall/p/9356535.html 一.Nginx Rewrite 规则 1. Nginx rewrite规则 Rewrite规则含义就是某 ...
- nginx+keepalived构建主备负载均衡代理服务器
一.架构 二.唠叨一会原理: 1.nginx Nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永 ...
- nginx+keepalived高可用web架构
nginx+keepalived高可用web架构 1.下载所需的软件包 (1).keepalived软件包keepalived-1.1.20.tar.gz (2).nginx软件包nginx-1.1. ...
- Nginx+keepalived负载均衡高可用篇第③版
Nginx+keepalived负载均衡高可用篇第③版 对付中.小型企业,假如没有资金去购买昂贵的四/七层负载均衡交换机,那么Nginx是不错的七层负载均衡选择,并且可以通过Nginx + Keepa ...
最新文章
- 带你彻彻底底搞懂朴素贝叶斯公式
- Sql Server2005 Transact-SQL 新兵器学习总结之-数据类型
- 能做存储的超级计算机——任宇翔和以色列团队的创业故事
- 面向对象JS编程(一)——创建对象
- 前端学习(3121):组件和模块
- Python面试题-交换两个数字的三种方法
- android 贝塞尔曲线_OpenGL 实践之贝塞尔曲线绘制
- 【线性代数本质】3:矩阵和线性变换的本质
- android 静态编译链接,Android NDK:使用预编译的静态库链接
- 摩拜不死,已入美团!
- 网络安全管理的“模拟人生”
- 计算机应用基础教学计划第二学期,计算机应用基础教学计划(中职) (1)
- Linux内核分析与驱动编程-1
- 凭据分配没有加密oracle_两种方法解决远程桌面出现“这可能是由于CredSSP加密Oracle修正”的问题-网络教程与技术
-亦是美网络...
- 抽象类是不是必须要有抽象方法
- vue.js毕业设计,基于vue.js前后端分离电影院售票小程序系统设计与实现
- 抗DDOS产品性价比?
- 期货市场之反转形态分析
- 视频工厂分享vlog拍摄技巧
- 自定义java对象转换工具类
热门文章
- 发送给客户的文件,怕泄漏怎么办?
- python将数字转换为中文_Python:将数字转换为文字
- python象棋编程_Python开发象棋小游戏(绘制棋盘)
- linux gic驱动
- php 文章页面阅读全文,给WordPress文章内容页增加阅读全文展开功能
- “插座”,“充电宝”,“数据线”用英语怎么说?
- ipados链接android,iPadOS怎么升级 iPadOS升级教程
- cf网络原因服务器无响应,CF老出现网络出现异常,与服务器断开
- Macbook上如何调整Windows分区大小,NTFS-FAT-FAT32
- cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-buil windows下的解决方案