FastDFS
FastDFS 集羣
克隆虛擬機
VMware修改mac
-
修改 ip地址
-
rm -f /etc/udev/rules.d/70-persistent-net.rules
-
reboot
Tracker集羣搭建
克隆出來之前已經裝好的兩臺虛擬機做Tracker節點
tarcker 節點 ip 131、132
啓動兩個Tracker節點
/etc/init.d/fdfs_trackerd start
查看端口
netstat -unltp |grep fdfs
啓動日誌
tail -100f trackerd.log
Storage集羣搭建
複製4臺虛擬機
第一組 ip: 181、182 對應 group1
第二組 ip: 191、192 對應 group2
修改配置文件/etc/fdfs/storage.conf
修改tracker_server 的ip地址,多個 tracker 直接添加多條配置
tracker_server=192.168.150.131:22122
tracker_server=192.168.150.132:22122
啓動Storage服務
/etc/init.d/fdfs_storaged start
fdfsMonitor
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
查看端口:netstat -unltp | grep fdfs
使用FastDFS中的Monitor查看:在所有的Storage啓動成功後執行下述命令
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
測試FastDfs集羣
測試文件上傳
fdfs_upload_file /etc/fdfs/client.conf tmp.sh
修改tracker.conf文件中group的負載策略
修改配置文件中的store_lookup,這個屬性的可選值有0,1,2。分別代表:
# 0: 輪詢
# 1: 指定卷,通過卷名指定訪問某一個卷
# 2: 訪問空閒磁盤空間較大的。是默認的負載策略。
Nginx集羣搭建
FastDFS 通過 Tracker 服務器,將文件放在 Storage 服務器存儲,但是同組存儲服務器之間需要進入文件複製,有同步延遲的問題。假設 Tracker 服務器將文件上傳到了 S1,上傳成功後文件 ID已經返回給客戶端。
此時 FastDFS 存儲集羣機制會將這個文件同步到同組存儲 S2,在文件還沒有複製完成的情況下,客戶端如果用這個文件 ID 在 S2 上取文件,就會出現文件無法訪問的錯誤。
而 fastdfs-nginx-module 可以重定向文件連接到源服務器(S1)取文件,避免客戶端由於複製延遲導致的文件無法訪問錯誤。
Storage節點Nginx反向代理
在每個Storage節點安裝配置Nginx
修改配置文件mod_fastdfs.conf
connect_timeout=10
#181、182 對應 group 1
#191、192 對應 group 2
group_name=
tracker_server=192.168.150.131:22122
tracker_server=192.168.150.132:22122
group_count = 2
# group settings for group #1
# since v1.14
# when support multi-group, uncomment following section
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/var/data/fastdfs-storage/store
#store_path1=/home/yuqing/fastdfs1
# group settings for group #2
# since v1.14
# when support multi-group, uncomment following section as neccessary
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/var/data/fastdfs-storage/store
檢查Nginx配置文件
端口80必須和Storage服務器中的/etc/fdfs/storage.conf配置文件中的http.server_port=80一致。
listen 80;
location ~ /group([0-9])/M00 {
# add_header Content-Disposition "attachment;filename=$arg_attname";
ngx_fastdfs_module;
}
測試訪問文件
報錯
ERROR - file: /root/fastdfs-nginx-module/src//common.c, line: 709, expect parameter token or ts in url, uri: /
group1/M00/00/00/wKiWtV1H4eKAKE4YAAABAE3E6HQ3627.gif
檢查防盜鏈系統
vi /etc/fdfs/http.conf
http.anti_steal.check_token=
Tracker節點 負載均衡反向代理
在每個Tracker節點安裝配置Nginx
在 tracker 上安裝的 nginx 主要爲了提供 http 訪問的反向代理、負載均衡以及緩存服務。
Nginx緩存
傳並解壓ngx_cache_purge-2.3.tar.gz
cd /root
tar -zxf ngx_cache_purge-2.3.tar.gz
編譯安裝nginx
./configure --prefix=/usr/local/tengine --add-module=/root/ngx_cache_purge-2.3
make
make install
配置nginx
ngx_cache_purge模塊的作用:用於清除指定url的緩存
下載地址:http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
Nginx.conf
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;
#access_log "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G" main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#設置緩存
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#設置緩存存儲路徑、存儲方式、分配內存大小、磁盤最大空間、緩存期限
#levels=1:2 表示緩存文件有兩級目錄 1表示第一級目錄名爲1位數,2表示第二級目錄名爲2位數
proxy_cache_path /var/data/cache/nginx/proxy_cache levels=1:2
#keys_zone 緩存區域名字,分配200m空間,最大緩存1g,有效期30天
keys_zone=http-cache:200m max_size=1g inactive=30d;
proxy_temp_path /var/data/cache/nginx/proxy_cache/tmp;
#設置 group1 的服務器
upstream fdfs_group1 {
server 192.168.150.181:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.150.18:80 weight=1 max_fails=2 fail_timeout=30s;
}
#設置 group2 的服務器
upstream fdfs_group2 {
server 192.168.150.191:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.150.192:80 weight=1 max_fails=2 fail_timeout=30s;
}
#gzip on;
listen 80;
#charset koi8-r;
#access_log logs/host.access.log main;
#access_log "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G" main;
server_name localhost;
location /group1/M00 {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
proxy_pass http://fdfs_group1;
expires 30d;
}
location /group2/M00 {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
proxy_pass http://fdfs_group2;
expires 30d;
}
#設置清除緩存的訪問權限
location ~/purge(/.*) {
allow 127.0.0.1;
allow 192.168.2.0/24;
deny all;
proxy_cache_purge http-cache $1$is_args$args;
}
purge命令清除靜態緩存
http://域名+purge+靜態資源相對路徑 來清除靜態資源緩存
添加Nginx模塊,保留原有模塊
查看nginx編譯安裝時的命令,安裝了哪些模塊
命令 /usr/local/nginx/sbin/nginx -V
編譯
./configure --prefix=/usr/local/tengine --add-module=/root/ngx_c --以前的模塊
ache_purge-2.3
make
注意make完 不要make install
拷貝obj下的 Nginx文件到 已安裝好的sbin目錄下 覆蓋即可
cp ./nginx /usr/local/tengine/sbin/
高可用
keepalived安裝
上傳並解壓keepalived-1.2.18.tar.gz
cd /root/
tar -zxf keepalived-1.2.18.tar.gz
編譯並安裝Keepalived
cd /root/keepalived-1.2.18
./configure --prefix=/usr/local/keepalived
make && make install
Keepalived安裝成Linux服務
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
設置開機啓動
chkconfig keepalived on
修改keepalived.conf
ts1
! Configuration File for keepalived
global_defs {
## keepalived 自帶的郵件提醒需要開啓 sendmail 服務。建議用獨立的監控或第三方 SMTP
router_id NK1 ## 標識本節點的字條串,通常爲 hostname
}
## keepalived 會定時執行腳本並對腳本執行的結果進行分析,動態調整 vrrp_instance 的優先級。如果腳本執行結果爲 0,並且 weight 配置的值大於 0,則優先級相應的增加。如果腳本執行結果非 0,並且 weight配置的值小於 0,則優先級相應的減少。其他情況,維持原本配置的優先級,即配置文件中 priority 對應的值。
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" ## 檢測 nginx 狀態的腳本路徑
interval 2 ## 檢測時間間隔
weight -20 ## 如果條件成立,權重-20
}
## 定義虛擬路由,VI_1 爲虛擬路由的標示符,自己定義名稱
vrrp_instance VI_1 {
state MASTER ## 主節點爲 MASTER,對應的備份節點爲 BACKUP
interface eth1 ## 綁定虛擬 IP 的網絡接口,與本機 IP 地址所在的網絡接口相同,我的是 eth1
virtual_router_id 51 ## 虛擬路由的 ID 號,兩個節點設置必須一樣,可選 IP 最後一段使用, 相同的 VRID 爲一個組,他將決定多播的 MAC 地址
mcast_src_ip 192.168.2.117 ## 本機 IP 地址
priority 100 ## 節點優先級,值範圍 0-254,MASTER 要比BACKUP 高
nopreempt ## 優先級高的設置 nopreempt 解決異常恢復後再次搶佔的問題
advert_int 1 ## 組播信息發送間隔,兩個節點設置必須一樣,默認 1s
## 設置驗證信息,兩個節點必須一致
authentication {
auth_type PASS
auth_pass 1111 ## 真實生產,按需求對應該過來
}
## 將 track_script 塊加入instance 配置塊
track_script {
chk_nginx ## 執行 Nginx 監控的服務
}
## 虛擬 IP 池, 兩個節點設置必須一樣
virtual_ipaddress {
192.168.150.138/24 dev eth0 label eth0:2
}
}
ts2
! Configuration File for keepalived
global_defs {
# notification_email {
# [email protected]
# [email protected]
# [email protected]
# }
# notification_email_from [email protected]
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
router_id ts2
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" ## 檢測 nginx 狀態的腳本路徑
interval 2 ## 檢測時間間隔
weight -20 ## 如果條件成立,權重-20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" ## 檢測 nginx 狀態的腳本路徑
router_id ts2
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" ## 檢測 nginx 狀態的腳本路徑
interval 2 ## 檢測時間間隔
weight -20 ## 如果條件成立,權重-20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
mcast_src_ip 192.168.150.132
track_script {
chk_nginx
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.150.138/24 dev eth0 label eth0:2
}
}
狀態檢查腳本
編寫nginx狀態檢查腳本/etc/keepalived/nginx_check.sh
在Keepalived配置中已用。腳本要求:如果 nginx 停止運行,嘗試啓動,如果無法啓動則殺死本機的 keepalived 進程, keepalied將虛擬 ip 綁定到 BACKUP 機器上。
vi /etc/keepalived/nginx_check.sh
內容
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
權限
chmod +x /etc/keepalived/nginx_check.sh
啓動
service keepalived start
下載文件
@RequestMapping("/down")
@ResponseBody
public ResponseEntity<byte[]> down(HttpServletResponse resp) {
DownloadByteArray cb = new DownloadByteArray();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "aaa.xx");
byte[] bs = fc.downloadFile("group1", "M00/00/00/wKiWDV0vAb-AcOaYABf1Yhcsfws9181.xx", cb);
return new ResponseEntity<>(bs,headers,HttpStatus.OK);
}