Nginx代理MogileFS并实现负载均衡和高可用
MogileFS
nginx
负载均衡
大纲
- 实验环境
- 实验步骤
- 配置MogileFS
- 配置Nginx
- 总结
前言
上篇文章我们了解分布式系统和MogileFS的基本使用, 但是那样的架构是有问题的, 本篇文章我们来了解一下如何使用
nginx-mogilefs-module-master
模块来构建一个不一样的 MogileFS Cluster
实验拓扑
实验环境
主机 | IP | 功用 |
---|---|---|
node6 | 172.16.1.7 | Nginx,Tracker, Storage,DataBase |
node7 | 172.16.1.8 | Tracker, Storage |
node8 | 172.16.1.9 | Tracker, Storage |
实验步骤
配置MogileFS
安装过程这里就不叙述了, 有疑问看我上一篇文章分布式系统介绍及MogileFS安装、基本配置
配置数据库:
[[email protected]~] service mysqld start
mysql> GRANT ALL ON *.* TO [email protected]‘%‘ IDENTIFIED BY ‘passwd‘ ; #配置一个可以远程连接的root用户Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL ON mogilefs.* TO [email protected]‘%‘ IDENTIFIED BY ‘passwd‘; #配置一个可管理mogilefs数据库的用户Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)
mysql> CREATE DATABASE mogilefs; #创建mogilefs数据库Query OK, 1 row affected (0.00 sec)
配置mogilefsd
[[email protected]~] vim /etc/mogilefs/mogilefsd.conf #node7, node8也要做操db_dsn = DBI:mysql:mogilefs:host=172.16.1.7db_user = mogileuserdb_pass = passwdlisten = 0.0.0.0:7001conf_port = 7001
[[email protected]~] service mogilefsd startStarting mogilefsd [ OK ]
[[email protected]~] mogadm host add node1 --ip=172.16.1.7 alive #只用在node6操作[[email protected]~] mogadm host add node2 --ip=172.16.1.8 alive #只用在node6操作[[email protected]~] mogadm host add node3 --ip=172.16.1.9 alive #只用在node6操作[[email protected]~] mogadm host listnode1 [1]: alive IP: 172.16.1.7:7500
node2 [2]: alive IP: 172.16.1.8:7500
node3 [3]: alive IP: 172.16.1.9:7500
[[email protected]~] scp /etc/mogilefs/mogilefsd.conf 172.16.1.8:/etc/mogilefs/ #复制配置文件给node7[[email protected]~] scp /etc/mogilefs/mogilefsd.conf 172.16.1.9:/etc/mogilefs/ #复制配置文件给node8
[[email protected]~] service mogilefsd start #node7启动mogfilefsdStarting mogilefsd [ OK ]
[[email protected]~] service mogilefsd start #node7启动mogfilefsdStarting mogilefsd [ OK ]
配置mogstored
[[email protected]~] mkdir /data/mogilefs/dev1 -pv mkdir: created directory `/data‘ mkdir: created directory `/data/mogilefs‘ mkdir: created directory `/data/mogilefs/dev1‘[[email protected]~] vim /etc/mogilefs/mogstored.conf maxconns = 10000 httplisten = 0.0.0.0:7500 mgmtlisten = 0.0.0.0:7501 docroot = /data/mogilefs/
[[email protected] ~]# chown mogilefs.mogilefs /data/mogilefs/ -R[[email protected]~] service mogstored startStarting mogstored [ OK ]
##node7, node8都要做上述操作
[[email protected] ~]# mogadm device add node1 1 [[email protected] ~]# mogadm device add node2 2[[email protected] ~]# mogadm device add node3 3
#查看设备信息[[email protected]~] mogadm device listnode1 [1]: alive used(G) free(G) total(G) weight(%) dev1: alive 2.069 72.365 74.435 100
node2 [2]: alive used(G) free(G) total(G) weight(%) dev2: alive 1.958 72.477 74.435 100
node3 [3]: alive used(G) free(G) total(G) weight(%) dev3: alive 1.954 72.480 74.435 100
创建域
[root@node6 ~]# mogadm domain add files[root@node6 ~]# mogadm domain add images
[root@node6~] mogadm domain list domain class mindevcount replpolicy hashtype-------------------- -------------------- ------------- ------------ ------- files default 2 MultipleHosts() NONE images default 2 MultipleHosts() NONE
上传文件并测试
[root@node6~] mogupload --trackers=172.16.1.7 --domain=images --key=‘pal5q.jpg‘ --file=1.jpg [root@node6~] mogfileinfo --trackers=172.16.1.7 --domain=images --key=‘pal5q.jpg‘- file: pal5q.jpg class: default devcount: 2 domain: images fid: 5 key: pal5q.jpg length: 103139 - http://172.16.1.8:7500/dev2/0/000/000/0000000005.fid - http://172.16.1.7:7500/dev1/0/000/000/0000000005.fid
配置Nginx
编译安装Nginx
#Nginx和模块自行下载[root@node6 ~] yum groupinstall "Development Tools" "Server Platform Developments" #安装开发包组[root@node6/usr/local/nginx-1.9.15] yum install pcre-devel openssl-devel -y #安装需要的软件包[root@node6~] tar xf nginx-1.9.15.tar.gz -C /usr/local/[root@node6~] tar xf nginx_mogilefs_module-1.0.4.tar.gz -C /usr/local/[root@node6~] cd /usr/local/nginx-1.9.15/
[root@node6/usr/local/nginx-1.9.15] ./configure \> --prefix=/usr \> --sbin-path=/usr/sbin/nginx \> --conf-path=/etc/nginx/nginx.conf \> --error-log-path=/var/log/nginx/error.log \> --http-log-path=/var/log/nginx/access.log \> --pid-path=/var/run/nginx/nginx.pid \> --lock-path=/var/lock/nginx.lock \> --user=nginx \> --group=nginx \> --with-http_ssl_module \> --with-http_flv_module \> --with-http_stub_status_module \> --with-http_gzip_static_module \> --with-pcre \> --with-debug \> --add-module=/usr/local/nginx_mogilefs_module-1.0.4/
[root@node6/usr/local/nginx-1.9.15] make && make install
配置Nginx
[[email protected]~] vim /etc/nginx/nginx.conf
user nobody;worker_processes 1;events { worker_connections 1024;}
http { include mime.types; default_type application/octet-stream;
sendfile on;
keepalive_timeout 65; upstream trackers { server 172.16.1.7:7001; server 172.16.1.8:7001; server 172.16.1.9:7001; }
server { listen 80; server_name localhost;
location / { root html; index index.html index.htm; }
location /images/ { mogilefs_tracker trackers; mogilefs_domain images; mogilefs_methods GET; mogilefs_noverify on;
mogilefs_pass { proxy_pass $mogilefs_path; proxy_hide_header Content-Type; proxy_buffering off; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
}
}
启动并测试
[root@node6~] /usr/sbin/nginx #启动nginx
我们直接通过key值就能够访问到图片, 停止后端任意主机都可以继续访问
总结
通过这个实验, 我们可以将通过MogileFS实现的分布式存储中的文件通过键值进行获取, 也就是说, 我们也可以在站点中直接引用, 如果是Tengine的话, 还能够通过Check_status看到更为详细的信息
作者水平很低, 如果有错误及时指出, 如果你觉得本文写的好请点一波赞~(≧▽≦)/~
作者: AnyISaIln QQ: 1449472454
感谢: MageEdu
时间: 2024-10-12 07:07:18