From d6ed23c10ea3a8338201fe6bb61241d0579f8fd8 Mon Sep 17 00:00:00 2001 From: gjmzj Date: Mon, 25 Feb 2019 22:16:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=88=A0=E9=99=A4=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E8=84=9A=E6=9C=AC=EF=BC=8C=E5=B9=B6=E9=9B=86=E6=88=90?= =?UTF-8?q?=E5=88=B0easzctl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/clean_one_node.yml | 8 +++++ tools/easzctl | 31 ++++++++++++++-- tools/remove_etcd_node.yml | 12 +++---- tools/trans_yml.sh | 72 -------------------------------------- 4 files changed, 42 insertions(+), 81 deletions(-) delete mode 100755 tools/trans_yml.sh diff --git a/tools/clean_one_node.yml b/tools/clean_one_node.yml index 1991dd6..8a1f41c 100644 --- a/tools/clean_one_node.yml +++ b/tools/clean_one_node.yml @@ -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 diff --git a/tools/easzctl b/tools/easzctl index 93d96df..55adc92 100755 --- a/tools/easzctl +++ b/tools/easzctl @@ -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 diff --git a/tools/remove_etcd_node.yml b/tools/remove_etcd_node.yml index 8e1d33a..b27a2e0 100644 --- a/tools/remove_etcd_node.yml +++ b/tools/remove_etcd_node.yml @@ -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 diff --git a/tools/trans_yml.sh b/tools/trans_yml.sh deleted file mode 100755 index d67579e..0000000 --- a/tools/trans_yml.sh +++ /dev/null @@ -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