第一部分 环境准备
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:2
keys_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" in
start)
$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 2
weight -10
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_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 ]; then
service nginx start
else
service 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" in
start)
$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 2
weight -10
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 140
advert_int 1
authentication {
auth_type PASS
auth_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 start
else
/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搭建完成