Docker Swarm集群部署脚本需求:预生产集群复刻至生产环境
Hey there! I’ve helped plenty of teams automate Docker Swarm deployments for production, so let’s get your 30-node cluster (7 managers, 23 workers) set up without any manual docker swarm init or node join commands. Here’s a robust script-based approach tailored to your needs:
Prerequisites First
Before running the script, make sure these boxes are checked:
- All 30 servers have matching Docker versions installed (critical for compatibility).
- Network ports required for Swarm are open:
2377/tcp(cluster management),7946/tcp/udp(node communication),4789/udp(overlay networking). - You have a "control node" (can be one of your manager servers or a separate machine) that can SSH into all 30 servers without a password (set up SSH key-based auth first).
The Automation Script
Save this as deploy-swarm-cluster.sh, update the node lists with your actual server IPs/hostnames, then run it from your control node.
#!/bin/bash # -------------------------- # Configuration - EDIT THESE # -------------------------- # List of your 7 manager nodes (IP or hostname) MANAGER_NODES=( "prod-manager-01" "prod-manager-02" "prod-manager-03" "prod-manager-04" "prod-manager-05" "prod-manager-06" "prod-manager-07" ) # List of your 23 worker nodes (IP or hostname) WORKER_NODES=( "prod-worker-01" "prod-worker-02" # ... add the remaining 21 worker nodes here ) # -------------------------- # Swarm Initialization Logic # -------------------------- # Pick the first manager to initialize the swarm FIRST_MANAGER="${MANAGER_NODES[0]}" echo "🔧 Initializing Swarm cluster on $FIRST_MANAGER..." # Initialize swarm with the node's public/cluster-facing IP # (Adjust the awk command if your server uses a different primary interface) CLUSTER_IP=$(ssh "$FIRST_MANAGER" "hostname -I | awk '{print \$1}'") ssh "$FIRST_MANAGER" "docker swarm init --advertise-addr $CLUSTER_IP" # Fetch join tokens for managers and workers (quiet mode to get just the token) MANAGER_TOKEN=$(ssh "$FIRST_MANAGER" "docker swarm join-token manager -q") WORKER_TOKEN=$(ssh "$FIRST_MANAGER" "docker swarm join-token worker -q") SWARM_ENDPOINT="$CLUSTER_IP:2377" # -------------------------- # Add Remaining Managers # -------------------------- echo "🤝 Adding remaining manager nodes..." for MANAGER in "${MANAGER_NODES[@]:1}"; do echo "Joining $MANAGER as a manager..." ssh "$MANAGER" "docker swarm join --token $MANAGER_TOKEN $SWARM_ENDPOINT" done # -------------------------- # Add All Worker Nodes # -------------------------- echo "👷 Adding worker nodes..." for WORKER in "${WORKER_NODES[@]}"; do echo "Joining $WORKER as a worker..." ssh "$WORKER" "docker swarm join --token $WORKER_TOKEN $SWARM_ENDPOINT" done # -------------------------- # Verify Cluster Health # -------------------------- echo "✅ Cluster setup complete! Checking node status:" ssh "$FIRST_MANAGER" "docker node ls"
Key Notes & Enhancements
- Error Handling: For production, add checks (e.g.,
if [ $? -ne 0 ]) to exit the script if any step fails (like SSH connection issues or Docker commands failing). - Interface Specificity: If your servers have multiple network interfaces, replace
hostname -I | awk '{print $1}'with the exact IP you want Swarm to use (e.g.,10.0.0.10). - Security: Ensure your SSH keys are secured (don’t use a passphrase-less key on shared systems, or use an SSH agent).
- Idempotency: Add a check to see if a node is already part of a swarm before running
docker swarm join(usedocker info | grep -q 'Swarm: active').
Verify the Cluster
After running the script, you can double-check the cluster state by running docker node ls on any manager node—you should see all 7 managers and 23 workers listed with a Ready status.
内容的提问来源于stack exchange,提问作者Hruday
相关产品推荐
相关产品推荐

