2018-11-05 12:47:25 +08:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2019-02-23 14:22:45 +08:00
|
|
|
# This script can be used to manage k8s clusters. (developing)
|
2018-11-05 12:47:25 +08:00
|
|
|
|
|
|
|
set -o nounset
|
2019-02-23 14:22:45 +08:00
|
|
|
#set -o errexit
|
2018-11-05 12:47:25 +08:00
|
|
|
#set -o xtrace
|
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
function usage() {
|
2018-11-05 12:47:25 +08:00
|
|
|
cat <<EOF
|
2019-02-23 14:22:45 +08:00
|
|
|
Usage: easzctl COMMAND [args]
|
2018-11-05 12:47:25 +08:00
|
|
|
Commands:
|
2019-02-23 14:22:45 +08:00
|
|
|
add-node To add a kube-node(work node) to the k8s cluster
|
|
|
|
add-master To add a kube-master(master node) to the k8s cluster
|
|
|
|
add-etcd To add a etcd-node to the etcd cluster
|
|
|
|
del-etcd To delete a etcd-node from the etcd cluster
|
|
|
|
clean-node To clean a node, whatever role the node plays
|
|
|
|
help To display usage information
|
|
|
|
|
|
|
|
Use "easzctl help <command>" for more information about a given command.
|
2018-11-05 12:47:25 +08:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
function process_cmd() {
|
|
|
|
echo -e "$ACTION : $CMD"
|
|
|
|
$CMD || { echo "Command failed : $CMD"; exit 1; }
|
|
|
|
echo -e "\033[32mdone\033[0m"
|
2018-11-05 12:47:25 +08:00
|
|
|
}
|
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
function add-node() {
|
|
|
|
# check new 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 "Invalid ip address!"; exit 2; }
|
2018-11-05 12:47:25 +08:00
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
# check if the new node already exsited
|
|
|
|
sed -n '/^\[kube-master/,/^\[harbor/p' $BASEPATH/hosts|grep "^$1" && { echo "$1 already existed!"; exit 2; }
|
2018-11-05 12:47:25 +08:00
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
# add a node in 'kube-node' group of ansible hosts
|
|
|
|
sed -i "/\[kube-node/a $1 NEW_NODE=yes" $BASEPATH/hosts
|
2018-11-05 12:47:25 +08:00
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
# check if playbook success
|
|
|
|
ansible-playbook $BASEPATH/20.addnode.yml -e NODE_TO_ADD=$1 || { sed -i "/$1 NEW_NODE=yes/d" $BASEPATH/hosts; exit 2; }
|
|
|
|
}
|
|
|
|
###############################################################
|
2018-11-05 12:47:25 +08:00
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
BASEPATH=/etc/ansible
|
|
|
|
|
|
|
|
[ "$#" -gt 1 ] || { usage >&2; exit 2; }
|
2018-11-05 12:47:25 +08:00
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
(add-node)
|
|
|
|
ACTION="+---\033[33maction add a k8s work node\033[0m---+"
|
|
|
|
CMD="add-node $2"
|
2018-11-05 12:47:25 +08:00
|
|
|
;;
|
2019-02-23 14:22:45 +08:00
|
|
|
(add-master)
|
|
|
|
ACTION="+---\033[33maction add a k8s master node\033[0m---+"
|
|
|
|
CMD="add-master $2"
|
2018-11-05 12:47:25 +08:00
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
process_cmd
|
2018-11-05 12:47:25 +08:00
|
|
|
|