项目背景:
为了数据库的性能和数据安全,即从原单台lnmp分离数据库
Mariadb实现数据库与Web服务器分离,实现如下目标:php
- 将旧的数据库备份,迁移到新的服务器
- 修改配置调用新的数据库服务器
问题:
单台数据库如何实现迁移,宕机迁移?锁表迁移?灰度发布?html
这里采用的是数据库冷备份即宕机迁移,在夜深无人使用的状况下直接拷贝数据停掉服务进行迁移
配置环境:
主机角色 | IP地址 |
---|---|
web | 192.168.2.11/24 |
数据库 | 192.168.2.21/24 |
client | 192.168.2.254/24 |
实现代码
步骤一:部署数据库服务器
1)准备一台独立的服务器,安装数据库软件包mysql
[root@database ~]# yum -y install mariadb mariadb-server mariadb-devel [root@database ~]# systemctl start mariadb [root@database ~]# systemctl enable mariadb
2)将以前单机版LNMP网站中的数据库迁移到新的数据库服务器。
登录192.168.2.11主机,备份数据库并拷贝给新的服务器,关闭旧的数据库服务。nginx
[root@centos7 ~]# mysqldump wordpress > wordpress.bak [root@centos7 ~]# scp wordpress.bak 192.168.2.21:/root/ [root@centos7 ~]# systemctl stop mariadb [root@centos7 ~]# systemctl disable mariadb
登录192.168.2.21主机,使用备份文件还原数据库。
建立空数据库:web
[root@database ~]# mysql MariaDB [(none)]> create database wordpress character set utf8mb4; MariaDB [(none)]> exit
使用备份文件还原数据:sql
[root@database ~]# mysql wordpress < wordpress.bak
从新建立帐户并受权访问:数据库
[root@database ~]# mysql MariaDB [(none)]> grant all on wordpress.* to wordpress@'%' identified by 'wordpress'; MariaDB [(none)]> flush privileges; MariaDB [(none)]> exit
3)修改wordpress网站配置文件,调用新的数据库服务器。
Wordpress在第一次初始化操做时会自动生产配置文件:wp-config.php,登录192.168.2.11修改该文件便可调用新的数据库服务。vim
[root@centos7 ~]# vim /usr/local/nginx/html/wp-config.php 修改前内容以下: define('DB_HOST', '192.168.2.11'); 修: ('DB_HOST', '192.168.2.21');
步骤二:客户端测试
1)客户端使用浏览器访问wordpress网站。后端
[root@client ~]# firefox http://192.168.2.11
服务软件选择:
数据库算是稳定了,可是随着用户量的激增,大量的并发脚本程序很快会消耗服务器的cpu资源,web服务也须要升级,这里加入反向代理服务器实现均衡负载解决问题。
反向代理服务器位于用户与目标服务器之间,可是对于用户而言,反向代理服务器就至关于目标服务器,即用户直接访问反向代理服务器就能够得到目标服务器的资源。同时,用户不须要知道目标服务器的地址,也无须在用户端做任何设定。反向代理服务器一般可用来做为Web加速,即便用反向代理做为Web服务器的前置机来下降网络和服务器的负载,提升访问效率。
Nginx/LVS/HAProxy是目前使用最普遍的三种负载均衡软件
建站初期调度的效率还不是突出的问题,此时须要考虑简单易用,能够知足业务功能,如Session保持,正则匹配等.显然nginx和haproxy更适合
要求:
使用HAProxy部署Web服务器集群,实现如下目标:
备注:实际操做中DNS服务代理服务器部署在同一台主机上(节约虚拟机资源)。centos
配置环境:
主机角色 | 主机名称 | IP地址 |
---|---|---|
代理服务器&DNS | proxy | 192.168.2.5/24,192.168.4.5/24 |
web1服务器 | web1 | 192.168.2.11/24 |
web2服务器 | web2 | 192.168.2.12/24 |
web3服务器 | web3 | 192.168.2.13/24 |
数据库服务器 | databse | 192.168.2.21/24 |
NFS服务器 | nfs | 192.168.2.31/24 |
client | room9pc01 | 192.168.2.254/24 |
代码部分:
步骤一:部署web2和web3服务器
1)安装LNP软件包
[root@web2 ~]# yum -y install gcc pcre-devel openssl-devel [root@web2 lnmp_soft]# tar -xf nginx-1.12.2.tar.gz [root@web2 lnmp_soft]# cd nginx-1.12.2/ [root@web2 nginx-1.12.2]# ./configure \ --with-http_ssl_module \ --with-http_stub_status_module [root@web2 nginx-1.12.2]# make && make instal [root@web2 ~]# yum -y install php php-fpm php-mysql mariadb-devel [root@web3 ~]# yum -y install gcc pcre-devel openssl-devel [root@web3 lnmp_soft]# tar -xf nginx-1.12.2.tar.gz [root@web3 lnmp_soft]# cd nginx-1.12.2/ [root@web3 nginx-1.12.2]# ./configure \ --with-http_ssl_module \ --with-http_stub_status_module [root@web3 nginx-1.12.2]# make && make instal [root@web3 ~]# yum -y install php php-fpm php-mysql mariadb-devel
2)修改nginx配置实现动静分离(web2和web3操做)
web2修改默认首页index.php,配置两个location实现动静分离。
[root@web2 ~]# vim /usr/local/nginx/conf/nginx.conf location / { root html; index index.php index.html index.htm; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
web3修改默认首页index.php,配置两个location实现动静分离。
[root@web3 ~]# vim /usr/local/nginx/conf/nginx.conf location / { root html; index index.php index.html index.htm; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
3)启动相关服务
[root@web2 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local [root@web2 ~]# chmod +x /etc/rc.local [root@web2 ~]# /usr/local/nginx/sbin/nginx [root@web2 ~]# systemctl start php-fpm #启动php-fpm服务 [root@web2 ~]# systemctl enable php-fpm [root@web3 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local [root@web3 ~]# chmod +x /etc/rc.local [root@web3 ~]# /usr/local/nginx/sbin/nginx [root@web3 ~]# systemctl start php-fpm #启动php-fpm服务 [root@web3 ~]# systemctl enable php-fpm
将nginx服务编入systemd管理(非必须)
[root@centos7 ~]# vim /usr/lib/systemd/system/nginx.service [Unit] Description=The Nginx HTTP Server #描述信息 After=network.target remote-fs.target nss-lookup.target #指定启动nginx以前须要其余的其余服务,如network.target等 [Service] Type=forking #Type为服务的类型,仅启动一个主进程的服务为simple,须要启动若干子进程的服务为forking ExecStart=/usr/local/nginx/sbin/nginx #设置执行systemctl start nginx后须要启动的具体命令. ExecReload=/usr/local/nginx/sbin/nginx -s reload #设置执行systemctl reload nginx后须要执行的具体命令. ExecStop=/bin/kill -s QUIT ${MAINPID} #设置执行systemctl stop nginx后须要执行的具体命令. [Install] WantedBy=multi-user.target
步骤二:部署NFS,将网站数据迁移至NFS共享服务器
1)部署NFS共享服务器
[root@nfs ~]# yum install nfs-utils [root@nfs ~]# mkdir /web_share [root@nfs ~]# vim /etc/exports /web_share 192.168.2.0/24(rw,no_root_squash) [root@nfs ~]# systemctl restart rpcbind [root@nfs ~]# systemctl eanble rpcbind
NFS使用的是随机端口,每次启动NFS都须要将本身的随机端口注册到rpcbind服务,这样客户端访问NFS时先到rpcbind查询端口信息,获得端口信息后再访问NFS服务。
[root@nfs ~]# systemctl restart nfs [root@nfs ~]# systemctl enable nfs
2)迁移旧的网站数据到NFS共享服务器
将web1(192.168.2.11)上的wordpress代码拷贝到NFS共享。
[root@web1 ~]# cd /usr/local/nginx/ [root@web1 nginx]# tar -czpf html.tar.gz html/ [root@web1 nginx]# scp html.tar.gz 192.168.2.31:/web_share/
登录nfs服务器,将压缩包解压
[root@nfs ~]# cd /web_share/ [root@nfs web_share]# tar -xf html.tar.gz``````
3)全部web服务器访问挂载NFS共享数据。
[root@web1 ~]# rm -rf /usr/local/nginx/html/* [root@web1 ~]# yum -y install nfs-utils [root@web1 ~]# echo "192.168.2.31:/web_share/html /usr/local/nginx/html/ nfs defaults 0 0" >> /etc/fstab [root@web1 ~]# mount -a [root@web2 ~]# yum -y install nfs-utils [root@web2 ~]# echo "192.168.2.31:/web_share/html /usr/local/nginx/html/ nfs defaults 0 0" >> /etc/fstab [root@web2 ~]# mount -a [root@web3 ~]# yum -y install nfs-utils [root@web3 ~]# echo "192.168.2.31:/web_share/html /usr/local/nginx/html/ nfs defaults 0 0" >> /etc/fstab [root@web3 ~]# mount -a
步骤三:部署HAProxy代理服务器
1)部署HAProxy
安装软件,手动修改配置文件,添加以下内容。
[root@proxy ~]# yum -y install haproxy [root@proxy ~]# vim /etc/haproxy/haproxy.cfg listen wordpress *:80 balance roundrobin server web1 192.168.2.11:80 check inter 2000 rise 2 fall 3 server web2 192.168.2.12:80 check inter 2000 rise 2 fall 3 server web3 192.168.2.13:80 check inter 2000 rise 2 fall 3 [root@proxy ~]# systemctl start haproxy [root@proxy ~]# systemctl enable haproxy
步骤三:部署DNS域名服务器
1)安装DNS相关软件(192.168.4.5操做)。
[root@proxy ~]# yum -y install bind bind-chroot
2)修改主配置文件,添加zone。
[root@proxy ~]# vim /etc/named.conf options { listen-on port 53 { any; }; #服务监听的地址与端口 directory "/var/named"; #数据文件路径 allow-query { any; }; #容许任何主机访问DNS服务 ... ... }; zone "lab.com" IN { #定义正向区域 type master; file "lab.com.zone"; }; #include "/etc/named.rfc1912.zones"; #注释掉改行 #include "/etc/named.root.key"; #注释掉改行 [root@proxy ~]# named-checkconf /etc/named.conf #检查语法
3)修改正向解析记录文件。
注意:保留文件权限。
root@proxy named]# cp -p /var/named/named.localhost /var/named/lab.com.zone [root@proxy named]# vim /var/named/lab.zone $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum @ NS dns.lab.com. dns A 192.168.4.5 www A 192.168.4.5
4)启动服务
[root@proxy named]# systemctl start named [root@proxy named]# systemctl enable named
5)客户端修改DNS解析文件
提示:作完实验修改回原始内容。
[root@room9pc01 data]# cat /etc/resolv.conf # Generated by NetworkManager search tedu.cn nameserver 192.168.4.5 nameserver 172.40.1.10 nameserver 192.168.0.220
步骤四:修改wordpress配置文件
1)修改wp-config.php
在define('DB_NAME', 'wordpress')这行前面添加以下两行内容:
[root@web3 html]# vim /usr/local/nginx/html/wp-config.php define('WP_SITEURL', 'http://www.lab.com'); define('WP_HOME', 'http://www.lab.com');
若是不添加这两行配置,浏览器访问网站某个子页面后,URL会固定到某一台后端服务器不轮询。