LNMT部署详细步骤并实现负载均衡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LNMT部署详细步骤并实现负载均衡相关的知识,希望对你有一定的参考价值。
实验环境
主机名 | IP | 操作系统 | 所需配置 |
---|---|---|---|
A | 192.168.24.131 | centos7 | 2个Tomcat |
B | 192.168.24.146 | centos7 | myself,nginx |
实验需求
主机B作为nginx服务器,主机A作为2个Tomcat服务器,客户端发来的请求首先经由nginx处理,由nignx反代至后端的Tomcat服务器实现负载均衡
实验部署如下
在主机A操作如下
部署2个Tomcat服务器
实验准备
关闭防火墙以及SELINX
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# sed -ri ‘s/^(SELINUX=).*/1disabled/g‘ /etc/selinux/config
[[email protected] ~]# setenforce 0
setenforce: SELinux is disabled
安装yum源
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] 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
[[email protected] yum.repos.d]# sed -i ‘s/$releasever/7/g‘ /etc/yum.repos.d/163.repo
[[email protected] yum.repos.d]# sed -i ‘s/enabled=0/enabled=1/g‘ /etc/yum.repos.d/163.repo
[[email protected] 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环境
[[email protected] ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
查看安装版本
[[email protected] ~]# 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
[[email protected] ~]cd /usr/src
[[email protected] src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
[[email protected] src]# ls
apache-tomcat-9.0.8.tar.gz debug kernels
解压部署
因为要部署2个tomcal,所以这里要实现2次解压
//第一次解压
[email protected] src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
[[email protected] src]# cd /usr/local/
[[email protected] local]# mv apache-tomcat-9.0.8/ doudou //解压后重命名为doudou,防止第二次解压在同一目录下同名
[[email protected] local]# ls doudou
bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
//第二次解压
[[email protected] local]# cd /usr/src
[[email protected] src]# ls
apache-tomcat-9.0.8.tar.gz debug kernels
[[email protected] src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
[[email protected] src]# cd /usr/local/
[[email protected] local]# ls
apache-tomcat-9.0.8 bin doudou etc games include lib lib64 libexec nginx sbin share src
[[email protected] local]# ln -s doudou/ tomcat1 //软连接为tomcat1
[[email protected] local]# ln -s apache-tomcat-9.0.8/ tomcat2 //软连接为tomcat2
[[email protected] 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
[[email protected] ~]# 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”
[[email protected] src]# cd /usr/local/tomcat1/webapps/
[[email protected] webapps]# ls
docs examples host-manager manager ROOT
[[email protected] webapps]# mkdir test
[[email protected] webapps]# cd test
[[email protected] test]# vim index.jsp
[[email protected] test]# tail index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("666");
%>
</body>
</html>
[[email protected] test]# cd /usr/local/tomcat2/webapps/
[[email protected] webapps]# ls
docs examples host-manager manager ROOT
[[email protected] webapps]# mkdir test
[[email protected] webapps]# cd test
[[email protected] test]# vim index.jsp
[[email protected] test]# tail index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("888");
%>
</body>
</html>
开启2个tomcat服务
[[email protected] ~]# /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.
[[email protected] ~]# /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.
[[email protected] ~]# 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 :::*
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# sed -ri ‘s/^(SELINUX=).*/1disabled/g‘ /etc/selinux/config
[[email protected] ~]# setenforce 0
setenforce: SELinux is disabled
安装yum源
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] 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
[[email protected] yum.repos.d]# sed -i ‘s/$releasever/7/g‘ /etc/yum.repos.d/163.repo
[[email protected] yum.repos.d]# sed -i ‘s/enabled=0/enabled=1/g‘ /etc/yum.repos.d/163.repo
[[email protected] 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
创建系统用户
[[email protected] ~]# groupadd -r nginx
[[email protected] ~]# useradd -r -M -s /sbin/nologin -g nginx nginx
安装依赖环境
[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
[[email protected] ~]# 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
创建日志存放目录
[[email protected] ~]# mkdir -p /var/log/nginx
[[email protected] ~]# chown -R nginx.nginx /var/log/nginx
下载nginx
[[email protected] ~]# cd /usr/src/
[[email protected] 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]
编译安装
[email protected] src]# ls
debug kernels nginx-1.14.0.tar.gz
[[email protected] src]# tar xf nginx-1.14.0.tar.gz
[[email protected] src]# cd nginx-1.14.0
[[email protected] 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
[[email protected] nginx-1.14.0]# make -j $(grep ‘processor‘ /proc/cpuinfo | wc -l) && make install
配置变量环境
[[email protected] ~]# echo ‘export PATH=/usr/local/nginx/sbin:$PATH‘ > /etc/profile.d/nginx.sh
[[email protected] ~]# . /etc/profile.d/nginx.sh
启动nginx
[[email protected] ~]# nginx
[[email protected] ~]# 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
安装依赖包
[[email protected] ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建用户和组
[[email protected] ~]# groupadd -r -g 306 mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
下载二进制格式的mysql软件包
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
解压软件至/usr/local/
[[email protected] 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
[[email protected] src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[[email protected] 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
[[email protected] src]# cd /usr/local/
[[email protected] local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[[email protected] 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的属主属组
[[email protected] ~]# chown -R mysql.mysql /usr/local/mysql
[[email protected] ~]# 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/
添加环境变量
[[email protected] ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[[email protected] ~]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[[email protected] ~]# . /etc/profile.d/mysql.sh
[[email protected] ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
建立数据存放目录
[[email protected] ~]# cd /usr/local/mysql
[[email protected] mysql]# mkdir /opt/data
[[email protected] mysql]# chown -R mysql.mysql /opt/data/
[[email protected] 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
初始化数据库
[[email protected] 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 [email protected]: B<HiGFoc.8yZ
//这个命令的最后会生成一个临时密码,此处密码是B<HiGFoc.8yZ
配置mysql
[[email protected] ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[[email protected] ~]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf
[[email protected] ~]# ldconfig -v
生成配置文件
[[email protected] ~]# 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
配置服务启动脚本
[[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# sed -ri ‘s#^(basedir=).*#1/usr/local/mysql#g‘ /etc/init.d/mysqld
[[email protected] ~]# sed -ri ‘s#^(datadir=).*#1/opt/data#g‘ /etc/init.d/mysqld
启动mysql
[[email protected] ~]# service mysqld start
Starting MySQL.Logging to ‘/opt/data/linfan.err‘.
SUCCESS!
[[email protected] ~]# 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
[[email protected] ~]# 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 :::*
修改密码
使用临时密码修改
[[email protected] ~]# 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的反向代理
[[email protected] ~]# 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 / {
proxy_pass http://tomcat;
}
测试语法并加载nginx
[[email protected] ~]# 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
[[email protected] ~]# nginx -s reload
验证
在浏览器输入192.168.24.146/test
并不断刷新网页
实验小结
发现不断轮询,说明成功实现负载均衡的效果
以上是关于LNMT部署详细步骤并实现负载均衡的主要内容,如果未能解决你的问题,请参考以下文章