首页 > 代码库 > NFS共享关系型数据库利用DNS轮询提供Web负载均衡
NFS共享关系型数据库利用DNS轮询提供Web负载均衡
前言:
用NFS、LAMP、BIND结合的方法,应对网络中访问量增大而导致服务器负载运行的情况。以实现Web服务器之间使用同一个MYSQL和相同的网页配置文件。
实验环境:
HostName | IP | 规划 |
nfs.preferred.com | 192.168.1.6 | NFS |
ns.preferred.com | 192.168.1.5 | DNS |
mysql.preferred.com | 192.168.1.4 | MYSQL |
www.preferred.com | 192.168.1.3 | WEB |
www.preferred.com | 192.168.1.2 | WEB |
实验拓扑:
实验步骤:
一、搭建NFS
以下操作在nfs.preferred.com服务器上实现
[root@localhost ~]# yum install nfs-utils -y <---这里需要安装nfs这个包(基本的NFS命令与监控程序) [root@localhost ~]# rpm -qa | grep nfs-utils --color nfs-utils-1.2.3-39.el6.x86_64
创建共享目录并配置nfs文件
[root@localhost~]# mkdir /mydata [root@localhost~]# mkdir /webdata [root@localhost~]# cd /webdata/ [root@localhost webdata]# unzip wordpress-3.2.1-zh_CN.zip &> /dev/null [root@localhost webdata]# chmod -R 777 wordpress [root@localhost webdata]# ls -l drwxrwxrwx 5 root root 4096 Oct 11 2016 wordpress -rw-r--r-- 1 root root 4372680 Oct 11 2016 wordpress-3.2.1-zh_CN.zip [root@localhost webdata]# vim /etc/exports /mydata 192.168.1.4(rw,no_root_squash) /webdata 192.168.1.2(rw,no_root_squash) 192.168.1.3(rw,no_root_squash) [root@localhost ~]# service rpcbind start 注:rpcbind是一个RPC服务,主要是在nfs共享的时候负责通知客户端、服务器nfs端口号。 [root@localhost ~]# service nfs start Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ] [root@localhost ~]# showmount -e 192.168.1.6 <---查看当前共享的目录 Export list for 192.168.1.6: /webdata 192.168.1.3,192.168.1.2 /mydata 192.168.1.4
二、搭建DNS
以下操作在ns.preferred.com服务器上实现
[root@localhost ~]# yum install bind -y | tail -n 5 Installed: bind.x86_64 32:9.8.2-0.47.rc1.el6_8.1 Complete! [root@localhost ~]# vim /etc/named.rfc1912.zones <---添加zone(正向与反向的) zone "preferred.com" IN { type master; file "preferred.com.zone"; }; zone "1.168.192.in-addr.arpa" IN { type master; file "192.168.1.5.zone"; }; [root@localhost ~]# vim /var/named/preferred.com.zone <---配置DNS正向区域解析 $TTL 86400 $ORIGIN preferred.com. @ IN SOA ns.preferred.com admin.preferred.com ( 2016101001 1D 5M 7D 1D ) IN NS ns ns IN A 192.168.1.5 www IN A 192.168.1.2 www IN A 192.168.1.3 nfs IN A 192.168.1.6 [root@localhost ~]# vim /var/named/192.168.1.5.zone <---配置DNS反向区域解析 $TTL 86400 @ IN SOA ns.preferred.com. admin.preferred.com ( 2016101001 1H 5M 7D 1D ) IN NS ns.preferred.com. 5 IN PTR ns.preferred.com. 2 IN PTR www.preferred.com. 3 IN PTR www.preferred.com. 6 IN PTR nfs.preferred.com. [root@localhost ~]# systemctl restart named [root@localhost ~]# dig -t A www.preferred.com @192.168.1.5 <---测试DNS能否正向解析 ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.4 <<>> -t A www.preferred.com @192.168.1.5 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23764 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;www.preferred.com. IN A ;; ANSWER SECTION: www.preferred.com. 86400 IN A 192.168.1.2 www.preferred.com. 86400 IN A 192.168.1.3 ;; AUTHORITY SECTION: preferred.com. 86400 IN NS ns.preferred.com. ;; ADDITIONAL SECTION: ns.preferred.com. 86400 IN A 192.168.1.5 ;; Query time: 0 msec ;; SERVER: 192.168.1.5#53(192.168.1.5) ;; WHEN: Tue Oct 11 04:22:36 EDT 2016 ;; MSG SIZE rcvd: 111 [root@localhost ~]# dig -x 192.168.1.2 @192.168.1.5 <---测试DNS能否反向解析 ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.4 <<>> -x 192.168.1.2 @192.168.1.5 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21950 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;2.1.168.192.in-addr.arpa. IN PTR ;; ANSWER SECTION: 2.1.168.192.in-addr.arpa. 86400 IN PTR www.preferred.com. ;; AUTHORITY SECTION: 1.168.192.in-addr.arpa. 86400 IN NS ns.preferred.com. ;; ADDITIONAL SECTION: ns.preferred.com. 86400 IN A 192.168.1.5 ;; Query time: 0 msec ;; SERVER: 192.168.1.5#53(192.168.1.5) ;; WHEN: Tue Oct 11 04:23:57 EDT 2016 ;; MSG SIZE rcvd: 117
三、搭建MYSQL
以下操作在mysql.preferred.com服务器上实现
[root@localhost ~]# mkdir /mydata/ [root@localhost ~]# mount -t nfs 192.168.1.6:/mydata/ /mydata/ <---挂载共享目录 [root@localhost ~]# groupadd -r mysql <---创建mysql组和用户 [root@localhost ~]# useradd -r -g mysql -s /sbin/nologin mysql [root@localhost ~]# cd /usr/local/ [root@localhost local]# ln -sv /root/mariadb-5.5.43-linux-x86_64 mysql "mysql" -> "/root/mariadb-5.5.43-linux-x86_64" [root@localhost local]# cd mysql/ [root@localhost mysql]# ls bin COPYING.LESSER EXCEPTIONS-CLIENT INSTALL-BINARY man README share support-files COPYING data include lib mysql-test scripts sql-bench [root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/ <---初始化数据库 Installing MariaDB/MySQL system tables in ‘/mydata‘ ... 161010 13:33:59 [Note] ./bin/mysqld (mysqld 5.5.43-MariaDB) starting as process 2183 ... OK Filling help tables... 161010 13:33:59 [Note] ./bin/mysqld (mysqld 5.5.43-MariaDB) starting as process 2191 ... OK ... [root@localhost mysql]# ls /mydata/ <--查看初始后生成的数据 aria_log.00000001 aria_log_control mysql performance_schema test [root@localhost mysql]# mkdir /etc/mysql/ [root@localhost mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf [root@localhost mysql]# vim /etc/mysql/my.cnf <---修改配置文件,添加以下三行 datadir = /mydata innodb_file_per_table = on skip_name_resolve = on [root@localhost mysql]# service mysqld start Starting MySQL... [确定] [root@localhost mysql]# ss -tnl | grep 3306 <---服务端口已经开启 LISTEN 0 50 *:3306 *:* *:* [root@localhost mysql]# mysql -bash: mysql: command not found 注:尝试登陆MYSQL却报错,不要紧张。这是因为/usr/local/bin目录下确实mysql导致,只需要建立一个软连接即可解决。 [root@localhost ~]# ln -sv /usr/local/mysql/bin/mysql /usr/bin/ "/usr/bin/mysql" -> "/usr/local/mysql/bin/mysql" [root@localhost ~]# mysql <---连接数据库创建用户密码与授权 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 115 Server version: 5.5.43-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. MariaDB [(none)]> CREATE DATABASE pre; Query OK, 1 rows affected (0.00 sec) MariaDB [(none)]> GRANT ALL ON pre.* TO ‘jds‘@‘%‘ IDENTIFIED BY ‘redhat‘; <---创建用户并赋予权限 Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
四、搭建WEB
以下操作在www.preferred.com(192.168.1.2)服务器上实现
[root@localhost ~]# yum install httpd php-mysql php -y | tail -n 10 warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Importing GPG key 0xC105B9DE: Userid: CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org> Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5) From: /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 php-cli.x86_64 0:5.3.3-48.el6_8 php-common.x86_64 0:5.3.3-48.el6_8 php-pdo.x86_64 0:5.3.3-48.el6_8 Updated: httpd.x86_64 0:2.2.15-54.el6.centos Dependency Updated: httpd-tools.x86_64 0:2.2.15-54.el6.centos Complete! [root@localhost ~]# mkdir /webdata [root@localhost ~]# vim /etc/httpd/conf/httpd.conf <---添加以下几行 ServerName www.preferred.com:80 <VirtualHost 192.168.1.2:80> ServerName www.preferred.com DocumentRoot /webdata/wordpress </VirtualHost> [root@localhost ~]# service rpcbind start [root@localhost ~]# mount -t nfs 192.168.1.6:/webdata /webdata [root@localhost ~]# ls /webdata/ wordpress wordpress-3.2.1-zh_CN.zip [root@localhost ~]# service httpd start Starting httpd: [ OK ]
接下来安装wordpress,没有的话可以自己去下载一个https://wordpress.org/download/
[root@localhost ~]# cd /webdata/wordpress [root@localhost wordpress]# ls index.php wp-app.php wp-config.php wp-includes wp-pass.php wp-settings.php license.txt wp-atom.php wp-config-sample.php wp-links-opml.php wp-rdf.php wp-signup.php readme.html wp-blog-header.php wp-content wp-load.php wp-register.php wp-trackback.php wp-activate.php wp-comments-post.php wp-cron.php wp-login.php wp-rss2.php xmlrpc.php wp-admin wp-commentsrss2.php wp-feed.php wp-mail.php wp-rss.php [root@localhost wordpress]# cp wp-config-sample.php wp-config.php <---这是一个示例配置文件,更名并配置 [root@localhost wordpress]# vim wp-config.php // ** MySQL 设置 - 具体信息来自您正在使用的主机 ** // /** WordPress 数据库的名称 */ define(‘DB_NAME‘, ‘pre‘); <---连接数据库所使用的,与刚刚在mysq中创建的一致,以下几个都是。 /** MySQL 数据库用户名 */ define(‘DB_USER‘, ‘jds‘); /** MySQL 数据库密码 */ define(‘DB_PASSWORD‘, ‘redhat‘); /** MySQL 主机 */ define(‘DB_HOST‘, ‘192.168.1.4‘); /** 创建数据表时默认的文字编码 */ define(‘DB_CHARSET‘, ‘utf8‘); /** 数据库整理类型。如不确定请勿更改 */ define(‘DB_COLLATE‘, ‘‘); [root@localhost ~]# mysql -ujds -predhat -h 192.168.1.4 <---尝试登陆 ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘192.168.1.4‘ (113) 注:遇到这个问题怎么个人都不好了,在MYSQL服务器上取消防火墙也不行。所以尝试在MYSQL服务器的防火墙上加上端口的允许。 [root@localhost ~]# iptables -L -n | grep 3306 [root@localhost ~]# iptables -I INPUT -s 0/0 -p tcp --dport 3306 -j ACCEPT [root@localhost mysql]# iptables -L -n | grep 3306 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 [root@localhost ~]# mysql -ujds -predhat -h 192.168.1.4 <---尝试再次登陆 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 116 Server version: 5.5.43-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | pre | <---之前创建的数据库可以访问 | test | +--------------------+ 5 rows in set (0.00 sec)
测试访问正常!!!
以下操作在www.preferred.com(192.168.1.3)服务器上实现
[root@localhost ~]# yum install httpd php-mysql php -y [root@localhost ~]# mkdir /webdata [root@localhost ~]# vim /etc/httpd/conf/httpd.conf <---添加以下几行 ServerName www.preferred.com:80 <VirtualHost 192.168.1.3:80> ServerName www.preferred.com DocumentRoot /webdata/wordpress </VirtualHost> [root@localhost ~]# service rpcbind start [root@localhost ~]# mount -t nfs 192.168.1.6:/webdata /webdata [root@localhost ~]# ls /webdata/ wordpress wordpress-3.2.1-zh_CN.zip [root@localhost ~]# service httpd start Starting httpd: [ OK ]
先用WEB(192.168.1.3)进行测试。发布一条文章
用WEB(192.168.1.2)进行测试。现在访问看看
看到了在WEB(192.168.1.3)上发布的文章,我们在WEB(192.168.1.2)上也发布一篇
最后我们在WEB(192.168.1.3)上查看
至此,我们实验的目的已经达到。
最后:
这是本人结合学习所写出来的一篇博客。勉强供小白学习借鉴,不喜勿喷。如果大家有什么疑问可以给我留言,有大神发现哪有错误也欢迎指出!
本文出自 “Preferred” 博客,请务必保留此出处http://preferreds.blog.51cto.com/11870667/1860719
NFS共享关系型数据库利用DNS轮询提供Web负载均衡
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。