項目背景:
爲了數據庫的性能和數據安全,即從原單臺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會固定到某一臺後端服務器不輪詢。