FastDFS蛋疼的集羣和負載均衡(七)之Nginx高可用集羣應用到FastDFS集羣

diary_report.jpg

Interesting things

這將會是關於FastDFS蛋疼的集羣和負載均衡系列收官之篇,不排除有總結篇!這篇就是將keepalived實現的nginx集羣高可用應用到我們的FastDFS集羣中。

What did you do today

  • 我們需要修改192.168.12.77和192.168.12.88設備上的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 {
   # cmazxiaoma is handsome
    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;
        upstream fastdfs_tracker {
                server  192.168.12.11:8000 weight=1 max_fails=2 fail_timeout=30s;
                server  192.168.12.22:8000 weight=1 max_fails=2 fail_timeout=30s;
        }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        access_log  logs/host.access.log  main;

        location /fastdfs {
                root html;
                index index.html index.htm;
                proxy_pass http://fastdfs_tracker/;
                proxy_set_header Host $http_host;
                proxy_set_header Cookie $http_cookie;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                client_max_body_size 300m;
        }

        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;
    #    }
    #}

}
  • 我們對配置做了一些修改。
    • 一處是添加了負載均衡upstream fastdfs_tracker.把192.168.12.11和192.168.12.22兩臺設備作爲tracker,現在我們集羣了一層nginx來代理2臺tracker
        upstream fastdfs_tracker {
                server  192.168.12.11:8000 weight=1 max_fails=2 fail_timeout=30s;
                server  192.168.12.22:8000 weight=1 max_fails=2 fail_timeout=30s;
        }
  • 另一處修改是添加了一個location,匹配路徑中含有fastdfs。

  • 我們再上傳一張圖片。上傳成功返回group1/M00/00/00/wKgMIVpE8IKAWxlrAADRd6mMX3g207.jpg

上傳圖片


Summary

FastDFS負載均衡和集羣終於搞定了。
1.虛擬ip 192.168.12.111提供入口
2.一層Nginx集羣負載均衡(192.168.12.77、192.168.12.88)
3.Tracker+Nginx負載均衡(192.168.12.11)
4.Tracker + Nginx負債均衡(192.168.12.22)
5.Group1包含Stroage+Nginx(192.168.12.33), Storage+Nginx(192.168.12.44)
6.Group2包含Stroage+Nginx(192.168.12.55), Storage+Nginx(192.168.12.66)
一共2臺tracker、4臺storage、8臺nginx。

What to do tomorrow

寫一篇關於FastDFS集羣和負載均衡的總結。這個星期把Spring Security和二叉樹兩篇博客搞定,預習Zookeeper。下一個星期把Zookeeper擼一遍。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章