引入easzctl工具,更新添加node脚本

pull/531/head
gjmzj 2019-02-23 14:22:45 +08:00
parent 3a59e82cec
commit 657a373b3f
2 changed files with 38 additions and 124 deletions

View File

@ -1,39 +1,10 @@
- hosts: new-node - hosts: "{{ NODE_TO_ADD }}"
roles: roles:
- { role: chrony, when: "hostvars[groups.deploy[0]]['NTP_ENABLED'] == 'yes'" } - { role: chrony, when: "hostvars[groups.deploy[0]]['NTP_ENABLED'] == 'yes'" }
- prepare - prepare
- docker - docker
- kube-node - kube-node
#
- { role: calico, when: "CLUSTER_NETWORK == 'calico'" } - { role: calico, when: "CLUSTER_NETWORK == 'calico'" }
- { role: cilium, when: "CLUSTER_NETWORK == 'cilium'" } - { role: cilium, when: "CLUSTER_NETWORK == 'cilium'" }
- { role: flannel, when: "CLUSTER_NETWORK == 'flannel'" } - { role: flannel, when: "CLUSTER_NETWORK == 'flannel'" }
- { role: kube-router, when: "CLUSTER_NETWORK == 'kube-router'" } - { role: kube-router, when: "CLUSTER_NETWORK == 'kube-router'" }
# modify the ansible hosts file
- hosts:
- new-node
tasks:
- name: tag new-node FINISHED=yes
shell: 'sed -i "/\[new-node/,/\[harbor/s/{{ inventory_hostname }}/{{ inventory_hostname }} FINISHED=yes/" {{ base_dir }}/hosts'
args:
warn: false
connection: local
- name: cp new-node to 'kube-node' group
lineinfile:
dest: "{{ base_dir }}/hosts"
state: present
insertafter: '^\[kube-node'
firstmatch: yes
line: "{{ inventory_hostname }} NEW_NODE=yes"
connection: local
- hosts: deploy
tasks:
- name: rm new-node in ansible hosts
lineinfile:
dest: "{{ base_dir }}/hosts"
state: absent
regexp: 'FINISHED=yes'
connection: local

View File

@ -1,116 +1,60 @@
#!/bin/bash #!/bin/bash
# #
# This script can be used to manage multiple k8s clusters. (unfinished) # This script can be used to manage k8s clusters. (developing)
set -o nounset set -o nounset
set -o errexit #set -o errexit
#set -o xtrace #set -o xtrace
function process_cmd { function usage() {
echo "$ACTION : $CMD"
$CMD
if [[ $? -ne 0 ]]; then
echo "Command failed $CMD"
exit 1
fi
}
function usage {
cat <<EOF cat <<EOF
Usage: $0 COMMAND [args] Usage: easzctl COMMAND [args]
Commands: Commands:
backup To backup the current cluster (data and config) add-node To add a kube-node(work node) to the k8s cluster
checkout <name> To checkout and to use the chosen cluster context add-master To add a kube-master(master node) to the k8s cluster
create <name> To create a new cluster and to checkout the new cluster add-etcd To add a etcd-node to the etcd cluster
delete To delete the current cluster (keeping backups) del-etcd To delete a etcd-node from the etcd cluster
destroy To delete the current cluster and its backups clean-node To clean a node, whatever role the node plays
help To display usage help To display usage information
list To list the existing cluster names
rename <oldname> <newname> To rename an existing cluster's name Use "easzctl help <command>" for more information about a given command.
restore To restore the current cluster from backups
test To do some smoke tests on the current cluster
untest To remove smoke tests on the current cluster
upgrade Upgrades the current kubernetes cluster
EOF EOF
} }
function cp_role_conf { function process_cmd() {
ROLESPATH=$BASEPATH/../roles echo -e "$ACTION : $CMD"
for ROLE in $(ls $ROLESPATH); $CMD || { echo "Command failed : $CMD"; exit 1; }
do echo -e "\033[32mdone\033[0m"
if [ -d "$ROLESPATH/$ROLE/defaults" ]; then
mkdir -p $BASEPATH/../.cluster/$CLUSTERNAME/$ROLE/defaults/
cp -rp $ROLESPATH/$ROLE/defaults/* $BASEPATH/../.cluster/$CLUSTERNAME/$ROLE/defaults/
fi
done;
} }
function init { function add-node() {
if [ ! -d "$BASEPATH/../.cluster" ]; then # check new node's address regexp
mkdir -p $BASEPATH/../.cluster [[ $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; }
CLUSTERNAME=default
cp_role_conf
fi
}
# check if the new node already exsited
sed -n '/^\[kube-master/,/^\[harbor/p' $BASEPATH/hosts|grep "^$1" && { echo "$1 already existed!"; exit 2; }
# add a node in 'kube-node' group of ansible hosts
sed -i "/\[kube-node/a $1 NEW_NODE=yes" $BASEPATH/hosts
# 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; }
}
############################################################### ###############################################################
BASEPATH=$(cd `dirname $0`; pwd) BASEPATH=/etc/ansible
init [ "$#" -gt 1 ] || { usage >&2; exit 2; }
[ "$#" -gt 0 ] || { usage >&2; exit 2; }
case "$1" in case "$1" in
(backup) (add-node)
ACTION="action backup" ACTION="+---\033[33maction add a k8s work node\033[0m---+"
EXTRA_OPTS="$EXTRA_OPTS -e kolla_action=precheck" CMD="add-node $2"
;; ;;
(checkout) (add-master)
ACTION="action checkout" ACTION="+---\033[33maction add a k8s master node\033[0m---+"
EXTRA_OPTS="$EXTRA_OPTS -e kolla_action=check" CMD="add-master $2"
;;
(create)
ACTION="action create"
EXTRA_OPTS="$EXTRA_OPTS -e kolla_action=deploy -e common_run=true"
PLAYBOOK="${BASEDIR}/ansible/mariadb_recovery.yml"
;;
(delete)
ACTION="action delete"
EXTRA_OPTS="$EXTRA_OPTS -e kolla_action=check"
;;
(destroy)
ACTION="action destroy"
EXTRA_OPTS="$EXTRA_OPTS -e kolla_action=check"
;;
(help)
usage
;;
(list)
ACTION="action list"
CMD="ls $BASEPATH/../.cluster"
process_cmd
;;
(rename)
ACTION="action rename"
EXTRA_OPTS="$EXTRA_OPTS -e kolla_action=check"
;;
(restore)
ACTION="action restore"
EXTRA_OPTS="$EXTRA_OPTS -e kolla_action=check"
;;
(test)
ACTION="action test"
EXTRA_OPTS="$EXTRA_OPTS -e kolla_action=check"
;;
(untest)
ACTION="action untest"
EXTRA_OPTS="$EXTRA_OPTS -e kolla_action=check"
;;
(upgrade)
ACTION="action upgrade"
EXTRA_OPTS="$EXTRA_OPTS -e kolla_action=check"
;; ;;
(*) (*)
usage usage
@ -118,6 +62,5 @@ case "$1" in
;; ;;
esac esac
#CMD="echo $BASEPATH $ACTION" process_cmd
#process_cmd