實驗架構:
黑線是正常狀況數據的流向
紅色是異常狀況下數據流向
![大型網站架構——百萬PV網站](http://static.javashuo.com/static/loading.gif)
實驗環境:
CentOS7-1(master) 192.168.13.128 nginx反向代理(主)、redis緩存處理器(主)、mysql數據庫(主)
CentOS7-2(backup) 192.168.13.129 nginx反向代理(備)、redis緩存處理器(備)、mysql數據庫(從)
CentOS7-3(tomcat1) 192.168.13.130 tomcat(主)
CentOS7-4(tomcat2) 192.168.13.131 tomcat(備)
1,安裝部署nginx和keepalive服務(主備都需安裝)
[root@master ~]# systemctl stop firewalld.service ##關閉防火牆
[root@master ~]# setenforce 0
[root@master ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
##安裝nginx源
[root@master ~]# yum install -y keepalived nginx ##下載nginx和keepalive服務
[root@master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
route_id NGINX_HA ##備份爲 NGINX_HB
} ##下面刪除4行
##觸發腳本
vrrp_script nginx {
script "/opt/shell/nginx.sh"
interval 2 ##間隔時間
}
vrrp_instance VI_1 {
state MASTER ##備份爲BACKUP
interface ens33 ##網卡
virtual_router_id 51
priority 100 ##備份優先級小於主
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { ##調用上面的函數
nginx
}
virtual_ipaddress { ##虛擬ip
192.168.200.200
}
}
//備服務器上修改
router_id NGINX_HB ##主備不同
state BACKUP ##主服務
priority 90 ## 優先級,備服務器設置 90
//
[root@master ~]# mkdir /opt/shell
[root@master ~]# vim /opt/shell/nginx.sh ##編寫nginx觸發腳本
#!/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
[root@master ~]# chmod +x /opt/shell/nginx.sh
[root@master ~]# vim /etc/nginx/nginx.conf ##在include 上面一行新增
upstream tomcat_pool {
server 192.168.13.130:8080; ##定義後端節點服務器地址池
server 192.168.13.131:8080;
ip_hash; ##會話穩固功能,不然沒法經過vip地址登錄
}
server {
listen 80;
server_name 192.168.13.100; ##虛擬出的ip
location / {
proxy_pass http://tomcat_pool;
proxy_set_header X-Real-IP $remote_addr;
}
}
[root@master ~]# nginx -t ##檢查nginx配置文件的語法有沒有問題
[root@master ~]# systemctl start keepalived.service ##啓動keepalive服務
[root@master ~]# ip a ##查看虛擬ip
2,安裝部署MySQL(主備都須要安裝)
[root@master ~]# yum install -y mariadb-server mariadb
[root@master ~]# systemctl start mariadb.service
[root@master ~]# systemctl enable mariadb.service
[root@master ~]# netstat -natp | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 38997/mysqld
[root@master ~]# mysql_secure_installation ##初始化數據庫
Enter current password for root (enter for none): ##此處直接回車
Set root password? [Y/n] n ##設置密碼
Password updated successfully!
Remove anonymous users? [Y/n] n ##是否刪除匿名用戶,選擇不刪除
Disallow root login remotely? [Y/n] n ##是否遠程鏈接
Remove test database and access to it? [Y/n] n ##是否刪除測試數據庫
Reload privilege tables now? [Y/n] y ##是否從新加載
... Success!
[root@master ~]# mysql -uroot -p < slsaledb-2014-4-10.sql ##導入數據庫
[root@master ~]# mysql -uroot -p ##進入數據庫
MariaDB [(none)]> show databases; ##查看數據庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| slsaledb | ##導入的數據庫
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY 'abc123';
##受權slsaledb庫下的全部給root用戶密碼爲abc123
MariaDB [(none)]> flush privileges; ##刷新權限
3,部署tomcat服務(兩臺節點服務器上作)
[root@tomcat01 ~]# systemctl stop firewalld.service
[root@tomcat01 ~]# setenforce 0
[root@tomcat01 ~]# tar zxvf apache-tomcat-8.5.23.tar.gz -C /opt/ ##解壓壓縮包
[root@tomcat01 ~]# tar zxvf jdk-8u144-linux-x64.tar.gz -C /opt/
[root@tomcat01 ~]# cp -r /opt/jdk1.8.0_144/ /usr/local/java ##複製jdk目錄到/usr/local/java
[root@tomcat01 ~]# 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
[root@tomcat01 ~]# source /etc/profile
[root@tomcat01 ~]# java -version ##查看版本
[root@tomcat01 ~]# cp -r /opt/apache-tomcat-8.5.23/ /usr/local/tomcat8 ##tomcat目錄
[root@tomcat01 ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup ##創建軟鏈接
[root@tomcat01 ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@tomcat01 ~]# tomcatup ##開啓tomcat
[root@tomcat01 ~]# netstat -natp | grep 8080 ##查看tomcat端口號
tcp6 0 0 :::8080 :::* LISTEN 37752/java
[root@tomcat01 ~]# vim /usr/local/tomcat8/webapps/ROOT/index.jsp //修改默認網頁內容
<h1>server 130</h1> //節點1的網頁內容
<h1>server 131</h1> //節點2的網頁內容
![大型網站架構——百萬PV網站](http://static.javashuo.com/static/loading.gif)
![大型網站架構——百萬PV網站](http://static.javashuo.com/static/loading.gif)
![大型網站架構——百萬PV網站](http://static.javashuo.com/static/loading.gif)
[root@tomcat01 ROOT]# cd /usr/local/tomcat8/conf/
[root@tomcat01 conf]# vim server.xml ##修改tomcat配置文件
148 <Host name="localhost" appBase="webapps"
149 unpackWARs="true" autoDeploy="true">
150 <Context path="" docBase="SLSaleSystem" reloadable="true" debug=" 0"></Context>
##日誌調試信息debug爲0表示信息越少,docBase指定訪問目錄
[root@tomcat01 conf]# tomcatup ##啓動tomcat服務
4,搭建商城網站
[root@tomcat01 ~]# tar zxvf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/
[root@tomcat01 ~]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
[root@tomcat01 classes]# vim jdbc.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://192.168.13.100\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8
##修改地址爲虛擬ip
uname=root
password=abc123 ##受權的用戶名和密碼
minIdle=10
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true
[root@tomcat01 classes]# tomcatdown ##關閉開啓tomca服務
[root@tomcat01 classes]# tomcatup
![大型網站架構——百萬PV網站](http://static.javashuo.com/static/loading.gif)
![大型網站架構——百萬PV網站](http://static.javashuo.com/static/loading.gif)
![大型網站架構——百萬PV網站](http://static.javashuo.com/static/loading.gif)
5,部署redis主從和羣集(在主備上配置)
[root@master ~]# yum install -y epel-release
[root@master ~]# yum install redis -y
[root@master ~]# cat /etc/redis.conf | grep -v "^#" | grep -v "^$"
[root@master ~]# vim /etc/redis.conf
bind 0.0.0.0
##從服務器上266行多以下一行配置
slaveof 192.168.200.128 6379 ##主服務器的IP不是虛擬IP
[root@master ~]# systemctl start redis.service ##開啓redis服務
[root@master ~]# netstat -natp | grep 6379
[root@master ~]# redis-cli -h 192.168.13.128 -p 6379 ##在主上設置鍵值
192.168.13.128:6379> set name test
OK
192.168.13.128:6379> get name
"test"
[root@master ~]# redis-cli -h 192.168.13.129 -p 6379 ##在從上查看,顯示主從同步
192.168.13.129:6379> get name
"test"
6,在tomcat服務器上配置(配置項目商城中鏈接redis的參數)
[root@tomcat01 classes]# vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml
47 <constructor-arg value="192.168.13.100"/> ##虛擬地址
48 <constructor-arg value="6379"/>
[root@tomcat01 classes]# tomcatdown ##關閉開啓tomca服務
[root@tomcat01 classes]# tomcatup
7,在主備服務器上配置(測試Redis緩存效果)
//登陸商城,而後反覆點擊須要數據庫參與的操做頁面,再回來檢查keyspace_hits或者keyspace_misses: 值變化
//keyspace_hits: 或者 keyspace_misses://關注這個值,命中數和未命中數
[root@master ~]# redis-cli -h 192.168.13.100 -p 6379
192.168.13.100:6379> info
expired_keys:0
evicted_keys:0
keyspace_hits:2 ##命中,說明redis參與工做了
keyspace_misses:0
pubsub_channels:0
8,配置redis集羣主從切換(只在主服務器上操做)
[root@master ~]# redis-cli -h 192.168.13.128 info Replication ##獲取當前服務器的角色
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.13.129,port=6379,state=online,offset=10800,lag=0
master_repl_offset:10800
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:10799
[root@master ~]# vim /etc/redis-sentinel.conf ##修改redis哨兵配置文件
17 protected-mode no
69 sentinel monitor mymaster 192.168.200.128 6379 1 ##1表示1臺從 注意:修改
98 sentinel down-after-milliseconds mymaster 3000 ##故障切換時間單位是毫秒
[root@master ~]# service redis-sentinel start ##啓動集羣
[root@master ~]# netstat -anpt | grep 26379 ##查看端口號
[root@master ~]# redis-cli -h 192.168.13.128 -p 26379 info Sentinel
# 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.13.128:6379,slaves=1,sentinels=1
//驗證主從切換//
[root@master ~]# systemctl stop redis.service ##關閉主的redis服務
[root@master ~]# redis-cli -h 192.168.13.128 -p 26379 info Sentinel
# 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.13.129:6379,slaves=1,sentinels=1
##此時切換到129備服務器上
[root@master ~]# systemctl start redis.service ##重啓開啓主的redis服務
9,在主備服務器上配置(配置mysql主從複製)
//主服務器上配置//
[root@master ~]# vim /etc/my.cnf
[mysqld]
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1
log_slave_updates=true
sync_binlog=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[root@master ~]# systemctl restart mariadb.service
[root@master ~]# netstat -natp | grep 3306
[root@master ~]# mysql -uroot
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.13.%' identified by '123456';
##受權複製權限
MariaDB [(none)]> flush privileges;
//備服務器上配置//
[root@backup ~]# vim /etc/my.cnf
[mysqld]
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=2 ##修改id爲2
log_slave_updates=true
sync_binlog=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[root@backup ~]# systemctl restart mariadb.service
[root@backup ~]# mysql -uroot
MariaDB [(none)]> change master to master_host='192.168.13.128',master_user='rep',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
##複製主服務器上的二進制日誌文件及位置
MariaDB [(none)]> start slave; ##開啓同步
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status\G ##查看從同步狀態
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
10,關閉master主服務器,驗證架構
![大型網站架構——百萬PV網站](http://static.javashuo.com/static/loading.gif)
謝謝閱讀!!!