日均百萬PV的網(wǎng)站站,費用也不低,并且CDN節(jié)點有時會出問題,還 需要每次的更改后刷新CDN,不太方便。
改造后期望:
配置獨立服務(wù)器,升級帶寬,更改環(huán)境以支持PHP的程序,實現(xiàn)本地化的廣告投放功能,以擴展自有化。
PV(Page View,頁面瀏覽量)即點擊量,通常意義上說PV的多少是衡量一個網(wǎng)絡(luò)新聞頻道或網(wǎng)站甚至一條網(wǎng)絡(luò)新聞的主要指標。pv的解釋是這樣的:一個訪問者在24小時(0點-23點)內(nèi)到底看了網(wǎng)站的幾個頁面。需要注意的是:同一個人瀏覽網(wǎng)站的同一個頁面,不重復(fù)計算pv量,點擊100次頁只算1次。
案例概述:本案例設(shè)計采用四層模型實現(xiàn),主要分為前端反向代理層,web層,數(shù)據(jù)庫緩存層和數(shù)據(jù)庫層。前端反向代理詞采用主備模式,web層采用群集模式,數(shù)據(jù)庫緩存層采用主備模式,數(shù)據(jù)層采用主從模式
具體實現(xiàn):
下面將記錄實現(xiàn)過程的點滴,期間得益于互聯(lián)網(wǎng)上許多資料的幫助,在此一并表示感謝。此文章力爭做到細致,清晰,希望對后來者起到一定幫助作用。
這里我為了節(jié)省資源,將前端代理層,數(shù)據(jù)庫緩存層,數(shù)據(jù)庫層部署在兩臺虛擬機上,將web層分別部署在兩臺虛擬機上。
拓撲圖如下:
實驗環(huán)境如下:
——————————————分割線——————————————
本文的源碼包可從以下信息得到下載:
點擊這個http://www.linuxidc.com/Linux/2013-12/93755.htm 鏈接去關(guān)注 Linux公社官方微信,關(guān)注后回復(fù)數(shù)字155206。即可得到網(wǎng)友的分享密碼。
如果取消關(guān)注Linux公社公眾號,即使再次關(guān)注,也將無法提供本服務(wù)!
鏈接: https://pan.baidu.com/s/1SK5Yv0bEPkzDjATqC67RZg 密碼:獲得見上面的方法,地址失效請在下面留言。
——————————————分割線——————————————
具體部署如下:
一 在前面兩臺主從服務(wù)器上安裝nginx和keepalived
rpm -ivh http://nginx.org/packages/CentOS/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm #
裝帶有nginx rpm軟件包的源,主從都要做
yum install nginx keepalived -y #使用centos默認的倉庫完成的安裝
vim /etc/keepalived/keepalived.conf #配置keepalived的主配置文件
! Configuration File for keepalived
vrrp_script nginx { #定義函數(shù)庫腳本
script “/opt/shell/nginx.sh” #添加腳本路徑
interval 2 #定義每次執(zhí)行該腳本的間隔為2s
}
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id NGINX_HA #定義該服務(wù)器的名稱,從服務(wù)器要定義不同的名稱好加以區(qū)分
}
vrrp_instance VI_1 {
state MASTER #將主服務(wù)器的角色定為MASTER,從服務(wù)器為BACKUP
interface ens33 #將網(wǎng)卡接口改為ens33
virtual_router_id 51
priority 100 #定義優(yōu)先級,主服務(wù)器要高于從服務(wù)器
advert_int 1
authentication {
auth_type PASS #這里表示主從服務(wù)器的同步密碼,保持默認即可
auth_pass 1111
}
track_script {
Nginx #在這里調(diào)用上面定義好的腳本,注意腳本名稱一定要相同
}
virtual_ipaddress {
192.168.199.188 #定義虛擬IP,虛擬IP一定要和服務(wù)器的真實IP在同一網(wǎng)段
}
}
在指定的目錄下創(chuàng)建腳本
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服務(wù)時就可以直接啟動nginx服務(wù)
chmod +x /opt/shell/nginx.sh #賦予腳本執(zhí)行權(quán)限
配置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; #會話穩(wěn)固功能,否則無法通過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 #測試配置文件語法
從服務(wù)器的配置和主服務(wù)器的配置基本相同,唯一不同地方在于keepalived的配置文件,在上方都有說明,這里就不再演示
systemctl start keepalived.service #開啟主服務(wù)器的keepalived服務(wù)
netstat -ntap | grep nginx #查看nginx是否開啟,要想關(guān)閉nginx,需要先關(guān)閉keepalived
systemctl start keepalived.service #開啟從服務(wù)器的keepalived服務(wù)
netstat -ntap | grep nginx #查看nginx是否開啟,要想關(guān)閉nginx,需要先關(guān)閉keepalived,注意第二臺的nginx啟動可能會有些慢,如果nginx端口一致啟動不了就去檢查keepalived的配置文件和自己定義的腳本,大部分錯誤都是這兩個地方
二 部署兩臺Tomcat節(jié)點服務(wù)器
解壓所需要的兩個軟件包
tar zxvf jdk-8u144-linux-x64.tar.gz -C /opt #jdk是一個java運行環(huán)境,要想安裝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,我將它們重命名到系統(tǒng)目錄下vim /etc/profile #添加環(huán)境變量
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 #重新加載環(huán)境變量
java -version #使用該命令查看jdk是否安裝成功,如果顯示版本號表示安裝成功
ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown #對Tomcat的開啟與關(guān)閉命令建立軟鏈接
tomcatup #開啟tomcat
cd /usr/local/tomcat8/webapps/ROOT/
mv index.jsp index.jsp.bk
vim index.jsp #為了方便之后的測試我修改一下tomcat的首頁
<h1>server 131!</h1>
再次對主節(jié)點的Tomcat進行訪問
從節(jié)點的配置與主節(jié)點完全一樣,為了便于區(qū)分我也修改了從節(jié)點的首頁
接著我用虛擬IP進行訪問,測試反向代理是否成功
cd /usr/local/tomcat8/conf #修改兩臺節(jié)點服務(wù)器的主配置文件
vim server.xml #跳到行尾,在Host name下新增 148
<Context path=”” docBase=”SLSaleSystem” reloadable=”true” debug=”0″></Context> #跳到行尾,在Host name下新增 日志調(diào)試信息debug為0表示信息越少,docBase指定訪問目錄
三 部署MySQL數(shù)據(jù)庫(在主從服務(wù)器上都要部署)
這里為了節(jié)省時間我采用yum安裝mariadb數(shù)據(jù)庫來代替MySQL數(shù)據(jù)庫,這兩個數(shù)據(jù)庫的功能相同,而且mariadb的配置更加簡單
yum install mariadb-server mariadb -y
systemctl enable mariadb.service
systemctl start mariadb.service #開啟數(shù)據(jù)庫
netstat -ntap | grep 3306 #查看數(shù)據(jù)庫端口
[root@localhost ~]# mysql_secure_installation #對數(shù)據(jù)庫進行常規(guī)安全設(shè)置
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 #這里問是否創(chuàng)建密碼,選擇是
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 #這里問是否刪除默認測試數(shù)據(jù)庫,選擇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 #最后問是否現(xiàn)在加載權(quán)限表,選擇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!
導(dǎo)入slsaledb數(shù)據(jù)庫
mysql -u root -p < slsaledb-2014-4-10.sql
mysql -u root -p
show databases;
MariaDB [(none)]> GRANT all ON slsaledb.* TO ‘root’@’%’ IDENTIFIED BY ‘123456’; #給root用戶授權(quán),讓其可以管理slsaledb
MariaDB [(none)]> flush privileges; #重新加載數(shù)據(jù)庫權(quán)限
在從數(shù)據(jù)庫上進行同樣的部署
以下操作在兩臺tomcat節(jié)點做
tar zxvf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/ #將會員商城的軟件包解壓至Tomcat的站點目錄下
cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
vim jdbc.properties #修改數(shù)據(jù)庫IP地址為VRRP虛擬地址,以及授權(quán)用戶root和密碼123456
tomcatdown
tomcatup #重新啟動Tomcat服務(wù)讓配置文件生效
http://192.168.199.188 #通過虛擬IP訪問商城
四 部署Redis群集(在主服務(wù)器和從服務(wù)器上做)
yum install epel-release -y #使用centos7.4 默認源安裝
yum install redis -y
vim /etc/redis.conf #修改Redis主配置文件
bind 0.0.0.0
systemctl start redis.service #開啟Redis服務(wù)
netstat -ntap | grep 6379 #查看監(jiān)聽端口
redis-cli -h 192.168.199.129 -p 6379 #在主服務(wù)器上測試連接
192.168.199.129:6379> set name test #設(shè)置name,值是test
OK
192.168.199.129:6379> get name #獲取name值
“test”
接著在從服務(wù)器上也是進行同樣的部署
在從服務(wù)器上需要在配置文件中多加如下這句話,將地址指向主服務(wù)器
systemctl start redis.service #開啟從服務(wù)器的Redis
redis-cli -h 192.168.199.130 -p 6379 #登錄Redis
192.168.199.130:6379> get name #查看name值
“test” #數(shù)據(jù)會從主服務(wù)器上同步
在兩臺Tomcat節(jié)點服務(wù)器配置商城項目中的連接redis的參數(shù)
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 #命中數(shù)
keyspace_misses:0 #未命中數(shù)
以下配置redis集群主從切換—-只在主服務(wù)器上操作
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表示有一臺從服務(wù)器
98 sentinel down-after-milliseconds mymaster 3000
systemctl start redis-sentinel.service #啟動集群
netstat -ntap | grep 26379
systemctl start redis-sentinel.service #在從服務(wù)器上啟動集群
netstat -ntap | grep 26379
redis-cli -h 192.168.199.129 -p 26379 info Sentinel #再次在主服務(wù)器上進行查看
#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 #主服務(wù)器進行了切換,如果再次開啟主服務(wù)器,角色并不會切換,除非關(guān)掉從服務(wù)器
驗證數(shù)據(jù)同步情況:
在主服務(wù)器上:
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”
在從服務(wù)器上:
redis-cli -h 192.168.199.130 -p 6379
192.168.199.130:6379> get name2
“test2” #如果無法連接或無法同步就重啟兩邊的群集服務(wù)和redis服務(wù)
五 部署MySQL主從同步
在主服務(wù)器上:
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數(shù)據(jù)庫使配置文件生效
netstat -ntap | grep 3306
mysql -u root -p #登錄數(shù)據(jù)庫
MariaDB [(none)]> show master status; #查看日志文件名稱和偏移量
MariaDB [(none)]> grant replication slave on *.* to ‘rep’@’192.168.199.%’ identified by ‘123456’; #給從數(shù)據(jù)庫授權(quán)
MariaDB [(none)]> flush privileges; #刷新權(quán)限
在從服務(wù)器上:
vim /etc/my.cnf
[mysqld]
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=2 #id值不能與主服務(wù)器相同
log_slave_updates=true
sync_binlog=1
systemctl restart mariadb.service #重新啟動數(shù)據(jù)庫,使配置文件生效
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; #設(shè)置主服務(wù)器的日志文件名稱和偏移量
MariaDB [(none)]> start slave; #開啟同步
MariaDB [(none)]> show slave statusG; #查看同步狀態(tài)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes #這兩條信息都顯示Yes表示同步成功
六 登錄驗證(登錄用戶名為admin)
http://192.168.199.188/
在主服務(wù)器上:
redis-cli -h 192.168.199.188 -p 6379
192.168.199.188:6379> info
keyspace_hits:4
#對命中數(shù)進行查看
到此百萬PV網(wǎng)站架構(gòu)搭建完成