设为首页 收藏本站
查看: 616|回复: 0

[经验分享] NFS共享MySQL使用DNS轮询实现Web负载均衡

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-3-30 14:48:06 | 显示全部楼层 |阅读模式
NFS共享MySQL使用DNS轮询实现Web负载均衡
前言:
[size=1em]今天学习了NFS,遂结合前面学习的LAMP+Bind做一个实验,实现两台Web服务器采用同一个MySQL数据库和相同的网页文件,对LAMP和Bind不了解的可以去查看我以前写的博客: AnyISalIn的文章
实验拓扑图
1459216390141843.png 1459212167926148.gif
实验环境[td]
主机名
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
实验步骤
搭建NFS以下操作在storage.anyisalin.com中执行[iyunv@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
创建文件夹并导出
[iyunv@storage ~]# mkdir /var/mydata
[iyunv@storage ~]# mkdir /var/webroot
[iyunv@storage var]# cd /var/webroot/
[iyunv@storage webroot]# unzip wordpress-4.4.1-zh_CN.zip &> /dev/null
[iyunv@storage webroot]# ls
wordpress  wordpress-4.4.1-zh_CN.zip
[iyunv@storage webroot]# chmod 777 wordpress -R
[iyunv@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)
[iyunv@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  ]
[iyunv@storage ~]# showmount -e localhost   #查看当前导出的目录
Export list for localhost:
/var/webroot 192.168.2.4,192.168.2.3
/var/mydata  192.168.2.2


搭建dns,MySQL环境
[size=1em]以下操作在ns.anyisalin.com中执行
DNS配置[iyunv@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!
[iyunv@localhost ~]# 在/etc/named.rfc1912.zones文件中添加以下几行
zone "anyisalin.com" IN {
        type master;
        file "anyisalin.com.zone";
};
配置区域解析库文件为如下
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实现轮询 1459212181877370.png
MySQL配置这里MySQL数据库文件通过NFS保存在远程主机[iyunv@localhost ~]# mkdir /mydata
[iyunv@localhost ~]# mount -t nfs 192.168.2.5:/var/mydata /mydata[iyunv@localhost ~]# cd /usr/src/
[iyunv@localhost src]# ls
debug  kernels  mariadb-5.5.32-linux-x86_64.tar.gz
[iyunv@localhost src]# tar xf mariadb-5.5.32-linux-x86_64.tar.gz
[iyunv@localhost src]# cd mariadb-5.5.32-linux-x86_64
[iyunv@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
[iyunv@localhost mariadb-5.5.32-linux-x86_64]# groupadd -r -g 3306 mysql
groupadd: group 'mysql' already exists
[iyunv@localhost mariadb-5.5.32-linux-x86_64]# useradd -r -g mysql -s /sbin/nologin mysql
useradd: user 'mysql' already exists
[iyunv@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
#内容省略

[iyunv@localhost mariadb-5.5.32-linux-x86_64]# ls /mydata/  #查看生成的数据库
aria_log.00000001  aria_log_control  mysql  performance_schema  test

[iyunv@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'
[iyunv@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld  
[iyunv@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf
[iyunv@localhost mysql]# vim /etc/my.cnf  #添加以下三行
datadir = /mydata
skip_name_resolve = on
innodb_file_per_table = on
[iyunv@localhost mysql]# export PATH=/usr/local/mysql/bin/:$PATH

[iyunv@localhost mysql]# service mysqld start    #测试MySQL是否能正常启动
Starting MySQL... SUCCESS!
[iyunv@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)


WEB配置第一台主机安装以下操作在www.anyisalin.com(192.168.2.3)中执行[iyunv@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!
[iyunv@localhost ~]# vi /etc/httpd/conf.d/virt.conf  #编辑配置文件

NameVirtualHost 192.168.2.3:80
NameVirtualHost 192.168.2.3:80

   ServerName www.anyisalin.com
   DocumentRoot /webroot/wordpress


[iyunv@localhost ~]# service rpcbind start
Starting rpcbind:                                          [  OK  ]
[iyunv@localhost ~]# mount ^C
[iyunv@localhost ~]# mkdir /webroot
[iyunv@localhost ~]# mount -t nfs 192.168.2.5:/var/webroot /webroot/
[iyunv@localhost ~]# ls /webroot/
wordpress  wordpress-4.4.1-zh_CN.zip
[iyunv@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  ]
[size=1em]安装前我们需要先将dns服务器中对192.168.2.4的解析注释,避免安装时出现解析问题
安装过程不做演示
1459212196700150.png
1459212220108728.png
第二台主机安装
[size=1em]以下操作在www.anyisalin.com(192.168.2.4)中执行
我们要将dns服务器中的A记录恢复到初始轮询的状态

[iyunv@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![iyunv@localhost ~]# vim /etc/httpd/conf.d/virt.conf

NameVirtualHost 192.168.2.4:80

   ServerName www.anyisalin.com
   DocumentRoot /webroot/wordpress

[iyunv@localhost ~]# service rpcbind start
Starting rpcbind:                                          [  OK  ]
[iyunv@localhost ~]# mkdir /webroot
[iyunv@localhost ~]# mount -t nfs 192.168.2.5:/var/webroot /webroot/
[iyunv@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  ]
[iyunv@localhost ~]# 确保DNS服务能提供以下效果 1459212383143904.png 测试
[size=1em]由于在Windows我们不好进行模拟, 遂修改HOSTS文件达到测试效果
web1测试
现在访问web1主机
1459212284934356.png
1459212291250612.png

发布一篇文章测试
1459212303832664.png
web2测试
现在访问web2主机
1459212310133380.png
还能够看到刚才发的文章
1459212323174125.png
再发布一篇文章
1459212328160318.png
1459212333430599.png

回到web1测试
再次访问web1主机
1459212351505273.png
虽然页面一样, 但是主机已经是web2了

总结

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-197710-1-1.html 上篇帖子: MySQL参数优化及基础操作 下篇帖子: MySQL5.5的安装与配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表