You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何将家用实验室单机Docker部署迁移至Swarm模式实现高可用?

Hey there! Let's walk through migrating your existing single-node Docker container setup to Docker Swarm for high availability—here's a step-by-step breakdown tailored to your current setup:

1. Initialize Your Docker Swarm Cluster

First, you'll need to set up the Swarm control plane. Pick one host to act as the initial manager node, then add your other hosts as either workers or additional managers (for Swarm itself to be highly available, aim for 3 manager nodes total—odd number prevents split-brain scenarios).

  • On your chosen manager node:
    docker swarm init --advertise-addr <MANAGER_NODE_PUBLIC_IP>
    
    This will output a docker swarm join command for worker nodes. To get the join token for additional managers, run:
    docker swarm join-token manager
    
  • On all other hosts, run the appropriate join command from the manager node to add them to the cluster.
2. Adapt NFS Shared Storage for Swarm

Your existing NFS setup works for single nodes, but Swarm needs shared storage accessible across all nodes via Docker volumes. Create a reusable NFS-backed volume that any service can mount:

docker volume create \
  --driver local \
  --opt type=nfs \
  --opt o=addr=<YOUR_NFS_SERVER_IP>,rw \
  --opt device=:/path/to/your/nfs/share \
  shared_configs

This volume will let Swarm services access your Plex configs, media, etc., regardless of which node they're scheduled on.

3. Migrate Containers to Swarm Services

Your current setup uses --network=host, which works in Swarm but has caveats for high availability. Below are two options:

Overlay networks are Swarm-native and let services communicate across nodes while keeping port management flexible. This is the best choice for full high availability:

  1. Create an overlay network first:
    docker network create --driver overlay --attachable app_overlay
    
  2. Deploy your Plex service (adjust ports/envs to match your original setup):
    docker service create \
      --name plex \
      --network app_overlay \
      --publish 32400:32400/tcp \
      --publish 3005:3005/tcp \
      --publish 8324:8324/tcp \
      --publish 32469:32469/tcp \
      --publish 1900:1900/udp \
      --publish 32410:32410/udp \
      --replicas 2 \
      --env TZ="<Your/Timezone>" \
      --env PLEX_CLAIM="<YourClaimToken>" \
      --mount source=shared_configs,target=/config \
      --mount source=shared_configs,target=/transcode \
      plexinc/pms-docker
    
    The --replicas 2 flag tells Swarm to run two instances across your cluster—if one node goes down, Swarm will spin up a replacement on a healthy node.

Option B: Keep Host Network (Limited HA)

If you rely on host network for features like DLNA broadcast, you can still use it, but note that host-networked services can't be load-balanced across nodes easily. Use a global service (runs one instance per node) or constrain to specific nodes:

docker service create \
  --name plex \
  --network host \
  --mode global \
  --env TZ="<Your/Timezone>" \
  --env PLEX_CLAIM="<YourClaimToken>" \
  --mount source=shared_configs,target=/config \
  --mount source=shared_configs,target=/transcode \
  plexinc/pms-docker

Or to limit to specific nodes, first label nodes:

docker node update --label-add plex_host=true <NODE_NAME>

Then deploy with constraints:

docker service create \
  --name plex \
  --network host \
  --constraint node.labels.plex_host=true \
  --replicas 2 \
  # ... rest of your env/mount flags
4. Secure Swarm Manager High Availability

As mentioned earlier, having 3 manager nodes ensures the Swarm control plane stays operational if one manager fails. Make sure you've added at least two additional managers to the cluster using the join-token manager command from step 1.

5. Verify and Test HA

Once your services are deployed, validate everything works:

  • Check service status: docker service ls
  • See where instances are running: docker service ps plex
  • Test failover: Stop a node running a Plex instance, then run docker service ps plex again—you should see Swarm scheduling a replacement on a healthy node.
6. Clean Up Old Single-Node Containers

Once you confirm the Swarm deployment is stable, you can remove your original single-node containers:

docker stop plex
docker rm plex

内容的提问来源于stack exchange,提问作者Rob Berry

火山引擎 最新活动