nginx1 : 20.0.0.13
nginx2 : 20.0.0.14
tomcat1 : 20.0.0.15
tomcat2 : 20.0.0.16
nfs : 20.0.0.17
client : 20.0.0.18
在nfs上
[root@nfs ~]# systemctl stop firewalld
[root@nfs ~]# setenforce 0
[root@nfs ~]# mkdir /web1
[root@nfs ~]# mkdir /web2
[root@nfs ~]# cd /web1
[root@nfs web1]# vi index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<html>
<head>
<title>tomcat1</title>
</head>
<body>
<% out.println("this is tomcat1 server");%>
<div>动态页面</div><br/><img src="1.jpg" />
</body>
</html>
[root@nfs web1]# cp index.jsp /web2
[root@nfs web1]# cd /web2
[root@nfs web2]# vi index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<html>
<head>
<title>tomcat2</title>
</head>
<body>
<% out.println("this is tomcat2 server");%>
<div>动态页面</div><br/><img src="2.jpg" />
</body>
</html>
[root@nfs web2]#cd
[root@nfs ~]# vi /etc/exports
/web1 20.0.0.15(ro)
/web2 20.0.0.16(ro)
[root@nfs ~]# systemctl start nfs
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# showmount -e
[root@tomcat1 ~]# systemctl stop firewalld
[root@tomcat1 ~]# setenforce 0
[root@tomcat1 ~]# ll
[root@tomcat1 ~]# tar zxvf apache-tomcat-8.5.16.tar.gz #解压软件包
[root@tomcat1 ~]# tar zxvf jdk-8u91-linux-x64.tar.gz
[root@tomcat1 ~]# mv jdk1.8.0_91/ /usr/local/java
[root@tomcat1 ~]# vi /etc/profile
在末尾添加
export JAVA_HOME=/usr/local/java # 设置全局变量
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre #设置环境变量
[root@tomcat1 ~]# source /etc/profile
[root@tomcat1 ~]# echo $PATH
[root@tomcat1 ~]# java -version #查看版本
[root@tomcat1 ~]# vi abc.java #编写一个Java小程序来验证
public class abc {
public static void main (String[] args) {
System.out.println("你好。。。。。");
}
}
[root@tomcat1 ~]# javac abc.java #编译
[root@tomcat1 ~]# java abc
#可以显示出测试内容,则Java组件是好的
先解压缩安装包
[root@tomcat1 ~]# mv apache-tomcat-8.5.16/ /usr/local/tomcat8
[root@tomcat1 ~]# mkdir /webapp1
[root@tomcat1 ~]# mount 20.0.0.17:/web1 /webapp1 #挂载
[root@tomcat1 ~]# df -Th
[root@tomcat1 ~]# vi /usr/local/tomcat8/conf/server.xml #站点目录
[root@tomcat1 ~]# vi /usr/local/tomcat8/conf/server.xml
添加
<Context docBase="/webapp1" path="" reloadable="false">
</Context>
验证
[root@tomcat1 ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@tomcat1 ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@tomcat1 ~]#tomcatup
[root@tomcat1 ~]# netstat -anpt | grep 8080
[root@tomcat1 ~]# curl http://localhost:8080
在tomcat2上
步骤与部署tomcat1一致
修改web1,webapp1为web2,webapp2即可
在webapp2文件中加入
访问测试
部署调度器
在nginx1上部署
先添加nginx,keepalived安装包
[root@nginx1 ~]# ls -lh
[root@nginx1 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@nginx1 ~]# useradd -M -s /sbin/nologin nginx #创建不可登录,不带宿主的用户
[root@nginx1 ~]# tar zxvf nginx-1.12.2.tar.gz # 解压缩
[root@nginx1 ~]# cd nginx-1.12.2/
[root@nginx1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx #安装路径,属主,属组设置
[root@nginx1 nginx-1.12.2]# make && make install # 编译安装
[root@nginx1 nginx-1.12.2]# ln -s /usr/local/nginx/conf/nginx.conf /etc/ #创建设置链接
[root@nginx1 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ # 创建命令链接
[root@nginx1 nginx-1.12.2]# cd
[root@nginx1 ~]# vi /usr/local/nginx/conf/nginx.conf
upstream tomcat_server {
server 20.0.0.15:8080 weight=1;
server 20.0.0.16:8080 weight=1;
}
location ~ \.jsp$ {
proxy_pass http://tomcat_server;
}
[root@nginx1 ~]# nginx -t #检查语法
[root@nginx1 ~]# nginx #服务启动
[root@nginx1 ~]# netstat -anpt | grep 80 # 查看端口状态
[root@nginx1 ~]# modprobe ip_vs #加载ip_vs模块
[root@nginx1 ~]# yum -y install ipvsadm.x86_64 #安装管理软件ipvsadm
[root@nginx1 ~]# yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel #安装编译工具及插件
[root@nginx1 ~]# tar zxvf keepalived-2.0.13.tar.gz #解压keepalived安装包
[root@nginx1 ~]# cd keepalived-2.0.13/
[root@nginx1 keepalived-2.0.13]# ./configure --prefix=/ #配置安装到/目录下
[root@nginx1 keepalived-2.0.13]# make && make install #编译与安装
[root@nginx1 keepalived-2.0.13]# cp keepalived/etc/init.d/keepalived /etc/init.d/ #加入系统管理服务,用以支持systemctl管理
[root@nginx1 keepalived-2.0.13]# systemctl enable keepalived #设置开机启动
[root@nginx1 keepalived-2.0.13]# vi /etc/keepalived/keepalived.conf #编辑配置文件
删除所有
添加
! Configuration File for keepalived
global_defs {
router_id nginx1 # 本服务器的名称
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx.sh" #脚本路径 最后手动执行此脚本,以确保脚本能够正常运行
interval 2 #检测间隔时间 单位秒
weight 2
}
vrrp_instance vi_1 {
state MASTER
interface ens33
virtual_router_id 51 #虚拟路由编号,主从要一致
priority 110 #优先级
adver_int 1 #检查间隔,默认为1s (vrrp组播周期秒数)
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #调用检测脚本
}
virtual_ipaddress {
20.0.0.100 虚拟ip
}
}
[root@nginx1 keepalived-2.0.13]# cp keepalived/etc/init.d/keepalived /etc/init.d
[root@nginx1 keepalived-2.0.13]# vi /etc/init.d/keepalived
[root@nginx1 keepalived-2.0.13]# chmod 755 /etc/init.d
[root@nginx1 keepalived-2.0.13]# chkconfig --add keepalived
[root@nginx1 keepalived-2.0.13]# chkconfig --list
# chkconfig: 35 21 79 运行级别
[root@nginx1 keepalived-2.0.13]# vi /usr/local/src/nginx.sh
[root@nginx1 keepalived-2.0.13]# chmod +x /usr/local/src/nginx.sh
[root@nginx1 keepalived-2.0.13]# systemctl start keepalived
[root@nginx1 keepalived-2.0.13]# systemctl status keepalived
[root@nginx1 ~]# tail -100 /var/log/messages #查看日志
[root@nginx1 ~]# ip addr #查看虚拟地址
[root@nginx2 ~]# ls -lh
[root@nginx2 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@nginx2 ~]# useradd -M -s /sbin/nologin nginx #创建不可登录,不带宿主的用户
[root@nginx2 ~]# tar zxvf nginx-1.12.2.tar.gz #解压缩
[root@nginx2 ~]# cd nginx-1.12.2/
[root@nginx2 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx #安装路径,属主,属组设置
[root@nginx2 nginx-1.12.2]# make && make install #编译安装
[root@nginx2 nginx-1.12.2]# ln -s /usr/local/nginx/conf/nginx.conf /etc/ #创建设置链接
[root@nginx2 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ #创建命令链接
[root@nginx2 nginx-1.12.2]# cd
[root@nginx2 ~]# cd /usr/local/nginx/html
添加图片
与nginx1上配置相同
[root@nginx2 ~]# yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
[root@nginx2 ~]# tar zxvf keepalived-2.0.13.tar.gz #解压缩
[root@nginx2 ~]# cd keepalived-2.0.13/
[root@nginx2 keepalived-2.0.13]# ./configure --prefix=/ #路径设置
[root@nginx2 keepalived-2.0.13]# make && make install #编译安装
[root@nginx2 keepalived-2.0.13]# cp keepalived/etc/init.d/keepalived /etc/init.d
[root@nginx2 keepalived-2.0.13]# vi /etc/init.d/keepalived
[root@nginx2 keepalived-2.0.13]# chmod 755 /etc/init.d
[root@nginx2 keepalived-2.0.13]# chkconfig --add keepalived
[root@nginx2 keepalived-2.0.13]# chkconfig --list
添加运行级别
# chkconfig: 35 21 79
[root@nginx2 keepalived-2.0.13]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx2
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx.sh"
interval 2
weight 2
}
vrrp_instance vi_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 105
adver_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
20.0.0.100
}
}
[root@nginx2 keepalived-2.0.13]# vi /usr/local/src/nginx.sh
[root@nginx2 keepalived-2.0.13]# chmod +x /usr/local/src/nginx.sh
[root@nginx2 keepalived-2.0.13]# systemctl start keepalived
[root@nginx2 keepalived-2.0.13]# systemctl status keepalived
#!/bin/bash
A=`ps -C nginx --no-header | wc -l` 查询进程数
if [ $A -eq 0 ]
then /usr/local/nginx/sbin/nginx
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]
then exit 1
else exit 0
fi
else
exit 0
fi
[root@nginx2 keepalived-2.0.13]# tail -100 /var/log/messages
[root@nginx2 keepalived-2.0.13]# ip addr