1, 搭建 mogilefs
MogileFS 是一个开源的分布式文件存储系统, 由 LiveJournal 旗下的 Danga Interactive 公司开发. Danga 团队开发了包括 Memcached,MogileFS,Perlbal 等多个知名的开源项目. 目前使用 MogileFS 的公司非常多, 如日本排名先前的几个互联公司及国内的 yupoo(又拍),digg, 豆瓣, 1 号店, 大众点评, 搜狗和安居客等, 分别为所在的组织或公司管理着海量的图片.
MogileFS 由 3 个部分组成:
(1) server: 主要包括 mogilefsd 和 mogstored 两个应用程序. mogilefsd 实现的是 tracker, 它通过数据库来保存元数据信息, 包括站点 domain,class,host 等; mogstored 是存储节点(store node), 它其实是个 WebDAV 服务, 默认监听在 7500 端口, 接受客户端的文件存储请求. 在 MogileFS 安装完后, 要运行 mogadm 工具将所有的 store node 注册到 mogilefsd 的数据库里, mogilefsd 会对这些节点进行管理和监控.
(2) utils(工具集): 主要是 MogileFS 的一些管理工具, 例如 mogadm 等.
(3) 客户端 API:MogileFS 的客户端 API 很多, 例如 Perl,PHP,Java,Python 等, 用这个模块可以编写客户端程序, 实现文件的备份管理功能等.
存储主机(节点)
这个是 MogileFS 存储文件存放在这些机器上, 也是 mogstored 节点, 也叫 Storage Server, 一台存储主要都要启动一个 mogstored 服务. 扩容就是增加这些机器.
设备(device)
一个存储节点, 以就是上面的主机, 可以有多个 device, 就是用来存放文件的目录(例如挂载的目录), 每个设备都有一个设备 id, 需要在 mogstored 的配置文件中的 docroot 配置的项目 指定的目录下面创建相应的设备的目录, 目录名为 $docroot/dev$id, 设备是不能删除的. 只能将其设备的状态的值置为 dead, 当一个设备 dead 之后, 就真的 dead 了, 里面的数据也无法恢复了, 且这个 dead 了的设备的 id 也不能再用.
分布式存储或分布式文件系统:
集中式:
共享存储
NAS
SAN
分布式存储或分布式文件系统一般遵循两个法则:
1. 文件在存储级别只是表现为数据流, 而不提供文件系统本身的功能
2. 提供文件系统功能
但是至少每个数据流要有一个唯一标识(按名存取), 元数据必不可少
分布式两种存储方式:
1. 分布式一般专门有一个服务器存元数据(元数据节点), 其他节点存储数据
元数据节点会分配好数据到各个服务器上 再对数据进行冗余 --datablock
2. 无专门提供的元数据节点时, 那每一个节点都存储了整个集群的完整的元数据, 和一部分数据(如果不是一部分就不叫分布式存储了) -- 好处在于没有单点
分布式存储的挑战:
节点间的通信
数据存储
数据空间平衡
容错
文件系统支持
分布式:
文件系统: 有文件系统接口
存储: 无文件系统接口, 通过 API 访问
分布式文件系统的常见实现:
GFS:google file system 海量数据高效存储
HDFS: Hadoop Distribute filesystem
元数据都放在元数据节点上, 但是这些元数据都是存储在内存中, 所以存储文件数量有限, 而且整个系统的单点所在, 对于存储海量小数据不是很理想, 但是试用于一些文件数不多的大文件
淘宝开源的存储文件系统
TFS:Taobao file system
开源将元数据存储于关系型数据库或其他高性能存储中, 从而能维护海量的文件元数据,
但是带来的劣势:
1. 依靠外部的组件来实现文件系统, 依赖的组件变多, 组件不在完整
2. 放在外部存储中, 没有放在内存中性能高
GlasterFS:
去中心化设计(没有专门的元数据节点), 即无专门提供的元数据节点, 每一个节点都存储了整个集群的完整的元数据, 和一部分数据
ceph:
整合进 Linux 内核当中的分布式文件系统, 已经被收录进内核
MooseFS:mfs
MogileFS: 经过测表明 MogileFS 的性能比 MooseF 高
MogileFS 架构: 适合存储海量小文件
1.Tracker: 元数据节点, 负责从 Database 中检索元数据或从 Database 中存储元数据
2.Database: 存文件的元数据(关系型数据库) 用户的访问权限也在其中存取
3.Storage Node
每一个客户端在实现数据存取时
1. 找 Tracker 获得元数据, Tracker 会告诉你数据在哪, 或者在哪个 Storage Node 上存储
2.client 再去相应的 Storage Node 上进行存取
橙色表示: 元数据存取线路, Database 和 Storage 交互的原因是因为 数据的访问权限也定义在 Database 中
绿色: client 请求路线
蓝色: Storage Node 中的数据冗余
特性:
mogilefs 的高可用不包括数据库 所以数据库的高可用要自己做
1. 基于 http 存储 所以非常适用于 Web 站点中
2. 数据存储冗余
3. 自动文件复制
4. 简单名称空间(可取名的范围就叫命名空间, MogileFS 没有目录的概念)
5. 不共享任何东西 shared-nothing
6. 自动提供冗余, 不需要 raid 设备
7. 不能追加写, 随机写, 只能整个文件替换
8.Tracker client 传输, 管理数据复制, 删除, 查询, 修复及其监控
9. 数据通过 HTTP/WEBDAV 服务上传到 Storage node(数据存储时的服务为 Mogstored)
10.MySQL 存储 mogilefs 的元数据
客户端请求:
1.client --> 请求反代服务器 (nginx(扮演 mogilefs client))--->tracker--->database(返回元数据)---> 反代服务器 (nginx)--->storage node---> 反代服务器(nginx)--->client
mogilefs high availability
Tracker 高可用:
每个节点既是 tracker 又是 mogstored
前面用 nginx 做反代, 因为是静态内容, 所以不需要 cookie 或者 session, 无状态的即可
要注意的是, nginx 要加入插件才可以进行 storage 的查询(因为在 nginx 访问数据的时候用的就不是 http 机制了, 此时 nginx 扮演 mogilefs 的客户端)
mogilefs 核心概念:
Domain: 描述一个名称空间 一个 mogilefs 可以有多个 domain(相当于目录)
用来存放不同的文件(大小, 类型)
同一个 Domain 内, key 必须唯一
不同 Domain 内, key 可以相同
class (将多个小文件合并成一个 class(单独被冗余的复制单元)最小复制单元)
一个 Domain 内可以有多个 class
文件属性管理
定义文件存储在不同设备商的份数
Domian+fid: 定位文件
mogilefs install:
mogilefs-server: 公共存储
mogilefs-utils 配置工具
- perl-perlbal
- perl-Danga-Socket
早期的 mogilefs 在 centos6 上能运行, 即使在 centos7 上也能运行但是可能会报错
所以:
安装之后编辑 / etc/rc.d/init.d/mogilefs
在其中加上 pidfile=/var/run/mogilefsd/mogstored.pid
start
在 lockfile 后加上 && echo $(pidof mogstored)>${pidfile}
stop:
lockfile 后加上 ${pidfile}
mogilefs 安装:
1. 使用 cpan 在线安装 MogileFS 和相关模块:
- [[email protected] ~]# perl -MCPAN -e shell
- cpan>install App::cpanminus
- cpan>install MogileFS::Server
- cpan>install MogileFS::Utils
- cpan>install IO::AIO
- cpan>install IO::WrapTie
- cpan>install Danga::Socket
- conf:
- /etc/mogilefs/{mogifsd.conf, mogstored.conf}
设定数据库:
- create database mogfsdb; # 创建数据库
- MariaDB [(none)]>grant all privileges on mogfsdb.* to 'moguser'@'127.0.0.1' identified by 'mogpass';
- MariaDB [(none)]> grant all privileges on mogfsdb.* to 'moguser'@'192.168.1.113' identified by 'mogpass';
- MariaDB [(none)]> flush privileges;
- mogdbsetup --dbhost=127.0.0.1 --dbpass=mogpass
用户名默认为: mogile
- useradd -r mogilefs
- mkdir /var/run/mogilefsd/
- chown -R mogilefs.mogilefs /var/run/mogilefsd
修改主配置文件:/etc/mogilefs/mogilefsd.conf
/etc 目录下创建 mogilefs 目录, 并提供配置文件 mogilefsd.conf
- [[email protected] ~]# VIM /etc/mogilefs/mogilefsd.conf
- # Enable daemon mode to work in background and use syslog
- daemonize = 1
- # Where to store the pid of the daemon (must be the same in the init script)
- pidfile = /var/run/mogilefsd/mogilefsd.pid # 指定 pid 文件位置
- # Database connection information
- db_dsn = DBI:MySQL:mogfsdb:host=127.0.0.1 # 修改为上面授权的账号和密码
- db_user = moguser # MySQL 用户
- db_pass = mogpass # MySQL 密码
- # IP:PORT to listen on for mogilefs client requests
- listen = 0.0.0.0:7001 # 监听的地址和端口(所有地址的 7001 端口)
- # Optional, if you don't define the port above.
- conf_port = 7001
- # Number of query workers to start by default.
- query_jobs = 10 # 用于查询的进程数
- # Number of delete workers to start by default.
- delete_jobs = 1 # 用于删除操作的进程数
- # Number of replicate workers to start by default.
- replicate_jobs = 5 # 用于复制的进程数
- # Number of reaper workers to start by default.
- # (you don't usually need to increase this)
- reaper_jobs = 1 # 用于回收资源的进程数
- # Number of fsck workers to start by default.
- # (these can cause a lot of load when fsck'ing)
- #fsck_jobs = 1
- # Minimum amount of space to reserve in megabytes
- # default: 100
- # Consider setting this to be larger than the largest file you
- # would normally be uploading.
- #min_free_space = 200
- # Number of seconds to wait for a storage node to respond.
- # default: 2
- # Keep this low, so busy storage nodes are quickly ignored.
- #node_timeout = 2
- # Number of seconds to wait to connect to a storage node.
- # default: 2
- # Keep this low so overloaded nodes get skipped.
- #conn_timeout = 2
- # Allow replication to use the secondary node get port,
- # if you have apache or similar configured for GET's
- #repl_use_get_port = 1
mogilefsd 服务脚本: mogilefs 在 centos7 的上用脚本运行可能会报错 修改地方
- #!/bin/bash
- #
- # mogilefsd - Startup script for the MogileFS tracker
- #
- # chkconfig: - 85 15
- # description: MogileFS tracker
- # processname: mogilefsd
- # config: /etc/mogilefs/mogilefsd.conf
pidfile: /var/run/mogilefsd/mogilefsd.pid ------ 开启 pid
- # Source function library.
- . /etc/rc.d/init.d/functions
- # Path to the apachectl script, server binary, and short-form for messages.
- lockfile=${LOCKFILE-/var/lock/subsys/mogilefsd}
- RETVAL=0
- prog=$(which mogilefsd)
- start() {
- ulimit -n 65535
- echo -n $"Starting mogilefsd"
- su - mogilefs -c "$prog -c /etc/mogilefs/mogilefsd.conf --daemon"
- RETVAL=$?
[ $RETVAL = 0 ] && success && touch ${lockfile} && echo $(pidof mogstored)> ${pidfile} || failure ----- 此处修改
- echo
- return $RETVAL
- }
- stop() {
- echo -n $"Stopping mogilefsd"
- netstat -nlp|grep "mogilefsd"|grep -v grep|awk '{print $7}'|awk -F"/" '{print $1}'|xargs kill -9
- RETVAL=$?
[ $RETVAL = 0 ] && success && rm -f ${lockfile} ${pidfile} || failure----- 此处修改
- echo
- }
- reload() {
- echo -n $"Reloading mogilefsd:"
- killall mogilefsd -HUP
- RETVAL=$?
- [ $RETVAL = 0 ] && success || failure
- echo
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status mogilefsd
- RETVAL=$?
- ;;
- restart)
- stop
- sleep 1
- start
- ;;
- reload)
- reload
- ;;
- *)
- echo $"Usage: mogilefsd {start|stop|restart|reload|status}"
- exit 1
- esac
- exit $RETVAL
存储节点:
解决依赖关系, 需要安装 perl-IO-AIO
启动:
- mogstored -c /etc/mogilefs/mogstored.conf -daemon
- #通知 Trackers 有什么 host 需要添加 有什么 Storage 需要添加
- mogadm host add 192.168.0.203 -ip=192.168.0.203 -port=7500 -status=alive
mogadm 等用法:
mogadm check 检测节点 查看节点状态
添加节点:
mogadm host list 每一个存储节点为一个 host
- mogadm host add 192.168.117.130(节点名称) --ip=192.168.117.130 --status=alive(默认为 done)
- mogadm host add 192.168.117.131(节点名称) --ip=192.168.117.131 --status=alive(默认为 done)
- mogadm host add 192.168.117.132(节点名称) --ip=192.168.117.132 --status=alive(默认为 done)
mogadm host add 192.168.117.130 1 添加第一个设备
mogadm host add 192.168.117.131 2 添加第二个设备
mogadm host add 192.168.117.132 3 添加第三个设备
mogadm domain add images 创建一个图片存储域
mogadm domain add files 创建名字为 files 的 domain 在其中保存 class
mogadm domain list 查看所以域
mogadm class list
中的 mindevcount: 最少设备数量 replpolicy: 复制策略, 基于主机 hashtype:hash 类型
复制策略表示最少保存几个副本 , 所以最少设备数量就要和复制策略数相等
mogadm class add images jpeg 创建 class class 类型为 jpeg 不指定则为 default
mogadm class add images PNG ....
mogadm class add files plaintext --maindevcount=1 不复制副本
mogadm class add files HTML --replpolicy="MultipleHost(3)" 最少保存三份副本
上传:
- mogupload
- mogupload --trackers=172.16.100.68:7001 --domain=images --class=jpeg --key='/1.jpg' --file='./1.jpg'
查询文件:
- mogfileinfo:
- mogfileinfo --trackers=host --domain=foo --key='./hello.jpg'
- mogfileinfo --trackers=192.168.117.130:7001 --domain=images --key='/1.jpg'
显示可用 key
- moglistkey:
- moglistkey --trackers=192.168.117.130:7001 --domain=images
列出指定域下的所以 id
- moglistfids
- moglistfid --trackers=192.168.117.130:7001
- moglistfid --trackers=192.168.117.130:7001 --fromfid=123(从哪个 id 开始) --count=5000(列出多少个)
- mogstats
- mogstats --db_dsn="DBI:mysql:mogilefs:host=192.168.117.130" --db_user='moguser' --db_pass='mogpass' --verbose --stats='devices,files'
删除:
mogadm domain delete images 删除 domain 先删除类
mogadm class delete images jpeg 注意的是 删除类要先删除类中的文件
mogdelete --trackers=192.168.117.130:7001 --domain=images --key='/1.jpg'
centos7 上编译安装 nginx 使其支持 nginx_mogilefs_module
1, 解决依赖关系
- # yum groupinstall "Development Tools" "Server Platform Deveopment"
- # yum install openssl-devel pcre-devel
2, 安装
首先添加用户 nginx, 实现以之运行 nginx 服务进程:
- # groupadd -r nginx
- # useradd -r -g nginx nginx
接着开始编译和安装:
- # ./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 \
- --http-client-body-temp-path=/var/tmp/nginx/client/ \
- --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
- --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
- --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
- --http-scgi-temp-path=/var/tmp/nginx/scgi \
- --with-pcre \
- --with-debug \
- --add-module=
- # make && make install make 时报警如果是可以忽略的 可以直接去修 Makefile 将其中的 -Werror 去了 即警告不阻止
在其中的 --add-module = 中加入你想要编译进去的模块 --add-module=../nginx_mogilefs_module-1.0.4
如果在编译时报错 要自己解决依赖关系 pcre-devel openssl-devel
说明:
1,Nginx 可以使用 Tmalloc(快速, 多线程的 malloc 库及优秀性能分析工具)来加速内存分配, 使用此功能需要事先安装 gperftools, 而后在编译 nginx 添加 --with-google_perftools_module 选项即可.
2, 如果想使用 nginx 的 perl 模块, 可以通过为 configure 脚本添加 --with-http_perl_module 选项来实现, 但目前此模块仍处于实验性使用阶段, 可能会在运行中出现意外, 因此, 其实现方式这里不再介绍. 如果想使用基于 nginx 的 CGI 功能, 也可以基于 FCGI 来实现, 具体实现方法请参照网上的文档.
centos7
提供 unit 文件
也可以提供 server
- [Unit]
- Description=The nginx HTTP and reverse proxy server
- after=network.target remote-fs.target nss-lookup.target
- [service]
- Type=forking
- PIDFile=/var/run/nginx/nginx.pid
- ExecStartPre=/usr/local/nginx/sbin/nginx -t (应用程序所在地)
- ExecStart=/usr/local/nginx/sbin/nginx
- ExecReload=/bin/kill -s HUP $MAINPID
- KillMode=process
- Killsignal=SIGQUIT
- TimeoutStopSec=5
- PrivateTmp=true
- [Install]
- WanteBY=multi-user.target
脚本文件修改完成后
systemctl daemon-reload 重载一下
2, 用 nginx 反代 mogilefs
Nginx 做为 MogileFS 的前端客户端
我们使用 Nginx 来获取文件, 做前端的查询代理时需要使用到 mogilefs 的这个模块. 可以下载这个模块编译进 Nginx 就行了. 直接使用 ./configure -add-module= 这个参数就可以了.
最新的这个模块的下载地址是: https://github.com/vkholodkov/nginx-mogilefs-module
使用这个需要考虑到网站原来的 url 是什么样的. 比如:
http://www.a.com/uploads/front_page/A6B00135E24AB17E043B9B5453762438.png
这个 URL 中的 UUID 是 A6B00135E24AB17E043B9B5453762438.PNG. 这时我们使用这个做 key 来存成 MogileFS 中就行.
再结合 rewrite, 只要 key 在 url 里有, 就能直接代理到后端的 mogilefs. 象如下的写法, 会直接取 A6B00135E24AB17E043B9B5453762438.PNG 来做查询用的 key.
- location ~ ([^\/]+)$ {
- #只要不是根结尾的都进行反代
- mogilefs_tracker 192.168.1.xxx:7001;
- mogilefs_domain img;
- mogilefs_methods GET;
mogilefs_noverify on; 是否不做校验
mogilefs_pass {
proxy_pass $mogilefs_path; 这个变量可以在反代时让 nginx 构建出一个 mogilefs 的请求报文发给后端的 tracker 主机
proxy_hide_header Content-Type; 隐藏 Content-Type
- proxy_buffering off;
- }
- }
如果使用了多个 tracker 的话, 要配置使用多个 tracker 来进行负载均衡和备份. 可以直接配置 tracker 为 upstrame:
- upstream online_mogilefs {
- server 10.0.0.1:7001;
- server 10.0.0.2:7001;
- }
然后后面配置 tracker 的连接时, 直接加上就行了
mogilefs_tracker online_mogilefs;
如果你还想配置使用 mogilefs 的 Nginx 上传, 使用其中的 put 功能然后就不要安装客户端上传送, 就需要打个补丁.
上面的配置其实也是一样, 关键对于上传的配置, 需要给方法修改为
mogilefs_methods PUT DETEL;
实例:
location /images { 如果在上传文件时 key 后加上了如 / 1.jpg 此处 images 后面不上 / 如果为 1.jpg 则 location 后 / images/
- mogilefs_tracker 192.168.117.130:7001;
- mogilefs_domain img;
- mogilefs_methods GET;
mogilefs_noverify on; 是否不做校验
mogilefs_pass {
proxy_pass $mogilefs_path; 这个变量可以在反代时让 nginx 构建出一个 mogilefs 的请求报文发给后端的 tracker 主机
proxy_hide_header Content-Type; 隐藏 Content-Type
- proxy_buffering off;
- }
Tracker 高可用:
在每一个服务器上启用 tracker
systemctl start mogilefsd.service
Tracker 负载均衡
- upstrem trackers{
- server 192.168.117.130:7001
- server 192.168.117.131:7001
- server 192.168.117.132:7001
check interval=1000 rise fall=5 timeout=1000 这种自检测 只有 tengine 可以使用 nginx 可能不支持
}
调用时:
- location /images {
- mogilefs_tracker trackers;
- mogilefs_domain img;
- mogilefs_methods GET;
mogilefs_noverify on; 是否不做校验
健康状态检测:
- localtion /status{
- check_status;
- allow 192.168.117.130;
- deny all;
- }
- systemctl reload nginx
- libfastcommon v1.13
- FastDFS v5.06
- fastdfs-nginx-module v1.17
- [[email protected] ~]# tar xjvf fdfs-5.06.tar.bz2
- [[email protected] ~]# yum install -y gcc perl # 安装依赖的软件包
- [[email protected] ~]# cd ~/fdfs/libfastcommon && ./make.sh && ./make.sh install
- [[email protected] ~]# cd ~/fdfs/fastdfs && ./make.sh && ./make.sh install
- [[email protected] ~]# ls /usr/bin/fdfs_*/usr/bin/fdfs_appender_test /usr/bin/fdfs_monitor
- /usr/bin/fdfs_appender_test1 /usr/bin/fdfs_storaged
- /usr/bin/fdfs_append_file /usr/bin/fdfs_test
- /usr/bin/fdfs_crc32 /usr/bin/fdfs_test1
- /usr/bin/fdfs_delete_file /usr/bin/fdfs_trackerd
- /usr/bin/fdfs_download_file /usr/bin/fdfs_upload_appender
- /usr/bin/fdfs_file_info /usr/bin/fdfs_upload_file
- [[email protected] ~]# ls /etc/fdfs
- client.conf.sample storage.conf.sample tracker.conf.sample
- firewall-cmd --zone=public --add-port=22122/tcp --permanent
- firewall-cmd --reload
- mkdir -p /data/fastdfs
- cd /etc/fdfs
- cp tracker.conf.sample tracker.conf
- cp /root/fdfs/fastdfs/conf/http.conf .
- cp /root/fdfs/fastdfs/conf/mime.types .
- sed -i 's:base_path=.*:base_path=/data/fastdfs:g' tracker.conf
- sed -i 's:http.server_port=.*:http.server_port=80:g' tracker.conf
- bash -c 'cat> /usr/lib/systemd/system/fdfs_trackerd.service <<EOF
- [Unit]
- Description=fastdfs tracker server
- After=network.target
- [Service]
- Type=forking
- PIDFile=/data/fastdfs/data/fdfs_trackerd.pid
- ExecStart=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
- ExecReload=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
- ExecStop=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop
- [Install]
- WantedBy=multi-user.target
- EOF'
- systemctl enable fdfs_trackerd.service
- systemctl start fdfs_trackerd.service
- firewall-cmd --permanent --zone=public --add-service=http
- firewall-cmd --reload
- yum install -y epel-release # 安装 EPEL 软件仓库
- yum install -y nginx
- systemctl enable nginx
- systemctl start nginx
- upstream fdfs {
- server 192.168.71.127:80;
- server 192.168.71.128:80;
- }
- location /M00 {
- proxy_pass http://fdfs;
- }
- firewall-cmd --zone=public --add-port=23000/tcp --permanent
- firewall-cmd --reload
- 1mkdir -p /data/fastdfs
- cd /etc/fdfs
- cp storage.conf.sample storage.conf
- cp /root/fastdfs/fastdfs/conf/http.conf .
- cp /root/fastdfs/fastdfs/conf/mime.types .
- sed -i 's:base_path=.*:base_path=/data/fastdfs:g' storage.conf
- sed -i 's:store_path0=.*:store_path0=/data/fastdfs:g' storage.conf
- sed -i 's/tracker_server=.*/tracker_server=192.168.71.126:22122/g' storage.conf
- sed -i 's:http.server_port=.*:http.server_port=80:g' storage.conf
- bash -c 'cat> /usr/lib/systemd/system/fdfs_storaged.service <<EOF
- [Unit]
- Description=fastdfs storage server
- After=network.target
- [Service]
- Type=forking
- PIDFile=/data/fastdfs/data/fdfs_storaged.pid
- ExecStart=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
- ExecReload=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
- ExecStop=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
- [Install]
- WantedBy=multi-user.target
- EOF'
- systemctl enable fdfs_storaged.service
- systemctl start fdfs_storaged.service
- mkdir -p /data/fastdfs
- cd /etc/fdfs
- cp client.conf.sample client.conf
- sed -i 's:base_path=.*:base_path=/data/fastdfs:g' client.conf
- sed -i 's/tracker_server=.*/tracker_server=192.168.71.126:22122/g' client.conf
- [email protected]: ~ $ fdfs_upload_file /etc/fdfs/client.conf pom.xml
- group1/M00/00/00/wKhHf1S-oryAZCpgAAAE2uRlJkA126.xml
- [email protected]: ~ $ fdfs_file_info /etc/fdfs/client.conf
- group1/M00/00/00/wKhHf1S-oryAZCpgAAAE2uRlJkA126.xml
- source storage id: 0
- source ip address: 192.168.71.127
- file create timestamp: 2015-01-26 02:47:24
- file size: 1242
- file crc32: 3831834176 (0xE4652640)
- [email protected]: ~ $ fdfs_download_file /etc/fdfs/client.conf \
- group1/M00/00/00/wKhHf1S-oryAZCpgAAAE2uRlJkA126.xml downtest.xml
- [email protected]: ~ $ ls
- cd ~/fdfs/FastDFS/php_client
- /usr/local/PHP-5.4.33/bin/phpize
- ./configure --with-PHP-config=/usr/local/PHP-5.4.33/bin/PHP-config
- make && make install
- mkdir /usr/local/PHP-5.4.33/etc/conf.d
- cp fastdfs_client.INI /usr/local/PHP-5.4.33/etc/conf.d
- <?PHP
- // fastdfs.PHP
- classFdfs{
- private$fdfs, $tracker, $storage;
- publicfunction__construct(){
- $this->fdfs =newFastDFS();
- // get a connected tracker server
- $this->tracker =$this->fdfs->tracker_get_connection();
- if(!$this->tracker) {
- thrownewException('cannot connect to tracker server:['.
- $this->fdfs->get_last_error_no() .']'.
- $this->fdfs->get_last_error_info());
- }
- // get the storage server info and connect to it
- $this->storage =$this->fdfs->tracker_query_storage_store();
- $this->server =$this->fdfs->connect_server(
- $this->storage['ip_addr'],$this->storage['port']);
- if($this->server ===false) {
- thrownewException('cannot connect to storage server'.
- $this->storage['ip_addr'] .':'.
- $this->storage['port'] .':['.
- $this->fdfs->get_last);
- }
- $this->storage['sock'] =$this->server['sock'];
- }
- publicfunctionupload($localfile, $ext_name){
- //$info = $this->fdfs->storage_upload_by_filename($localfile);
- $info =$this->fdfs->storage_upload_by_filename($localfile, $ext_name,
- array(),null,$this->tracker,$this->storage);
- if(is_array($info)) {
- $group_name = $info['group_name'];
- $remote_filename = $info['filename'];
- $source_info =$this->fdfs->get_file_info($group_name,
- $remote_filename);
- $source_ip = $source_info['source_ip_addr'];
- $file_size = $source_info['file_size'];
- returncompact('group_name','remote_filename',
- 'source_ip','file_size');
- }
- returnfalse;
- }
- publicfunctiondownload_to_buff($group_name, $remote_filename){
- $content =$this->fdfs->storage_download_file_to_buff(
- $group_name, $remote_filename);
- return$content;
- }
- publicfunctiondownload_to_file($group_name,
- $remote_filename, $dst_localfile){
- return$this->fdfs->storage_download_file_to_file($group_name,
- $remote_filename, $dst_localfile);
- }
- publicfunctiondelete($group_name, $remote_filename){
- return$this->fdfs->storage_delete_file($group_name, $remote_filename);
- }
- publicfunctionexists($group_name, $remote_filename){
- return$this->fdfs->storage_file_exist($group_name, $remote_filename);
- }
- publicfunctionget_file_info($group_name, $remote_filename){
- return$this->fdfs->get_file_info($group_name, $remote_filename);
- }
- }
- # cat test.PHP
- <?PHP
- require_once('fastdfs.php');
- $fdfs = new FDFS();
- $localfile = './test.xml';
- $fileinfo = $fdfs->upload($localfile);
- if ($fileinfo) {
- // update file info in the database etc
- }
- var_dump($fileinfo);
- # PHP test.PHP
- array(4) {
- ["group_name"]=>
- string(6) "group1"
- ["remote_filename"]=>
- string(44) "M00/00/00/wKhHf1S-qbuAISbeAAAE2uRlJkA789.xml"
- ["source_ip"]=>
- string(14) "192.168.71.127"
- ["file_size"]=>
- int(1242)
- }
- firewall-cmd --permanent --zone=public --add-service=http
- firewall-cmd --reload
- yum install -y epel-release # 安装 EPEL 软件仓库
- yum install -y nginx
- systemctl enable nginx
- systemctl start nginx
- [[email protected] ~]# nginx -V
- nginx version: nginx/1.6.2
- built by gcc 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)
- TLS SNI support enabled
- configure arguments: --prefix=/usr/share/nginx --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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
- yum install -y RedHat-rpm-config pcre-devel openssl-devel libxml2-devel \
- libxslt-devel gd-devel perl-ExtUtils-Embed GeoIP-devel gperftools \
- gperftools-devel
- tar xvf nginx-1.6.2.tar.gz
- cd nginx-1.6.2
- ./configure --prefix=/usr/share/nginx --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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/root/fastdfs/fastdfs-nginx-module/src
- mv /usr/sbin/nginx /usr/sbin/nginx.`date +'%Y%m%d'`
- cp objs/nginx /usr/sbin/nginx
- cp /root/fastdfs/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
- touch /data/fastdfs/logs/mod_fastdfs.log
- chown nginx:nginx /data/fastdfs/logs/mod_fastdfs.log
- vi /etc/fdfs/mod_fastdfs.conf
- base_path=/data/fastdfs
- tracker_server=192.168.71.126:22122
- store_path0=/data/fastdfs
- log_filename=/data/fastdfs/logs/mod_fastdfs.log
- location /M00 {
- alias /data/fastdfs/data;
- ngx_fastdfs_module;
- }
- systemctl stop nginx
- systemctl start nginx
- # rpm -Uvh https://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
- # yum clean all
- # yum -y install zabbix-server-MySQL zabbix-Web-MySQL zabbix-agent 安装 zabbix 的数据库交互端, Web 交互端, 以及 agent 端
- yum install mariadb-server -y #安装数据库服务
- [[email protected] ~]# VIM /etc/my.cnf #修改数据库配置跳过名称解析
- [mysqld]
- datadir=/var/lib/MySQL
- socket=/var/lib/MySQL/MySQL.sock
- skip_name_resolve=ON
- MySQL> create database zabbix character set utf8 collate utf8_bin;
- MySQL> grant all privileges on zabbix.* to [email protected]'192.168.1.%' identified by 'centos';
- MySQL> quit;
- DBPassword=CentOS
- DBHost=192.168.1.196
- # 修改 Web 配置文件中的 PHP 时区
- php_value date.timezone Asia/Shanghai
- # systemctl restart zabbix-server zabbix-agent httpd #启动服务
- # systemctl enable zabbix-server zabbix-agent httpd
- UserParameter=Nginx.active[*], /usr/bin/curl -s "http://$1:$2/status" | awk '/^Active/ {print $NF}'
- UserParameter=Nginx.reading[*], /usr/bin/curl -s "http://$1:$2/status" | grep 'Reading' | cut -d" " -f2
- UserParameter=Nginx.writing[*], /usr/bin/curl -s "http://$1:$2/status" | grep 'Writing' | cut -d" " -f4
- UserParameter=Nginx.waiting[*], /usr/bin/curl -s "http://$1:$2/status" | grep 'Waiting' | cut -d" " -f6
- UserParameter=Nginx.accepted[*], /usr/bin/curl -s "http://$1:$2/status" | awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$1}'
- UserParameter=Nginx.handled[*], /usr/bin/curl -s "http://$1:$2/status" | awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$2}'
- UserParameter=Nginx.requests[*], /usr/bin/curl -s "http://$1:$2/status" | awk '/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$3}'
来源: http://www.bubuko.com/infodetail-3263490.html