优化删除节点脚本,并集成到easzctl

pull/531/head
gjmzj 2019-02-25 22:16:03 +08:00
parent caabb89531
commit d6ed23c10e
4 changed files with 42 additions and 81 deletions

View File

@ -11,6 +11,14 @@
private: no
confirm: yes
tasks:
- name: fail info1
fail: msg="you CAN NOT delete the last member of etcd cluster!"
when: "groups['etcd']|length < 2 and NODE_TO_DEL in groups['etcd']"
- name: fail info2
fail: msg="you CAN NOT delete the last member of kube-master!"
when: "groups['kube-master']|length < 2 and NODE_TO_DEL in groups['kube-master']"
- name: 执行kubectl drain(节点可能是kube-node节点)
shell: "{{ bin_dir }}/kubectl drain {{ NODE_TO_DEL }} --ignore-daemonsets --delete-local-data"
ignore_errors: true

View File

@ -23,8 +23,8 @@ EOF
function process_cmd() {
echo -e "$ACTION : $CMD"
$CMD || { echo "Command failed : $CMD"; exit 1; }
echo -e "\033[32mdone\033[0m"
$CMD || { echo -e "+---\033[31mAction failed\033[0m---+ : $CMD"; exit 1; }
echo -e "+---\033[32mAction successed\033[0m---+ : $CMD"
}
function add-node() {
@ -66,7 +66,7 @@ function add-etcd() {
sed -n '/^\[etcd/,/^\[kube-master/p' $BASEPATH/hosts|grep "^$1" && { echo "ERROR: node $1 already existed!"; exit 2; }
# input an unique NODE_NAME of the node in etcd cluster
echo "give an unique name(string) for the new node: "
echo "Please input an UNIQUE name(string) for the new node: "
read NAME
sed -n '/^\[etcd/,/^\[kube-master/p' $BASEPATH/hosts|grep "$NAME" && { echo "ERROR: name [$NAME] already existed!"; exit 2; }
@ -76,6 +76,23 @@ function add-etcd() {
# check if playbook runs successfully
ansible-playbook $BASEPATH/19.addetcd.yml -e NODE_TO_ADD=$1 || { sed -i "/$1 NODE_NAME=$NAME/d" $BASEPATH/hosts; exit 2; }
}
function del-etcd() {
# check node's address regexp
[[ $1 =~ ^(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})(\.(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})){3}$ ]] || { echo "ERROR: Invalid ip address!"; exit 2; }
#
ansible-playbook $BASEPATH/tools/remove_etcd_node.yml -e ETCD_TO_DEL=$1
}
function clean-node() {
# check node's address regexp
[[ $1 =~ ^(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})(\.(2(5[0-5]{1}|[0-4][0-9]{1})|[0-1]?[0-9]{1,2})){3}$ ]] || { echo "ERROR: Invalid ip address!"; exit 2; }
#
ansible-playbook $BASEPATH/tools/clean_one_node.yml -e NODE_TO_DEL=$1
}
###############################################################
BASEPATH=/etc/ansible
@ -96,6 +113,14 @@ case "$1" in
ACTION="+---\033[33mAction: add a etcd node\033[0m---+"
CMD="add-etcd $2"
;;
(del-etcd)
ACTION="+---\033[33mAction: delete a etcd node\033[0m---+"
CMD="del-etcd $2"
;;
(clean-node)
ACTION="+---\033[33mAction: clean a node\033[0m---+"
CMD="clean-node $2"
;;
(*)
usage
exit 0

View File

@ -6,13 +6,13 @@
private: no
confirm: yes
tasks:
- name: set warnning info
set_fact: WARN_INFO="CAN NOT DELETE THIS NODE!!!!!!"
when: "groups['etcd']|length < 2 or ETCD_TO_DEL not in groups['etcd']"
- name: fail info1
fail: msg="{{ ETCD_TO_DEL }} is NOT a member of etcd cluster!"
when: "ETCD_TO_DEL not in groups['etcd']"
- name: show warnning info
debug: var="WARN_INFO"
when: "groups['etcd']|length < 2 or ETCD_TO_DEL not in groups['etcd']"
- name: fail info2
fail: msg="you CAN NOT delete the last member of etcd cluster!"
when: "groups['etcd']|length < 2"
- block:
- name: get ID of etcd node to delete

View File

@ -1,72 +0,0 @@
#!/bin/bash
# WARNNING: 此脚本还在修改中,还未完成
# 说明不同k8s版本使用的'api-versions'版本不同此脚本用于切换yaml文件使用的'api-versions'
# Example 01: 转换单个配置文件为k8s 1.8的版本
# bash tools/trans_yml.sh -v 1.8 -f manifests/dashboard/kubernetes-dashboard.yaml
# Example 02: 转换某个目录下所有yaml文件为k8s 1.8的版本
# for YML in `find manifests/heapster/ -name '*.yaml'`;do bash tools/trans_yml.sh -v 1.8 -f $YML;done;
#set -x
show_usage()
{
echo -e "\nUsage: $0 <-v K8S_VER> <-f YAML_FILE>"
echo -e "\nK8S_VER: support 1.8/1.9/1.10"
}
#check_arg -------------------------------------------------
K8S_VER=""
YML_FILE=""
while getopts "v:f:" arg
do
case $arg in
v)
K8S_VER=$OPTARG
;;
f)
if [ -w "$OPTARG" ];then
YML_FILE=$OPTARG
else
echo File:"$OPTARG not found or not writeable."
exit 1
fi
;;
?)
echo -e "unkown argument"
show_usage
exit 1
;;
esac
done
if [ "$K8S_VER" = "" ] || [ "$YML_FILE" = "" ];then
echo "error argument"
show_usage
exit 1
fi
main()
{
case "$K8S_VER" in
1.8)
sed -i 's/apps\/v1/extensions\/v1beta1/g' $YML_FILE
exit 0
;;
1.9)
echo "K8s_VER is $K8S_VER"
exit 0
;;
1.10)
sed -i 's/extensions\/v1beta1/apps\/v1/g' $YML_FILE
exit 0
;;
?)
;;
esac
echo "not supported K8s_VER:$K8S_VER"
exit 1
}
main