提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
本人之前一直使用k8s部署集群,使用了6年,中途发生过各种问题,无论是网络、存储、堵塞、容器假死等等,虽然项目不是很大,一天百十万的请求量,但是运维难度有点大,非常耗时,又因k8s新版本不再支持docker,所以转换部署方式,使用docker swarm,但是存在一个问题,docker swarm不支持热更新,一更新服务就停止,经学习可使用一个应用双service的方式实现。
一、docker swarm是什么?
Docker Swarm是Docker 官方提供的一款集群管理工具,其主要作用是把若干台 Docker 宿主机抽象为一个整体,并通过一个入口统一管理这些 Docker 宿主机上的各种 Docker 资源。
Docker Swarm和Kubernetes 比较类似,但是它更加轻量级,且有的功能较 Kubernetes 少一些。有了Docker Swarm 集群,便可以实现应用的负载均衡与失败迁移功能。
二、实现方式
1.创建服务
service1.yml:
version: '3.4'
services:
server_1:
image: <你的镜像>:<镜像版本>
expose:
- "8080"
deploy:
replicas: 4
restart_policy:
condition: on-failure
environment:
TZ: Asia/Shanghai
configs:
- source: app-config
target: /app/resources/application.yml
- source: log-config
target: /app/resources/log4j.properties
networks:
- app
networks:
app:
external: true
configs:
app-config:
file: ./application.yml
log-config:
file: ./log4j.properties
service2.yml:
version: '3.4'
services:
server_2:
image: <你的镜像>:<镜像版本>
expose:
- "8080"
deploy:
replicas: 4
restart_policy:
condition: on-failure
environment:
TZ: Asia/Shanghai
configs:
- source: app-config-2
target: /app/resources/application.yml
- source: log-config-2
target: /app/resources/log4j.properties
networks:
- app
networks:
app:
external: true
configs:
app-config-2:
file: ./application.yml
log-config-2:
file: ./log4j.properties
2.创建服务
代码如下(xxx为空间名称):
docker stack deploy -c service1.yml xxx
docker stack deploy -c service2.yml xxx
3.热更新
代码如下:
docker service update --image <你的镜像>:<镜像版本> xxx_server_1
等服务1启动完成后再更新服务2:
docker service update --image <你的镜像>:<镜像版本> xxx_server_2
总结
提示:service为内网服务,还需要加一层nginx作为负载,即可实现热更新。
本人已使用这种方式成功运维两年,中途未发生过故障,相比k8s节省了不少时间!