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-26 15:04:16 +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() {
|
2019-02-26 15:04:16 +08:00
|
|
|
echo -e "+---\033[33m$ACTION\033[0m---+ : $CMD"
|
2019-02-25 22:16:03 +08:00
|
|
|
$CMD || { echo -e "+---\033[31mAction failed\033[0m---+ : $CMD"; exit 1; }
|
|
|
|
echo -e "+---\033[32mAction successed\033[0m---+ : $CMD"
|
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
|
2019-02-24 12:56:47 +08:00
|
|
|
[[ $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; }
|
2018-11-05 12:47:25 +08:00
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
# check if the new node already exsited
|
2019-02-24 12:56:47 +08:00
|
|
|
sed -n '/^\[kube-master/,/^\[harbor/p' $BASEPATH/hosts|grep "^$1" && { echo "ERROR: node $1 already existed!"; exit 2; }
|
2018-11-05 12:47:25 +08:00
|
|
|
|
2019-02-24 12:56:47 +08:00
|
|
|
# add a node into 'kube-node' group
|
2019-02-23 14:22:45 +08:00
|
|
|
sed -i "/\[kube-node/a $1 NEW_NODE=yes" $BASEPATH/hosts
|
2018-11-05 12:47:25 +08:00
|
|
|
|
2019-02-24 12:56:47 +08:00
|
|
|
# check if playbook runs successfully
|
2019-02-27 10:53:02 +08:00
|
|
|
ansible-playbook $BASEPATH/tools/20.addnode.yml -e NODE_TO_ADD=$1 || { sed -i "/$1 NEW_NODE=yes/d" $BASEPATH/hosts; exit 2; }
|
2019-02-23 14:22:45 +08:00
|
|
|
}
|
2019-02-24 12:56:47 +08:00
|
|
|
|
|
|
|
function add-master() {
|
|
|
|
# check new master'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; }
|
|
|
|
|
|
|
|
# check if k8s with DPLOY_MODE='multi-master'
|
|
|
|
grep '^DEPLOY_MODE=multi-master' $BASEPATH/hosts || { echo "ERROR: only k8s with DPLOY_MODE='multi-master' can have master node added!"; exit 2; }
|
|
|
|
|
|
|
|
# check if the new master already exsited
|
|
|
|
sed -n '/^\[kube-master/,/^\[kube-node/p' $BASEPATH/hosts|grep "^$1" && { echo "ERROR: master $1 already existed!"; exit 2; }
|
|
|
|
|
|
|
|
# add a node into 'kube-master' group
|
|
|
|
sed -i "/\[kube-master/a $1 NEW_MASTER=yes" $BASEPATH/hosts
|
|
|
|
|
|
|
|
# check if playbook runs successfully
|
2019-02-27 10:53:02 +08:00
|
|
|
ansible-playbook $BASEPATH/tools/21.addmaster.yml -e NODE_TO_ADD=$1 || { sed -i "/$1 NEW_MASTER=yes/d" $BASEPATH/hosts; exit 2; }
|
2019-02-24 12:56:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function add-etcd() {
|
|
|
|
# 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 "ERROR: Invalid ip address!"; exit 2; }
|
|
|
|
|
|
|
|
# check if the new node already exsited
|
|
|
|
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
|
2019-02-25 22:16:03 +08:00
|
|
|
echo "Please input an UNIQUE name(string) for the new node: "
|
2019-02-24 12:56:47 +08:00
|
|
|
read NAME
|
|
|
|
sed -n '/^\[etcd/,/^\[kube-master/p' $BASEPATH/hosts|grep "$NAME" && { echo "ERROR: name [$NAME] already existed!"; exit 2; }
|
|
|
|
|
|
|
|
# add a node into 'kube-node' group
|
|
|
|
sed -i "/\[etcd/a $1 NODE_NAME=$NAME" $BASEPATH/hosts
|
|
|
|
|
|
|
|
# check if playbook runs successfully
|
2019-02-27 10:53:02 +08:00
|
|
|
ansible-playbook $BASEPATH/tools/19.addetcd.yml -e NODE_TO_ADD=$1 || { sed -i "/$1 NODE_NAME=$NAME/d" $BASEPATH/hosts; exit 2; }
|
2019-02-24 12:56:47 +08:00
|
|
|
}
|
2019-02-25 22:16:03 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-26 15:04:16 +08:00
|
|
|
function help-info() {
|
|
|
|
case "$1" in
|
|
|
|
(add-node)
|
|
|
|
echo -e "Usage: easzctl add-node <new_node_ip>\n\nMore information please refer to 'docs/op/AddNode.md'"
|
|
|
|
;;
|
|
|
|
(add-master)
|
|
|
|
echo -e "Usage: easzctl add-master <new_master_ip>\n\nMore information please refer to 'docs/op/AddMaster.md'"
|
|
|
|
;;
|
|
|
|
(add-etcd)
|
|
|
|
echo -e "Usage: easzctl add-etcd <new_etcd_ip>\n\nMore information please refer to 'docs/op/op-etcd.md'"
|
|
|
|
;;
|
|
|
|
(del-etcd)
|
|
|
|
echo -e "Usage: easzctl del-etcd <etcd_ip>\n\nMore information please refer to 'docs/op/op-etcd.md'"
|
|
|
|
;;
|
|
|
|
(clean-node)
|
|
|
|
echo -e "Usage: easzctl clean-node <node_ip>\n\nMore information please refer to 'docs/op/clean_one_node.md'"
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2019-03-03 22:50:15 +08:00
|
|
|
function start() {
|
|
|
|
case "$1" in
|
|
|
|
(aio)
|
|
|
|
start-aio
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
function start-aio(){
|
|
|
|
if [ ! -n "$KUBEASZ_DOCKER_HOST" ]; then
|
|
|
|
# easzctl runs in a host machine, get host's ip
|
|
|
|
HOST_IF=$(ip route|grep default|cut -d' ' -f5)
|
|
|
|
HOST_IP=$(ip a|grep $HOST_IF|awk 'NR==2{print $2}'|cut -d'/' -f1)
|
|
|
|
cp -f $BASEPATH/example/hosts.allinone.example.en /root/hosts.aio
|
|
|
|
sed -i "s/192.168.1.1/$HOST_IP/g" /root/hosts.aio
|
|
|
|
ansible-playbook -i /root/hosts.aio 90.setup.yml
|
|
|
|
else
|
|
|
|
# easzctl runs in a container
|
|
|
|
cp -f $BASEPATH/example/hosts.allinone.example.en /root/hosts.aio
|
|
|
|
sed -i "s/192.168.1.1/$KUBEASZ_DOCKER_HOST/g" /root/hosts.aio
|
|
|
|
ansible-playbook -i /root/hosts.aio 90.setup.yml
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-02-23 14:22:45 +08:00
|
|
|
###############################################################
|
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-26 15:04:16 +08:00
|
|
|
|
|
|
|
(add-node)
|
|
|
|
ACTION="Action: add a k8s work node"
|
2019-02-23 14:22:45 +08:00
|
|
|
CMD="add-node $2"
|
2018-11-05 12:47:25 +08:00
|
|
|
;;
|
2019-02-26 15:04:16 +08:00
|
|
|
(add-master)
|
|
|
|
ACTION="Action: add a k8s master node"
|
2019-02-23 14:22:45 +08:00
|
|
|
CMD="add-master $2"
|
2018-11-05 12:47:25 +08:00
|
|
|
;;
|
2019-02-26 15:04:16 +08:00
|
|
|
(add-etcd)
|
|
|
|
ACTION="Action: add a etcd node"
|
2019-02-24 12:56:47 +08:00
|
|
|
CMD="add-etcd $2"
|
|
|
|
;;
|
2019-02-26 15:04:16 +08:00
|
|
|
(del-etcd)
|
|
|
|
ACTION="Action: delete a etcd node"
|
2019-02-25 22:16:03 +08:00
|
|
|
CMD="del-etcd $2"
|
|
|
|
;;
|
2019-02-26 15:04:16 +08:00
|
|
|
(clean-node)
|
|
|
|
ACTION="Action: clean a node"
|
2019-02-25 22:16:03 +08:00
|
|
|
CMD="clean-node $2"
|
|
|
|
;;
|
2019-02-26 15:04:16 +08:00
|
|
|
(help)
|
|
|
|
help-info $2
|
|
|
|
exit 0
|
|
|
|
;;
|
2019-03-03 22:50:15 +08:00
|
|
|
(start)
|
|
|
|
ACTION="Action: start an AllInOne cluster"
|
|
|
|
CMD="start $2"
|
|
|
|
;;
|
2019-02-26 15:04:16 +08:00
|
|
|
(*)
|
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
|
|
|
|