-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathACS-CS-DedicatedMigration.json
354 lines (354 loc) · 14.6 KB
/
ACS-CS-DedicatedMigration.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
{
"FormatVersion": "OOS-2019-06-01",
"Description": {
"en": "Sleep control plane, make etcd snapshot and upload it to oss bucket",
"zh-cn": "ACK专属版master休眠&etcd备份上传",
"name-en": "ACS-CS-DedicatedMigration",
"name-zh-cn": "ACK专属版master休眠&etcd备份上传",
"categories": [
"others"
]
},
"Parameters": {
"regionId": {
"Type": "String",
"Label": {
"en": "RegionId",
"zh-cn": "地域ID"
},
"AssociationProperty": "RegionId",
"Default": "{{ ACS::RegionId }}"
},
"workingDir": {
"Label": {
"en": "WorkingDir",
"zh-cn": "ECS实例中运行命令的目录"
},
"Type": "String",
"Default": "/root"
},
"rateControl": {
"Label": {
"en": "RateControl",
"zh-cn": "任务执行的并发比率"
},
"Type": "Json",
"AssociationProperty": "RateControl",
"Default": {
"Mode": "Concurrency",
"MaxErrors": 0,
"Concurrency": 5
}
},
"targets": {
"Label": {
"en": "TargetInstance",
"zh-cn": "目标实例"
},
"Type": "Json",
"AssociationProperty": "Targets",
"AssociationPropertyMetadata": {
"ResourceType": "ALIYUN::ECS::Instance",
"RegionId": "regionId"
}
},
"action": {
"Type": "String",
"Label": {
"en": "Action",
"zh-cn": "配置方式"
},
"Default": "rollback",
"AllowedValues": [
"migrate",
"rollback"
]
},
"OOSAssumeRole": {
"Label": {
"en": "OOSAssumeRole",
"zh-cn": "OOS扮演的RAM角色"
},
"Type": "String",
"Default": ""
},
"BucketName": {
"Label": {
"en": "BucketName",
"zh-cn": "需要上传snapshot的oss路径"
},
"Type": "String"
},
"OSSEndpoint": {
"Label": {
"en": "OSSEndpoint",
"zh-cn": "需要上传snapshot的oss对应的endpoint"
},
"Type": "String"
},
"ClusterID": {
"Label": {
"en": "ClusterID",
"zh-cn": "集群的ID"
},
"Type": "String"
}
},
"RamRole": "{{ OOSAssumeRole }}",
"Tasks": [
{
"Name": "getInstance",
"Description": {
"en": "Views the ECS instances",
"zh-cn": "获取ECS实例"
},
"Action": "ACS::SelectTargets",
"Properties": {
"ResourceType": "ALIYUN::ECS::Instance",
"RegionId": "{{ regionId }}",
"Filters": [
"{{ targets }}"
]
},
"Outputs": {
"instanceIds": {
"Type": "List",
"ValueSelector": "Instances.Instance[].InstanceId"
}
}
},
{
"Action": "ACS::ECS::RunCommand",
"OnError": "rollback",
"Description": {
"en": "Execute cloud assistant command",
"zh-cn": "执行云助手命令"
},
"Properties": {
"regionId": "{{ regionId }}",
"commandContent": "#!/bin/bash\nset -e\nif [ \"{{action}}\" = \"migrate\" ]; then\n mkdir -p /etc/kubernetes/manifests.backup\n if_move=$(ls /etc/kubernetes/manifests/ | wc -l)\n if [ \"$if_move\" != \"0\" ]; then\n mv -f /etc/kubernetes/manifests/* /etc/kubernetes/manifests.backup/\n fi\n is_ok=0\n set +e\n ps -o cmd -p `pidof kubelet` | grep 'container-runtime-endpoint=/var/run/containerd/containerd.sock'\n if [ $? -ne 0 ]; then\n echo \"容器运行时不为containerd\"\n for ((integer = 0; integer < 150; integer++)); do\n count=$(docker ps | grep kube-apiserver | wc -l)\n if [ \"$count\" = \"0\" ]; then\n is_ok=1\n break\n else\n sleep 2\n fi\n done\n else\n echo \"容器运行时为containerd\"\n for ((integer = 0; integer < 150; integer++)); do\n count=$(crictl --runtime-endpoint /var/run/containerd/containerd.sock ps |grep kube-apiserver | wc -l)\n if [ \"$count\" = \"0\" ]; then\n is_ok=1\n break\n else\n sleep 2\n fi\n done\n fi\n set -e\n if [ \"$is_ok\" == \"0\" ]; then\n mv -f /etc/kubernetes/manifests.backup/* /etc/kubernetes/manifests/\n echo \"Rollback finish\"\n exit 1\n else\n echo \"The control plane is sleeping now.\"\n fi\nelif [ \"{{action}}\" = \"rollback\" ]; then\n mkdir -p /etc/kubernetes/manifests.backup\n if_move=$(ls /etc/kubernetes/manifests.backup/ | wc -l)\n if [ \"$if_move\" != \"0\" ]; then\n mv -f /etc/kubernetes/manifests.backup/* /etc/kubernetes/manifests/\n fi\n echo \"The control plane is wakeup now.\"\nelse\n echo \"action must be migrate or rollback\"\n exit 1\nfi",
"instanceId": "{{ ACS::TaskLoopItem }}",
"commandType": "RunShellScript",
"workingDir": "{{ workingDir }}",
"timeout": 240
},
"Loop": {
"Items": "{{ getInstance.instanceIds }}",
"RateControl": "{{ rateControl }}",
"Outputs": {
"commandOutputs": {
"AggregateType": "Fn::ListJoin",
"AggregateField": "commandOutput"
}
}
},
"Outputs": {
"commandOutput": {
"ValueSelector": "invocationOutput",
"Type": "String"
}
},
"Name": "sleepOrWakeupControlPlane"
},
{
"Action": "ACS::ECS::RunCommand",
"OnError": "rollback",
"Description": {
"en": "Execute cloud assistant command",
"zh-cn": "执行云助手命令"
},
"Properties": {
"regionId": "{{ regionId }}",
"commandContent": "#!/bin/bash\nset -e\nif [ \"{{action}}\" = \"rollback\" ]; then\n exit 0\nfi\n# 获取eth0 IP\nIP=$(/sbin/ifconfig eth0 | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d \"addr:\")\nENDPOINT=\"https://$IP:2379\"\necho \"ENDPOINT: \"$ENDPOINT\nset +e\n# 查询etcd endpoints status,判断该etcd是否为leader\nETCDCTL_API=3 /usr/bin/etcdctl --cacert=/var/lib/etcd/cert/ca.pem --cert=/var/lib/etcd/cert/etcd-server.pem --key=/var/lib/etcd/cert/etcd-server-key.pem --endpoints=$ENDPOINT endpoint status | grep true\nif [ $? -ne 0 ]; then\n echo \"不为etcd leader所在机器,退出。\"\n exit 0\nfi\nset -e\nyum install curl wget jq -y\nif [ ! -f \"/tmp/ossutil64\" ]; then\n # wget ossutil,保存到/tmp/目录下\n wget -c -t 10 -O /tmp/ossutil64 https://oos-public-{{regionId}}.oss-{{regionId}}-internal.aliyuncs.com/x64/ossutil64\n if [ $? -ne 0 ]; then\n echo \"下载ossutil工具,退出。\"\n exit 1\n fi\n chmod +x /tmp/ossutil64\nfi\nif [ ! -f \"/tmp/modify-prefix-v2\" ]; then\n echo \"下载modify-prefix-v2\"\n wget -c -t 10 -O /tmp/modify-prefix-v2 https://aliacs-k8s-{{regionId}}.oss-{{regionId}}-internal.aliyuncs.com/public/pkg/etcd/modify-prefix-v2\n if [ $? -ne 0 ]; then\n echo \"下载修改prefix工具出错,退出。\"\n exit 1\n fi\n chmod +x /tmp/modify-prefix-v2\nfi\nif ! [[ {{ClusterID}} =~ ^c.* ]];then\n\techo \"clusterID: {{ClusterID}}不是正确的集群id,退出。\"\n exit 1\nfi\necho \"clusterID: {{ClusterID}}\"\n# 为leader则做snapshot,将snapshot存在在/tmp/\nTIMESTAMP=$(date \"+%Y%m%d%H%M%S\")\nmkdir -p /tmp/etcdsnap\nset -x\nSNAP_NAME=etcd_{{ClusterID}}_$TIMESTAMP\necho \"开始备份,备份名为/tmp/\"$SNAP_NAME\nDestPrefix=\"/\"{{ClusterID}}\nETCDCTL_API=3 /usr/bin/etcdctl --cacert=/var/lib/etcd/cert/ca.pem --cert=/var/lib/etcd/cert/etcd-server.pem --key=/var/lib/etcd/cert/etcd-server-key.pem --endpoints=$ENDPOINT snapshot save /tmp/etcdsnap/$SNAP_NAME\nset +e\n/tmp/modify-prefix-v2 change-prefix --db=/tmp/etcdsnap/$SNAP_NAME --dest-prefix=$DestPrefix \nif [ $? -ne 0 ]; then\n echo \"修改prefix出错,退出。\"\n exit 1\nfi\nset -e\n# 获取oss 相关地址,上传\nROLE=$(curl -s 100.100.100.200/latest/meta-data/ram/security-credentials/)\nROLERES=$(curl -s 100.100.100.200/latest/meta-data/ram/security-credentials/$ROLE)\nAccessKeyId=$(echo $ROLERES | jq .AccessKeyId|sed 's/\\\"//g')\nAccessKeySecret=$(echo $ROLERES | jq .AccessKeySecret|sed 's/\\\"//g')\nSecurityToken=$(echo $ROLERES | jq .SecurityToken|sed 's/\\\"//g')\n# put object to oss\necho \"begin put object to oss\"\nset +e\n/tmp/ossutil64 -t $SecurityToken -i $AccessKeyId -k $AccessKeySecret -e {{OSSEndpoint}} cp /tmp/etcdsnap/$SNAP_NAME oss://{{BucketName}}/$SNAP_NAME\nif [ $? -ne 0 ]; then\n echo \"推送数据到{{BucketName}} bucket失败,退出。\"\n exit 1\nfi\nset -e\n# sign\noss_url=$(/tmp/ossutil64 -t $SecurityToken -i $AccessKeyId -k $AccessKeySecret -e {{OSSEndpoint}} sign --timeout 2400 oss://{{BucketName}}/$SNAP_NAME | grep -v \"elapsed\" | tr -d '\\n')\nset +x\nsakey=$(cat /etc/kubernetes/pki/sa.key | base64 -w0)\nsapub=$(cat /etc/kubernetes/pki/sa.pub | base64 -w0)\nfrontcrt=$(cat /etc/kubernetes/pki/front-proxy-ca.crt | base64 -w0)\nfrontkey=$(cat /etc/kubernetes/pki/front-proxy-ca.key | base64 -w0)\necho \"{\\\"sakey\\\":\\\"$sakey\\\",\\\"sapub\\\":\\\"$sapub\\\",\\\"frontcrt\\\":\\\"$frontcrt\\\",\\\"frontkey\\\":\\\"$frontkey\\\",\\\"oss_url\\\":\\\"$oss_url\\\"}\" >/tmp/etcdsnap/sign",
"instanceId": "{{ ACS::TaskLoopItem }}",
"commandType": "RunShellScript",
"workingDir": "{{ workingDir }}",
"timeout": 600
},
"Loop": {
"Items": "{{ getInstance.instanceIds }}",
"RateControl": "{{ rateControl }}",
"Outputs": {
"commandOutputs": {
"AggregateType": "Fn::ListJoin",
"AggregateField": "commandOutput"
}
}
},
"Outputs": {
"commandOutput": {
"ValueSelector": "invocationOutput",
"Type": "String"
}
},
"Name": "etcdCheckout"
},
{
"Action": "ACS::ECS::RunCommand",
"OnError": "rollback",
"Description": {
"En": "Execute cloud assistant command",
"Zh-cn": "执行云助手命令"
},
"Properties": {
"regionId": "{{ regionId }}",
"commandContent": "#!/bin/bash\nif [ \"{{action}}\" = \"rollback\" ]; then\n exit 0\nfi\nif [ -e /tmp/etcdsnap/sign ]; then\n curl --retry 10 -sSL 100.100.100.200/latest/meta-data/instance-id\nfi",
"instanceId": "{{ ACS::TaskLoopItem }}",
"commandType": "RunShellScript",
"workingDir": "{{ workingDir }}",
"timeout": 60
},
"Loop": {
"Items": "{{ getInstance.instanceIds }}",
"RateControl": "{{ rateControl }}",
"Outputs": {
"commandOutputs": {
"AggregateType": "Fn::ListJoin",
"AggregateField": "commandOutput"
}
}
},
"Outputs": {
"commandOutput": {
"ValueSelector": "invocationOutput",
"Type": "String"
}
},
"Name": "findLeader"
},
{
"Action": "ACS::ECS::RunCommand",
"OnError": "rollback",
"OnSuccess": "ACS::END",
"Description": {
"en": "Execute cloud assistant command",
"zh-cn": "执行云助手命令"
},
"Properties": {
"regionId": "{{ regionId }}",
"commandContent": "#!/bin/bash\nif [ \"{{action}}\" = \"rollback\" ]; then\n exit 0\nfi\nif [ -e /tmp/etcdsnap/sign ]; then\n cat /tmp/etcdsnap/sign\n rm -rf /tmp/etcdsnap/sign\nfi",
"instanceId": "{{ ACS::TaskLoopItem }}",
"commandType": "RunShellScript",
"workingDir": "{{ workingDir }}",
"timeout": 60
},
"Loop": {
"Items": {
"Fn::Intersection": [
"{{ getInstance.instanceIds }}",
"{{ findLeader.commandOutputs }}"
]
},
"RateControl": "{{ rateControl }}",
"Outputs": {
"commandOutputs": {
"AggregateType": "Fn::ListJoin",
"AggregateField": "commandOutput"
}
}
},
"Outputs": {
"commandOutput": {
"ValueSelector": "invocationOutput",
"Type": "String"
}
},
"Name": "readSign"
},
{
"Action": "ACS::ECS::RunCommand",
"Description": {
"en": "Execute cloud assistant command",
"zh-cn": "执行云助手命令"
},
"Properties": {
"regionId": "{{ regionId }}",
"commandContent": "#!/bin/bash\nset -e\nmkdir -p /etc/kubernetes/manifests.backup\nif_move=$(ls /etc/kubernetes/manifests.backup/ | wc -l)\nif [ \"$if_move\" != \"0\" ]; then\n mv -f /etc/kubernetes/manifests.backup/* /etc/kubernetes/manifests/\nfi\necho \"The control plane is wakeup now.\"",
"instanceId": "{{ ACS::TaskLoopItem }}",
"commandType": "RunShellScript",
"workingDir": "{{ workingDir }}",
"timeout": 240
},
"Loop": {
"Items": "{{ getInstance.instanceIds }}",
"RateControl": "{{ rateControl }}",
"Outputs": {
"commandOutputs": {
"AggregateType": "Fn::ListJoin",
"AggregateField": "commandOutput"
}
}
},
"Outputs": {
"commandOutput": {
"ValueSelector": "invocationOutput",
"Type": "String"
}
},
"Name": "rollback"
}
],
"Outputs": {
"sleepOrWakeupControlPlaneOutputs": {
"Type": "List",
"Value": "{{ sleepOrWakeupControlPlane.commandOutputs }}"
},
"etcdCheckoutOutputs": {
"Type": "List",
"Value": "{{ etcdCheckout.commandOutputs }}"
},
"findLeaderOutputs": {
"Type": "List",
"Value": "{{ findLeader.commandOutputs }}"
},
"readSignOutputs": {
"Type": "List",
"Value": "{{ readSign.commandOutputs }}"
}
},
"Metadata": {
"ALIYUN::OOS::Interface": {
"ParameterGroups": [
{
"Parameters": [
"ClusterID",
"action",
"BucketName",
"OSSEndpoint",
"workingDir"
],
"Label": {
"default": {
"zh-cn": "配置参数",
"en": "Configure Parameters"
}
}
},
{
"Parameters": [
"regionId",
"targets"
],
"Label": {
"default": {
"zh-cn": "选择实例",
"en": "Select ECS Instance"
}
}
},
{
"Parameters": [
"rateControl",
"OOSAssumeRole"
],
"Label": {
"default": {
"zh-cn": "高级选项",
"en": "Control Options"
}
}
}
]
}
}
}