1. 程式人生 > >Docker Swarm 服務版本更新與回滾

Docker Swarm 服務版本更新與回滾

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 service create \
# 副本數
--replicas 3 \
# 服務名
--name redis \
# 設定容器間更新時間間隔
--update-delay 10s \
# 更新時同時並行更新數量,預設1
--update-parallelism 2 \
# 任務容器更新失敗時的模式 continue為繼續使用
--update-failure-action continue \
# 映象
redis:3.0.6
命令解析
命令:docker service ls
ID            NAME   MODE        REPLICAS  IMAGE
v76p9bl941bk  redis  replicated  
3/3 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
檢視啟動情況

2、管理節點:手動更新服務版本

docker service update --image redis:3.0.7 redis
命令: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 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:[email protected]: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:[email protected]: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 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      
檢視建立服務

2、管理節點:更新服務提升一個版本

docker service update --image nginx:1.13 my_web
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 service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
nikder7y06o2        my_web              replicated          10/10               nginx:1.12        
查看回滾版本