简介:
本项目案例结合SVN、LNMP和MySQL三种环境,部署一个社交网站,本社交网站采用PHP语言开发,搭建SVN服务器进行版本控制和集中管理PHP程序员开发的代码,以Nginx作为前端服务器,通过fastcgi协议访问后端的PHP服务器,调用PHP页面;为保证数据安全,搭建MySQL主从复制环境存储用户重要数据;另外搭建MFS分布式文件系统来存储用户照片。
1.SVN服务器:简单一点SVN就是用于多个人共同开发同一个项目,共用资源的目的
2.MFS分布式文件系统,比NFS分布式文件系统更加强大,其他分布式式文件系统还有HDFS分布式文件系统和fastDFS分布式文件系统等
3.MySQL环境,此案列环境限于本人机器设备配置问题,只配置了mysql主从复制,如果是真实环境中,还可以配置双击热备,借助amoeba环境实现读写分离,负载均衡等功能,大型网络环境还可以配置MySQL集群
案例设计:
采用四层模式实现,主要分为前端反向代理层、Web层、数据库缓存层和数据库层。前端反向代理层层采用主备模式,Web层采用群集模式,数据库缓存层采用主备模式,数据库层采用主从模式。
为了更接近生产环境,采用两台实体机部署此次环境,将前端反向代理层、数据库缓存层、数据库层部署在实体机上,只将Web层部署在KVM虚拟机当中。同时将每一层都做了高可用架构,保证业务的稳定性。
拓扑架构如图所示:实现是正常情况数据流向,虚线是异常情况下的数据流向
实验环境:
实验步骤:
一、Nginx反向代理和keepalived
1、前端两台反向代理服务器安装带有nginx rpm软件包的源(主从都要做)
[[email protected] ~]#rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2、使用yum仓库完成下面的安装
[[email protected] ~]#yum install -y keepalived nginx
3、配置前端反向代理主机的/etc/keepalived/keepalived.conf
[[email protected] ~]#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script nginx {
interval 2
}
global_defs {
notification_email {
}
global_defs {
notification_email {
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
! Configuration File for keepalived
vrrp_script nginx { #添加
script "/opt/shell/nginx.sh"
interval 2
}
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGINX_HA
}
state MASTER
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGINX_HA
}
vrrp_instance VI_1 {
state MASTER #主服务器
interface ens33
virtual_router_id 51
priority 100 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { #添加
nginx
}
virtual_ipaddress {
192.168.126.188 #内网虚拟IP
192.168.100.188 #外网虚拟IP
}
}
注:在配置文件里面载入了一个nginx.sh脚本,脚本的含义是如果keepalived已启动,那么每2秒检查并启动一次nginx服务
4、创建/opt/shell/nginx.sh文件
[[email protected] ~]# mkdir /opt/shell
[[email protected] ~]# cd /opt/shell/
[[email protected]ocalhost shell]# vim nginx.sh
#!/bin/bash
k=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $k -gt 0 ];then
/bin/systemctl start nginx.service
else
/bin/systemctl stop nginx.service
fi
[[email protected] shell]# chmod +x nginx.sh #脚本增加可执行权限
5、前端反向代理备机的配置与主服务器的配置基本上相同,唯一不同的是keepalived的配置,如下
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script nginx { #nginx触发脚本
script "/opt/shell/nginx.sh"
interval 2
}
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGINX_HB # 名称
}
vrrp_instance VI_1 {
state BACKUP #从服务器
interface ens33
virtual_router_id 52 #id号
priority 90 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { #添加
nginx
}
virtual_ipaddress {
192.168.126.188 #虚拟IP
192.168.100.188
}
}
另外一台也一样,这里不再赘述
6、在主从服务器上配置nginx前端调度功能
[[email protected] PV]# vim /etc/nginx/nginx.conf
#gzip on;
upstream tomcat_pool {
server 192.168.126.193:8080; #两台节点服务器的IP
server 192.168.126.194:8080;
ip_hash; #会话稳固功能,否则无法通过vip地址登陆
}
server {
listen 80;
server_name 192.168.126.188; #虚拟出的IP
location / {
proxy_pass http://tomcat_pool; proxy_set_header X-Real-IP $remote_addr;
}
}
1)测试配置文件语法
[[email protected] PV]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
2)启动keepalived服务后,nginx服务也会启动。
[[email protected] PV]# systemctl stop firewalld.service
[[email protected] PV]# setenforce 0
[[email protected] PV]# systemctl start keepalived.service
3)测试keepalived的双机热备功能
关闭主机的Keepalived服务,查看虚拟IP是否可以自动漂移到备机上。
主服务器:
[[email protected] PV]# ip addr
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33
valid_lft 1697sec preferred_lft 1697sec
inet 192.168.126.188/32 scope global ens33
valid_lft forever preferred_lft forever
[[email protected] PV]# systemctl stop keepalived.service
link/ether 00:0c:29:15:68:5d brd ff:ff:ff:ff:ff:ff
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33
valid_lft 1625sec preferred_lft 1625sec
inet6 fe80::7f06:3732:bb38:c9e1/64 scope link
valid_lft forever preferred_lft forever
备份服务器:
[[email protected] ~]# ip addr
link/ether 00:0c:29:5b:03:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.126.192/24 brd 192.168.126.255 scope global dynamic ens33
valid_lft 1180sec preferred_lft 1180sec
inet 192.168.126.188/32 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.100.188/32 scope global ens33 #虚拟IP自动漂移成功
注意:再次将主服务器的keepalived服务开启后,虚拟IP又会漂移到主服务器上。
二、在两台节点服务器上安装Tomcat(配置相同)
1.安装jdk-8u144-linux-x64.tar.gz和apache-tomcat-8.5.23.tar.gz包
[[email protected] PV]# tar zxvf jdk-8u144-linux-x64.tar.gz -C /opt/
[[email protected] PV]# tar zxvf apache-tomcat-8.5.23.tar.gz -C /opt/
2.把解压后的文件移动到/usr/local目录下并且创建新的目录,方便管理。
[[email protected] PV]# cd /opt/
[[email protected] opt]# ls
apache-tomcat-8.5.16 apache-tomcat-8.5.23 jdk1.8.0_144 jdk1.8.0_91 rh
[[email protected] opt]# mv jdk1.8.0_144/ /usr/local/java
[[email protected] opt]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8
3.在/etc/profile配置文件行尾,添加以下内容:
[[email protected] opt]# vim /etc/profile
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
[[email protected] opt]# source /etc/profile #启动
[[email protected] opt]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup[[email protected] opt]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
4.启动tomcat服务并查看其8080端口是否开启
[[email protected] opt]# systemctl stop firewalld.service
[[email protected] opt]# setenforce 0
[[email protected] opt]# tomcatup
[[email protected] opt]# netstat -ntap | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 3255/java
5.打开浏览器测试默认测试页是否正常显示
6.修改默认网页内容
[[email protected] opt]# cd /usr/local/tomcat8/webapps/ROOT/
[[email protected] ROOT]# mv index.jsp index.jsp.bk
[[email protected] ROOT]# vim index.jsp
<h1>Server abc01!!</h1>
7.输入调度器地址,也就是虚拟地址,测试两台节点的调度情况。如图所示:
停止节点服务器1的tomcat服务,再次刷新浏览器,查看是否调度成功。如图所示:
[[email protected] ROOT]# tomcatdown
8.修改/usr/local/tomcat8/conf/server.xml在Host name下新增以下内容:
[[email protected] conf]# vim server.xml
<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context> #日志调试信息debug为0表示信息越少,docBase指定访问目录。
三、在主从服务器安装Mysql数据库
1.安装mariadb服务
[[email protected] PV]# yum install mariadb-server mariadb -y
[[email protected] PV]# systemctl enable mariadb.service #开启自启动
[[email protected] PV]# systemctl start mariadb.service #开启服务
[[email protected] PV]# netstat -ntap | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 28285/mysqld
2.常规安全设置
[[email protected] PV]# mysql_secure_installation
Enter current password for root (enter for none): #Enter
Set root password? [Y/n] y 设置用户密码
New password: #abc123
Re-enter new password: #确认密码abc123
Remove anonymous users? 删除匿名用户 n
Disallow root login remotely? 不允许远程root登录吗? n
Remove test database and access to it? 删除测试数据库并访问它? n
Reload privilege tables now? 现在重新加载权限表吗? y
3.导入数据库slsaledb
[[email protected] PV]# mysql -u root -p < slsaledb-2014-4-10.sql
[[email protected] PV]# mysql -u root -p
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| slsaledb |
| test |
+--------------------+
5 rows in set (0.02 sec)
MariaDB [(none)]> GRANT all ON slsaledb.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘abc123‘; #授权
MariaDB [(none)]> flush privileges; #刷新
4.在两台tomcat节点修改数据库IP地址是VRRP的虚拟IP,以及授权的用户名root和密码abc123。
[[email protected] PV]# tar zxvf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/
[[email protected] PV]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/
classes/
[[email protected] classes]# vim jdbc.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://192.168.126.188\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8
uname=root #用户名
password=abc123 #密码
minIdle=10
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true
5.打开浏览器进行测试网站
三、安装并配置redis主从缓存服务器
redis简介:
redis是一个高性能的key-value数据库,和Memcached类似,但它支持的value类型更多。与Memcached一样,为了保证效率,数据都是缓存在内存中的。
区别是:redis会周期性地把更新的数据写入磁盘或者把修改操作写入追加的记录文件中,并且在此基础上实现了master-slave(主从)同步。
1.安装并配置redis主从
[[email protected] ~]# yum install -y epel-release
[[email protected] ~]#yum install redis -y
2.修改主从缓存服务器的redis主配置文件/etc/redis.conf中的监听端口,修改为bind 0.0.0.0。
[[email protected] ~]# vim /etc/redis.conf
bind 0.0.0.0 #61行
从服务器上多如下一行配置
slaveof 192.168.126.138 6379 #主服务器的IP不是虚拟IP(266行)
3.启动redis服务
[[email protected] ~]# systemctl start redis.service
[[email protected] ~]# netstat -anpt | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 50475/redis-server
4.测试连接
主服务器:
[[email protected] ~]# redis-cli -h 192.168.126.138 -p 6379
192.168.126.138:6379> set name test #设置name 值是test
OK
192.168.126.138:6379> get name #获取name值
"test"
从服务器:
[[email protected] ~]# redis-cli -h 192.168.126.192 -p 6379 #登录从,获取值,成功说明主从同步成功。
192.168.126.192:6379> get name
"test"
192.168.126.192:6379>
5.在tomcat节点服务器上,配置商城项目中连接redis的参数。
[[email protected] classes]#cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
[[email protected] classes]# vim applicationContext-mybatis.xml
<constructor-arg value="192.168.126.188"/> #47行
<constructor-arg value="6379"/> #48行
6.测试缓存效果
[[email protected] ~]# redis-cli -h 192.168.126.188 -p 6379
192.168.126.188:6379> info
.....//省略
evicted_keys:0
keyspace_hits:1 #keyspace_hits:1 或者 keyspace_misses:2//关注这个值,命中数和未命中数
keyspace_misses:0
pubsub_channels:0
登录商城,多次点击需要数据库参与的操作页面,再回来检查keyspace_hits或者keyspace_misses: 值变化。如果keyspace_hits:1 值变为2,说明redis在正常工作。
四、配置redis集群主从切换---只在主服务器是操作
[[email protected] ~]# vi /etc/redis-sentinel.conf
protected-mode no #17行 开启
sentinel monitor mymaster 192.168.126.138 6379 1 #1表示1台从 注意:修改为主IP
sentinel down-after-milliseconds mymaster 3000 #故障切换时间单位是毫秒
1.启动集群
[[email protected] ~]# service redis-sentinel start
[[email protected] ~]# netstat -anpt | grep 26379
tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 57151/redis-sentine
tcp6 0 0 :::26379 :::* LISTEN 57151/redis-sentine
[[email protected]lhost ~]# redis-cli -h 192.168.126.138 -p 26379 infoSentinel #查看集群信息
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.126.138:6379,slaves=1,sentinels=1
2.验证主从切换
主服务器:
[email protected] ~]# systemctl stop redis.service #关闭主服务器的redis服务
[[email protected] ~]# redis-cli -h 192.168.126.138 -p 26379 info Sentinel # Sentinel #发现主切换成从服务器的IP
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.126.192:6379,slaves=1,sentinels=1
3.验证数据同步情况
在主服务器上:
[[email protected] ~]# redis-cli -h 192.168.126.192 -p 6379
192.168.126.192:6379> set name2 test2
OK
192.168.126.192:6379> get name2
"test2"
192.168.126.192:6379>
在从服务器上查看:
[[email protected] ~]# redis-cli -h 192.168.126.192 -p 6379
192.168.126.192:6379> get name2
"test2"
启动主服务器的redis服务:
[[email protected] ~]# systemctl start redis.service
[[email protected] ~]# redis-cli -h 192.168.126.138 -p 6379
192.168.126.138:6379> get name2
"test2" #同步成功
五、在主从服务器上配置Mysql主从复制
1.首选主库需要开启binlog日志,在打开主库的/etc/my.cnf文件里面添加以下内容:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-ignore-db=mysql,information_schema #日志
character_set_server=utf8 #字符集
log_bin=mysql_bin #二进制日志
server_id=1 #从服务器的id号不能相同
log_slave_updates=true
sync_binlog=1
2.重启数据库
[[email protected] ~]# systemctl restart mariadb.service
[[email protected] ~]# netstat -ntap | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 65321/mysqld
3.登录主库并授权,允许从库的指定用户从主库读取日志。
[[email protected] ~]# mysql -u root -p
MariaDB [(none)]> show master status; #记录日志文件名称和 位置值
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> grant replication slave on *.* to ‘rep‘@‘192.168.126.%‘ identified by ‘123456‘; #授权
MariaDB [(none)]> flush privileges;
4.Mysql从数据库配置
[[email protected] ~]# mysql -u root -p
MariaDB [(none)]> change master to master_host=‘192.168.126.138‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=245;
MariaDB [(none)]> start slave;
Ma
riaDB [(none)]> show slave status\G;
.....//省略
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5.测试主从同步
在Mysql主服务器上创建一个库,看从服务器上是否能够同步主服务器的新数据库。
主服务器:
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| slsaledb |
| test |
+--------------------+
6 rows in set (0.28 sec)
从服务器:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school | #
| slsaledb |
| test |
+--------------------+
6 rows in set (0.19 sec)
一步一步操作,你也可以成功!
原文地址:http://blog.51cto.com/13687553/2152773