目录
一、软件信息
二、构建fastdfs镜像
三、docker 启动fdfs服务
四、k8s部署fdfs服务
1、fdfs部署文件
五、外部服务访问
一、软件信息
-
fastdfs版本:fastdfs:V5.11
-
libfastcommon版本: V1.0.36
-
fastdfs-nginx-module版本:V1.20
-
nginx版本:1.18.0
二、构建fastdfs镜像
为了减少操作了文件数,我是直接把启动文件和dockerfile写一起了,也可以把启动脚本 start.sh 单独于dockerfile;然后在构建镜像的时候把启动文件 COPY 到镜像里。
以下是包括了启动文件为一起的dockerfile构建文件。直接使用该dockerfile即可完成镜像构建。然后按照自己的实际情况来修改配置文件和挂载,即可完成容器化运行fastdfs件存储服务。
FROM centos:7
LABEL creator=tudou date="2024-06-02"
ENV FASTDFS_PATH=/var/fastdfs \
TZ=Asia/Shanghai \
TRACKER_PORT= \
STORAGE_PORT= \
TRACKER_SERVER= \
GROUP_NAME=
#get all the dependences
RUN yum install -y git wget zip unzip gcc make \
perl openssl openssl-devel pcre pcre-devel zlib zlib-devel libevent libevent-devel \
&& yum clean all \
&& mkdir -p ${FASTDFS_PATH}/files
RUN git clone --branch V1.0.36 --depth 1 https://github.com/happyfish100/libfastcommon.git ${FASTDFS_PATH}/libfastcommon \
&& cd ${FASTDFS_PATH}/libfastcommon \
&& ./make.sh \
&& ./make.sh install \
&& rm -rf ${FASTDFS_PATH}/libfastcommon
RUN git clone --branch V5.11 --depth 1 https://github.com/happyfish100/fastdfs.git ${FASTDFS_PATH}/fastdfs \
&& cd ${FASTDFS_PATH}/fastdfs \
&& ./make.sh \
&& ./make.sh install \
&& cp -r conf/*.* /etc/fdfs/ \
&& rm -rf ${FASTDFS_PATH}/fastdfs
RUN git clone --branch V1.20 https://github.com/happyfish100/fastdfs-nginx-module.git /usr/local/fastdfs-nginx-module \
&& cp /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so \
&& sed -i "s@/usr/local/include@/usr/include/fastdfs /usr/include/fastcommon@g" /usr/local/fastdfs-nginx-module/src/config \
&& cd ${FASTDFS_PATH} \
&& wget https://nginx.org/download/nginx-1.18.0.tar.gz \
&& tar -xzvf nginx-1.18.0.tar.gz \
&& cd nginx-1.18.0 \
&& ./configure --prefix=/usr/local/nginx --add-module=/usr/local/fastdfs-nginx-module/src \
&& make \
&& make install \
&& cp /usr/local/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ \
&& rm -rf ${FASTDFS_PATH}/nginx-1.18.0*
WORKDIR ${FASTDFS_PATH}
# 启动脚本 start.sh
RUN cat > /usr/bin/start.sh <<-EOF
#!/bin/bash
cd \$(dirname \$0)
# start.sh trackerd
# start.sh storaged
# # 自定义环境变量初始化,若无自定义环境变量,则直接跳过使用配置文件的默认值
function init_config(){
if [[ -n \${STORAGE_PORT} ]];then
sed -i "s|^port=.*$|port=\${STORAGE_PORT}|g" /etc/fdfs/storage.conf
fi
if [[ -n \${TRACKER_PORT} ]];then
sed -i "s|^port=.*$|port=\${TRACKER_PORT}|g" /etc/fdfs/tracker.conf
fi
if [[ -n \${GROUP_NAME} ]];then
sed -i "s|group_name=.*$|group_name=\${GROUP_NAME}|g" /etc/fdfs/storage.conf
sed -i "s|group_name=.*$|group_name=\${GROUP_NAME}|g" /etc/fdfs/mod_fastdfs.conf
sed -i "s|url_have_group_name =.*$|url_have_group_name = true|g" /etc/fdfs/mod_fastdfs.conf
fi
if [[ -n \${TRACKER_SERVER} ]];then
sed -i "s|tracker_server=.*$|tracker_server=\${TRACKER_SERVER}|g" /etc/fdfs/storage.conf
sed -i "s|tracker_server=.*$|tracker_server=\${TRACKER_SERVER}|g" /etc/fdfs/client.conf
sed -i "s|tracker_server=.*$|tracker_server=\${TRACKER_SERVER}|g" /etc/fdfs/mod_fastdfs.conf
fi
sed -i "s|base_path=.*$|base_path=\${FASTDFS_PATH}|g" /etc/fdfs/tracker.conf
sed -i "s|base_path=.*$|base_path=\${FASTDFS_PATH}|g" /etc/fdfs/storage.conf
sed -i "s|base_path=.*$|base_path=\${FASTDFS_PATH}|g" /etc/fdfs/client.conf
sed -i "s|base_path=.*$|base_path=\${FASTDFS_PATH}|g" /etc/fdfs/mod_fastdfs.conf
}
init_config
case \$1 in
trackerd)
echo "start trackerd ..."
fdfs_trackerd /etc/fdfs/tracker.conf start && tail -f ${FASTDFS_PATH}/logs/trackerd.log
;;
storaged)
sed -i "s|store_path0.*$|store_path0=\${FASTDFS_PATH}/files|g" /etc/fdfs/storage.conf
sed -i "s|store_path0.*$|store_path0=\${FASTDFS_PATH}/files|g" /etc/fdfs/mod_fastdfs.conf
echo "start storaged ..."
fdfs_storaged /etc/fdfs/storage.conf start \
&& /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf \
&& tail -f ${FASTDFS_PATH}/logs/storaged.log
;;
*)
echo "USAGE sh $0 [ trackerd|storaged ]"
exit 1
;;
esac
EOF
# 启动脚本 start.sh
# 默认fastdfs端口
EXPOSE 22122 23000 8080 8888 80
RUN chmod a+x /usr/bin/start.sh
ENTRYPOINT ["/usr/bin/start.sh"]
CMD ["trackerd"]
#CMD ["storaged"]
# 构建 fdfs 应用镜像
# docker build -t fdfs:v1.0 -f dockerfile . --no-cache
docker build -t fdfs:v1.0 -f dockerfile .
三、docker 启动fdfs服务
# 容器启动 trackerd 服务,并且自定义环境变量值,可不指定环境变量值
docker run -itd --name trackerd -e TRACKER_PORT=22122 -e TRACKER_SERVER=192.168.159.132:22122 -p 22122:22122 fdfs:v1.0 trackerd
# 容器启动 storaged 服务,并且自定义环境变量值,可不指定环境变量值
docker run -itd --name storage -e STORAGE_PORT=23000 -e TRACKER_SERVER=192.168.159.132:22122 -p 23000:23000 fdfs:v1.0 storaged
# 分别查看容器应用的日志
docker logs trackerd
docker logs storage
四、k8s部署fdfs服务
1、fdfs部署文件
由于storage存储是各自独立的,所以要使用有状态部署。tracker是透明无状态的,可以使用无状态负载deploy部署,也可以使用有状态负载StatefulSet部署。
# kubectl apply -f fdfs.yaml -n fdfs
# fdfs 配置文件
---
kind: ConfigMap
apiVersion: v1
metadata:
name: fdfs-config
namespace: fdfs
annotations:
kubesphere.io/creator: admin
data:
mod_fastdfs.conf: |-
base_path=/var/fastdfs
tracker_server=tracker-svc:22122
storage_server_port=23000
group_name=group1
url_have_group_name = true
store_path_count=1
store_path0=/var/fastdfs/files
group_count = 1
# 在末尾增加组的具体信息
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/var/fastdfs/files
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/data/fastdfs
nginx.conf: |-
user root;
worker_processes 2;
events {
worker_connections 65535;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
charset utf-8;
server_tokens off;
location ~group[0-9]/M00 {
root /var/fastdfs/files/data;
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
storage.conf: |-
group_name=group1
port=23000
base_path=/var/fastdfs
store_path_count=1
store_path0=/var/fastdfs/files
tracker_server=tracker-svc:22122
http.server_port=80
tracker.conf: |-
disabled=false
port=22122
base_path=/var/fastdfs
http.server_port=80
---
# kubectl apply -f fdfs-trackerd.yaml -n fdfs
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: fdfs-trackerd
name: fdfs-trackerd
namespace: fdfs
spec:
replicas: 1
selector:
matchLabels:
app: fdfs-trackerd
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: fdfs-trackerd
spec:
containers:
- args:
- trackerd
command:
- /usr/bin/start.sh
image: swr.cn-south-1.myhuaweicloud.com/tudou/fastdfs:latest
imagePullPolicy: IfNotPresent
name: trackerd
ports:
- containerPort: 22122
name: http-0
protocol: TCP
- containerPort: 8080
name: http-1
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/fdfs/tracker.conf
name: volume-b8e30i
readOnly: true
subPath: tracker.conf
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
volumes:
- configMap:
defaultMode: 420
name: fdfs-config
name: volume-b8e30i
---
---
apiVersion: v1
kind: Service
metadata:
labels:
app: fdfs-trackerd
name: fdfs-trackerd
namespace: fdfs
spec:
ports:
- name: port-1
port: 22122
protocol: TCP
targetPort: 22122
- name: port-2
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: fdfs-trackerd
type: ClusterIP
---
# kubectl apply -f fdfs-storaged.yaml -n fdfs
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app: fdfs-storaged
name: fdfs-storaged
namespace: fdfs
spec:
podManagementPolicy: OrderedReady
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: fdfs-storaged
serviceName: fdfs-storaged
template:
metadata:
labels:
app: fdfs-storaged
spec:
containers:
- args:
- storaged
command:
- /usr/bin/start.sh
image: swr.cn-south-1.myhuaweicloud.com/tudou/fastdfs:latest
imagePullPolicy: IfNotPresent
name: storage
ports:
- containerPort: 23000
name: http-0
protocol: TCP
- containerPort: 8888
name: http-1
protocol: TCP
- containerPort: 80
name: http-2
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/fdfs/storage.conf
name: volume-ssmj62
readOnly: true
subPath: storage.conf
- mountPath: /etc/fdfs/mod_fastdfs.conf
name: volume-zlknkz
readOnly: true
subPath: mod_fastdfs.conf
- mountPath: /usr/local/nginx/conf/nginx.conf
name: volume-q4ylwb
readOnly: true
subPath: nginx.conf
- mountPath: /var/fastdfs
name: fdfs-storage-data
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
volumes:
- configMap:
defaultMode: 420
name: fdfs-config
name: volume-ssmj62
- configMap:
defaultMode: 420
name: fdfs-config
name: volume-zlknkz
- configMap:
defaultMode: 420
name: fdfs-config
name: volume-q4ylwb
updateStrategy:
rollingUpdate:
partition: 0
type: RollingUpdate
volumeClaimTemplates:
- apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: fdfs-storage-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 120Gi
storageClassName: local
volumeMode: Filesystem
---
---
apiVersion: v1
kind: Service
metadata:
labels:
app: fdfs-storaged
name: fdfs-storaged
namespace: fdfs
spec:
clusterIP: None
clusterIPs:
- None
internalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- name: http-0
port: 23000
protocol: TCP
targetPort: 23000
- name: http-1
port: 8888
protocol: TCP
targetPort: 8888
- name: http-2
port: 80
protocol: TCP
targetPort: 80
selector:
app: fdfs-storaged
sessionAffinity: None
type: ClusterIP
---
五、外部服务访问
当外部服务需要访问fdfs时,需要将服务的 22122和服务的 80 端口暴露。可以将这两个端口对应的服务创建NodePort类型的服务来实现端口暴露。