diff --git a/tools/easzup b/tools/easzup index 2e1709e..484dae6 100755 --- a/tools/easzup +++ b/tools/easzup @@ -14,23 +14,33 @@ set -o errexit #set -o xtrace # default version, can be overridden by cmd line options -export DOCKER_VER=18.09.6 -export KUBEASZ_VER=1.3.1 -export K8S_BIN_VER=v1.14.3 +export DOCKER_VER=18.09.7 +export KUBEASZ_VER=1.3.0 +export K8S_BIN_VER=v1.15.0 export EXT_BIN_VER=0.3.0 +export SYS_PKG_VER=0.3.2 function install_docker() { # check if a container runtime is already installed systemctl status docker|grep Active|grep -q running && { echo "[WARN] docker is already running."; return 0; } systemctl status containerd|grep Active|grep -q running && { echo "[ERROR] containerd is running, unsupported."; exit 1; } + + if [[ "$REGISTRY_MIRROR" == CN ]];then + DOCKER_URL="https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/static/stable/x86_64/docker-${DOCKER_VER}.tgz" + else + DOCKER_URL="https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKER_VER}.tgz" + fi - DOCKER_URL=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/static/stable/x86_64/docker-${DOCKER_VER}.tgz mkdir -p /opt/kube/bin /etc/docker /etc/ansible/down if [[ -f "/etc/ansible/down/docker-${DOCKER_VER}.tgz" ]];then echo "[INFO] docker binaries already existed" else echo -e "[INFO] \033[33mdownloading docker binaries\033[0m $DOCKER_VER" - curl -C- -O --retry 3 "$DOCKER_URL" || { echo "[ERROR] downloading docker failed"; exit 1; } + if [[ -e /usr/bin/curl ]];then + curl -C- -O --retry 3 "$DOCKER_URL" || { echo "[ERROR] downloading docker failed"; exit 1; } + else + wget -c "$DOCKER_URL" || { echo "[ERROR] downloading docker failed"; exit 1; } + fi mv ./docker-${DOCKER_VER}.tgz /etc/ansible/down fi @@ -59,8 +69,11 @@ KillMode=process WantedBy=multi-user.target EOF + # configuration for dockerd echo "[INFO] generate docker config file" - cat > /etc/docker/daemon.json << EOF + if [[ "$REGISTRY_MIRROR" == CN ]];then + echo "[INFO] prepare register mirror for $REGISTRY_MIRROR" + cat > /etc/docker/daemon.json << EOF { "registry-mirrors": [ "https://dockerhub.azk8s.cn", @@ -77,6 +90,21 @@ EOF "data-root": "/var/lib/docker" } EOF + else + echo "[INFO] standard config without registry mirrors" + cat > /etc/docker/daemon.json << EOF +{ + "max-concurrent-downloads": 10, + "log-driver": "json-file", + "log-level": "warn", + "log-opts": { + "max-size": "10m", + "max-file": "3" + }, + "data-root": "/var/lib/docker" +} +EOF + fi if [[ -e /etc/centos-release || -e /etc/redhat-release ]]; then echo "[INFO] turn off selinux in CentOS/Redhat" @@ -93,17 +121,20 @@ function get_kubeasz() { # check if kubeasz already existed [[ -d "/etc/ansible/roles/kube-node" ]] && { echo "[WARN] kubeasz already existed"; return 0; } - echo -e "[INFO] \033[33mdownloading kubeasz\033[0m $KUBEASZ_VER" && \ - docker pull easzlab/kubeasz:${KUBEASZ_VER} && \ - echo "[INFO] run a temporary container" && \ - docker run -d --name temp_easz easzlab/kubeasz:${KUBEASZ_VER} && \ - mv "/etc/ansible/down/docker-${DOCKER_VER}.tgz" /tmp && \ + echo -e "[INFO] \033[33mdownloading kubeasz\033[0m $KUBEASZ_VER" + echo "[INFO] run a temporary container" + docker run -d --name temp_easz easzlab/kubeasz:${KUBEASZ_VER} || { echo "[ERROR] download failed."; exit 1; } + + [[ -f "/etc/ansible/down/docker-${DOCKER_VER}.tgz" ]] && mv "/etc/ansible/down/docker-${DOCKER_VER}.tgz" /tmp + rm -rf /etc/ansible && \ echo "[INFO] cp kubeasz code from the temporary container" && \ docker cp temp_easz:/etc/ansible /etc/ansible && \ echo "[INFO] stop&remove temporary container" && \ - docker rm -f temp_easz && \ - mv "/tmp/docker-${DOCKER_VER}.tgz" /etc/ansible/down + docker rm -f temp_easz + + [[ -f "/tmp/docker-${DOCKER_VER}.tgz" ]] && mv "/tmp/docker-${DOCKER_VER}.tgz" /etc/ansible/down + return 0 } function get_k8s_bin() { @@ -136,6 +167,19 @@ function get_ext_bin() { rm -rf /extra_bin_tmp } +function get_sys_pkg() { + [[ -f "/etc/ansible/down/packages/chrony_xenial.tar.gz" ]] && { echo "[WARN] system packages existed"; return 0; } + + echo -e "[INFO] \033[33mdownloading system packages\033[0m kubeasz-sys-pkg:$SYS_PKG_VER" + docker pull easzlab/kubeasz-sys-pkg:${SYS_PKG_VER} && \ + echo "[INFO] run a temporary container" && \ + docker run -d --name temp_sys_pkg easzlab/kubeasz-sys-pkg:${SYS_PKG_VER} && \ + echo "[INFO] cp system packages" && \ + docker cp temp_sys_pkg:/packages /etc/ansible/down && \ + echo "[INFO] stop&remove temporary container" && \ + docker rm -f temp_sys_pkg +} + function get_offline_image() { # images needed by k8s cluster calicoVer=v3.4.4 @@ -143,7 +187,7 @@ function get_offline_image() { dashboardVer=v1.10.1 flannelVer=v0.11.0-amd64 heapsterVer=v1.5.4 - metricsVer=v0.3.2 + metricsVer=v0.3.3 pauseVer=3.1 traefikVer=v1.7.12 @@ -186,6 +230,10 @@ function get_offline_image() { docker pull traefik:${traefikVer} && \ docker save -o ${imageDir}/traefik_${traefikVer}.tar traefik:${traefikVer} fi + if [[ ! -f "$imageDir/kubeasz_$KUBEASZ_VER.tar" ]];then + docker pull easzlab/kubeasz:${KUBEASZ_VER} && \ + docker save -o ${imageDir}/kubeasz_${KUBEASZ_VER}.tar easzlab/kubeasz:${KUBEASZ_VER} + fi } function download_all() { @@ -193,6 +241,7 @@ function download_all() { get_kubeasz && \ get_k8s_bin && \ get_ext_bin && \ + get_sys_pkg && \ get_offline_image } @@ -203,7 +252,24 @@ function start_kubeasz_docker() { host_if=$(ip route|grep default|cut -d' ' -f5) host_ip=$(ip a|grep "$host_if$"|awk '{print $2}'|cut -d'/' -f1) echo "[INFO] get host IP: $host_ip" - + + # allow ssh login using key locally + if [[ ! -e /root/.ssh/id_rsa ]]; then + echo "[INFO] generate ssh key pair" + ssh-keygen -t rsa -b 2048 -N '' -f /root/.ssh/id_rsa > /dev/null + cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys + ssh-keyscan -t ecdsa -H "$host_ip" >> /root/.ssh/known_hosts + fi + + # create a link '/usr/bin/python' in Ubuntu1604 + if [[ ! -e /usr/bin/python && -e /etc/debian_version ]]; then + echo "[INFO] create a soft link '/usr/bin/python'" + ln -s /usr/bin/python3 /usr/bin/python + fi + + # + docker load -i /etc/ansible/down/kubeasz_${KUBEASZ_VER}.tar + # run kubeasz docker container echo "[INFO] run kubeasz in a container" docker run --detach \ @@ -215,7 +281,7 @@ function start_kubeasz_docker() { --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 \ - easzlab/kubeasz:${KUBEASZ_VER} + easzlab/kubeasz:${KUBEASZ_VER} sleep 36000 } function clean_container() { @@ -233,6 +299,8 @@ Usage: easzup [options] [args] -d set docker-ce version, default "$DOCKER_VER" -e set kubeasz-ext-bin version, default "$EXT_BIN_VER" -k set kubeasz-k8s-bin version, default "$K8S_BIN_VER" + -m set docker registry mirrors, default "CN"(used in Mainland,China) + -p set kubeasz-sys-pkg version, default "$SYS_PKG_VER" -z set kubeasz version, default "$KUBEASZ_VER" see more at https://github.com/kubeasz/dockerfiles @@ -248,8 +316,9 @@ function main() { [[ "$#" -eq 0 ]] && { usage >&2; exit 1; } + export REGISTRY_MIRROR="CN" ACTION="" - while getopts "CDSd:e:k:z:" OPTION; do + while getopts "CDSd:e:k:m:p:z:" OPTION; do case "$OPTION" in C) ACTION="clean_container" @@ -269,6 +338,12 @@ function main() { k) export K8S_BIN_VER="$OPTARG" ;; + m) + export REGISTRY_MIRROR="$OPTARG" + ;; + p) + export SYS_PKG_VER="$OPTARG" + ;; z) export KUBEASZ_VER="$OPTARG" ;;