要求:
1、动静分离HTML交给Nginx本身去处理 2、客户端访问:www.bbb.com可以访问到博客 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? www.aaa.com可以访问的超市项目
本次实验需要9台虚拟机(不加DNS服务器)
| 系统 | IP | VIP | 主机名 | mmm群集 | CentOS 7.4 | 192.168.2.9 | 192.168.2.100(跳转) | Master1 | CentOS 7.4 | 192.168.2.8 | 192.168.2.100(跳转) | Master2 | CentOS 7.4 | 192.168.2.7 | 192.168.2.101 | Slave2 | CentOS 7.4 | 192.168.2.6 | 192.168.2.102 | Slave1 | CentOS 7.4 | 192.168.2.3 | 无 | Monitor | 超市项目 | CentOS 7.4 | 192.168.2.10 | 无 | Tomcat | 博客 | CentOS 7.4 | 192.168.2.2 | 无 | bk | nginx高可用 | CentOS 7.4 | 192.168.2.11 | 192.168.2.200(跳转) | Nginx1 | CentOS 7.4 | 192.168.2.12 | 192.168.2.200(跳转) | Nginx2 |
?添加DNS解析域名买一台服务器都需要加;在参考内容中有一样的命令无需操作
echo '
192.168.2.6 Slave1
192.168.2.7 Slave2
192.168.2.8 Master2
192.168.2.9 Master1
192.168.2.3 Monitor
192.168.2.10 bk
192.168.2.2 Tomcat
192.168.2.11 Nginx1
192.168.2.12 Nginx2
192.168.2.200 www.aaa.com
192.168.2.200 www.bbb.com'>> /etc/hosts
?注意修改服务器名称
一、搭建mysql-mmm高可用群集
?参考:mysql-mmm高可用群集
搭建完成后测试主主复制主从复制
在 master1 上创建 user 库
mysql> create database user;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| user |
+--------------------+
5 rows in set (0.00 sec)
————————————————————————————————————————
master2
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| user |
+--------------------+
5 rows in set (0.00 sec)
————————————————————————————————————————
slave1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| user |
+--------------------+
5 rows in set (0.00 sec)
————————————————————————————————————————
slave2
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| user |
+--------------------+
5 rows in set (0.00 sec)
在 Monitor 上查看状态
[root@monitor ~]# mmm_control show
db1(192.168.2.9) master/ONLINE. Roles: writer(192.168.2.100)
db2(192.168.2.8) master/ONLINE. Roles:
db3(192.168.2.7) slave/ONLINE. Roles: reader(192.168.2.101)
db4(192.168.2.6) slave/ONLINE. Roles: reader(192.168.2.102)
没有vip手动创建在mmm的文档中有
二、部署超市项目和博客
1、部署博客
参考:Centos 7.4——LAMP架构分离部署+社区论坛部署
注意在参考内容中不做:(四)、安装phpMyAdmin?
注意:授权用户和密码、和服务器地址
2、部署超市项目
参考:Tomcat应用部署
使用:apache-tomcat-8.5.16.tar.gz 版本安装步骤相同
?注意:指定的数据库地址
三、部署Ngixn高可用
1、安装nginx
安装nginx参考:Nginx-简介及安装
注意:建议使用编译安装
Nginx1和Nginx2安装相同
?配置ngixn后重启
ngixn1 配置
————————————————————————————————————————————————————
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server { #虚拟主机1
listen 80;
server_name www.ccc.com;
location ~ .*\.(html|htm|css)$ {
root html;
index index.html index.htm;
}
}
server { #虚拟主机2
listen 80;
server_name www.aaa.com;
location / {
proxy_pass http://192.168.2.2:8080/WebRoot;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
}
}
server { #虚拟主机3
listen 80;
server_name www.bbb.com;
location / {
proxy_pass http://192.168.2.10/sqlt/upload/forum.php;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
}
}
}
保存
nginx -s reload
————————————————————————————————————————————————————————————————————————————————
ngixn2 配置
—————————————
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server { #虚拟主机1
listen 80;
server_name www.ccc.com;
location ~ .*\.(html|htm|css)$ {
root html;
index index.html index.htm;
}
}
server { #虚拟主机2
listen 80;
server_name www.aaa.com;
location / {
proxy_pass http://192.168.2.2:8080/WebRoot;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
}
}
server { #虚拟主机3
listen 80;
server_name www.bbb.com;
location / {
proxy_pass http://192.168.2.10/sqlt/upload/forum.php;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
}
}
}
保存
nginx -s reload
2、keepalived高可用
在Nginx1和Nginx2操作
yum -y install keepalived ipvsadm
systemctl enable keepalived
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
————————————————————————————————————————————————————————————
————————ngixn1 ——————————————注意修改IP
vim /etc/keepalived/keepalived.conf #空文件
global_defs {
router_id HA_TEST_R1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123.com
}
virtual_ipaddress {
192.168.2.200
}
virtual_server 192.168.2.200 80 {
delay_loop 15
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.2.12 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.2.11 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
保存
systemctl start keepalived #启动
————————————————————————————————————————————————
————————ngixn2 ——————————————注意修改IP
vim /etc/keepalived/keepalived.conf #空文件
global_defs {
router_id HA_TEST_R2 #注意修改
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 1
priority 90 #注意修改
advert_int 1
authentication {
auth_type PASS
auth_pass 123.com
}
virtual_ipaddress {
192.168.2.200
}
virtual_server 192.168.2.200 80 {
delay_loop 15
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.2.12 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.2.11 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
保存
systemctl start keepalived #启动
在nginx上分别查看VIP:只有ngixn1上有VIP,只有nginx1宕机VIP才会到ngixn2上
四、 测试?
?注意:本机需要修改hosts文件:如192.168.2.200 www.aaa.com
访问 www.aaa.com 为超市管理系统
访问 www.bbb.com 是社区论坛
访问 www.ccc.com 是静态页面
|