如何将家用实验室单机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:
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:
This will output adocker swarm init --advertise-addr <MANAGER_NODE_PUBLIC_IP>docker swarm joincommand 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.
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.
Your current setup uses --network=host, which works in Swarm but has caveats for high availability. Below are two options:
Option A: Switch to Overlay Network (Recommended for True HA)
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:
- Create an overlay network first:
docker network create --driver overlay --attachable app_overlay - Deploy your Plex service (adjust ports/envs to match your original setup):
Thedocker 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--replicas 2flag 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
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.
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 plexagain—you should see Swarm scheduling a replacement on a healthy node.
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




