目录

k8s的master节点恢复操作

项目笔记:k8s的master节点恢复操作

如果k8s的master节点异常,更新节点设备后,需要对该master节点进行k8s恢复。 场景:k8s高可用环境中,某master节点异常,少了一个master节点,会导致etcd的高可用状态不稳定。所以需要进行该master节点恢复。

自动化恢复脚本

recover_k8s_master.sh

此脚本处理k8s1.14.8版本的master组件恢复,包括:hosts_config、[docker-conf、] etcd、kubelet、master、calico

  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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#!/bin/bash
# 此脚本处理k8s1.14.8版本的master组件恢复,包括:hosts_config、[docker-conf、] etcd、kubelet、master、calico

# k8s恢复脚本执行预置条件:
# 1. 新节点已配置待恢复节点的ip和hostname
# 2. 新节点已配置集群节点hosts,并能与原集群节点免密访问
# 3. 新节点已安装了etcd和k8s相关组件
# 4. 已知要执行恢复操作的版本是 v3_1(k8s1.14.8) 还是 v3_2(k8s1.20.5)
# 5. 已知原节点的hostname、ip、etcdname和待恢复节点的hostname、ip、etcdname


stop_commons(){
	#systemctl stop harbor

	systemctl stop kubelet

	systemctl stop etcd

	systemctl stop docker

}

start_commons(){
	systemctl daemon-reload
	systemctl restart docker
	#sleep 2s
	systemctl restart etcd
	sleep 2s
	systemctl enable kubelet
	systemctl restart kubelet
	sleep 5s
}


hosts_config(){

## 1. hosts-conf
set -e
scp -r ${src_ip}:/etc/hosts /etc/hosts

set +e
}


etcd_collect_config(){
	
	# src_ip=10.151.11.61
	# new_ip=192.168.100.3
	# src_hostname=node1
	# new_hostname=node3
	# src_etcdname=etcd1
	# new_etcdname=etcd3
	set -e
	scp -r  ${src_ip}:/etc/ssl/etcd /home/recover/etc/
	set +e
	cp -r /etc/ssl/etcd /home/tmp/etcd

	rm -rf /etc/ssl/etcd
	cp -r /home/recover/etc/etcd  /etc/ssl/


	## check etcd ssl 
	# grep_ret=$(ls /etc/ssl/etcd/ssl |grep node1|wc -l)
	grep_ret=$(ls /etc/ssl/etcd/ssl |grep ${new_hostname} |wc -l)

	if [[ ${grep_ret} == 0 ]]; then
		#statements
		echo "Failed to etcd_collect_config etcd ssl."
		exit 11
	fi

	## etcd cofig
	set -e
	scp -r ${src_ip}:/etc/etcd.env /etc/etcd.env
	scp -r ${src_ip}:/etc/systemd/system/etcd.service  /etc/systemd/system/etcd.service
	scp -r ${src_ip}:/usr/local/bin/etcd  /usr/local/bin/etcd
	scp -r ${src_ip}:/usr/local/bin/etcdctl /usr/local/bin/etcdctl
	# scp -r ${src_ip}:/usr/local/bin/etcd-scripts /usr/local/bin/etcd-scripts

	set +e
}

etcd_common_config(){
	set -e
	## vi /etc/etcd.env

	etcd_env=/etc/etcd.env

	# src_ip=10.151.11.61
	# new_ip=192.168.100.3
	# src_hostname=node1
	# new_hostname=node3
	# src_etcdname=etcd1
	# new_etcdname=etcd3

	
	# sed -n '/^ETCD_INITIAL_CLUSTER=/!s/'${src_ip}'/'${new_ip}'/gp' ${etcd_env}
	sed -i '/^ETCD_INITIAL_CLUSTER=/!s/'${src_ip}'/'${new_ip}'/g' ${etcd_env}
	sed -i 's|ETCD_NAME=.*$|ETCD_NAME='${new_etcdname}'|g' ${etcd_env}
	sed -i 's|'${src_hostname}'|'${new_hostname}'|g' ${etcd_env}

	## vi /etc/systemd/system/etcd.service

	etcd_service=/etc/systemd/system/etcd.service
	sed -i 's|'${src_etcdname}'|'${new_etcdname}'|g' ${etcd_service}


	## vi /usr/local/bin/etcd
	etcd_bin=/usr/local/bin/etcd
	sed -i 's|'${src_etcdname}'|'${new_etcdname}'|g' ${etcd_bin}
	
	set +e
	
	
	## config new etcd member 
	
	systemctl stop etcd
	# ETCDCTL_API=3 etcdctl --endpoints=https://100.7.36.106:2379 --cacert=/etc/ssl/etcd/ssl/ca.pem --cert=/etc/ssl/etcd/ssl/member-node1.pem --key=/etc/ssl/etcd/ssl/member-node1-key.pem member list |grep etcd3 |cut -d"," -f1
	
	
	rm -rf  /var/lib/etcd/
	
	## get new_etcdid
	new_etcdid=$(ETCDCTL_API=3 etcdctl --endpoints=https://${src_ip}:2379 --cacert=/etc/ssl/etcd/ssl/ca.pem --cert=/etc/ssl/etcd/ssl/member-${src_hostname}.pem --key=/etc/ssl/etcd/ssl/member-${src_hostname}-key.pem member list |grep ${new_etcdname} |cut -d"," -f1)
	
	if [[ "x"${new_etcdid}"x" == "xx" ]];then
        echo "Warning: Failed to find ${new_etcdname} in etcd member list. Check the new_etcdname validity"
        exit 12
    fi
	
	
	## remove this new_etcdid member
	ETCDCTL_API=3 etcdctl --endpoints=https://${src_ip}:2379 --cacert=/etc/ssl/etcd/ssl/ca.pem --cert=/etc/ssl/etcd/ssl/member-${src_hostname}.pem --key=/etc/ssl/etcd/ssl/member-${src_hostname}-key.pem member remove ${new_etcdid}
	## add this new_etcdname member
	ETCDCTL_API=3 etcdctl --endpoints=https://${src_ip}:2379 --cacert=/etc/ssl/etcd/ssl/ca.pem --cert=/etc/ssl/etcd/ssl/member-${src_hostname}.pem --key=/etc/ssl/etcd/ssl/member-${src_hostname}-key.pem member add ${new_etcdname} --peer-urls=https://${new_ip}:2380

	

	systemctl daemon-reload
	systemctl start docker
	systemctl enable etcd
	systemctl restart etcd

	sleep 15s

	## check etcd
	ETCDCTL_API=3 etcdctl --endpoints=https://${new_ip}:2379 --cacert=/etc/ssl/etcd/ssl/ca.pem --cert=/etc/ssl/etcd/ssl/member-${new_hostname}.pem --key=/etc/ssl/etcd/ssl/member-${new_hostname}-key.pem member list
	echo ""
	ETCDCTL_API=3 etcdctl --endpoints=https://${new_ip}:2379 --cacert=/etc/ssl/etcd/ssl/ca.pem --cert=/etc/ssl/etcd/ssl/member-${new_hostname}.pem --key=/etc/ssl/etcd/ssl/member-${new_hostname}-key.pem endpoint status --cluster -w table

	echo ""
	echo ""
	echo ""
}

# config aist-v3.1: etcd v3.3.10
etcd_config_v3_1(){
	etcd_common_config
}

# config aist-v3.2: etcd v3.4.13
etcd_config_v3_2(){
	etcd_common_config
}



k8s_collect_config(){

	# src_ip=10.151.11.61
	# new_ip=192.168.100.3
	# src_hostname=node1
	# new_hostname=node3
	# src_etcdname=etcd1
	# new_etcdname=etcd3

	# 1. 收集k8s etc config
	## 备份拷贝到临时目录
	cp -r /etc/kubernetes /home/tmp/kubernetes
	## 收集配置文件
	set -e
	scp -r ${src_ip}:/etc/kubernetes /home/recover/kubernetes
	set +e

	rm -rf /etc/kubernetes
	cp -r /home/recover/kubernetes /etc/kubernetes

	# 2. 收集kubelet
	## 拷贝过程会提示kubelet.sock无法拷贝,无影响

	rm -rf /var/lib/kubelet

	scp -r  ${src_ip}:/var/lib/kubelet /var/lib/kubelet
	scp -r ${src_ip}:/etc/systemd/system/kubelet.service /etc/systemd/system/kubelet.service


	#3 . 收集cni配置和程序文件
	## 备份拷贝到临时目录
	cp -r /etc/cni /home/tmp/cni
	cp -r /opt/cni /home/tmp/cni

	## --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/cni/bin
	## 直接拷贝到目标目录

	rm -rf /etc/cni
	rm -rf /opt/cni
	set -e
	scp -r  ${src_ip}:/etc/cni /etc/cni
	scp -r  ${src_ip}:/opt/cni /opt/cni
	set +e
}



calico_common_config(){
	# src_ip=10.151.11.61
	# new_ip=192.168.100.3
	# src_hostname=node1
	# new_hostname=node3
	# src_etcdname=etcd1
	# new_etcdname=etcd3
	set -e
	# vi /etc/cni/net.d/calico.conflist.template /etc/cni/net.d/10-calico.conflist 
	calico_conflist_template=/etc/cni/net.d/calico.conflist.template
	calico_conflist=/etc/cni/net.d/10-calico.conflist
	#sed -i 's|"nodename": "'${src_hostname}'"|"nodename": "'${new_hostname}'"|g' ${calico_conflist}
	
	sed -i 's|'${src_hostname}'|'${new_hostname}'|g' ${calico_conflist_template}

	sed -i 's|'${src_hostname}'|'${new_hostname}'|g' ${calico_conflist}

	
	
	# vi /var/lib/calico/nodename
	echo "${new_hostname}" > /var/lib/calico/nodename
	
	set +e
	rm -rf /var/lib/cni/cache

}

# calico_crerts_config for aist-v3.1: k8s1.14.8
calico_crerts_config(){
	calico_cert_dir=/etc/calico/certs
	kube_etcd_cacert_file=ca.pem
	kube_etcd_cert_file=node-${new_hostname}.pem
	kube_etcd_key_file=node-${new_hostname}-key.pem

	rm -rf ${calico_cert_dir}
	mkdir -p ${calico_cert_dir}

	cd /etc/ssl/etcd/ssl

	/bin/cp -rf ${kube_etcd_cacert_file} ${calico_cert_dir}/ca_cert.crt

	/bin/cp -rf ${kube_etcd_cert_file} ${calico_cert_dir}/cert.crt

	/bin/cp -rf ${kube_etcd_key_file} ${calico_cert_dir}/key.pem

	# ll ${calico_cert_dir}

	grep_ret=$(ls ${calico_cert_dir} |wc -l)

	if [[ ${grep_ret} != 3 ]]; then
		#statements
		echo "Failed to calico_crerts_config crerts."
		exit 31
	fi

	echo ""
	echo ""


}


# config aist-v3.1: k8s v1.14.8
calico_config_v3_1(){
	calico_common_config
	calico_crerts_config

}

# config aist-v3.2: k8s v1.20.5
calico_config_v3_2(){
	calico_common_config

}

sysctl_config_v3_2(){
	scp -r  ${src_ip}:/etc/sysctl.d/bridge-nf-call.conf  /etc/sysctl.d/bridge-nf-call.conf
	sysctl -p /etc/sysctl.d/bridge-nf-call.conf

}

label_master_node(){
	kubectl label --overwrite nodes ${new_hostname} node-role.kubernetes.io/master=true
	
	if [[ $? != 0 ]]; then
		#statements
		echo "Failed to kubectl label --overwrite nodes ${new_hostname} node-role.kubernetes.io/master=true, and to retry..."
		sleep 3s
		kubectl label --overwrite nodes ${new_hostname} node-role.kubernetes.io/master=true
	fi
}


# config aist-v3.1: k8s v1.14.8
k8s_config_v3_1(){

	# src_ip=10.151.11.61
	# new_ip=192.168.100.3
	# src_hostname=node1
	# new_hostname=node3
	# src_etcdname=etcd1
	# new_etcdname=etcd3
	
	stop_commons

	calico_config_v3_1
	
	set -e
	## vi /etc/kubernetes/kubelet.env

	kubelet_env=/etc/kubernetes/kubelet.env
	# kubelet_env=kubelet.env

	sed -i 's|--address='${src_ip}'|--address='${new_ip}'|g' ${kubelet_env}
	sed -i 's|--node-ip='${src_ip}'|--node-ip='${new_ip}'|g' ${kubelet_env}
	sed -i 's|--hostname-override='${src_hostname}'|--hostname-override='${new_hostname}'|g' ${kubelet_env}


	## vi /etc/kubernetes/kubeadm-config.yaml
	
	kubeadm_config=/etc/kubernetes/kubeadm-config.yaml
	# kubeadm_config=kubeadm-config.yaml

	# advertiseAddress: 10.151.11.61
	sed -i 's|advertiseAddress: '${src_ip}'|advertiseAddress: '${new_ip}'|g' ${kubeadm_config}
	# sed -n 's|^[#]*[ ]*name: '${src_hostname}'|  name: '${new_hostname}'|gp' ${kubeadm_config}
	# sed -n 's|  name: '${src_hostname}'|  name: '${new_hostname}'|gp' ${kubeadm_config}
	sed -i 's|  name: '${src_hostname}'|  name: '${new_hostname}'|g' ${kubeadm_config}

	# sed -n 's|/etc/ssl/etcd/ssl/node-'${src_hostname}'.pem|/etc/ssl/etcd/ssl/node-'${new_hostname}'.pem|gp' ${kubeadm_config}
	# sed -n 's|/etc/ssl/etcd/ssl/node-'${src_hostname}'-key.pem|/etc/ssl/etcd/ssl/node-'${new_hostname}'-key.pem|gp' ${kubeadm_config}
	sed -i 's|/etc/ssl/etcd/ssl/node-'${src_hostname}'.pem|/etc/ssl/etcd/ssl/node-'${new_hostname}'.pem|g' ${kubeadm_config}
	sed -i 's|/etc/ssl/etcd/ssl/node-'${src_hostname}'-key.pem|/etc/ssl/etcd/ssl/node-'${new_hostname}'-key.pem|g' ${kubeadm_config}


	sed -i 's|    bind-address: '${src_ip}'|    bind-address: '${new_ip}'|g' ${kubeadm_config}
	sed -i 's|    insecure-bind-address: '${src_ip}'|    insecure-bind-address: '${new_ip}'|g' ${kubeadm_config}


	sed -i 's|hostnameOverride: '${src_hostname}'|hostnameOverride: '${new_hostname}'|g' ${kubeadm_config}
	set +e



	## cache delete

	rm -rf /etc/kubernetes/admin.conf
 
	rm -rf /etc/kubernetes/kubelet.conf

	# 删除kubelet的pki
	rm -rf /var/lib/kubelet/pki
	# 如果安装包指定kubelet其它数据目录(如:/var/lib/data/kubelet/),则也同样删除
	rm -rf /var/lib/data/kubelet/pki


	cd /etc/kubernetes
	/usr/local/bin/kubeadm init --config=/etc/kubernetes/kubeadm-config.yaml --ignore-preflight-errors=all

	echo ""
	echo ""
	echo ""

	# debug
	# cat /etc/kubernetes/admin.conf
	# cat /etc/kubernetes/kubelet.conf



	mkdir -p /root/.kube
	sudo /bin/cp -rf /etc/kubernetes/admin.conf /root/.kube/config

	

	start_commons
	
	label_master_node

	sleep 5s


}

# config aist-v3.2: k8s v1.20.5
k8s_config_v3_2(){

	# src_ip=10.151.11.61
	# new_ip=192.168.100.3
	# src_hostname=node1
	# new_hostname=node3
	# src_etcdname=etcd1
	# new_etcdname=etcd3
	
	stop_commons

	calico_config_v3_2
	
	
	set -e
	## vi /etc/kubernetes/kubelet.env

	kubelet_env=/etc/kubernetes/kubelet.env
	# kubelet_env=kubelet.env

	sed -i 's|--address='${src_ip}'|--address='${new_ip}'|g' ${kubelet_env}
	sed -i 's|--node-ip='${src_ip}'|--node-ip='${new_ip}'|g' ${kubelet_env}
	sed -i 's|--hostname-override='${src_hostname}'|--hostname-override='${new_hostname}'|g' ${kubelet_env}


	## vi /etc/kubernetes/kubeadm-config.yaml
	
	kubeadm_config=/etc/kubernetes/kubeadm-config.yaml
	#kubeadm_config=kubeadm-config.yaml

	# advertiseAddress: 10.151.11.61
	sed -i 's|advertiseAddress: '${src_ip}'|advertiseAddress: '${new_ip}'|g' ${kubeadm_config}
	# sed -n 's|^[#]*[ ]*name: '${src_hostname}'|  name: '${new_hostname}'|gp' ${kubeadm_config}
	# sed -n 's|  name: '${src_hostname}'|  name: '${new_hostname}'|gp' ${kubeadm_config}
	sed -i 's|  name: '${src_hostname}'|  name: '${new_hostname}'|g' ${kubeadm_config}

	# sed -n 's|/etc/ssl/etcd/ssl/node-'${src_hostname}'.pem|/etc/ssl/etcd/ssl/node-'${new_hostname}'.pem|gp' ${kubeadm_config}
	# sed -n 's|/etc/ssl/etcd/ssl/node-'${src_hostname}'-key.pem|/etc/ssl/etcd/ssl/node-'${new_hostname}'-key.pem|gp' ${kubeadm_config}
	sed -i 's|/etc/ssl/etcd/ssl/node-'${src_hostname}'.pem|/etc/ssl/etcd/ssl/node-'${new_hostname}'.pem|g' ${kubeadm_config}
	sed -i 's|/etc/ssl/etcd/ssl/node-'${src_hostname}'-key.pem|/etc/ssl/etcd/ssl/node-'${new_hostname}'-key.pem|g' ${kubeadm_config}


	sed -i 's|    bind-address: '${src_ip}'|    bind-address: '${new_ip}'|g' ${kubeadm_config}
	# k8s1.20.5: insecure-bind-address=0
	# sed -i 's|    insecure-bind-address: '${src_ip}'|    insecure-bind-address: '${new_ip}'|g' ${kubeadm_config}


	sed -i 's|hostnameOverride: '${src_hostname}'|hostnameOverride: '${new_hostname}'|g' ${kubeadm_config}
	set +e



	## cache delete

	rm -rf /etc/kubernetes/admin.conf
 
	rm -rf /etc/kubernetes/kubelet.conf

	# 删除kubelet的pki
	rm -rf /var/lib/kubelet/pki
	# 如果安装包指定kubelet其它数据目录(如:/var/lib/data/kubelet/),则也同样删除
	rm -rf /var/lib/data/kubelet/pki


	cd /etc/kubernetes
	/usr/local/bin/kubeadm init --config=/etc/kubernetes/kubeadm-config.yaml --ignore-preflight-errors=all

	echo ""
	echo ""
	echo ""

	# debug
	# cat /etc/kubernetes/admin.conf
	# cat /etc/kubernetes/kubelet.conf



	mkdir -p /root/.kube
	sudo /bin/cp -rf /etc/kubernetes/admin.conf /root/.kube/config

	

	# only for v3_2 基于spray 这个不是必需的了
	# sysctl_config_v3_2


	start_commons
	
	label_master_node
	
	sleep 5s
}




recover_k8s_v3_1(){
	echo "recover_k8s_v3_1 START..."
	# 2. etcd
	echo "etcd_config ..."
	etcd_collect_config
	etcd_config_v3_1


	# 3. k8s
	echo "k8s_config ..."
	k8s_collect_config
	k8s_config_v3_1
	echo "recover_k8s_v3_1 END..."

}

recover_k8s_v3_2(){
	echo "recover_k8s_v3_2 START..."
	# 2. etcd
	echo "etcd_config ..."
	etcd_collect_config
	etcd_config_v3_2


	# 3. k8s
	echo "k8s_config ..."
	k8s_collect_config
	k8s_config_v3_2
	echo "recover_k8s_v3_2 END..."

}


k8s_vip_config(){
	echo "k8s_vip_config ..."
	scp -r  ${src_ip}:/etc/kolla/haproxy/k8s.cfg  /etc/kolla/haproxy/k8s.cfg
	docker restart haproxy
	echo "k8s_vip_config END..."
	
}


k8s_deploy_config(){
	echo "k8s_deploy_config ..."
	# fix deploy version mismatch bug
	
	if [ ! -f "/etc/kubernetes/coredns-deployment.yml" ];then
		cp -r /tmp/coredns-deployment.yml /etc/kubernetes/coredns-deployment.yml
		# 100.2.126.55:5000/com.inspur
		imageRepository=$(grep imageRepository /etc/kubernetes/kubeadm-images.yaml |cut -d"\"" -f2)
		
		sed -i 's|HARBOR_IP/com.inspur|'${imageRepository}'|g' /etc/kubernetes/coredns-deployment.yml		
	
	fi
	
	kubectl delete -f /etc/kubernetes/coredns-deployment.yml
	kubectl apply -f /etc/kubernetes/coredns-deployment.yml
	echo "k8s_deploy_config END..."
	
}

######################################
######################################
######################################
# main()


if [[ $# != 7 ]]; then
    echo "usage $0  version src_hostname src_ip src_etcdname new_hostname new_ip new_etcdname"
    echo "version: v3_1 or v3_2"
    echo "for example, recover process: $0 v3_1 node1 192.168.100.1 etcd1 node3 192.168.100.3 etcd3"
    exit 1
fi



# check version

if [[ $1 != "v3_1" && $1 != "v3_2" ]]; then
    #statements
    echo "$0 param:$1 must be 'v3_1' or 'v3_2'"
    echo "usage $0  version src_hostname src_ip src_etcdname new_hostname new_ip new_etcdname"
    echo "version: v3_1 or v3_2"
    echo "for example, recover process: $0 v3_1 node1 192.168.100.1 etcd1 node3 192.168.100.3 etcd3"
    exit 2
fi



# params setting
version=$1

src_hostname=$2
src_ip=$3
src_etcdname=$4

new_hostname=$5
new_ip=$6
new_etcdname=$7





# 0. 停止新节点已安装组件

stop_commons

## 1. hosts-conf

hosts_config

## 1. docker-conf

## scp -r node1:/etc/systemd/system/docker.service.d/* /etc/systemd/system/docker.service.d/
## scp -r node1:/etc/systemd/system/docker.service /etc/systemd/system/docker.service
#set -e
#scp -r ${src_ip}:/etc/systemd/system/docker.service.d/* /etc/systemd/system/docker.service.d/
#scp -r ${src_ip}:/etc/systemd/system/docker.service /etc/systemd/system/docker.service
#set +e

#systemctl daemon-reload
## systemctl restart docker
#systemctl enable docker

## check harbor
# echo eb5a7c0df3494817845d1fcd21133afa | docker login -u admin --password-stdin  100.2.126.121:5000



### 停止新节点已安装组件 retry
#stop_commons




# 拷贝到新节node3本地临时目录,临时目录/home/recover/和/home/tmp/在完成全部恢复操作(etcd和k8s)后删除

rm -rf /home/recover/
rm -rf /home/tmp/

mkdir -p /home/recover/
mkdir -p /home/recover/etc/etcd
mkdir -p /home/tmp/


## 2. k8s-conf
if [[ ${version} == "v3_1" ]]; then
    recover_k8s_v3_1
	### 2.1 k8s_deploy_config
	k8s_deploy_config
elif [[ ${action} == "v3_2" ]]; then
    recover_k8s_v3_2
fi

## 3. k8s_vip_config
k8s_vip_config



# clear cache tmp dir
rm -rf /home/recover/
rm -rf /home/tmp/