lnmt搭建及负载轮询
lnmt服务的搭建及其负载轮询
1、前提条件
准备三台服务器
服务器 | ip | 部署 |
---|---|---|
服务端 | 192.168.7.10 | mysql、nginx、tomcat |
客户端1 | 192.168.7.11 | tomcat9 |
客户端2 | 192.168.7.12 | tomcat9 |
三台同时关闭防火墙
# systemctl stop firewalld # systemctl disable firewalld # setent 0
2、server端部署
2.1安装nginx
nginx加入systemctl启动脚本:https://files.cnblogs.com/files/liuzhijun666/nginx启动脚本.rar
nginx 下载地址: :http://nginx.org/download/nginx-1.14.0.tar.gz
# yum -y install pcre-devel openssl openssl-devel gd-devel lrzsz # yum -y groups mark install ‘Development Tools‘ # useradd -r -M -s /sbin/nologin nginx //创建日志目录 # mkdir -p /var/log/nginx # chown -R nginx.nginx /var/log/nginx # wget http://nginx.org/download/nginx-1.14.0.tar.gz # tar -xf nginx-1.14.0.tar.gz nginx-1.14.0.tar.gz nginx-1.14.0 # pwd /root/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 # make -j $(grep ‘processor‘ /proc/cpuinfo | wc -l) && make install //安装 # /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf # cd /usr/local/nginx/sbin/ # ls nginx # cd /etc/init.d # rz //上传ngin的启动脚本 # ls ... nginx ..... # chmod +x nginx # source nginx //执行该脚本 # systemctl restart nginx # 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 :::*
3.2安装mysql
下载地址: https://dev.mysql.com/downloads/mysql/
# yum -y install ncurses-devel openssl-devel openssl cmake mariadbdevel # tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz ?-C /usr/local/ # pwd /usr/src # cd /usr/local # ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql # groupadd -r mysql //mysql创建组和用户 # useradd -M -s /sbin/nologin -g mysql mysql # chown -R mysql.mysql mysql # echo "export PATH=/usr/local/mysql/bin/:$PATH" > /etc/profile.d/mysql.sh //环境变量 # source /etc/profile.d/mysql.sh # mkdir /opt/data //生成数据库目录 # chown -R mysql.mysql /opt/data/ # mysqld --initialize --user=mysql --datadir=/opt/data/ //初始化数据库 2020-04-26T13:28:47.194209Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-04-26T13:28:47.739448Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-04-26T13:28:47.878674Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-04-26T13:28:48.064606Z 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: dc2dc2c2-87c1-11ea-b320-000c29361d6b. 2020-04-26T13:28:48.065569Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened. 2020-04-26T13:28:48.066537Z 1 [Note] A temporary password is generated for : ,8wypuC8Swht //密码 # cd # echo ",8wypuC8Swht" > passwd.txt //保存密码 # vim /etc/my.cnf //编写配置文件 # cat /etc/my.cnf [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 # cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld # sed -ri ‘s#^(datadir=).*#\1/opt/data#g‘ /etc/init.d/mysqld # sed -ri ‘s#^(basedir=).*#\1/usr/local/mysql#g‘ /etc/init.d/mysqld # service mysqld start //启动 # 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 :::* LISTEN 0 80 :::3306 #mysql端口号
3、部署tomcat
tomcat9 下载地址:
# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel //下载java # wget https://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.36/bin/apache-tomcat-9.0.36.tar.gz # ls apache-tomcat-9.0.36.tar.gz # tar -xf apache-tomcat-9.0.36.tar.gz -C /usr/local # echo "export PATH=/usr/local/apache-tomcat-9.0.33/bin:$PATH" > /etc/profile.d/tomcat.sh //环境变量 # startup.sh # 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 100 :::8080 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* LISTEN 0 80 :::3306 :::*
3、实现负载均衡
客户端1:
# cd /usr/local/apache-tomcat-9.0.36/webapps/ # mkdir test # cd test # vim index.jsp <html> <head> <title>第一个</title> </head> <body> 1111 <body> </html>
客户端2:
# cd /usr/local/apache-tomcat-9.0.36/webapps/ # mkdir test # cd test # vim index.jsp <html> <head> <title>第二个</title> </head> <body> 2222 <body> </html>
服务端
修改配置文件:/usr/local/nginx/conf/nginx.conf
...... #添加 upstream www{ server 192.168.7.12:8080; #客户端2 server 192.168.7.11:8080; #客户端1 } server { listen 80; # 端口 server_name xxx.example.com; #设置域名 access_log logs/host.access.log; #指定日志文件 location ~*/test/*.(do|jsp) { #两个客户端的test proxy_pass http://www; #www要与upstream 一样 index index.html index.htm; } } ......
效果: