From ce6aba37ed7af31df577770e755e31d0c0f1fba4 Mon Sep 17 00:00:00 2001 From: gjmzj Date: Mon, 4 Mar 2019 11:23:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=AF=E5=8A=A8kubeasz=20?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E7=9A=84=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 99.clean.yml | 2 +- tools/kubeasz-docker | 73 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100755 tools/kubeasz-docker diff --git a/99.clean.yml b/99.clean.yml index 5676e5d..ae10ffc 100644 --- a/99.clean.yml +++ b/99.clean.yml @@ -200,7 +200,7 @@ with_items: - "/etc/kubernetes/" - "{{ ca_dir }}" - - "/root/.kube/" + - "/root/.kube/config" - "/etc/docker/" - name: clean 'ENV PATH' diff --git a/tools/kubeasz-docker b/tools/kubeasz-docker new file mode 100755 index 0000000..213bab1 --- /dev/null +++ b/tools/kubeasz-docker @@ -0,0 +1,73 @@ +#!/bin/bash + +set -o nounset +set -o errexit + +function usage() { + cat < To start a kubeasz(tagged) container + $0 clean To stop and remove all running containers + +EOF +} + +function start_kubeasz_docker() { + # kubeasz docker version + KUBEASZ_DOCKER_VER=kubeasz:$1 + echo "VERSION $KUBEASZ_DOCKER_VER" + + # init host dir + if [ -d "/etc/ansible/roles/" ];then + echo "==> aleady Initialized." + else + rm -rf /etc/ansible + echo "==> run a temporary container" + docker run -d --name temp_easz $KUBEASZ_DOCKER_VER + echo "==> init host kubeasz dir..." + docker cp temp_easz:/etc/ansible /etc/ansible + echo "==> stop&remove temporary container" + docker stop temp_easz + docker rm temp_easz + fi + + # 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) + echo "==> get host IP: $HOST_IP" + + # run kubeasz docker container + echo "==> run kubeasz in a container" + docker run --detach \ + --name kubeasz \ + --restart always \ + --env KUBEASZ_DOCKER_HOST=$HOST_IP \ + --volume /etc/ansible:/etc/ansible \ + --volume /root/.kube:/root/.kube \ + --volume /root/.ssh/id_rsa:/root/.ssh/id_rsa:ro \ + --volume /root/.ssh/id_rsa.pub:/root/.ssh/id_rsa.pub:ro \ + --volume /root/.ssh/known_hosts:/root/.ssh/known_hosts:ro \ + $KUBEASZ_DOCKER_VER +} + +function clean_container() { + echo "==> clean all running containers" + docker ps -a|awk 'NR>1{print $1}'|xargs docker rm -f +} + +[ "$#" -gt 0 ] || { usage >&2; exit 2; } + +case "$1" in + + (start) + [ "$#" -gt 1 ] || { usage >&2; exit 2; } + start_kubeasz_docker $2 + ;; + (clean) + clean_container + ;; + (*) + usage + exit 0 + ;; +esac