本教程基于docker和docker-compose环境,使用了morunchang/fastdfs镜像(包含nginx访问模块).可实现部署FastDFS集群(一台tracker服务器,两台storage服务器分别属于不同的组).

一、 建立docker文件结构

1、tracker文件结构如下:

2、storage1文件结构如下:

3、storage2文件目录结构:

二、编写docker-compose的相关文件

一、tracker

1、docker-compose.yml文件如下

version: '3'

services:
 tracker:
  build: ./tracker
  ports:
  - "22122:22122"
  - "8000:8000"
  volumes:
    - /data/fdfs/tracker:/data/fast_data
  • 注意:

        1、22122是tracker的端口,除非修改配置文件,否则不要去改,通过映射的方法也不行.8000是tracker的nginx负载均衡端口,也可以通过这个端口来访问无论哪个storage里面的文件.

       2、- /data/fdfs/tracker:/data/fast_data,目录映射,/data/fast_data

2、Dockerfile文件如下

FROM morunchang/fastdfs
COPY nginx.conf /etc/nginx/conf/nginx.conf
COPY tracker.sh /tracker.sh
ENTRYPOINT sh tracker.sh

3、配置负载均衡、通过tracker服务器就可以访问的nginx.conf文件如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # 对group1做负载均衡
    upstream fdfs_group1 {
         server 192.168.190.133:8080 weight=1 max_fails=2 fail_timeout=30s;
    }
    # 对group2做负载均衡
    upstream fdfs_group2 {
         server 192.168.190.135:8080 weight=1 max_fails=2 fail_timeout=30s;
    }

    server {
        listen       8000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #location /group1/M00 {
            #proxy_next_upstream http_502 http_504 error timeout invalid_header;
            #proxy_pass http://fdfs_group1;
            #expires 30d;
        #}

	location /group2/M00 {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_pass http://fdfs_group2;
            expires 30d;
        }
	
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
  • 注意:

        # 对group1做负载均衡
       upstream fdfs_group1 {
             server 192.168.190.133:8080 weight=1 max_fails=2 fail_timeout=30s;     # 192.168.190.133:8080为storage1的ip和端口
       }
       # 对group2做负载均衡
       upstream fdfs_group2 {
              server 192.168.190.135:8080 weight=1 max_fails=2 fail_timeout=30s;   # 192.168.190.135:8080为storage2的ip和端口
        }

       listen端口号改为8000

4、tracker.sh文件如下:

#!/bin/sh
/data/fastdfs/tracker/fdfs_trackerd /etc/fdfs/tracker.conf
/etc/nginx/sbin/nginx
tail -f /data/fast_data/logs/trackerd.log

二、storage1

1、docker-compose.yml文件如下

version: '3'

services:
 storage:
  build: ./storage
  environment:
   GROUP_NAME: group1
   TRACKER_IP: 192.168.190.132:22122
  ports:
  - "8080:8080"
  - "23000:23000"
  volumes:
    - /data/fdfs/storage:/data/fast_data
  • 注意:

        23000是storage的端口,除非修改配置文件,否则不要去改,通过映射的方法也不行.8080是nginx端口,TRACKER_IP是tracker的ip和端口

       group1为组名

2、Dockerfile文件如下

FROM morunchang/fastdfs
COPY nginx.conf /data/nginx/conf/nginx.conf
ENTRYPOINT sh storage.sh

3、nginx.conf文件如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8888;
        server_name  localhost;

        #charset koi8-r;
   	location ~/group([0-9])/M00 {
		#alias /fastdfs/storage/data;
		ngx_fastdfs_module;
	}

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
  • 注意:

        1、listen       8888;端口是与storage.conf中的http_server_port相对应的要一样

        2、配置集群这个一定要加上:     

          location ~/group([0-9])/M00 {
                  #alias /fastdfs/storage/data;
                 ngx_fastdfs_module;
           }

三、storage2

1、docker-compose.yml文件如下

version: '3'

services:
 storage:
  build: ./storage
  environment:
   GROUP_NAME: group2
   TRACKER_IP: 192.168.190.132:22122
  ports:
  - "8080:8080"
  - "23000:23000"
  volumes:
    - /data/fdfs/storage:/data/fast_data
  • 注意:

        23000是storage的端口,除非修改配置文件,否则不要去改,通过映射的方法也不行.8080是nginx端口,TRACKER_IP是tracker的ip和端口

       group2为组名

2、Dockerfile文件如下

FROM morunchang/fastdfs
COPY nginx.conf /data/nginx/conf/nginx.conf
ENTRYPOINT sh storage.sh

3、nginx.conf文件如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8888;
        server_name  localhost;

        #charset koi8-r;
   	location ~/group([0-9])/M00 {
		#alias /fastdfs/storage/data;
		ngx_fastdfs_module;
	}

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
  • 注意:

        1、listen       8888;端口是与storage.conf中的http_server_port相对应的要一样

        2、配置集群这个一定要加上:     

          location ~/group([0-9])/M00 {
                  #alias /fastdfs/storage/data;
                 ngx_fastdfs_module;
           }

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐