在k8s中部署seaweedfs,上传文件到seaweedfs方法
在 Kubernetes 中部署 SeaweedFS 后,上传文件的方式取决于你部署的组件(如 Filer、S3 Gateway 等),以下是几种常用方法,可根据部署架构选择:
前提:确认 SeaweedFS 服务暴露方式
首先需确认 SeaweedFS 的核心服务(Filer 或 S3 Gateway)在 K8s 中是否已通过 Service 暴露(ClusterIP、NodePort、LoadBalancer 等),以便外部或集群内访问。
例如,若 Filer 部署为 StatefulSet,对应的 Service 可能类似:
kubectl get svc -n seaweedfs # 假设部署在 seaweedfs 命名空间
# 输出可能包含:seaweedfs-filer ClusterIP 10.96.xx.xx <none> 8888/TCP ...
方法 1:通过 Filer 的 HTTP API 上传(推荐)
SeaweedFS Filer 提供 HTTP 接口,支持直接上传文件,适合集群内 Pod 或外部通过 Service 访问。
步骤:
-
获取 Filer 访问地址
- 集群内访问:直接使用 Service 域名
seaweedfs-filer.seaweedfs.svc.cluster.local:8888(格式:<service-name>.<namespace>.svc.cluster.local:<port>)。 - 外部访问:若使用 NodePort,可通过
节点IP:NodePort访问(如192.168.xx.xx:30088,需提前在 Service 中配置nodePort: 30088)。
- 集群内访问:直接使用 Service 域名
-
上传文件(集群内/外均可)
使用curl或工具调用 HTTP API:# 示例:上传本地文件 test.txt 到 Filer 的 /data 目录下 curl -X POST -F "file=@test.txt" "http://<filer-address>/data/"- 上传成功后,可通过
curl http://<filer-address>/data/test.txt验证文件是否存在。
- 上传成功后,可通过
方法 2:通过 S3 Gateway 兼容接口上传
若部署了 SeaweedFS 的 S3 Gateway(兼容 AWS S3 API),可使用 s3cmd、aws cli 等工具上传,适合习惯 S3 操作的场景。
步骤:
-
部署并暴露 S3 Gateway
确保 S3 Gateway 已部署(通常作为 Deployment 或 StatefulSet),并通过 Service 暴露端口(默认 8333)。 -
配置 S3 客户端
使用aws cli时,需先配置访问密钥(SeaweedFS 默认可用空密钥,或在 Filer 中配置):aws configure --profile seaweedfs # 输入: # AWS Access Key ID: any (如 "AKIAEXAMPLE") # AWS Secret Access Key: any (如 "secret") # Default region name: us-east-1 (任意) # Default output format: json -
上传文件
# 示例:上传 test.txt 到名为 "mybucket" 的桶(需先创建桶) aws --profile seaweedfs --endpoint-url http://<s3-gateway-address>:8333 s3 mb s3://mybucket aws --profile seaweedfs --endpoint-url http://<s3-gateway-address>:8333 s3 cp test.txt s3://mybucket/
方法 3:通过 FUSE 挂载到 Pod 中上传
在 K8s Pod 中通过 FUSE 挂载 SeaweedFS 目录,直接像操作本地文件一样上传,适合应用需要直接读写文件系统的场景。
步骤:
-
在 Pod 中部署 FUSE 客户端
编写 Pod 配置(示例),通过seaweedfs mount命令挂载 Filer 目录:apiVersion: v1 kind: Pod metadata:name: seaweedfs-fuse-demo spec:containers:- name: fuse-clientimage: chrislusf/seaweedfs:latestcommand: ["/bin/sh", "-c"]args:- |seaweedfs mount -filer=seaweedfs-filer.seaweedfs.svc.cluster.local:8888 /mnt/seaweedsleep infinityvolumeMounts:- name: fuse-mountmountPath: /mnt/seaweedvolumes:- name: fuse-mountemptyDir: {}(注意:需确保容器有
privileged权限或CAP_SYS_ADMIN能力,否则 FUSE 挂载可能失败) -
进入 Pod 上传文件
kubectl exec -it seaweedfs-fuse-demo -- /bin/sh # 在容器内执行: echo "hello seaweedfs" > /mnt/seaweed/test.txt # 直接写入文件
方法 4:通过 Kubernetes 存储卷(PV/PVC)上传
若已通过 SeaweedFS CSI 驱动创建了 PV/PVC,可将卷挂载到 Pod 中,直接在 Pod 内读写文件(文件会自动存储到 SeaweedFS)。
步骤:
-
创建 PVC(使用 SeaweedFS 的 StorageClass)
apiVersion: v1 kind: PersistentVolumeClaim metadata:name: seaweedfs-pvc spec:accessModes:- ReadWriteOncestorageClassName: seaweedfs-storage # 对应 SeaweedFS 的 StorageClassresources:requests:storage: 1Gi -
部署 Pod 挂载 PVC
apiVersion: v1 kind: Pod metadata:name: seaweedfs-pvc-demo spec:containers:- name: appimage: busyboxcommand: ["sleep", "infinity"]volumeMounts:- name: seaweedfs-volumemountPath: /datavolumes:- name: seaweedfs-volumepersistentVolumeClaim:claimName: seaweedfs-pvc -
进入 Pod 上传文件
kubectl exec -it seaweedfs-pvc-demo -- /bin/sh echo "data from pvc" > /data/file.txt # 写入的文件会存储到 SeaweedFS
验证文件是否上传成功
- 通过 Filer 接口查看:
curl http://<filer-address>/dir/ls?path=/data(列出/data目录下的文件)。 - 通过 S3 客户端查看:
aws --endpoint-url <s3-address> s3 ls s3://mybucket。 - 在 FUSE 或 PVC 挂载的 Pod 中直接查看文件。
根据你的使用场景选择合适的方式:简单测试用 HTTP API,兼容 S3 生态用 S3 Gateway,应用直接读写用 FUSE 或 PVC 挂载。如果需要具体部署 YAML 或客户端配置细节,可以告诉我,我会进一步补充。
