0
点赞
收藏
分享

微信扫一扫

搭建LNMT架构

LNMT:

操作系统 :CentOS Linux release 7.4.1708 (Core) 

Nginx服务器:nginx/1.22.0

Tomcat服务器: Apache Tomcat/8.5.60

数据库服务器:mysql  Ver 15.1 Distrib 5.5.68-MariaDB

搭建LNMT架构_mysql

搭建LNMT架构_mysql_02

一、部署Nginx服务器

# hostnamectl set-hostname  nginx-200
# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.10.200
NETMASK=255.255.255.0
GATEWAY=192.168.10.2
DNS1=114.114.114.114
DNS2=8.8.8.8
# yum install gcc zlib zlib-devel pcre pcre-devel openssl openssl-devel
# useradd -g nginx -M -u 888 -s /sbin/nologin nginx
# tar xf nginx-1.22.0.tar.gz
# cd nginx-1.22.0/
[root@nginx-200 /usr/local/src/nginx-1.22.0]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_sub_module --with-http_ssl_module --with-pcre
[root@nginx-200 /usr/local/src/nginx-1.22.0]# make && make install
[root@nginx-200 /usr/local]# netstat -lant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 36 192.168.10.200:22 192.168.10.1:1452 ESTABLISHED
tcp6 0 0 :::22 :::* LISTEN
[root@nginx-200 /usr/local/nginx/sbin]# ./nginx
[root@nginx-200 /usr/local/nginx/sbin]# netstat -lant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 36 192.168.10.200:22 192.168.10.1:1452 ESTABLISHED
tcp6 0 0 :::22 :::* LISTEN
[root@nginx-200 /usr/local/nginx/conf]# cat nginx.conf
user nginx nginx;
worker_processes 1;
error_log logs/error.log;
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"';
sendfile on;
keepalive_timeout 65;
charset utf-8;

upstream tomcat-apps {
server 192.168.10.202:8080 weight=1 max_fails=3 fail_timeout=10s;
server 192.168.10.203:8080 weight=3 max_fails=3 fail_timeout=10s;
server 192.168.10.204:8080 weight=1 max_fails=3 fail_timeout=10s;
}

server {
listen 80;
server_name www.class1.com;
access_log /usr/local/nginx/logs/class1.com.access.log main;
location / {
proxy_pass http://tomcat-apps;
index index.html;
}
}

server {
listen 80;
server_name www.class2.com;
access_log /usr/local/nginx/logs/class2.com.access.log main;
location / {
root /data/web-data/class2.com;
index index.html;
}
}

server {
listen 82;
access_log off;
location / {
root /data/web-data/test.com;
}
}

}

[root@nginx-200 ~]# /usr/local/nginx/sbin/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@nginx-200 ~]# curl localhost
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.22.0</center>
</body>
</html>

二、部署Tomcat服务器并Tomcat集群

搭建LNMT架构_nginx_03

## 解压jdk :
[root@tomcat-202 /usr/local]# tar xf jdk-8u201-linux-x64.tar.gz
[root@tomcat-202 /usr/local]# ls

## 创建 jdk 软连接
[root@tomcat-202 /usr/local]# ln -s /usr/local/jdk1.8.0_201 /usr/local/jdk

## 解压 Tomcat
[root@tomcat-202 /usr/local]# tar xf apache-tomcat-8.5.60.tar.gz

## 创建tomcat软连接
[root@tomcat-202 /usr/local]# ln -s /usr/local/apache-tomcat-8.5.60 /usr/local/tomcat
[root@tomcat-202 /usr/local]# ls


########### 修改环境变量配置文件
[root@tomcat-202 ~]# vim /etc/profile
按G跳转至最后一行
按o 新起一行进入模式
粘贴以下内容
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib:/test
export PATH=$JAVA_HOME/bin:$PATH
export TOMCAT_HOME=/usr/local/tomcat

#按ESC退出插入模式,输入 :wq 退出

## 当前shell进程重新读取 /etc/profile 配置文件
[root@tomcat-202 ~]# source /etc/profile
[root@tomcat-202 /]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
[root@tomcat-202 /]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
root@tomcat-202 /]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::8080 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 127.0.0.1:8005 :::* LISTEN
[root@nginx-200 /usr/local/nginx]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
error_log logs/error.log;
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"';
sendfile on;
keepalive_timeout 65;
charset utf-8;

upstream tomcat-apps {
server 192.168.10.202:8080 weight=1 max_fails=3 fail_timeout=10s;
server 192.168.10.203:8080 weight=1 max_fails=3 fail_timeout=10s;
server 192.168.10.204:8080 weight=1 max_fails=3 fail_timeout=10s;
}

server {
listen 80;
server_name www.class1.com;
access_log /usr/local/nginx/logs/class1.com.access.log main;
location / {
proxy_pass http://tomcat-apps;
index index.html;
}
}

server {
listen 80;
server_name www.class2.com;
access_log /usr/local/nginx/logs/class2.com.access.log main;
location / {
root /data/web-data/class2.com;
index index.html;
}
}
}

## 保存退出,重载nginx 服务器
[root@nginx-200 /usr/local/nginx]# /usr/local/nginx/sbin/nginx -s reload

三、admin跳板机:配置Ansible自动化运维

[root@admin-199 ~]# ssh-keygen -t rsa
[root@admin-199 ~]# ssh-copy-id 192.168.10.200
[root@admin-199 ~]# ssh-copy-id 192.168.10.202
[root@admin-199 ~]# ssh-copy-id 192.168.10.203
[root@admin-199 ~]# ssh-copy-id 192.168.10.204
[root@admin-199 ~]# ssh 192.168.10.200 hostname
nginx-200
[root@admin-199 ~]# ssh 192.168.10.202 hostname
tomcat-202
[root@admin-199 ~]# ssh 192.168.10.203 hostname
tomcat-203
[root@admin-199 ~]# ssh 192.168.10.204 hostname
tomcat-204
[root@admin-199 ~]# yum install ansible -y
[root@admin-199 ~]# ansible --version
ansible 2.9.27
## 创建主机清单
[root@admin-199 ~]# vim /etc/ansible/hosts
[nginx]
192.168.10.200
[tomcat]
192.168.10.202
192.168.10.203
192.168.10.204

[root@admin-199 ~]# ansible tomcat -m command -a "hostname"
192.168.10.204 | CHANGED | rc=0 >>
tomcat-204
192.168.10.203 | CHANGED | rc=0 >>
tomcat-203
192.168.10.202 | CHANGED | rc=0 >>
tomcat-202
[root@admin-199 /data/ansible-file]# ansible tomcat -m shell -a "ls /usr/local/tomcat/webapps"
192.168.10.204 | CHANGED | rc=0 >>
app1
docs
examples
host-manager
manager
Pro1
Pro1.war
ROOT
192.168.10.203 | CHANGED | rc=0 >>
app1
docs
examples
host-manager
manager
Pro1
Pro1.war
ROOT
192.168.10.202 | CHANGED | rc=0 >>
app1
docs
examples
host-manager
manager
Pro1
Pro1.war
ROOT
ansible db,tomcat,nginx -m shell -a "shutdown -h now"

四、安装部署mysql数据库服务器

## 安装 mariadb工具包,和服务器包 
[root@mysql-225 ~]# yum install mariadb mariadb-server
.... .... ...
作为依赖被升级:
mariadb-libs.x86_64 1:5.5.68-1.el7
完毕!

####### 初始化数据库

[root@mysql-225 ~]# cd /var/lib/mysql/
[root@mysql-225 /var/lib/mysql]# mysql_install_db --datadir=/var/lib/mysql --user=mysql
[root@mysql-225 /var/lib/mysql]# ls
aria_log.00000001 aria_log_control mysql performance_schema test

### 启动mysql服务,监听3306 端口
[root@mysql-225 /var/lib/mysql]# systemctl start mariadb
[root@mysql-225 ~]# netstat -lntat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 192.168.10.225:3306 192.168.10.226:49370 ESTABLISHED
tcp 0 36 192.168.10.225:22 192.168.10.1:1211 ESTABLISHED
tcp6 0 0 :::22 :::* LISTEN

五、部署主从数据库

#主数据库:
[root@mysql-225 ~]# vim /etc/my.cnf
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 225
MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant replication slave on *.* to 'user'@'192.168.10.226' identified by '000000';
Query OK, 0 rows affected (0.00 sec)

#从数据库
[root@mysql-226 ~]# vim /etc/my.cnf
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 226
MariaDB [(none)]> change master to master_host='mysql-225',master_user='user',master_password='000000';
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.225
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000006
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 245
Relay_Log_Space: 1109
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 225
1 row in set (0.01 sec)
ERROR: No query specified

六、测试结果如下:

搭建LNMT架构_tomcat_04

搭建LNMT架构_nginx_05

搭建LNMT架构_tomcat_06

举报

相关推荐

0 条评论