分布式文件系统及监控系统

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和相关模块:

        [root@centos7 ~]# 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

        [root@centos7 ~]# 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 

原文链接:https://blog.csdn.net/a824701419/article/details/81698579

3、搭建fastdfs

1.fastDFS介绍:

fastDFS 是以C语言开发的一项开源轻量级分布式文件系统,他对文件进行管理,主要功能有:文件存储,文件同步,文件访问(文件上传/下载),特别适合以文件为载体的在线服务,如图片网站,视频网站等

分布式文件系统:

基于客户端/服务器的文件存储系统

对等特性允许一些系统扮演客户端和服务器的双重角色,可供多个用户访问的服务器,比如,用户可以“发表”一个允许其他客户机访问的目录,一旦被访问,这个目录对客户机来说就像使用本地驱动器一样

FastDFS由跟踪服务器(Tracker Server)、存储服务器(Storage Server)和客户端(Client)构成。

Tracker server 追踪服务器

追踪服务器负责接收客户端的请求,选择合适的组合storage server ,tracker server 与 storage server之间也会用心跳机制来检测对方是否活着。

Tracker需要管理的信息也都放在内存中,并且里面所有的Tracker都是对等的(每个节点地位相等),很容易扩展

客户端访问集群的时候会随机分配一个Tracker来和客户端交互。

Storage server 储存服务器

实际存储数据,分成若干个组(group),实际traker就是管理的storage中的组,而组内机器中则存储数据,group可以隔离不同应用的数据,不同的应用的数据放在不同group里面,

优点:

海量的存储:主从型分布式存储,存储空间方便拓展,

fastDFS对文件内容做hash处理,避免出现重复文件

然后fastDFS结合Nginx集成, 提供网站效率

客户端Client

主要是上传下载数据的服务器,也就是我们自己的项目所部署在的服务器。每个客户端服务器都需要安装Nginx


 

本安装使用 CentOS 7 x86_64版操作系统,按照以下网络结构进行部署:


 

下载安装文件

FastDFS 5.x 取消了对 libevent 的依赖,添加了对 libfastcommon 的依赖。

本部署说明用到的软件版本:

libfastcommon v1.13

FastDFS v5.06

fastdfs-nginx-module v1.17

可从上面的sourceforge或github中下载,或者直接下载本文附带的压缩包。详细的安装说明可参照代码中的INSTALL。

安装FastDFS

在每一台tracker和storage服务器上执行

[root@localhost ~]# tar xjvf fdfs-5.06.tar.bz2

[root@localhost ~]# yum install -y gcc perl  # 安装依赖的软件包

[root@localhost ~]# cd ~/fdfs/libfastcommon && ./make.sh && ./make.sh install

[root@localhost ~]# cd ~/fdfs/fastdfs && ./make.sh && ./make.sh install

安装完成后,所有可执行文件在目录/usr/bin下,以fdfs_开头:

[root@localhost ~]# 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

配置文件在目录/etc/fdfs下:

[root@localhost ~]# ls /etc/fdfs

client.conf.sample  storage.conf.sample  tracker.conf.sample

FastDFS配置

配置Tracker跟踪器

开放tracker监听端口访问

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

确认tracker是否启动成功

1cat /data/fastdfs/logs/trackerd.log

安装并配置 nginx 反向代理

防火墙开放http服务

firewall-cmd --permanent --zone=public --add-service=http

firewall-cmd --reload

安装并启用nginx

yum install -y epel-release    # 安装 EPEL 软件仓库

yum install -y nginx

systemctl enable nginx

systemctl start nginx

配置反向代理

打开 /etc/nginx/nginx.conf,在 http {} 中添加:

upstream fdfs {

    server  192.168.71.127:80;

    server  192.168.71.128:80;

}

在 server{} 中添加:

location /M00 {

    proxy_pass http://fdfs;

}

配置Storage存储节点

开放tracker监听端口访问

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

确认storage是否启动成功

1cat /data/fastdfs/logs/storaged.log

client客户端配置

在tracker, storage之外的一台主机上安装FastDFS,然后执行:

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

FastDFS测试

上传测试:

joelhy@arminix: ~ $ fdfs_upload_file /etc/fdfs/client.conf pom.xml

group1/M00/00/00/wKhHf1S-oryAZCpgAAAE2uRlJkA126.xml

查看文件信息:

joelhy@arminix: ~ $ 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)

下载测试:

joelhy@arminix: ~ $ fdfs_download_file /etc/fdfs/client.conf \

    group1/M00/00/00/wKhHf1S-oryAZCpgAAAE2uRlJkA126.xml downtest.xml

joelhy@arminix: ~ $ ls

downtest.xml

FastDFS客户端

PHP扩展安装

client主机上安装的是php v5.4.33

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扩展使用

<?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);

    }

}

PHP客户端上传图片测试

# 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)

}

fastdfs-nginx-module 安装配置

fastdfs-nginx-module用于解决同步延迟问题:

同组之间的服务器需要复制文件,有延迟的问题.

假设Tracker服务器将文件上传到了192.168.1.80,文件ID已经返回客户端,

这时,后台会将这个文件复制到192.168.1.30,如果复制没有完成,客户端就用这个ID在192.168.1.30取文件,肯定会出现错误

这个fastdfs-nginx-module可以重定向连接到源服务器取文件,避免客户端由于复制延迟的问题,出现错误。

fastdfs-nginx-module模块只需要安装到storage上。

storage server 安装 nginx 服务器

防火墙开放http服务

firewall-cmd --permanent --zone=public --add-service=http

firewall-cmd --reload

安装并启用nginx

yum install -y epel-release    # 安装 EPEL 软件仓库

yum install -y nginx

systemctl enable nginx

systemctl start nginx

查看前面安装的nginx编译参数

[root@localhost ~]# 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'

下载nginx源码

1curl -O http://nginx.org/download/nginx-1.6.2.tar.gz

安装nginx依赖的软件包

yum install -y redhat-rpm-config pcre-devel openssl-devel libxml2-devel \

    libxslt-devel gd-devel perl-ExtUtils-Embed GeoIP-devel gperftools \

    gperftools-devel

重新编译nginx

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

fastdfs模块配置

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

Storage服务器nginx配置

server{} 里添加

location /M00 {

  alias /data/fastdfs/data;

  ngx_fastdfs_module;

}

重启Nginx

systemctl stop nginx

systemctl start nginx

原文链接
http://joelhy.github.io/2015/01/27/FastDFS-v5-06-deploy/

4、搭建zabbix监控服务

官网下载地址

https://www.zabbix.com/download

下载zabbix的包仓库

# 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 #安装数据库服务

[root@centos7 ~]# 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 zabbix@'192.168.1.%' identified by 'centos';

mysql> quit;

将包中的mysql脚本展开并且导入到zabbix库中创建符合zabbix存储的表结构定义

zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pcentos zabbix

修改file /etc/zabbix/zabbix_server.conf #修改配置文件中的账户及其他信息

DBPassword=centos

DBHost=192.168.1.196

#修改web配置文件中的php时区

编辑file /etc/httpd/conf.d/zabbix.conf

php_value date.timezone Asia/Shanghai

# systemctl restart zabbix-server zabbix-agent httpd #启动服务

# systemctl enable zabbix-server zabbix-agent httpd


 

 

 

搭建成功

登录web页面 用户名默认为 Admin 密码为zabbix


 

5、添加memory监控项,并出图

在本机启动模式


 

yum install zabbix-agent 在被监控节点安装agent端用于监控通信


 

修改server的ip地址


 

HostnameItem=system.hostname #也可以使用item监控到的主机名作为主机名

之后在server端定义item


 

单独添加一个主机组


 

添加主机至myhostgroup组

点击item。添加监控项

 

定义监控的key#key为写好的shell命令的名称,可选系统自带中的key

https://www.zabbix.com/documentation/3.4/manual/config/items/itemtypes/zabbix_agent

官方定义的key文档,有些key可以传递参数


定义一个cpu上下文切换时长监控项

定义抓包网络,参数为ens33,packets 为网卡,和抓包数


定义一个网络状况监控项

 

 

 

定义内存监控项,我这里给的参数为free剩余的内存

对应于此命令中的几个字段


 

 

6、实现报警

定义触发器,实现报警功能


 

生成表达式


 

 

选择函数,


 

 

此表达式表示为,centos7(hostname):net.if.in[ens33,packets] (key,表示监控项).last(#1)=50(表示一个统计函数,满足大于50报文的条件,则实现告警)


 

 

显示triggers


 

ping -f 192.168.1.196 #使用大量ping

这里会出现红色警告


 

 

定义告警的动作

 

需要定义一个媒介,使用什么通道告警,邮件,短信,脚本模拟登陆邮件发送。等等


 

定义用户,来接收邮件(使用刚刚创建的通信媒介)


 

然后在定义事件,选为触发器事件


 

定义条件,操作,发送信息


 

 

 

宏的概念

一种变量,将变量名替换成为变量值,变量值通常为字符串

分为内键宏和自定义宏,内建宏没有$符号,自定义宏需要带$符号

级别:全局,模板,主机


定义全局宏

主机级别的宏

定义模板

使用内建模板,或者在此链接https://share.zabbix.com/使用别人做好的模板

在添加主机时可以将模板链接至主机


 

也可以创建自定义模板,然后在定义监控项,触发器等等


 

自定义key:

在 zabbix_agentd.conf中定义或者 zabbix_agent.d/目录中以.conf结尾的文件

以下为示例格式[]中括号内表示向key传递参数

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}'

猜你喜欢

转载自www.cnblogs.com/woaiyitiaochai/p/11757987.html