Swarm 服务版本更新|回滚

Docker Swarm 服务版本更新

**环境:**web

  • 系统:Centos 7.4 x64

  • 应用版本:Docker 18.09.0

  • 管理节点:192.168.1.79

  • 工做节点:192.168.1.78

  • 工做节点:192.168.1.77

一、管理节点:建立服务设置更新策略redis

docker service create \
--replicas 3 \
--name redis \
--update-delay 10s \
--update-parallelism 2 \
--update-failure-action continue \
redis:3.0.6

命令解析

# 建立命令
docker service create \
# 副本数
--replicas 3 \
# 服务名
--name redis \
# 设定容器间更新时间间隔
--update-delay 10s \
# 更新时同时并行更新数量,默认1
--update-parallelism 2 \
# 任务容器更新失败时的模式 continue为继续使用
--update-failure-action continue \
# 镜像
redis:3.0.6

查看添加服务

命令: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

查看启动状况

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

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状态,再继续往下更新其余容器任务,若是更新失败它会暂停更新,切换到下一个容器任务继续更新直到全部容器。ide

Docker Swarm 服务版本回滚

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

docker service create \
--name my_web \
--replicas 10 \
--rollback-parallelism 2 \
--rollback-monitor 20s \
--rollback-max-failure-ratio .2 \
nginx:1.12
# 建立服务
docker service create \
# 服务名
--name my_web \
# 副本数
--replicas 10 \
# 同时执行回滚数动做
--rollback-parallelism 2 \
#  每次容器与容器之间的回滚时间间隔
--rollback-monitor 20s \
# 回滚故障率若是小于百分比容许运行
--rollback-max-failure-ratio .2 \
# 使用镜像
nginx:1.12

查看建立服务

命令: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      

二、管理节点:更新服务提高一个版本 <参考上面版本更新>

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          

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

docker service update --rollback my_web

查看回滚版本

命令:docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
nikder7y06o2        my_web              replicated          10/10               nginx:1.12        

适合 master 一个版本,强制更新

docker service update --force service_name

适合版本更新

docker service update --image nginx:latest nginx
# 或者
docker stack deploy wordpress --compose-file=docker-compose.yml 

Last updated