NFS共享关系型数据库利用DNS轮询提供Web负载均衡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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服务器上实现
[[email protected] ~]# yum install nfs-utils -y <---这里需要安装nfs这个包(基本的NFS命令与监控程序) [[email protected] ~]# rpm -qa | grep nfs-utils --color nfs-utils-1.2.3-39.el6.x86_64
创建共享目录并配置nfs文件
[[email protected]~]# mkdir /mydata [[email protected]~]# mkdir /webdata [[email protected]~]# cd /webdata/ [[email protected] webdata]# unzip wordpress-3.2.1-zh_CN.zip &> /dev/null [[email protected] webdata]# chmod -R 777 wordpress [[email protected] 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 [[email protected] 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) [[email protected] ~]# service rpcbind start 注:rpcbind是一个RPC服务,主要是在nfs共享的时候负责通知客户端、服务器nfs端口号。 [[email protected] ~]# service nfs start Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ] [[email protected] ~]# 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服务器上实现
[[email protected] ~]# yum install bind -y | tail -n 5 Installed: bind.x86_64 32:9.8.2-0.47.rc1.el6_8.1 Complete! [[email protected] ~]# 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"; }; [[email protected] ~]# 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 [[email protected] ~]# 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. [[email protected] ~]# systemctl restart named [[email protected] ~]# 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 [[email protected] ~]# 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服务器上实现
[[email protected] ~]# mkdir /mydata/ [[email protected] ~]# mount -t nfs 192.168.1.6:/mydata/ /mydata/ <---挂载共享目录 [[email protected] ~]# groupadd -r mysql <---创建mysql组和用户 [[email protected] ~]# useradd -r -g mysql -s /sbin/nologin mysql [[email protected] ~]# cd /usr/local/ [[email protected] local]# ln -sv /root/mariadb-5.5.43-linux-x86_64 mysql "mysql" -> "/root/mariadb-5.5.43-linux-x86_64" [[email protected] local]# cd mysql/ [[email protected] mysql]# ls bin COPYING.LESSER EXCEPTIONS-CLIENT INSTALL-BINARY man README share support-files COPYING data include lib mysql-test scripts sql-bench [[email protected] 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 ... [[email protected] mysql]# ls /mydata/ <--查看初始后生成的数据 aria_log.00000001 aria_log_control mysql performance_schema test [[email protected] mysql]# mkdir /etc/mysql/ [[email protected] mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf [[email protected] mysql]# vim /etc/mysql/my.cnf <---修改配置文件,添加以下三行 datadir = /mydata innodb_file_per_table = on skip_name_resolve = on [[email protected] mysql]# service mysqld start Starting MySQL... [确定] [[email protected] mysql]# ss -tnl | grep 3306 <---服务端口已经开启 LISTEN 0 50 *:3306 *:* *:* [[email protected] mysql]# mysql -bash: mysql: command not found 注:尝试登陆MYSQL却报错,不要紧张。这是因为/usr/local/bin目录下确实mysql导致,只需要建立一个软连接即可解决。 [[email protected] ~]# ln -sv /usr/local/mysql/bin/mysql /usr/bin/ "/usr/bin/mysql" -> "/usr/local/mysql/bin/mysql" [[email protected] ~]# 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)服务器上实现
[[email protected] ~]# 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) <[email protected]> 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! [[email protected] ~]# mkdir /webdata [[email protected] ~]# 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> [[email protected] ~]# service rpcbind start [[email protected] ~]# mount -t nfs 192.168.1.6:/webdata /webdata [[email protected] ~]# ls /webdata/ wordpress wordpress-3.2.1-zh_CN.zip [[email protected] ~]# service httpd start Starting httpd: [ OK ]
接下来安装wordpress,没有的话可以自己去下载一个https://wordpress.org/download/
[[email protected] ~]# cd /webdata/wordpress [[email protected] 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 [[email protected] wordpress]# cp wp-config-sample.php wp-config.php <---这是一个示例配置文件,更名并配置 [[email protected] 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‘, ‘‘); [[email protected] ~]# 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服务器的防火墙上加上端口的允许。 [[email protected] ~]# iptables -L -n | grep 3306 [[email protected] ~]# iptables -I INPUT -s 0/0 -p tcp --dport 3306 -j ACCEPT [[email protected] mysql]# iptables -L -n | grep 3306 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 [[email protected] ~]# 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)服务器上实现
[[email protected] ~]# yum install httpd php-mysql php -y [[email protected] ~]# mkdir /webdata [[email protected] ~]# 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> [[email protected] ~]# service rpcbind start [[email protected] ~]# mount -t nfs 192.168.1.6:/webdata /webdata [[email protected] ~]# ls /webdata/ wordpress wordpress-3.2.1-zh_CN.zip [[email protected] ~]# 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负载均衡的主要内容,如果未能解决你的问题,请参考以下文章
web服务器(LAMP)通过DNS轮询功能和nfs共享实现负载均衡