Mega PV Site Architecture case

Source: Internet
Author: User
Tags node server nginx reverse proxy redis cluster install redis

One, million PV architecture case overview

PV (Page view, pageviews) is the number of clicks, usually the main indicator of how popular a website is.

This case is implemented in four-layer mode, which is mainly divided into front-end reverse proxy layer, Web layer, database cache layer and database layer. The front-end reverse proxy layer adopts the main standby mode, the Web layer adopts the cluster mode, the database cache layer adopts the main standby mode, and the database layer adopts the master-slave mode. Each layer achieves a highly available architecture, which greatly improves the stability of the business.

The case architecture diagram is as follows: The solid line indicates that the data flows normally, and the dashed lines represent the data flow in an irregular situation.

The case environment is shown in the following table:

Host Role IP Address Use
Master 192.168.174.139 Front-end Nginx Reverse proxy host, Redis cache host, MySQL data main library
Backup 192.168.174.141 Front-end Nginx reverse proxy standby, Redis cache standby, MySQL data backup library
Tomcat-node1 192.168.174.142 Tomcat Server
Tomcat-node2 192.168.174.165 Tomcat Server
Second, install the nginx+keepalived (1) Install the source with the Nginx RPM software package
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
(2) Install keepalived and Nginx service with Yum
yum install -y keepalived nginx
(3) Modify the keepalived.conf configuration file
vi /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {    route_id NGINX_HA     //主服务器id为NGINX_HA,从是NGINX_HB}vrrp_script nginx {    script "/opt/shell/nginx.sh"   //编写脚本,待会创建    interval 2}vrrp_instance VI_1 {    state MASTER    interface eth0    virtual_router_id 51    priority 100  //master优先级要高于backup    advert_int 1    authentication {        auth_type PASS        auth_pass 1111}track_script {    nginx           //触发脚本}virtual_ipaddress {    192.168.174.110    //虚拟ip地址    }}

(4) Create a Nginx.shell script directory and write scripts

mkdir /opt/shellvi /opt/shell/nginx.sh#!/bin/bashk=`ps -ef | grep keepalived | grep -v grep | wc -l`if [ $k -gt 0 ];then    

The keepalived configuration operation from the server is the same as the primary server operation.

(5) Edit nginx.conf config file, same as master and slave operation

upstream tomcat_pool {                server 192.168.174.165:8080;  //两台节点服务器地址                server 192.168.174.142:8080;                ip_hash;        }        server {                listen 80;                server_name 192.168.174.110; //虚拟ip地址                location / {                        proxy_pass http://tomcat_pool;                        proxy_set_header X-Real-IP $remote_addr;                }        }

(6) Check the syntax and open the service

nginx -t -c /etc/nginx/nginx.conf  //测试配置文件语法systemctl start keepalived.service    //nginx启动会等待一会

(7) test the function of keepalived is normal
Primary server: IP addr

Turn off the keepalived service to view the primary server again, the virtual IP address no longer exists

Switch to view from the server, at which point the virtual IP automatically drifts to the slave server.

Turn on the keepalived service on the primary server, when the virtual IP is again drifting to the primary server.

Third, Tomcat installation

The two-node server installation process is the same.

(1) Unzip Apache and JDK,

tar xf apache-tomcat-8.5.23.tar.gztar xf jdk-8u144-linux-x64.tar.gzcp -rv jdk1.8.0_144/ /usr/local/java

(2) Configuring the JDK environment variable

vi /etc/profileexport JAVA_HOME=/usr/local/javaexport JRE_HOME=/usr/local/java/jreexport PATH=$PATH:/usr/local/java/binexport CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/libsource /etc/profile  //让环境变量及时生效java -version  //查看java版本

(3) Create a Tomcat open and close link and turn on the service

cp -r apache-tomcat-8.5.23 /usr/local/tomcat8ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatupln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdowntomcatupnetstat -anpt | grep 8080

(4) test whether the default test page is displayed correctly

http://192.168.174.165:8080/  http://192.168.174.142:8080/vi /usr/local/tomcat8/webapps/ROOT/index.jsp //修改默认网页内容

Enter the dispatcher address, which is the virtual address http://192.168.174.110/, to test the scheduling of two nodes

When you drop 165 of this host, look at the information displayed on the page, which shows the homepage of another Tomcat server

cd /usr/local/tomcat8/conf/vi server.xml //跳到行尾,在Host name下新增 148<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>日志调试信息debug为0表示信息越少,docBase指定访问目录
Iv. MySQL Installation

(1) Yum installation MARIDB database

yum install -y mariadb-server mariadbsystemctl start mariadb.service  //开启服务systemctl enable mariadb.service  netstat -anpt | grep 3306   //查看服务是否开启成功

Mysql_secure_installation//General Security settings

Enter current password for root (enter for none): #敲回车OK, successfully used password, moving on...Set root password? [Y/n] yNew password: Re-enter new password: Password updated successfully!Reloading privilege tables.. ... Success!Remove anonymous users? [Y/n] n ... skipping.Disallow root login remotely? [Y/n] n ... skipping.Remove test database and access to it? [Y/n] n ... skipping.Reload privilege tables now? [Y/n] y ... Success!

(2) test whether the MARIADB is available

mysql -uroot -p  //进入数据库

(3) Import Mall data

mysql -u root -p  < slsaledb-2014-4-10.sqlmysql -uroot -pshow databases;GRANT all ON slsaledb.* TO ‘root‘@‘%‘ IDENTIFIED BY  ‘abc123‘; //给root用户授权flush privileges; //刷新

(5) Build a website on two tomcat servers

tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps///解压商城cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classesvi jdbc.properties //修改数据库IP地址是VRRP的虚拟IP,以及授权的用户名root和密码abc123。driverClassName=com.mysql.jdbc.Driverurl=jdbc\:mysql\://192.168.174.110\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8uname=rootpassword=abc123minIdle=10maxIdle=50initialSize=5maxActive=100maxWait=100removeAbandonedTimeout=180removeAbandoned=true

(6) Website testing

http://192.168.1754.142:8080/   //默认的用户名admin 密码:123456http://192.168.174.165:8080/http://192.168.174.110 //输入虚拟地址测试登录,并且关闭主再测试登录



V. Redis Cluster

(1) Install Epel source

yum install -y epel-release

(2) Installing Redis

yum install redis -y

(3) Changing the redis.conf configuration file

vim /etc/redis.confbind 0.0.0.0

Configure multiple rows from the server as follows

slaveof 192.168.174.139  6379   //主服务器的IP不是虚拟IP

Turn on the service and check that the port is open

systemctl start redis.servicenetstat -anpt | grep 6379

(4) Enter the master server database to create the data

redis-cli -h 192.168.174.139 -p 6379192.168.174.139:6379> set username zhangsanOK192.168.174.139:6379> get username"zhangsan"

(5) Enter from the server, test whether synchronization

redis-cli -h 192.168.174.141 -p 6379192.168.174.141:6379> get username"zhangsan"     //获取到主服务器创建的数据,说明主从同步成功

(6) Configuring site link Redis Parameters on two tomcat servers

cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/ vim applicationContext-mybatis.xml <constructor-arg value="192.168.174.110"/>         #47行                <constructor-arg value="6379"/>    #48行

(7) Open the Site page, click on the need to make a database call, and test whether the cache is turned on

192.168.174.110:6379> info     //查看缓存是否开启成功# Stats...keyspace_hits:2  //当命中为2时说明缓存开启成功keyspace_misses:20   //未命中...

(8) master server configuration Redis master-slave switch

vi /etc/redis-sentinel.conf sentinel monitor mymaster 192.168.174.139 6379 1 #1表示1台从(这边的ip地址是主服务器的ip地址) sentinel down-after-milliseconds mymaster 3000   #故障切换时间单位是毫秒

(9) Start the Cluster service and view the cluster information

service redis-sentinel startnetstat -anpt | grep 26379redis-cli -h 192.168.174.139 -p 26379 192.168.174.139:26379> info    #查看集群信息# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=192.168.174.139:6379,slaves=1,sentinels=1   //此时redis主服务器为139这台主机

(10) Turn off the master server Redis service to see if the Redis home server IP in the cluster information has changed

systemctl stop redis.service   #关闭主服务器的redis服务 redis-cli -h 192.168.174.139 -p 26379  192.168.174.139:26379> info  # Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=192.168.174.141:6379,slaves=1,sentinels=1  //此时发现Redis主服务器ip地址切换为从redis服务器地址,说明主从redis配置成功
Vi. MySQL master and slave configuration

1. Configuration on the primary server

(1) Editing the MY.CNF configuration file

vi /etc/my.cnf[mysqld]binlog-ignore-db=mysql,information_schemacharacter_set_server=utf8log_bin=mysql_binserver_id=1log_slave_updates=truesync_binlog=1

(2) Turn on the MySQL service and check the port status

systemctl restart mariadbnetstat -anpt | grep 3306tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      13998/mysqld

(3) Enter the database to view the master status,

mysql -u rootMariaDB [(none)]>show master status; //记录日志文件名称和 位置值+------------------+----------+--------------+--------------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |+------------------+----------+--------------+--------------------------+| mysql_bin.000001 |      473 |              | mysql,information_schema |+------------------+----------+--------------+--------------------------+1 row in set (0.00 sec)grant replication slave on *.* to ‘rep‘@‘192.168.174.%‘ identified by ‘123456‘;flush privileges;

3, from the server configuration

(1) Editing the MY.CNF configuration file

[mysqld]binlog-ignore-db=mysql,information_schemacharacter_set_server=utf8log_bin=mysql_binserver_id=2    //server_id改为2,表示为是第一台从服务器,为1,表示为是mysql主服务器log_slave_updates=truesync_binlog=1

(2) Turn on MySQL service to view port status

systemctl restart mariadbnetstat -anpt | grep 3306

(3) Access to the database

change master to master_host=‘192.168.174.139‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=473;
start slave;show slave status\G;  //查看slave状态Slave_IO_Running: Yes   //当都为yes时表示开启成功Slave_SQL_Running: Yes

(4) Test master-Slave synchronization

Create checkmysql on the primary server

MariaDB [(none)]> create database checkMysql;Query OK, 1 row affected (0.01 sec)MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || checkMysql         || mysql              || performance_schema || slsaledb           || test               |+--------------------+6 rows in set (0.00 sec)

View MySQL from the server database, where Checkmysql exists, indicating that MySQL master-slave creation succeeded.

MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || checkMysql         || mysql              || performance_schema || slsaledb           || test               |+--------------------+6 rows in set (0.00 sec)

Mega PV Site Architecture case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.