Debian Docker 高可用落地方案
一、总体架构与前提
二、在 Debian 上搭建 Docker Swarm 高可用集群
sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-releasecurl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpgecho "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/debian bookworm stable" | sudo tee /etc/apt/sources.list.d/docker.list > /devrefsudo apt-get update && sudo apt-get install -y docker-ce docker-ce-cli containerd.iosudo systemctl enable --now dockerdocker swarm init --advertise-addr <MANAGER-IP>docker swarm join --token <TOKEN> <MANAGER-IP>:2377docker network create --driver overlay --attachable my-overlayfirewall-cmd --permanent --add-port={2377/tcp,7946/tcp,7946/udp,4789/udp} && firewall-cmd --reloaddocker node ls、docker info | grep -i swarm 查看集群状态与节点角色。三、部署高可用服务与运维要点
docker service create --name nginx --replicas 3 --publish published=8080,target=80 --network my-overlay nginx:latestdocker service update --restart-condition any --restart-delay 5s --restart-max-attempts 3 nginxdocker service update --health-cmd "curl -f http://localhost || exit 1" --health-interval 5s --health-retries 3 --health-timeout 2s nginxdocker service update --update-parallelism 2 --update-delay 10s --image nginx:1.27 nginxdocker service scale nginx=5docker node update --availability drain node2docker node update --availability active node2docker swarm ca --rotatetar czvf swarm-backup.tar.gz /var/lib/docker/swarm/(轮换证书后再备份更安全)四、入口流量高可用与扩展方案
--publish published=8080,target=80 发布服务,Swarm 在所有可达管理节点暴露 8080 并做内部负载均衡,适合无状态服务;对外可配合 VIP + Keepalived 或云负载均衡实现入口高可用。五、数据持久化、监控与常见坑