部署集群 (*)

架构
Seafile 集群解决方案采用三层架构:
  • 负载均衡器层:将传入流量分配到 Seafile 服务器。通过部署多个负载均衡器实例可以实现高可用。
  • Seafile 服务器集群:由多个 Seafile 服务器实例组成的集群。如果一个实例出现故障,负载均衡器将停止向其分配流量,实现高可用。
  • 后端存储:分布式存储集群,例如 S3、Openstack Swift 或 Ceph。
这种架构可以水平扩展,你可以通过增加更多的机器来处理更多的流量。该架构在下图中进行了可视化展示。
Seafile 服务器节点上有两个主要组件:Web 服务器(Nginx/Apache)和 Seafile 应用服务器。Web 服务器将客户端的请求传递给 Seafile 应用服务器,Seafile 应用服务器独立工作,它们不知道彼此的状态。这意味着一个应用服务器出现故障后不会影响其他应用服务器实例,负载均衡器负责检测故障并重新路由请求。
尽管 Seafile 应用服务器独立工作,但它们仍需共享一些会话信息,所有共享的会话信息都存储在内存缓存中。因此,所有 Seafile 应用服务器都必须连接到同一个内存缓存服务器/集群。 在 11.0 之前,仅支持 memcached, 自专业版 11.0 起,memcached 和 Redis 均可作为内存缓存使用。有关内存缓存配置的更多详细信息将在后面提供。
后端服务器是各种后台任务的主力军,包括全文索引、办公文件预览、病毒扫描、LDAP 同步等。为了获得更好的性能,它通常应该服务器专用。目前,整个集群中只能运行一个后台任务服务器。如果有多个后台服务器在运行,它们在执行某些任务时可能会相互冲突。如果您需要为后台任务服务器提供高可用性(HA),可以考虑使用 Keepalived 为其构建热备份。
在 Seafile 集群中, 只有一台服务器 运行后台任务,包括:
  • 生成索引
  • 邮件通知
  • LDAP同步
  • 病毒扫描
假设你的集群中有三个节点:A、B 和 C。
  • 节点 A 是运行后台任务的后端节点。
  • 节点 B 和 C 是前端节点,为来自客户端的请求提供服务。
环境
自 11.0 版本起,Redis 也可用作缓存服务器。但目前仅支持单节点 Redis。
  • 我们假设你已经在不同的机器上部署了内存缓存服务器(例如 Memcached)、MariaDB、文件索引服务(例如 ElasticSearch、SeaSearch),并使用类似 S3 的对象存储。
  • 通常,Seafile 集群的每个节点应至少有 2 核 2G 内存 。如果上述服务与 Seafile 集群中的一个节点一起部署,特别是 ElasticSearch ,我们建议该节点配置为 4 核 4G 内存
Seafile 服务器:2个前端节点,1个后端节点。
有关节点数量的更多详细信息
  1. 如果你的节点数量不符合我们的推荐数量(即 3 个节点),请按照以下策略进行调整:
  • 2 个节点:前端服务和后端服务在同一个节点上
  • 1 个节点:请参考在单个节点而非集群中部署 Seafile。
2. 如果您有更多可用的 Seafile 服务器节点,请将它们提供给 Seafile 前端服务,并确保只有一个后端服务在运行。这里是 Seafile 前端服务数量( Nf )和总节点数(Nt)之间的一个简单关系:
Nf = Nt - 1,其中 Nf 是指 Seafile 前端服务的数量,Nt 是指总节点数,1 表示有一个节点用于 Seafile 后端服务。
部署 Seafile 服务
部署第一个 Seafile 前端节点
  1. 创建挂载目录
            
                
                
mkdir -p /opt/seafile/shared
2. 拉取seafile镜像
自 12.0 版本起,Seafile 专业版版本托管在 DockerHub 上,下载时不需要用户名和密码。
            
                
                
#12.0
docker pull seafileltd/seafile-pro-mc:12.0-latest

#13.0
docker pull seafileltd/seafile-pro-mc:13.0-latest
3. 下载文件 seafile-server.yml   和 .env
            
                
                
#12.0
wget -O .env https://manual.seafile.com/12.0/repo/docker/cluster/env
wget https://manual.seafile.com/12.0/repo/docker/cluster/seafile-server.yml

#13.0
wget -O .env https://manual.seafile.com/13.0/repo/docker/cluster/env
wget https://manual.seafile.com/13.0/repo/docker/cluster/seafile-server.yml
4. 修改 .env 中的 变量 (特别是类似 <...> 这样的项)
如果你已经部署了 S3 存储后端,并计划将其应用于 Seafile 集群,您可以修改 .env 中的变量 设置
5. 添加license许可
如果你有一个   seafile-license.txt 许可文件,只需将其放入 Seafile 容器的卷中。在 Compose 文件中,该卷的默认路径是     /opt/seafile/shared 。如果你修改了路径,请将许可证文件保存到自定义的路径下。
注意: 如果许可证文件名称不是 seafile-license.txt 或无法读取,Seafile 服务器将以试用模式启动,最多支持三个用户。
6. 启动seafile容器
            
                
                
docker compose up -d
因为在 .env 文件中 CLUSTER_INIT_MODE 为 true,所以 Seafile Docker 将以初始化模式启动并生成配置文件。因此,如果跟踪 Seafile 容器(即 docker logs seafile ),可以看到以下内容:
            
                
                
---------------------------------
This is your configuration
---------------------------------

    server name: seafile
    server ip/domain: seafile.example.com

    seafile data dir: /opt/seafile/seafile-data
    fileserver port: 8082

    database: create new
    ccnet database: ccnet_db
    seafile database: seafile_db
    seahub database: seahub_db
    database user: seafile


Generating seafile configuration ...

done
Generating seahub configuration ...


-----------------------------------------------------------------
Your seafile server configuration has been finished successfully.
-----------------------------------------------------------------


[2024-11-21 02:22:37] Updating version stamp
Start init

Init success
7. 在初始化模式下,服务将不会启动。在此期间,可以在配置文件中检查生成的配置文件(例如,MySQL、Memcached、Elasticsearch)。
8. 对于Seafile pro 12.0版本,集群初始化完成后,可以在 .env 中删除以下字段。
  • CLUSTER_INIT_MODE , 务必要从.env文件中删除
  • CLUSTER_INIT_MEMCACHED_HOST
  • CLUSTER_INIT_ES_HOST
  • CLUSTER_INIT_ES_PORT
  • INIT_S3_STORAGE_BACKEND_CONFIG
  • INIT_S3_COMMIT_BUCKET
  • INIT_S3_FS_BUCKET
  • INIT_S3_BLOCK_BUCKET
  • INIT_S3_KEY_ID
  • INIT_S3_SECRET_KEY
对于Seafile pro 13.0版本,集群初始化完成后, 可以在 .env 中删除以下字段。
  • CLUSTER_INIT_MODE , 务必要从.env文件中删除
  • CLUSTER_INIT_ES_HOST
  • CLUSTER_INIT_ES_PORT
我们建议你在服务正式启动之前检查相关配置文件是否正确,并复制 SEAFILE_VOLUME 目录,因为只有在初始化后才会生成配置文件。你可以使用以下命令在以后直接将整个复制的 SEAFILE_VOLUME 迁移到其他节点。
            
                
                
cp -r /opt/seafile/shared /opt/seafile/shared-bak
9. 重启容器,启动前端节点服务
            
                
                
docker compose down
docker compose up -d
执行上述命令后,你可以跟踪容器 seafile (即 docker logs seafile )的日志。如果前端节点启动成功,你可以看到以下消息:
            
                
                
*** Running /etc/my_init.d/01_create_data_links.sh...
*** Booting runit daemon...
*** Runit started as PID 20
*** Running /scripts/enterpoint.sh...
2024-11-21 03:02:35 Nginx ready

2024-11-21 03:02:35 This is an idle script (infinite loop) to keep container running.
---------------------------------

Seafile cluster frontend mode

---------------------------------


Starting seafile server, please wait ...
Seafile server started

Done.

Starting seahub at port 8000 ...

Seahub is started

Done.
注意: 头像默认使用数据库存储,默认会将此配置初始化到seahub_settings.py中
            
                
                
#指定头像存储到数据库(默认存储到本地seahub-data目录)
AVATAR_FILE_STORAGE = 'seahub.base.database_storage.DatabaseStorage'
您也可以选择将头像存储在本地目录,这种情况下seahub-data要在seafile服务器间做共享。 避免出现访问到不同节点时,头像访问不到或不一致的问题。
部署其他Seafile 前端节点
  1. 创建挂载目录
            
                
                
mkdir -p /opt/seafile/shared
2. 拉取seafile镜像
3. 从第一个前端节点复制 seafile-server.yml .env 和配置文件。
4. 启动服务
            
                
                
docker compose up -d
部署Seafile 后端节点
  1. 创建挂载目录
            
                
                
mkdir -p /opt/seafile/shared
2. 拉取seafile镜像
3. 从前端节点复制 seafile-server.yml .env 和配置文件
注意:从前端节点复制的配置文件必须与前端节点一样,放在相同的路径中,即 /opt/seafile/shared/seafile/conf/*
4. 修改 .env 文件,将 CLUSTER_MODE 设置为 backend
5. 启动后端节点服务
            
                
                
docker compose up -d
执行上述命令后,可以跟踪容器 seafile (即 docker logs seafile )的日志。如果后端节点启动成功,将看到以下消息:
            
                
                
After executing the above command, you can trace the logs of container seafile (i.e., docker logs seafile). You can see the following message if the backend node starts successfully:

*** Running /etc/my_init.d/01_create_data_links.sh...
*** Booting runit daemon...
*** Runit started as PID 21
*** Running /scripts/enterpoint.sh...
2024-11-21 03:11:59 Nginx ready
2024-11-21 03:11:59 This is an idle script (infinite loop) to keep container running.

---------------------------------

Seafile cluster backend mode

---------------------------------


Starting seafile server, please wait ...
Seafile server started

Done.

Starting seafile background tasks ...
Done.
部署负载均衡服务(可选)
自 Seafile Pro 6.0.0 版本起,集群部署需要在负载均衡器中设置 “ sticky session ”。否则,有时在 Web 下载文件夹可能无法正常工作。有关详细信息,请阅读下面的 “负载均衡器设置” 部分。
一般来说,为了更好地访问 Seafile 服务,我们建议使用负载均衡服务来访问 Seafile 集群,并将域名(例如 seafile.cluster.com )绑定到负载均衡服务。通常,你可以使用:
  • 云服务提供商的负载均衡服务(例如:AWS 弹性负载均衡器)。
  • 部署你自己的负载均衡服务,我们的文档将给出两种常见的负载均衡服务:
    • Nginx
    • HAproxy
AWS 负载均衡器(ELB)
在 AWS 负载均衡(ELB)管理控制台中,将 Seafile 服务器实例添加到实例列表后,你应该再进行两项配置。
首先,你应该设置 HTTP (S) 监听器。将 ELB 的 443 端口和 80 端口转发到 Seafile 服务器的 80 端口或 443 端口。
然后开启设置健康检查。
参考 AWS文档 了解如何设置 sticky session
Nginx
  1. 如果你想部署负载均衡服务,先要在主机中安装 Nginx
            
                
                
sudo apt update
sudo apt install nginx
2. 为 Seafile 集群创建配置文件
            
                
                
sudo nano /etc/nginx/sites-available/seafile-cluster
然后,将以下内容添加到文件中:
            
                
                
upstream seafile_cluster {
    server <IP: your frontend node 1>:80;
    server <IP: your frontend node 2>:80;
    ...
}

server {
    listen 80;
    server_name <your domain>;

    location / {
        proxy_pass http://seafile_cluster;
        proxy_set_header Host $host;
        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;
        http_502 http_503 http_504;
    }
}
3. 将配置文件链接到 sites-enabled 目录:
            
                
                
sudo ln -s /etc/nginx/sites-available/seafile-cluster /etc/nginx/sites-enabled/
4. 测试启用配置
            
                
                
sudo nginx -t
sudo nginx -s reload
HAproxy 和 Keepalived 服务
在两台 Seafile 前端服务器上执行以下命令:
            
                
                
apt install haproxy keepalived -y

mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

cat > /etc/haproxy/haproxy.cfg << 'EOF'
global
     log 127.0.0.1 local1 notice
     maxconn 4096
     user haproxy
     group haproxy

defaults
     log global
     mode http
     retries 3
     timeout connect 10000
     timeout client 300000
     timeout server 36000000

listen seafile 0.0.0.0:80
     mode http
     option httplog
     option dontlognull
     option forwardfor
     cookie SERVERID insert indirect nocache
     server seafile01 Front-End01-IP:8001 check port 11001 cookie seafile01
     server seafile02 Front-End02-IP:8001 check port 11001 cookie seafile02
EOF
注意: 请正确修改上述配置文件中前端服务器的 IP 地址(“Front-End01-IP” 和 “Front-End02-IP”),否则它将无法正常工作。
从上述两台服务器中选择一台作为主节点,另一台作为从节点。
在主节点上执行以下操作:
            
                
                
cat > /etc/keepalived/keepalived.conf << 'EOF'
! Configuration File for keepalived

global_defs {
     notification_email {
         root@localhost
     }
     notification_email_from keepalived@localhost
     smtp_server 127.0.0.1
     smtp_connect_timeout 30
     router_id node1
     vrrp_mcast_group4 224.0.100.18
}

vrrp_instance VI_1 {
     state MASTER
     interface eno1 # Set to the device name of a valid network interface on the current server, and the virtual IP will be bound to the network interface
     virtual_router_id 50
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass seafile123
     }
     virtual_ipaddress {
         172.26.154.45/24 dev eno1 # Configure to the correct virtual IP and network interface device name
     }
}
EOF
注意:请正确配置上述文件中的虚拟 IP 地址和网络接口设备名称,否则将无法正常工作。
在备用节点上执行以下操作:
            
                
                
cat > /etc/keepalived/keepalived.conf << 'EOF'
! Configuration File for keepalived

global_defs {
     notification_email {
         root@localhost
     }
     notification_email_from keepalived@localhost
     smtp_server 127.0.0.1
     smtp_connect_timeout 30
     router_id node2
     vrrp_mcast_group4 224.0.100.18
}

vrrp_instance VI_1 {
     state BACKUP
     interface eno1 # Set to the device name of a valid network interface on the current server, and the virtual IP will be bound to the network interface
     virtual_router_id 50
     priority 98
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass seafile123
     }
     virtual_ipaddress {
         172.26.154.45/24 dev eno1 # Configure to the correct virtual IP and network interface device name
     }
}
EOF
最后,在两台 Seafile 前端服务器上运行以下命令以启动相应的服务:
            
                
                
systemctl enable --now haproxy
systemctl enable --now keepalived
到目前为止,Seafile 集群已部署完成。
HTTPS
你可以在负载均衡服务中启用 HTTPS,因为你可以使用证书管理器(例如 Certbot )为你的 Seafile 集群获取并启用 HTTPS。在启用 HTTPS 后,你必须将 seahub_settings.py  和   .env 中的相对 URL 从前缀 http:// 修改为 https://。
部署SeaDoc服务器(可选)
你可以点击 这里 来部署 SeaDoc 服务器。然后在你的 .env 文件中修改 SEADOC_SERVER_URL