公司共有两个业务,网上图书馆和一个电商网站。php
现要求运维设计一个安全架构,本着高可用、廉价的原则。html
具体状况以下:mysql
网上图书馆是基于jsp开发;linux
电商系统是基于php开发;nginx
两个网站都是使用的mysql数据库;web
要求你提供一个高可用且廉价的架构方案,部署分发业务到外网;sql
请画出拓扑图及经过虚拟机搭建测试环境。数据库
目录apache
一. 系统概述 1tomcat
1. 服务器的配置以及信息 1
二 各模块的分析 4
1. lb模块(2个项目共用) 4
三 项目搭建(网上图书馆) 5
1. 安装JDK ,tomcat nginx (web1 web2同样安装) 5
2. 共享文件服务器(nfs)安装 7
3. nfs共享文件里面的数据实时同步到backup服务器上 8
4. 安装mysql 10
四 项目搭建(电商系统) 11
1. 安装 nginx php (web3 web4同样安装) 11
2. mysql 建立一个支持php的数据库 13
3. 挂载文件服务器 13
4. 图片和文件资源放在/data 目录下 13
五 搭建负载均衡器(nginx + keepalived) 14
1. 安装nginx keepalived 14
2. 修改nginx的配置文件(2台nginx都同样) 14
3. 编写脚本 15
4配置keepalived 15
系统 |
配置 |
ip |
模块名 |
CentOS release 6.5 64位 |
1核1G 50G 硬盘 |
172.16.1.5/10.0.0.5 |
lb01 |
CentOS release 6.5 64位 |
1核1G 50G 硬盘 |
172.16.1.6/10.0.0.6 |
lb02 |
CentOS release 6.5 64位 |
1核1G 50G 硬盘 |
172.16.1.7/10.0.0.7 |
web01 |
CentOS release 6.5 64位 |
1核1G 50G 硬盘 |
172.16.1.8/10.0.0.8 |
web02 |
CentOS release 6.5 64位 |
1核1G 50G 硬盘 |
172.16.1.9/10.0.0.9 |
web03 |
CentOS release 6.5 64位 |
1核1G 50G 硬盘 |
172.16.1.10/10.0.0.10 |
web04 |
CentOS release 6.5 64位 |
1核1G 50G 硬盘 |
172.16.1.31/10.0.0.31 |
NFS |
CentOS release 6.5 64位 |
1核1G 50G 硬盘 |
172.16.1.41/10.0.0.41 |
BACKUP |
CentOS release 6.5 64位 |
1核1G 50G 硬盘 |
172.16.1.51/10.0.0.51 |
db |
软件的名称 |
版本 |
备注 |
mysql |
5.6.34 |
64位 |
nginx |
1.12.2 |
64位 |
keepalived |
1.2.13-5 |
64位 |
nfs-utils |
1.2.3-39 |
64位 |
rpcbind |
0.2.0-11 |
64位 |
php |
5.5.32 |
64位 |
tomcat |
8.0.43 |
64位 |
jdk |
1.8.0_161 |
64位 |
wordpress |
4.7.3 |
64位 |
图一
图二:
1) lb模块采用的负载均衡机制, 运用了vrrp(路由冗余协议), 保证一台宕机 不影响业务的运转, 基于并发量的考虑咱们这边使用了(keepalived+nginx)这种架构, 当后面并发上来,能够考虑换成(Keepalived + LVS )架构, 或者商业硬件,好比F5,A10之类的。
2) 咱们这里作了互为主备配置
lb的物理ip |
主 |
备 |
虚拟ip |
172.16.1.5/10.0.0.5(lb01) |
网上图书馆 |
电商系统 |
10.0.0.3 |
172.16.1.6/10.0.0.6(lb02) |
电商系统 |
网上图书馆 |
10.0.0.4 |
基于安全考虑nginx 反向代理tomcat, 把tomcat放在后面, 不直接对外开放, 一些须要用到的图片资源,文件资源, 用共享文件服务器(nfs),而后数据直接同步到备份服务器。数据直接写入主mysql,从从mysql里面读数据。咱们这里直接就一台mysql, 没有作读写分离
主机名 |
安装的软件 |
ip |
web01 |
tomcat+ jdk + nginx |
172.16.1.7/10.0.0.7 |
web02 |
tomcat+ jdk + nginx |
172.16.1.8/10.0.0.8 |
nfs01 |
nfs-utils rpcbind rsync sersync |
172.16.1.31/10.0.0.31 |
backup |
rsync |
172.16.1.41/10.0.0.41 |
db |
mysql |
172.16.1.51/10.0.0.51 |
一些须要用到的图片资源,文件资源, 用共享文件服务器(nfs), 而后数据直接同步到备份服务器。 数据直接写入主mysql,从从mysql里面读数据。咱们这里直接就一台mysql,没有作读写分离
主机名 |
安装的软件 |
ip |
web03 |
nginx+ php |
172.16.1.7/10.0.0.7 |
web04 |
nginx+ php |
172.16.1.8/10.0.0.8 |
nfs01 |
nfs-utils rpcbind rsync sersync |
172.16.1.31/10.0.0.31 |
backup |
rsync |
172.16.1.41/10.0.0.41 |
db |
mysql |
172.16.1.51/10.0.0.51 |
#解压tomcat到/application目录下
[root@web01 tools]# tar -xf apache-tomcat-8.0.43.tar.gz -C /application/
#解压JDK 并配置环境变量
[root@web01 tools]# tar -xf jdk-8u161-linux-x64.tar.gz
[root@web01 tools]# tail -2 /etc/profile
export JAVA_HOME=/server/tools/jdk1.8.0_161
export PATH=$JAVA_HOME/bin:$PATH
#启动tomcat
/application/apache-tomcat-8.0.43/bin/startup.sh
#查看进程
[root@web01 bin]# ps -ef | grep tomcat
#安装nginx
yum install -y pcre-devel openssl-devel
useradd www -s /sbin/nologin/ -M
cd /server/tools&& cd /server/tools
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar xf nginx-1.12.2.tar.gz
#编译安装
./configure --prefix=/application/nginx-12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make && make install
#软链接
ln -s /application/nginx-1.12.2 /application/nginx
#编写nginx.conf
[root@web01 nginx]# cat conf/nginx.conf
worker_processes 1;
error_log logs/test_error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
upstream tomcat {
server localhost:8080;
}
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
location / {
proxy_pass http://tomcat;
proxy_set_header host $host;
}
}
}
#启动nginx
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx
#下载 nfs-utils rpcbind
[root@nfs01 ~]# yum install -y nfs-utils rpcbind
#验证是否安装
[root@nfs01 ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-12.el6.x86_64
nfs-utils-1.2.3-70.el6_8.2.x86_64
#启动rpcbind 再启动nfs
/etc/init.d/rpcbind start
/etc/init.d/nfs start
#配置文件/etc/exports /backup网上图书馆 /backup1电商系统
[root@nfs01 ~]# cat /etc/exports
/backup 172.16.1.31/24(rw,sync,all_squash)
/backup1 172.16.1.31/24(rw,sync,all_squash)
#其余主机测试
[root@web02 tools]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/backup1 172.16.1.31/24
/backup 172.16.1.31/24
#受权
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /backup
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /backup1
#web01 web02 挂载
[root@web01 nginx]# mount -t nfs 172.16.1.31:/backup /data
[root@web01 nginx]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 49G 2.7G 43G 6% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 194M 34M 151M 19% /boot
172.16.1.31:/backup 49G 2.2G 44G 5% /data
#服务端确认软件是否安装
[root@backup backup]# rpm -qa rsync
rsync-3.0.6-9.el6_4.1.x86_6
#编写配置文件
[root@backup backup]# cat /etc/rsyncd.conf
#rsync_config
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "网上图书馆"
path = /backup
[backup1]
comment = "电商系统"
path = /backup1
#建立用户
[root@backup backup]# useradd rsync -M -s /sbin/nologin
#建立备份目录
[root@backup backup]# mkdir /backup /backup1
[root@backup backup]# chown -R rsync.rsync /backup
[root@backup backup]# chown -R rsync.rsync /backup1
#建立认证文件
[root@backup backup]# echo "rsync_backup:oldboy123" >>/etc/rsync.password
[root@backup backup]# chmod 600 /etc/rsync.password
#启动
[root@backup backup]# rsync --daemon
#客户端确认软件是否安装 nfs服务器上
[root@nfs01 ~]# rpm -qa rsync
rsync-3.0.6-9.el6_4.1.x86_64
#客户端建立密码
echo "oldboy123" >>/etc/rsync.password
chmod 600 /etc/rsync.password
#测试
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
sent 189 bytes received 27 bytes 432.00 bytes/sec
total size is 352 speedup is 1.63
#基于企业中经常使用sersync
#nfs服务器 安装sersync软件
#下载安装 sersync
unzip sersync_installdir_64bit.zip
cd sersync_installdir_64bit/
mv sersync/ /usr/local/
cd /usr/local/sersync/conf/;ls
confxml.xml
#修改的配置文件 起2个配置文件
[root@nfs01 conf]# cat /usr/local/sersync/conf/confxml.xml
<sersync>
<localpath watch="/backup">
<remote ip="172.16.1.41" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
[root@nfs01 conf]# cat /usr/local/sersync/conf/confxml_backup1.xml
<sersync>
<localpath watch="/backup1">
<remote ip="172.16.1.41" name="backup1"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
#解压安装
tar xf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.34-linux-glibc2.5-x86_64 /application/mysql-5.6.34
ln -sf /application/mysql-5.6.34/ /application/mysql
#建立用户
useradd mysql -M -s /sbin/nologin
chown -R mysql.mysql /application/mysql/data/
#初始化数据库
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data/ --user=mysql
#启动mysql服务
cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
sed -ri 's#/usr/local#/application#g' /etc/init.d/mysqld /application/mysql/bin/mysqld_safe
cp /application/mysql/support-files/my-default.cnf /etc/my.cnf
/etc/init.d/mysqld start
#设置密码
/application/mysql/bin/mysqladmin -uroot password "oldboy123"
/application/mysql/bin/mysql -uroot -poldboy123
#设置MySQL开机自启动
chkconfig --add mysqld
chkconfig mysqld on
#配置环境变量
echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
source /etc/profile
#建立一个对应的项目库
mysql> create database on_library;
Query OK, 1 row affected (0.00 sec)
安装nginx 忽略
安装php
#安装依赖包
yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libmcrypt-devel mhash mcrypt mysql-libs mysql-devel
#安装libiconv软件
tar zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make
make install
cd ../
#安装php
[root@web04 php-5.5.32]# tar -xf php-5.5.32.tar.gz
[root@web04 php-5.5.32]# cd php-5.5.32
./configure \
--prefix=/application/php-5.5.32 \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-ftp \
--enable-opcache=no \
--with-mysql
#软链接
ln -s /application/php-5.5.32/ /application/php
#.配置php
cp php.ini-production /application/php-5.5.32/lib/
cd /application/php/etc/
cp php-fpm.conf.default php-fpm.conf
#启动php程序服务
/application/php/sbin/php-fpm
#修改nginx支持php解析
[root@web02 html]# cat ../conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root html;
index index.php index.html index.htm;
location ~* .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
#php测试代码
[root@web04 html]# cat /application/nginx/html/test_info.php
<?php
phpinfo();
?>
create database php_data
[root@web04 ~]# mkdir /data
[root@web04 ~]# mount -t nfs 172.16.1.31:/backup1 /data
nginx 安装忽略
yum install keepalived -y
#须要实现监听本地网卡上没有的ip地址(lb1 lb2都修改)
echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf
sysctl -p
[root@lb01 ~]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream on{
server 10.0.0.7:80;
server 10.0.0.8:80;
}
server {
listen 10.0.0.3:80;
server_name localhost;
root html;
index index.html index.htm;
location /{
proxy_pass http://on;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.4:80;
server_name localhost;
root html;
index index.html index.htm;
location /{
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
#实现当nginx挂了 同时停对应的Keepalived
[root@lb01 ~]# cat /server/scripts/check_web.sh
#!/bin/bash
web_info=$(ps -ef|grep [n]ginx|wc -l)
if [ $web_info -lt 2 ]
then
/etc/init.d/keepalived stop
fi
[root@lb01 ~]# chmod +x /server/scripts/check_web.sh
#实现高可用互为主备
#lb1
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script check_web {
#定义一个监控脚本,脚本必须有执行权限
script "/server/scripts/check_web.sh"
#指定脚本间隔时间
interval 2
#脚本执行完成,让优先级值和权重值进行运算,从而实现主备切换
weight 2
}
vrrp_instance gorup01 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
track_script {
check_web
}
}
vrrp_instance gorup02 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:2
}
track_script {
check_web
}
#
}
#lb2
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_script check_web {
#定义一个监控脚本,脚本必须有执行权限
script "/server/scripts/check_web.sh"
#指定脚本间隔时间
interval 2
#脚本执行完成,让优先级值和权重值进行运算,从而实现主备切换
weight 2
}
vrrp_instance gorup01 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
track_script {
check_web
}
}
vrrp_instance gorup02 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:2
}
track_script {
check_web
}
}
[root@lb02 conf]# netstat -tulnp | grep nginx
tcp 0 0 10.0.0.4:80 0.0.0.0:* LISTEN 71920/nginx
tcp 0 0 10.0.0.3:80 0.0.0.0:* LISTEN 71920/nginx