今天學習了
NFS
,遂結合前面學習的LAMP
+Bind
作一個實驗,實現兩臺Web服務器採用同一個MySQL數據庫和相同的網頁文件,對LAMP和Bind不瞭解的能夠去查看我之前寫的博客: AnyISalIn的文章php
主機名 | IP地址 | 實現 |
---|---|---|
storage.anyisalin.com | 192.168.2.5 | NFS |
ns.anyisalin.com | 192.168.2.2 | dns,MySQL |
www.anyisalin.com | 192.168.2.3 | web |
www.anyisalin.com | 192.168.2.4 | web |
本文全部主機皆關閉SElinux
和IPtables
python
[root@storage ~]# yum install nfs-utils | tail -n 10 #安裝nfs-utils 這裏已經安裝過了
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirrors.pubyun.com
* extras: mirrors.skyshe.cn
* updates: mirrors.pubyun.com
Package 1:nfs-utils-1.2.3-64.el6.x86_64 already installed and latest version
Nothing to do
建立文件夾並導出mysql
[root@storage ~]# mkdir /var/mydata
[root@storage ~]# mkdir /var/webroot
[root@storage var]# cd /var/webroot/
[root@storage webroot]# unzip wordpress-4.4.1-zh_CN.zip &> /dev/null
[root@storage webroot]# ls
wordpress wordpress-4.4.1-zh_CN.zip
[root@storage webroot]# chmod 777 wordpress -R
[root@storage ~]# vim /etc/exports
/var/mydata 192.168.2.2(rw,no_root_squash)
/var/webroot 192.168.2.3(rw,no_root_squash) 192.168.2.4(rw,no_root_squash)
[root@storage ~]# service rpcbind start && service nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
[root@storage ~]# showmount -e localhost #查看當前導出的目錄
Export list for localhost:
/var/webroot 192.168.2.4,192.168.2.3
/var/mydata 192.168.2.2
如下操做在ns.anyisalin.com中執行linux
[root@localhost ~]# yum install bind mysql-server -y | tail -n 10 #信息過長遂使用tail限制
perl-DBD-MySQL.x86_64 0:4.013-3.el6
perl-DBI.x86_64 0:1.609-4.el6
perl-Module-Pluggable.x86_64 1:3.90-141.el6_7.1
perl-Pod-Escapes.x86_64 1:1.04-141.el6_7.1
perl-Pod-Simple.x86_64 1:3.13-141.el6_7.1
perl-libs.x86_64 4:5.10.1-141.el6_7.1
perl-version.x86_64 3:0.77-141.el6_7.1
portreserve.x86_64 0:0.0.4-9.el6
Complete!
[root@localhost ~]#
在/etc/named.rfc1912.zones文件中添加如下幾行web
zone "anyisalin.com" IN {
type master;
file "anyisalin.com.zone";
};
配置區域解析庫文件爲以下sql
vim /var/named/anyisalin.com.zone
$TTL 600
$ORIGIN anyisalin.com.
@ IN SOA ns.anyisalin.com amdin.anyisalin.com (
20160328
1D
5M
7D
1D
)
IN NS ns
ns IN A 192.168.2.2
www IN A 192.168.2.3
www IN A 192.168.2.4
storage IN A 192.168.2.5
測試效果以下,以達到DNS實現輪詢數據庫
[root@localhost ~]# mkdir /mydata
[root@localhost ~]# mount -t nfs 192.168.2.5:/var/mydata /mydata
[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug kernels mariadb-5.5.32-linux-x86_64.tar.gz
[root@localhost src]# tar xf mariadb-5.5.32-linux-x86_64.tar.gz
[root@localhost src]# cd mariadb-5.5.32-linux-x86_64
[root@localhost mariadb-5.5.32-linux-x86_64]# ls
bin COPYING COPYING.LESSER data docs include INSTALL-BINARY lib man mysql-test README scripts share sql-bench support-files
[root@localhost mariadb-5.5.32-linux-x86_64]# groupadd -r -g 3306 mysql
groupadd: group 'mysql' already exists
[root@localhost mariadb-5.5.32-linux-x86_64]# useradd -r -g mysql -s /sbin/nologin mysql
useradd: user 'mysql' already exists
[root@localhost mariadb-5.5.32-linux-x86_64]# ./scripts/mysql_install_db --datadir=/mydata --user=mysql #初始化數據庫
Installing MariaDB/MySQL system tables in '/mydata' ...
OK
Filling help tables...
OK
#內容省略
[root@localhost mariadb-5.5.32-linux-x86_64]# ls /mydata/ #查看生成的數據庫
aria_log.00000001 aria_log_control mysql performance_schema test
[root@localhost src]# ln -sv /usr/src/mariadb-5.5.32-linux-x86_64 /usr/local/mysql
`/usr/local/mysql' -> `/usr/src/mariadb-5.5.32-linux-x86_64'
[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf
[root@localhost mysql]# vim /etc/my.cnf #添加如下三行
datadir = /mydata
skip_name_resolve = on
innodb_file_per_table = on
[root@localhost mysql]# export PATH=/usr/local/mysql/bin/:$PATH
[root@localhost mysql]# service mysqld start #測試MySQL是否能正常啓動
Starting MySQL... SUCCESS!
[root@localhost mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.32-MariaDB-log MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> GRANT ALL ON wp.* TO 'wpuser'@'%' IDENTIFIED BY 'passwd';
Query OK, 0 rows affected (0.04 sec)
mysql> CREATE DATABASE wp;
Query OK, 1 row affected (0.02 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
[root@localhost ~]# yum install httpd php php-mysql -y | tail -n 10
apr-util.x86_64 0:1.3.9-3.el6_0.1
apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1
httpd-tools.x86_64 0:2.2.15-45.el6.centos
libedit.x86_64 0:2.11-4.20080712cvs.1.el6
mailcap.noarch 0:2.1.31-2.el6
php-cli.x86_64 0:5.3.3-40.el6_6
php-common.x86_64 0:5.3.3-40.el6_6
php-pdo.x86_64 0:5.3.3-40.el6_6
Complete!
[root@localhost ~]# vi /etc/httpd/conf.d/virt.conf #編輯配置文件
NameVirtualHost 192.168.2.3:80
NameVirtualHost 192.168.2.3:80
<VirtualHost 192.168.2.3:80>
ServerName www.anyisalin.com
DocumentRoot /webroot/wordpress
</VirtualHost>
[root@localhost ~]# service rpcbind start
Starting rpcbind: [ OK ]
[root@localhost ~]# mount ^C
[root@localhost ~]# mkdir /webroot
[root@localhost ~]# mount -t nfs 192.168.2.5:/var/webroot /webroot/
[root@localhost ~]# ls /webroot/
wordpress wordpress-4.4.1-zh_CN.zip
[root@localhost ~]# service httpd start
Starting httpd:
httpd: apr_sockaddr_info_get() failed for www.anyisalin.com
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[Wed Mar 23 18:05:15 2016] [warn] NameVirtualHost 192.168.2.3:80 has no VirtualHosts
[ OK ]
安裝前咱們須要先將dns服務器中對192.168.2.4的解析註釋,避免安裝時出現解析問題vim
安裝過程不作演示
centos
如下操做在www.anyisalin.com(192.168.2.4)中執行
咱們要將dns服務器中的A記錄恢復到初始輪詢的狀態
bash
[root@localhost ~]# yum install httpd php php-mysql nfs-utils -y | tail -n 10
Installed:
httpd.x86_64 0:2.2.15-45.el6.centos nfs-utils.x86_64 1:1.2.3-64.el6 php.x86_64 0:5.3.3-40.el6_6 php-mysql.x86_64 0:5.3.3-40.el6_6
Dependency Installed:
apr.x86_64 0:1.3.9-5.el6_2 apr-util.x86_64 0:1.3.9-3.el6_0.1 apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 httpd-tools.x86_64 0:2.2.15-45.el6.centos
keyutils.x86_64 0:1.4-5.el6 libedit.x86_64 0:2.11-4.20080712cvs.1.el6 libevent.x86_64 0:1.4.13-4.el6 libgssglue.x86_64 0:0.1-11.el6
libtirpc.x86_64 0:0.2.1-10.el6 mailcap.noarch 0:2.1.31-2.el6 nfs-utils-lib.x86_64 0:1.1.5-11.el6 php-cli.x86_64 0:5.3.3-40.el6_6
php-common.x86_64 0:5.3.3-40.el6_6 php-pdo.x86_64 0:5.3.3-40.el6_6 python-argparse.noarch 0:1.2.1-2.1.el6 rpcbind.x86_64 0:0.2.0-11.el6
Complete!
[root@localhost ~]# vim /etc/httpd/conf.d/virt.conf
NameVirtualHost 192.168.2.4:80
<VirtualHost 192.168.2.4:80>
ServerName www.anyisalin.com
DocumentRoot /webroot/wordpress
</VirtualHost>
[root@localhost ~]# service rpcbind start
Starting rpcbind: [ OK ]
[root@localhost ~]# mkdir /webroot
[root@localhost ~]# mount -t nfs 192.168.2.5:/var/webroot /webroot/
[root@localhost ~]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using www.anyisalin.com for ServerName
[ OK ]
[root@localhost ~]#
因爲在Windows咱們很差進行模擬, 遂修改
HOSTS
文件達到測試效果
如今訪問web1主機
發佈一篇文章測試
如今訪問web2主機
還可以看到剛纔發的文章
再次訪問web1主機
雖然頁面同樣, 可是主機已是web2了
雖然本文最後測試時只修改了hosts文件進行測試,可是若是指定DNS地址爲192.168.2.2的話是可以完成負載均衡的效果的,可是NFS的網絡傳輸文件的效率並很差,後期會學習LVS和Nginx, HAproxy等專用負載均衡軟件再給你們寫一篇。 做者: AnyISalIn 感謝: MagEdu