docker API接口service update错误记录 error while removing network:…
一、问题 error while removing network:…
创建srvice的参数的时候,如果设置了network参数(接口POST:service/create)
[{ "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 3, "Command": [], "ContainerEnv": [], "PortConfig": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8081 } ], "Networks": [ { "Target": "chow", "Aliases": null, "DriverOpts": null } ], "UpdateConfig": { "Parallelism": 1, "Delay": 10000000000, "FailureAction": "", "Monitor": null, "MaxFailureRatio": 0, "Order": null }, "RollbackConfig": { "Parallelism": null, "Delay": null, "FailureAction": null, "Monitor": null, "MaxFailureRatio": 0, "Order": "" }, "RestartPolicy": { "Condition": null, "Delay": null, "MaxAttempts": null }, "WarehouseUrl": null, "WarehousePwd": null, "WarehouseUserName": null, "Mount": [] }]
而更新的时候,没有传network参数,会报错:error while removing network:…
更新service参数如下:(接口PUT:/service/update)
{ "ServiceId": "itnl2vrkixib9hlgtmsuj4awu", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2 }
同时,因为端口等其他参数也没传,造成更新之后,端口啥的都消失了,截图如下:
1. 创建service的时候
2. 更新service之后
二、分析错误原因
先不考虑端口等其他问题,先分析造成network错误的原因,上面是创建service的时候设置了network参数,下面对应几个更新service的时候传不同参数的结果
1. 更新的时候 network不变,相当于不更改network,(更改了副本数)
(1)只传部分参数,报错
{ // 更新的时候传的参数 "ServiceId": "9t2jucsc3t1i0t9bx2n7csmjf", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2, "Networks": [ { "Target": "chow", "Aliases": null, "DriverOpts": null } ] }
(2)、全量传参,不报错
{ "ServiceId": "i8cdkcbb4o9ly0nczqlgt5l3l", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2, "Command": [], "ContainerEnv": [], "PortConfig": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8082 } ], "Networks": [ { "Target": "chow", "Aliases": null, "DriverOpts": null } ], "UpdateConfig": { "Parallelism": 1, "Delay": 10000000000, "FailureAction": "", "Monitor": null, "MaxFailureRatio": 0, "Order": null }, "RollbackConfig": { "Parallelism": null, "Delay": 10000000000, "FailureAction": null, "Monitor": null, "MaxFailureRatio": 0, "Order": "" }, "RestartPolicy": { "Condition": null, "Delay": null, "MaxAttempts": null }, "WarehouseUrl": null, "WarehousePwd": null, "WarehouseUserName": null, "Mount": [] }
2. 更新的时候,不传network参数,相当于移除network
(1)部分传参,报错
{ "ServiceId": "tx8882z81lh834uenw4tcgt48", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2 }
(2)全量传参,不报错(参数太多,博客省略部分,实际传参的时候有)
{ "ServiceId": "uvknv24zb8q2iouoz9lv5v13n", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2, "Command": [], "ContainerEnv": [], "PortConfig": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8082 } ], ....省略 }
3. 更新的时候,传network,但是是添加network
(1)部分传参,报错
{ "ServiceId": "aqp2b7cmrh5egjezenuf0vx6r", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2, "Networks": [ { "Target": "chow", "Aliases": null, "DriverOpts": null }, { "Target": "my-net", "Aliases": null, "DriverOpts": null } ] }
(2)全量传参,不报错
{ "ServiceId": "bn0k9vxspi98m8qz9ceh14jc1", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2, "Command": [], "ContainerEnv": [], "PortConfig": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8082 } ], "Networks": [ { "Target": "chow", "Aliases": null, "DriverOpts": null }, { "Target": "my-net", "Aliases": null, "DriverOpts": null } ], ...省略 }
4. 更新的时候,传network,但是是移除一个network
(1)部分传参,报错
{ "ServiceId": "5k0nt8l45nbycxpzb0o5gzy38", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2, "Networks": [ { "Target": "chow", "Aliases": null, "DriverOpts": null } ] }
(2)全量传参,不报错
{ "ServiceId": "21ixmxc0xvh4s0t2jr1ke1ofl", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2, "Command": [], "ContainerEnv": [], "PortConfig": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 8082 } ], "Networks": [ { "Target": "chow", "Aliases": null, "DriverOpts": null } ], ...省略 }
总结以上发现,只要是只传部分参数,就会报错,但我又不能每次更新都让调用接口的人把所有的参数传过来,怎么办呢?
大量测试,从全量传参一个一个减少参数,看看到底是哪个参数不传造成的,经过测试发现必须要穿更新配置参数UpdateConfig,并且Parallelism参数(同时更新数)必须设置为1(个人猜测是docker service异步更新造成的?)
{ "ServiceId": "ldpoqhv2yufff7i7ulqutii9c", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2, "Networks": [ { "Target": "chow", "Aliases": null, "DriverOpts": null } ], "UpdateConfig": { "Parallelism": 1, "Delay": 10000000000, "FailureAction": "", "Monitor": null, "MaxFailureRatio": 0, "Order": null } }
既然找到原因了,那么设置成这个参数为非空必传就行了,但是有个问题,每次都要取查询上次的这个参数,比较麻烦,个人解决方案是再程序内部判断接口是否传这个参数
如果接口没传这个参数,内部逻辑代码是主动去查,用查到的填充
如果接口传了这个参数,内部逻辑代码是主动去查,用查到的数据判断有没有network配置,如果有network配置,再去判断传的参数是否设置Parallelism为1,如果不是,提醒接口调用者
最后又做了不等service 的副本容器启动起来,立即更新srvice,报错
5. 创建的时候,不全两传参且不设置network,更新的时候,完全没问题,即使更新的时候不穿UpdateConfig参数
service/create
[{ "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 3 }]
service/update
{ "ServiceId": "vi7y48jxaj05h2w0vjnpb3oi0", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2 }
6. 创建service的时候不全传,但参设置network,不设置UpdateConfig
1. 更新的时候不传network和UpdateConfig,报错
创建参数:
[{ "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 3, "Networks": [ { "Target": "chow", "Aliases": null, "DriverOpts": null }, { "Target": "my-net", "Aliases": null, "DriverOpts": null } ] }]
更新参数:
{ "ServiceId": "t7f637mq0e3cpczdtrrjm72kf", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2 }
2. 更新的时候不传network,传UpdateConfig,不报错
创建参数:
[{ "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 3, "Networks": [ { "Target": "chow", "Aliases": null, "DriverOpts": null }, { "Target": "my-net", "Aliases": null, "DriverOpts": null } ] }]
更新参数:
{ "ServiceId": "6vlve54skeq2odj5py4aign3f", "Image": "nginx", "ImageVersion": "1.17", "ServiceName": "test-chow", "Replicas": 2, "UpdateConfig": { "Parallelism": 1, "Delay": 10000000000, "FailureAction": "", "Monitor": null, "MaxFailureRatio": 0, "Order": null } }
三、结论:
经过测试(没有大量测试),目前发现的是
如果创建service的时候设置network,那么更新service的时候需要传参UpdateConfig参数
如果创建service的时候不设置network,那么更新service的时候,可以不穿UpdateConfig参数