Docker Swarm 服务版本更新

环境:

  • 系统:Centos 7.4 x64
  • 应用版本:Docker 18.09.0
  • 管理节点:192.168.1.79
  • 工作节点:192.168.1.78
  • 工作节点:192.168.1.77

1、管理节点:创建服务设置更新策略

docker service create \
--replicas 3 \
--name redis \
--update-delay 10s \
--update-parallelism 2 \
--update-failure-action continue \
redis:3.0.6
Docker Swarm 服务版本更新与回滚_3dDocker Swarm 服务版本更新与回滚_回滚_02
# 创建命令
docker service create \
# 副本数
--replicas 3 \
# 服务名
--name redis \
# 设定容器间更新时间间隔
--update-delay 10s \
# 更新时同时并行更新数量,默认1
--update-parallelism 2 \
# 任务容器更新失败时的模式 continue为继续使用
--update-failure-action continue \
# 镜像
redis:3.0.6
命令解析
Docker Swarm 服务版本更新与回滚_3dDocker Swarm 服务版本更新与回滚_回滚_02
命令:docker service ls
ID            NAME   MODE        REPLICAS  IMAGE
v76p9bl941bk  redis  replicated  3/3       redis:3.0.6
查看添加服务
Docker Swarm 服务版本更新与回滚_3dDocker Swarm 服务版本更新与回滚_回滚_02
命令:docker service ps redis
ID            NAME     IMAGE        NODE     DESIRED STATE  CURRENT STATE          ERROR  PORTS
2pruajw13gud  redis.1  redis:3.0.6  slave02  Running        Running 4 minutes ago         
ma8ait8xcagl  redis.2  redis:3.0.6  slave02  Running        Running 4 minutes ago         
igpwcuyumwzu  redis.3  redis:3.0.6  slave01  Running        Running 5 minutes ago
查看启动情况

2、管理节点:手动更新服务版本

docker service update --image redis:3.0.7 redis
Docker Swarm 服务版本更新与回滚_3dDocker Swarm 服务版本更新与回滚_回滚_02
命令:docker service ps redis
ID            NAME         IMAGE        NODE     DESIRED STATE  CURRENT STATE           ERROR  PORTS
tio9f89x7fzk  redis.1      redis:3.0.7  slave02  Running        Running 2 minutes ago          
2pruajw13gud   \_ redis.1  redis:3.0.6  slave02  Shutdown       Shutdown 2 minutes ago         
e3fuv9fm202x  redis.2      redis:3.0.7  slave01  Running        Running 2 minutes ago          
ma8ait8xcagl   \_ redis.2  redis:3.0.6  slave02  Shutdown       Shutdown 2 minutes ago         
zku5h3h4dqkn  redis.3      redis:3.0.7  slave01  Running        Running 2 minutes ago          
igpwcuyumwzu   \_ redis.3  redis:3.0.6  slave01  Shutdown       Shutdown 3 minutes ago
查看更新情况
Docker Swarm 服务版本更新与回滚_3dDocker Swarm 服务版本更新与回滚_回滚_02
docker service inspect redis
[
    {
        "ID": "v76p9bl941bk87pwwk3nvtwfe",
        "Version": {
            "Index": 1479
        },
        "CreatedAt": "2018-11-07T06:19:23.210686656Z",
        "UpdatedAt": "2018-11-07T06:30:53.565888582Z",
        "Spec": {
             # 服务名称
            "Name": "redis",
            "TaskTemplate": {
                "ContainerSpec": {
                    # 服务镜像
                    "Image": "redis:3.0.7@sha256:730b765df9fe96af414da64a2b67f3a5f70b8fd13a31e5096fee4807ed802e20",
                    "DNSConfig": {}
                },
                "Resources": {
                    "Limits": {},
                    "Reservations": {}
                },
                "RestartPolicy": {
                    "Condition": "any",
                    "MaxAttempts": 0
                },
                "Placement": {},
                "ForceUpdate": 0
            },
            "Mode": {
                # 模式
                "Replicated": {
                     # 副本数
                    "Replicas": 3
                }
            },
             # 服务更新时应用策略
            "UpdateConfig": {
                # 同时更新任务数
                "Parallelism": 1,
                 # 更新间隔时间
                "Delay": 10000000000,
                # 更新失败后动作 pause暂停
                "FailureAction": "pause",
                # 更新失败后继续运行半分比
                "MaxFailureRatio": 0
            },
            "EndpointSpec": {
                # 默认分配1个虚拟IP
                "Mode": "vip"
            }
        },
        "PreviousSpec": {
            "Name": "redis",
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "redis:3.0.6@sha256:6a692a76c2081888b589e26e6ec835743119fe453d67ecf03df7de5b73d69842",
                    "DNSConfig": {}
                },
                "Resources": {
                    "Limits": {},
                    "Reservations": {}
                },
                "RestartPolicy": {
                    "Condition": "any",
                    "MaxAttempts": 0
                },
                "Placement": {},
                "ForceUpdate": 0
            },
            "Mode": {
                "Replicated": {
                    "Replicas": 3
                }
            },
            "UpdateConfig": {
                "Parallelism": 1,
                "Delay": 10000000000,
                "FailureAction": "pause",
                "MaxFailureRatio": 0
            },
            "EndpointSpec": {
                "Mode": "vip"
            }
        },
        "Endpoint": {
            "Spec": {}
        },
        "UpdateStatus": {
            "State": "completed",
            "StartedAt": "2018-11-07T06:29:00.44192283Z",
            "CompletedAt": "2018-11-07T06:30:53.56587273Z",
            "Message": "update completed"
        }
    }
]
查看服务详细信息

注:升级过程,按顺序停止一个容器任务去对这个停止的任务进行更新,最后更新为running状态,再继续往下更新其他容器任务,如果更新失败它会暂停更新,切换到下一个容器任务继续更新直到所有容器。


Docker Swarm 服务版本回滚

1、管理节点:创建服务时设定回滚策略

docker service create \
--name my_web \
--replicas 10 \
--rollback-parallelism 2 \
--rollback-monitor 20s \
--rollback-max-failure-ratio .2 \
nginx:1.12
Docker Swarm 服务版本更新与回滚_3dDocker Swarm 服务版本更新与回滚_回滚_02
# 创建服务
docker service create \
# 服务名
--name my_web \
# 副本数
--replicas 10 \
# 同时执行回滚数动作
--rollback-parallelism 2 \
#  每次容器与容器之间的回滚时间间隔
--rollback-monitor 20s \
# 回滚故障率如果小于百分比允许运行
--rollback-max-failure-ratio .2 \
# 使用镜像
nginx:1.12
命令解析
Docker Swarm 服务版本更新与回滚_3dDocker Swarm 服务版本更新与回滚_回滚_02
命令:docker service ps -f "DESIRED-STATE=running" my_web
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
bgqpm1s0a24l        my_web.1            nginx:1.12          slave01             Running             Running about a minute ago                       
jttvdgzpiz02        my_web.2            nginx:1.12          slave01             Running             Running about a minute ago                       
t8yqfrzoa7fj        my_web.3            nginx:1.12          slave02             Running             Running about a minute ago                       
xgh712gi2eld        my_web.4            nginx:1.12          slave02             Running             Running about a minute ago                       
2tnw3tnofyn5        my_web.5            nginx:1.12          slave01             Running             Running about a minute ago                       
on4khcpkb2h7        my_web.6            nginx:1.12          slave02             Running             Running about a minute ago                       
gxdeqfbmq60k        my_web.7            nginx:1.12          slave01             Running             Running about a minute ago                       
kmyz0btgb0hk        my_web.8            nginx:1.12          slave02             Running             Running about a minute ago                       
lso2ry7f2v0o        my_web.9            nginx:1.12          slave01             Running             Running 2 minutes ago                            
ee21b2ud0mkk        my_web.10           nginx:1.12          slave02             Running             Running about a minute ago      
查看创建服务

2、管理节点:更新服务提升一个版本

docker service update --image nginx:1.13 my_web
Docker Swarm 服务版本更新与回滚_3dDocker Swarm 服务版本更新与回滚_回滚_02
docker service ps -f "DESIRED-STATE=running" my_web
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
bgqpm1s0a24l        my_web.1            nginx:1.13          slave01             Running             Running about a minute ago                       
jttvdgzpiz02        my_web.2            nginx:1.13          slave01             Running             Running about a minute ago                       
t8yqfrzoa7fj        my_web.3            nginx:1.13          slave02             Running             Running about a minute ago                       
xgh712gi2eld        my_web.4            nginx:1.13          slave02             Running             Running about a minute ago                       
2tnw3tnofyn5        my_web.5            nginx:1.13          slave01             Running             Running about a minute ago                       
on4khcpkb2h7        my_web.6            nginx:1.13          slave02             Running             Running about a minute ago                       
gxdeqfbmq60k        my_web.7            nginx:1.13          slave01             Running             Running about a minute ago                       
kmyz0btgb0hk        my_web.8            nginx:1.13          slave02             Running             Running about a minute ago                       
lso2ry7f2v0o        my_web.9            nginx:1.13          slave01             Running             Running 2 minutes ago                            
ee21b2ud0mkk        my_web.10           nginx:1.13          slave02             Running             Running about a minute ago          
查看更新服务

3、管理节点:手动回滚到上一个版本

docker service update --rollback my_web
Docker Swarm 服务版本更新与回滚_3dDocker Swarm 服务版本更新与回滚_回滚_02
命令:docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
nikder7y06o2        my_web              replicated          10/10               nginx:1.12        
查看回滚版本