实验环境
主机名 | IP | 操作系统 | 所需配置 |
A | 192.168.24.131 | centos7 | 2个Tomcat |
B | 192.168.24.146 | centos7 | mysql,nginx |
实验需求
主机B作为nginx服务器,主机A作为2个Tomcat服务器,客户端发来的请求首先经由nginx处理,由nignx反代至后端的Tomcat服务器实现负载均衡
实验部署如下
在主机A操作如下
部署2个Tomcat服务器
实验准备
关闭防火墙以及SELINX
[root@linfan ~]# systemctl stop firewalld
[root@linfan ~]# systemctl disable firewalld
[root@linfan ~]# sed -ri 's/^(SELINUX=).*/disabled/g' /etc/selinux/config
[root@linfan ~]# setenforce 0
setenforce: SELinux is disabled
安装yum源
[root@linfan ~]# cd /etc/yum.repos.d/
[root@linfan yum.repos.d]# curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1572 100 1572 0 0 9484 0 --:--:-- --:--:-- --:--:-- 9527
[root@linfan yum.repos.d]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/163.repo
[root@linfan yum.repos.d]# sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/163.repo
[root@linfan yum.repos.d]# yum clean all
yum.repos.d]# yum -y install epel-release
Loaded plugins: fastestmirror
Cleaning repos: base centosplus extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
java环境安装
安装jdk环境
[root@linfan ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
查看安装版本
[root@linfan ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
tomcat部署
下载tomcat
[root@linfan ~]cd /usr/src
[root@linfan src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
[root@linfan src]# ls
apache-tomcat-9.0.8.tar.gz debug kernels
解压部署
因为要部署2个tomcal,所以这里要实现2次解压
//第一次解压
root@linfan src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
[root@linfan src]# cd /usr/local/
[root@linfan local]# mv apache-tomcat-9.0.8/ doudou //解压后重命名为doudou,防止第二次解压在同一目录下同名
[root@linfan local]# ls doudou
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
//第二次解压
[root@linfan local]# cd /usr/src
[root@linfan src]# ls
apache-tomcat-9.0.8.tar.gz debug kernels
[root@linfan src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
[root@linfan src]# cd /usr/local/
[root@linfan local]# ls
apache-tomcat-9.0.8 bin doudou etc games include lib lib64 libexec nginx sbin share src
[root@linfan local]# ln -s doudou/ tomcat1 //软连接为tomcat1
[root@linfan local]# ln -s apache-tomcat-9.0.8/ tomcat2 //软连接为tomcat2
[root@linfan local]# ll
total 0
drwxr-xr-x 9 root root 160 Sep 5 01:36 apache-tomcat-9.0.8
drwxr-xr-x. 2 root root 6 Nov 5 2016 bin
drwxr-xr-x 9 root root 160 Sep 5 01:34 doudou
drwxr-xr-x. 2 root root 6 Nov 5 2016 etc
drwxr-xr-x. 2 root root 6 Nov 5 2016 games
drwxr-xr-x. 2 root root 6 Nov 5 2016 include
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib64
drwxr-xr-x. 2 root root 6 Nov 5 2016 libexec
drwxr-xr-x. 11 root root 151 Sep 3 03:16 nginx
drwxr-xr-x. 2 root root 6 Nov 5 2016 sbin
drwxr-xr-x. 5 root root 49 Aug 30 04:37 share
drwxr-xr-x. 2 root root 6 Nov 5 2016 src
lrwxrwxrwx 1 root root 7 Sep 5 01:37 tomcat1 -> doudou/
lrwxrwxrwx 1 root root 20 Sep 5 01:37 tomcat2 -> apache-tomcat-9.0.8/
为了避免端口冲突,修改tomcat配置文件的端口号(修改其中一个即可)在这里我修改tomcat1的配置文件
tomcat1的端口改为8081
[root@linfan ~]# vim /usr/local/tomcat1/conf/server.xml
...
...
-->
<Server port="8006" shutdown="SHUTDOWN">//修改为8006
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
...
...
-->
<Connector port="8081" //修改为8081
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8444" />//修改为8444
<!-- A "Connector" using the shared thread pool-->
<!--
...
...
<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />//修改为8010 修改为8444
<!-- An Engine represents the entry point (within Catalina) that processes
在2个Tomcat上写不同字样的java页面
- tomcat1 写“666”
- tomcat2写“888”
[root@linfan src]# cd /usr/local/tomcat1/webapps/
[root@linfan webapps]# ls
docs examples host-manager manager ROOT
[root@linfan webapps]# mkdir test
[root@linfan webapps]# cd test
[root@linfan test]# vim index.jsp
[root@linfan test]# tail index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("666");
%>
</body>
</html>
[root@linfan test]# cd /usr/local/tomcat2/webapps/
[root@linfan webapps]# ls
docs examples host-manager manager ROOT
[root@linfan webapps]# mkdir test
[root@linfan webapps]# cd test
[root@linfan test]# vim index.jsp
[root@linfan test]# tail index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("888");
%>
</body>
</html>
开启2个tomcat服务
[root@linfan ~]# /usr/local/tomcat1/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat1
Using CATALINA_HOME: /usr/local/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.
[root@linfan ~]# /usr/local/tomcat2/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat2
Using CATALINA_HOME: /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.
[root@linfan ~]# ss -natl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 :::8080 :::*
LISTEN 0 100 :::8081 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8006 :::*
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8010 :::*
[root@linfan ~]# ps -ef |grep tomcat1
root 2268 1 11 02:00 pts/1 00:00:03 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat1 -Dcatalina.home=/usr/local/tomcat1 -Djava.io.tmpdir=/usr/local/tomcat1/temp org.apache.catalina.startup.Bootstrap start
root 2374 1919 0 02:00 pts/1 00:00:00 grep --color=auto tomcat1
[root@linfan ~]# ps -ef |grep tomcat2
root 2323 1 15 02:00 pts/1 00:00:03 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat2/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat2 -Dcatalina.home=/usr/local/tomcat2 -Djava.io.tmpdir=/usr/local/tomcat2/temp org.apache.catalina.startup.Bootstrap start
root 2376 1919 0 02:00 pts/1 00:00:00 grep --color=auto tomcat2
验证
在浏览器分别输入192.168.24.131:8080/test
和192.168.24.131:8081/test
得出以上2个页面说明2个tomcat服务部署成功
在主机B上部署
在此主机上安装nginx 和 mysql
实验准备
关闭防火墙以及SELINX
[root@linfan ~]# systemctl stop firewalld
[root@linfan ~]# systemctl disable firewalld
[root@linfan ~]# sed -ri 's/^(SELINUX=).*/disabled/g' /etc/selinux/config
[root@linfan ~]# setenforce 0
setenforce: SELinux is disabled
安装yum源
[root@linfan ~]# cd /etc/yum.repos.d/
[root@linfan yum.repos.d]# curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1572 100 1572 0 0 9484 0 --:--:-- --:--:-- --:--:-- 9527
[root@linfan yum.repos.d]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/163.repo
[root@linfan yum.repos.d]# sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/163.repo
[root@linfan yum.repos.d]# yum clean all
yum.repos.d]# yum -y install epel-release
Loaded plugins: fastestmirror
Cleaning repos: base centosplus extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
安装nginx
创建系统用户
[root@linfan ~]# groupadd -r nginx
[root@linfan ~]# useradd -r -M -s /sbin/nologin -g nginx nginx
安装依赖环境
[root@linfan ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
[root@linfan ~]# yum -y groups mark install 'Development Tools'
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
* epel: mirrors.aliyun.com
Marked install: Development Tools
创建日志存放目录
[root@linfan ~]# mkdir -p /var/log/nginx
[root@linfan ~]# chown -R nginx.nginx /var/log/nginx
下载nginx
[root@linfan ~]# cd /usr/src/
[root@linfan src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
--2018-08-24 13:26:36-- http://nginx.org/download/nginx-1.14.0.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2606:7100:1:69::3f, ...
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://64.123.28.133/files/21490000000827F6/nginx.org/download/nginx-1.14.0.tar.gz [following]
--2018-08-24 13:26:36-- http://64.123.28.133/files/21490000000827F6/nginx.org/download/nginx-1.14.0.tar.gz
Connecting to 64.123.28.133:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1016272 (992K) [application/octet-stream]
Saving to: ‘nginx-1.14.0.tar.gz’
100%[================================================================================>] 1,016,272 --.-K/s in 0.1s
2018-08-24 13:26:36 (8.10 MB/s) - ‘nginx-1.14.0.tar.gz’ saved [1016272/1016272]
编译安装
root@linfan src]# ls
debug kernels nginx-1.14.0.tar.gz
[root@linfan src]# tar xf nginx-1.14.0.tar.gz
[root@linfan src]# cd nginx-1.14.0
[root@linfan nginx-1.14.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@linfan nginx-1.14.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
配置变量环境
[root@linfan ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@linfan ~]# . /etc/profile.d/nginx.sh
启动nginx
[root@linfan ~]# nginx
[root@linfan ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
安装mysql
安装依赖包
[root@linfan ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建用户和组
[root@linfan ~]# groupadd -r -g 306 mysql
[root@linfan ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
下载二进制格式的mysql软件包
[root@linfan ~]# cd /usr/src/
[root@linfan src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
解压软件至/usr/local/
[root@linfan src]# ls
apr-1.6.3 apr-util-1.6.1 debug mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
apr-1.6.3.tar.bz2 apr-util-1.6.1.tar.bz2 kernels
[root@linfan src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@linfan src]# ls /usr/local/
apache apr-util etc include lib64 mysql-5.7.22-linux-glibc2.12-x86_64 share
apr bin games lib libexec sbin src
[root@linfan src]# cd /usr/local/
[root@linfan local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@linfan local]# ll
total 0
drwxr-xr-x. 13 root root 152 Aug 17 12:46 apache
drwxr-xr-x. 6 root root 58 Aug 17 12:35 apr
drwxr-xr-x. 5 root root 43 Aug 17 12:40 apr-util
drwxr-xr-x. 2 root root 6 Nov 5 2016 bin
drwxr-xr-x. 2 root root 6 Nov 5 2016 etc
drwxr-xr-x. 2 root root 6 Nov 5 2016 games
drwxr-xr-x. 2 root root 6 Nov 5 2016 include
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib64
drwxr-xr-x. 2 root root 6 Nov 5 2016 libexec
lrwxrwxrwx. 1 root root 36 Aug 17 13:54 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Aug 17 13:30 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Nov 5 2016 sbin
drwxr-xr-x. 5 root root 49 Jul 11 15:44 share
drwxr-xr-x. 2 root root 6 Nov 5 2016 src
修改目录/usr/locaal/mysql的属主属组
[root@linfan ~]# chown -R mysql.mysql /usr/local/mysql
[root@linfan ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Aug 17 13:54 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
添加环境变量
[root@linfan ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@linfan ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@linfan ~]# . /etc/profile.d/mysql.sh
[root@linfan ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
建立数据存放目录
[root@linfan ~]# cd /usr/local/mysql
[root@linfan mysql]# mkdir /opt/data
[root@linfan mysql]# chown -R mysql.mysql /opt/data/
[root@linfan mysql]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug 17 14:05 data
drwxr-xr-x. 8 root root 220 Jul 18 17:09 lin.d
初始化数据库
[root@linfan mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-17T06:08:33.347313Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-17T06:08:33.873415Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-17T06:08:33.953310Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-17T06:08:34.016549Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f8e46285-a1e3-11e8-b6bf-000c29c9d4ed.
2018-08-17T06:08:34.019542Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-17T06:08:34.023380Z 1 [Note] A temporary password is generated for root@localhost: B<HiGFoc.8yZ
//这个命令的最后会生成一个临时密码,此处密码是B<HiGFoc.8yZ
配置mysql
[root@linfan ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@linfan ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@linfan ~]# ldconfig -v
生成配置文件
[root@linfan ~]# cat > /etc/my.cnf <<EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF
配置服务启动脚本
[root@linfan ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@linfan ~]# sed -ri 's#^(basedir=).*#/usr/local/mysql#g' /etc/init.d/mysqld
[root@linfan ~]# sed -ri 's#^(datadir=).*#/opt/data#g' /etc/init.d/mysqld
启动mysql
[root@linfan ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/linfan.err'.
SUCCESS!
[root@linfan ~]# ps -ef|grep mysql
root 52200 1 0 14:25 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 52378 52200 4 14:25 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=linfan.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 52408 2998 0 14:25 pts/1 00:00:00 grep --color=auto mysql
[root@linfan ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
修改密码
使用临时密码修改
[root@linfan ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password = password('linfan123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
配置nginx
在配置文件中配置nginx作为后端服务器192.168.24.131中2个tomcal的反向代理
[root@linfan ~]# vim /usr/local/nginx/conf/nginx.conf
...
...
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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
//在此添加以下内容
upstream tomcat {
server 192.168.24.131:8080;
server 192.168.24.131:8081;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
在此添加以下内容
}
location ~* \.(jsp|do)$ {
proxy_pass http://tomcat;
}
测试语法并加载nginx
[root@linfan ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@linfan ~]# nginx -s reload
在ngin的网页根目录下面写入内容
[root@linfan logs]# cd /usr/local/nginx/html/
[root@linfan html]# echo "111" >> index.html
验证
在浏览器上输入192.168.24.146
在浏览器输入192.168.24.146/test/index.jsp
并不断刷新网页
发现不断轮询,说明成功实现负载均衡的效果
###实验小结
- 发现当访问静态网页时,映射到nginx的网页目录下
当访问动态网页时,映射到tomcat的网页目录下
实现了动静分离 - 当不断刷新
192.168.24.146/test/index.jsp
时,不断轮询,实现了负载均衡