1. 程式人生 > >CentOS 7下搭建百萬PV網站架構詳述

CentOS 7下搭建百萬PV網站架構詳述

日均百萬PV的網站站,費用也不低,並且CDN節點有時會出問題,還 需要每次的更改後重新整理CDN,不太方便。

改造後期望:

配置獨立伺服器,升級頻寬,更改環境以支援PHP的程式,實現本地化的廣告投放功能,以擴充套件自有化。

PV(Page View,頁面瀏覽量)即點選量,通常意義上說PV的多少是衡量一個網路新聞頻道或網站甚至一條網路新聞的主要指標。pv的解釋是這樣的:一個訪問者在24小時(0點-23點)內到底看了網站的幾個頁面。需要注意的是:同一個人瀏覽網站的同一個頁面,不重複計算pv量,點選100次頁只算1次。

案例概述:本案例設計採用四層模型實現,主要分為前端反向代理層,web層,資料庫快取層和資料庫層。前端反向代理詞采用主備模式,web層採用群集模式,資料庫快取層採用主備模式,資料層採用主從模式

具體實現:

下面將記錄實現過程的點滴,期間得益於網際網路上許多資料的幫助,在此一併表示感謝。此文章力爭做到細緻,清晰,希望對後來者起到一定幫助作用。

這裡我為了節省資源,將前端代理層,資料庫快取層,資料庫層部署在兩臺虛擬機器上,將web層分別部署在兩臺虛擬機器上。

拓撲圖如下:
CentOS 7下搭建百萬PV網站架構詳述

實驗環境如下:
CentOS 7下搭建百萬PV網站架構詳述

------------------------------------------分割線------------------------------------------

本文的原始碼包可從以下資訊得到下載:

如果取消關注Linux公社公眾號,即使再次關注,也將無法提供本服務!

連結:

https://pan.baidu.com/s/1SK5Yv0bEPkzDjATqC67RZg  密碼:獲得見上面的方法,地址失效請在下面留言。

------------------------------------------分割線------------------------------------------

裝帶有nginx rpm軟體包的源,主從都要做

yum install nginx keepalived -y            #使用centos預設的倉庫完成的安裝
vim /etc/keepalived/keepalived.conf      #配置keepalived的主配置檔案
! Configuration File for keepalived
vrrp_script nginx {                      #定義函式庫指令碼
        script "/opt/shell/nginx.sh"      #新增指令碼路徑
        interval 2                    #定義每次執行該指令碼的間隔為2s
}

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          #定義該伺服器的名稱,從伺服器要定義不同的名稱好加以區分
}

vrrp_instance VI_1 {
    state MASTER              #將主伺服器的角色定為MASTER,從伺服器為BACKUP
    interface ens33            #將網絡卡介面改為ens33
    virtual_router_id 51       
    priority 100                #定義優先順序,主伺服器要高於從伺服器
    advert_int 1
    authentication {
        auth_type PASS        #這裡表示主從伺服器的同步密碼,保持預設即可
        auth_pass 1111
    } 
    track_script {
        Nginx              #在這裡呼叫上面定義好的指令碼,注意指令碼名稱一定要相同
    } 
    virtual_ipaddress {
        192.168.199.188        #定義虛擬IP,虛擬IP一定要和伺服器的真實IP在同一網段
    } 

在指定的目錄下建立指令碼

mkdir /opt/shell
vim /opt/shell/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                              #該指令碼作用是在啟動keepalived服務時就可以直接啟動nginx服務
chmod +x /opt/shell/nginx.sh      #賦予指令碼執行許可權

配置nginx反向代理:

vim /etc/nginx/nginx.conf        #配置nginx的配置檔案
upstream tomcat_pool {
                server 192.168.199.131:8080; 
                server 192.168.199.132:8080;      #定義後端的兩臺Tomcat地址
                ip_hash;            #會話穩固功能,否則無法通過vip地址登入
        }     
        server {
                listen 80;
                server_name 192.168.199.188;    #虛擬IP
                location / {
                        proxy_pass http://tomcat_pool;
                        proxy_set_header X-Real-IP $remote_addr;
                }
        }


nginx -t -c /etc/nginx/nginx.conf #測試配置檔案語法

從伺服器的配置和主伺服器的配置基本相同,唯一不同地方在於keepalived的配置檔案,在上方都有說明,這裡就不再演示

systemctl start keepalived.service      #開啟主伺服器的keepalived服務
netstat -ntap | grep nginx      #檢視nginx是否開啟,要想關閉nginx,需要先關閉keepalived

CentOS 7下搭建百萬PV網站架構詳述

systemctl start keepalived.service      #開啟從伺服器的keepalived服務

netstat -ntap | grep nginx      #檢視nginx是否開啟,要想關閉nginx,需要先關閉keepalived,注意第二臺的nginx啟動可能會有些慢,如果nginx埠一致啟動不了就去檢查keepalived的配置檔案和自己定義的指令碼,大部分錯誤都是這兩個地方

CentOS 7下搭建百萬PV網站架構詳述

二 部署兩臺Tomcat節點伺服器
解壓所需要的兩個軟體包

tar zxvf jdk-8u144-linux-x64.tar.gz -C /opt        #jdk是一個java執行環境,要想安裝tomcat必須先安裝jdk
tar zxvf apache-tomcat-8.5.23.tar.gz -C /opt
mv jdk1.8.0_144/ /usr/local/java
mv apache-tomcat-8.5.23/ /usr/local/tomcat8    #為了方便使用jdk與Tomcat,我將它們重新命名到系統目錄下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

source /etc/profile #重新載入環境變數

java -version #使用該命令檢視jdk是否安裝成功,如果顯示版本號表示安裝成功

CentOS 7下搭建百萬PV網站架構詳述

ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown      #對Tomcat的開啟與關閉命令建立軟連結

tomcatup #開啟tomcat

CentOS 7下搭建百萬PV網站架構詳述

cd /usr/local/tomcat8/webapps/ROOT/
mv index.jsp index.jsp.bk
vim index.jsp                        #為了方便之後的測試我修改一下tomcat的首頁
<h1>server 131!</h1>

再次對主節點的Tomcat進行訪問
CentOS 7下搭建百萬PV網站架構詳述

從節點的配置與主節點完全一樣,為了便於區分我也修改了從節點的首頁
CentOS 7下搭建百萬PV網站架構詳述

接著我用虛擬IP進行訪問,測試反向代理是否成功
CentOS 7下搭建百萬PV網站架構詳述

cd /usr/local/tomcat8/conf      #修改兩臺節點伺服器的主配置檔案
vim server.xml    #跳到行尾,在Host name下新增 148
<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>    #跳到行尾,在Host name下新增 日誌除錯資訊debug為0表示資訊越少,docBase指定訪問目錄

CentOS 7下搭建百萬PV網站架構詳述

三 部署MySQL資料庫(在主從伺服器上都要部署)
這裡為了節省時間我採用yum安裝mariadb資料庫來代替MySQL資料庫,這兩個資料庫的功能相同,而且mariadb的配置更加簡單

yum install mariadb-server mariadb -y
systemctl enable mariadb.service
systemctl start mariadb.service        #開啟資料庫
netstat -ntap | grep 3306            #檢視資料庫埠

[[email protected] ~]# mysql_secure_installation        #對資料庫進行常規安全設定

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):                #這裡可以直接回車,這時我還沒有密碼
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y                            #這裡問是否建立密碼,選擇是
New password:                                      #輸入新的密碼
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n      #這裡問是否刪除匿名使用者,選擇no
 ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n    #這裡問是否拒絕管理員進行遠端登入,選擇no
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n      #這裡問是否刪除預設測試資料庫,選擇no
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y            #最後問是否現在載入許可權表,選擇yes
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

匯入slsaledb資料庫

mysql -u root -p < slsaledb-2014-4-10.sql
mysql -u root -p
show databases;

CentOS 7下搭建百萬PV網站架構詳述

MariaDB [(none)]> GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY '123456';    #給root使用者授權,讓其可以管理slsaledb
MariaDB [(none)]> flush privileges;      #重新載入資料庫許可權

在從資料庫上進行同樣的部署

以下操作在兩臺tomcat節點做

tar zxvf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/        #將會員商城的軟體包解壓至Tomcat的站點目錄下
cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
vim jdbc.properties          #修改資料庫IP地址為VRRP虛擬地址,以及授權使用者root和密碼123456

CentOS 7下搭建百萬PV網站架構詳述

tomcatdown
tomcatup      #重新啟動Tomcat服務讓配置檔案生效

http://192.168.199.188 #通過虛擬IP訪問商城
CentOS 7下搭建百萬PV網站架構詳述

四 部署Redis群集(在主伺服器和從伺服器上做)

yum install epel-release -y        #使用centos7.4 預設源安裝
yum install redis -y
vim /etc/redis.conf          #修改Redis主配置檔案
bind 0.0.0.0

CentOS 7下搭建百萬PV網站架構詳述

systemctl start redis.service      #開啟Redis服務
netstat -ntap | grep 6379        #檢視監聽埠
redis-cli -h 192.168.199.129 -p 6379    #在主伺服器上測試連線
192.168.199.129:6379> set name test    #設定name,值是test
OK
192.168.199.129:6379> get name        #獲取name值
"test"

接著在從伺服器上也是進行同樣的部署
CentOS 7下搭建百萬PV網站架構詳述
在從伺服器上需要在配置檔案中多加如下這句話,將地址指向主伺服器
CentOS 7下搭建百萬PV網站架構詳述

systemctl start redis.service            #開啟從伺服器的Redis
redis-cli -h 192.168.199.130 -p 6379    #登入Redis
192.168.199.130:6379> get name      #檢視name值
"test"                            #資料會從主伺服器上同步

在兩臺Tomcat節點伺服器配置商城專案中的連線redis的引數

vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml
38        <!--redis 配置 開始-->
47                <constructor-arg value="192.168.199.188"/>
 48                <constructor-arg value="6379"/>

測試快取效果

redis-cli -h 192.168.199.188 -p 6379
192.168.199.188:6379> info
keyspace_hits:1            #命中數
keyspace_misses:0          #未命中數

以下配置redis叢集主從切換----只在主伺服器上操作

redis-cli -h 192.168.199.129 info Replication
#Replication
role:master
connected_slaves:1
slave0:ip=192.168.199.130,port=6379,state=online,offset=1541,lag=1
master_repl_offset:1541
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1540

vim /etc/redis-sentinel.conf
17 protected-mode no

69 sentinel monitor mymaster 192.168.199.129 6379 1    #1表示有一臺從伺服器

98 sentinel down-after-milliseconds mymaster 3000

systemctl start redis-sentinel.service    #啟動叢集
netstat -ntap | grep 26379

systemctl start redis-sentinel.service    #在從伺服器上啟動叢集
netstat -ntap | grep 26379

redis-cli -h 192.168.199.129 -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.199.129:6379,slaves=1,sentinels=2

驗證主從切換
在主上

systemctl stop redis
redis-cli -h 192.168.199.129 -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.199.130:6379,slaves=1,sentinels=3    #主伺服器進行了切換,如果再次開啟主伺服器,角色並不會切換,除非關掉從伺服器

驗證資料同步情況:

在主伺服器上:
redis-cli -h 192.168.199.129 -p 6379
192.168.199.129:6379> set name2 test2
OK
192.168.199.129:6379> get name2
"test2"

在從伺服器上:
redis-cli -h 192.168.199.130 -p 6379
192.168.199.130:6379> get name2
"test2"                                #如果無法連線或無法同步就重啟兩邊的群集服務和redis服務

五 部署MySQL主從同步

在主伺服器上:
vim /etc/my.cnf          #配置主MySQL的配置檔案
[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

systemctl restart mariadb.service    #重新啟動mariadb資料庫使配置檔案生效

netstat -ntap | grep 3306

mysql -u root -p          #登入資料庫

MariaDB [(none)]> show master status;      #檢視日誌檔名稱和偏移量
MariaDB [(none)]> grant replication slave on *.* to 'rep'@'192.168.199.%' identified by '123456';    #給從資料庫授權
MariaDB [(none)]> flush privileges;          #重新整理許可權

在從伺服器上:
vim /etc/my.cnf
[mysqld]
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=2            #id值不能與主伺服器相同
log_slave_updates=true
sync_binlog=1

systemctl restart mariadb.service      #重新啟動資料庫,使配置檔案生效

netstat -ntap | grep 3306

mysql -u root -p
MariaDB [(none)]> change master to master_host='192.168.199.129',master_user='rep',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=473;    #設定主伺服器的日誌檔名稱和偏移量
MariaDB [(none)]> start slave;          #開啟同步
MariaDB [(none)]> show slave status\G;    #檢視同步狀態
Slave_IO_Running: Yes
Slave_SQL_Running: Yes              #這兩條資訊都顯示Yes表示同步成功

六 登入驗證(登入使用者名稱為admin)

http://192.168.199.188/


CentOS 7下搭建百萬PV網站架構詳述

在主伺服器上:
redis-cli -h 192.168.199.188 -p 6379 
192.168.199.188:6379> info
keyspace_hits:4       

#對命中數進行檢視

到此百萬PV網站架構搭建完成