fix: 初始化脚本tools/easzup

dev1
gjmzj 2019-06-16 16:10:50 +08:00
parent 90a1fd2c20
commit 9f73d9392f
1 changed files with 162 additions and 151 deletions

View File

@ -1,31 +1,52 @@
#!/bin/bash
#--------------------------------------------------
# This script is for:
# 1. to download the scripts/binaries/images needed for installing a k8s cluster with kubeasz
# 2. to run kubeasz in a container
# @author: gjmzj
# @usage: ./easzup
# @repo: https://github.com/easzlab/kubeasz
# @ref: https://github.com/kubeasz/dockerfiles
#--------------------------------------------------
set -o nounset
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.0
export K8S_BIN_VER=v1.14.3
export EXT_BIN_VER=0.3.0
function install_docker() {
systemctl status docker|grep Active|grep -q running && { echo "[WARN] dockerd already running!"; return 0; }
# 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; }
systemctl status containerd|grep Active|grep -q running && { echo "[ERROR] containerd running, but unsupported!"; exit 1; }
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"
wget -c -P /etc/ansible/down "$DOCKER_URL" || { echo "[ERROR] downloading docker failed"; exit 1; }
fi
mkdir -p /opt/kube/bin /etc/docker
echo -e "[INFO] \033[33mdownloading docker\033[0m $DOCKER_VER"
wget -c https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/static/stable/x86_64/docker-$DOCKER_VER.tgz
tar zxf docker-$DOCKER_VER.tgz
mv docker/* /opt/kube/bin
rm -rf docker*
ln -sf /opt/kube/bin/docker /bin
tar zxf /etc/ansible/down/docker-${DOCKER_VER}.tgz -C /etc/ansible/down
mv /etc/ansible/down/docker/* /opt/kube/bin
ln -sf /opt/kube/bin/docker /bin/docker
echo "[INFO] generate docker service file"
echo '[Unit]
cat > /etc/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.io
[Service]
Environment="PATH=/opt/kube/bin:/bin:/sbin:/usr/bin:/usr/sbin"
ExecStart=/opt/kube/bin/dockerd
ExecStartPost=/sbin/iptables -I FORWARD -s 0.0.0.0/0 -j ACCEPT
ExecReload=/bin/kill -s HUP $MAINPID
ExecReload=/bin/kill -s HUP \$MAINPID
Restart=on-failure
RestartSec=5
LimitNOFILE=infinity
@ -34,10 +55,12 @@ LimitCORE=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target' > /etc/systemd/system/docker.service
WantedBy=multi-user.target
EOF
echo "[INFO] generate docker config file"
echo '{
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": [
"https://dockerhub.azk8s.cn",
"https://docker.mirrors.ustc.edu.cn",
@ -51,7 +74,8 @@ WantedBy=multi-user.target' > /etc/systemd/system/docker.service
"max-file": "3"
},
"data-root": "/var/lib/docker"
}' > /etc/docker/daemon.json
}
EOF
echo "[INFO] enable and start docker"
systemctl enable docker
@ -59,55 +83,54 @@ WantedBy=multi-user.target' > /etc/systemd/system/docker.service
}
function get_kubeasz() {
if [ -d "/etc/ansible/roles/kube-node" ];then
echo "[WARN] kubeasz already Initialized."
else
echo -e "[INFO] \033[33mdownloading kubeasz\033[0m $KUBEASZ_VER"
rm -rf /etc/ansible
docker pull easzlab/kubeasz:$KUBEASZ_VER
echo "[INFO] run a temporary container"
docker run -d --name temp_easz easzlab/kubeasz:$KUBEASZ_VER
echo "[INFO] init kubeasz code"
docker cp temp_easz:/etc/ansible /etc/ansible
echo "[INFO] stop&remove temporary container"
docker rm -f temp_easz
fi
# check if kubeasz already existed
[[ -d "/etc/ansible/roles/kube-node" ]] && { echo "[WARN] kubeasz already existed"; return 0; }
# temporarily move docker binaries
[[ -f "/etc/ansible/down/docker-${DOCKER_VER}.tgz" ]] && mv "/etc/ansible/down/docker-${DOCKER_VER}.tgz" /tmp
rm -rf /etc/ansible && \
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} && \
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 && \
[[ -f "/tmp/docker-${DOCKER_VER}.tgz" ]] && mv "/tmp/docker-${DOCKER_VER}.tgz" /etc/ansible/down || true
}
function get_k8s_bin() {
if [ -f "/etc/ansible/bin/kubelet" ];then
echo "[WARN] kubernetes:$K8S_BIN_VER binaries already existed."
else
[[ -f "/etc/ansible/bin/kubelet" ]] && { echo "[WARN] kubernetes binaries existed"; return 0; }
echo -e "[INFO] \033[33mdownloading kubernetes\033[0m $K8S_BIN_VER binaries"
docker pull easzlab/kubeasz-k8s-bin:$K8S_BIN_VER
echo "[INFO] run a temporary container"
docker run -d --name temp_k8s_bin easzlab/kubeasz-k8s-bin:$K8S_BIN_VER
echo "[INFO] cp k8s binaries"
docker cp temp_k8s_bin:/k8s /k8s_bin_tmp && mv /k8s_bin_tmp/* /etc/ansible/bin
echo "[INFO] stop&remove temporary container"
docker rm -f temp_k8s_bin
docker pull easzlab/kubeasz-k8s-bin:${K8S_BIN_VER} && \
echo "[INFO] run a temporary container" && \
docker run -d --name temp_k8s_bin easzlab/kubeasz-k8s-bin:${K8S_BIN_VER} && \
echo "[INFO] cp k8s binaries" && \
docker cp temp_k8s_bin:/k8s /k8s_bin_tmp && \
mv /k8s_bin_tmp/* /etc/ansible/bin && \
echo "[INFO] stop&remove temporary container" && \
docker rm -f temp_k8s_bin && \
rm -rf /k8s_bin_tmp
fi
}
function get_ext_bin() {
if [ -f "/etc/ansible/bin/etcdctl" ];then
echo "[WARN] extral binaries already existed."
else
[[ -f "/etc/ansible/bin/etcdctl" ]] && { echo "[WARN] extral binaries existed"; return 0; }
echo -e "[INFO] \033[33mdownloading extral binaries\033[0m kubeasz-ext-bin:$EXT_BIN_VER"
docker pull easzlab/kubeasz-ext-bin:$EXT_BIN_VER
echo "[INFO] run a temporary container"
docker run -d --name temp_ext_bin easzlab/kubeasz-ext-bin:$EXT_BIN_VER
echo "[INFO] cp extral binaries"
docker cp temp_ext_bin:/extra /extra_bin_tmp && mv /extra_bin_tmp/* /etc/ansible/bin
echo "[INFO] stop&remove temporary container"
docker rm -f temp_ext_bin
docker pull easzlab/kubeasz-ext-bin:${EXT_BIN_VER} && \
echo "[INFO] run a temporary container" && \
docker run -d --name temp_ext_bin easzlab/kubeasz-ext-bin:${EXT_BIN_VER} && \
echo "[INFO] cp extral binaries" && \
docker cp temp_ext_bin:/extra /extra_bin_tmp && \
mv /extra_bin_tmp/* /etc/ansible/bin && \
echo "[INFO] stop&remove temporary container" && \
docker rm -f temp_ext_bin && \
rm -rf /extra_bin_tmp
fi
}
function get_offline_image() {
calicoVer=v3.4.4
corednsVer=1.5.0
dashboardVer=v1.10.1
@ -118,63 +141,60 @@ function get_offline_image() {
traefikVer=v1.7.12
imageDir=/etc/ansible/down
[ -d "$imageDir" ] || { echo "[ERROR] $imageDir not existed!"; exit 1; }
[[ -d "$imageDir" ]] || { echo "[ERROR] $imageDir not existed!"; exit 1; }
echo -e "[INFO] \033[33mdownloading offline images\033[0m"
if [ ! -f "$imageDir/calico_${calicoVer}.tar" ];then
docker pull calico/cni:$calicoVer
docker pull calico/kube-controllers:$calicoVer
docker pull calico/node:$calicoVer
docker save -o $imageDir/calico_${calicoVer}.tar calico/cni:$calicoVer calico/kube-controllers:$calicoVer calico/node:$calicoVer
if [[ ! -f "$imageDir/calico_$calicoVer.tar" ]];then
docker pull calico/cni:${calicoVer} && \
docker pull calico/kube-controllers:${calicoVer} && \
docker pull calico/node:${calicoVer} && \
docker save -o ${imageDir}/calico_${calicoVer}.tar calico/cni:${calicoVer} calico/kube-controllers:${calicoVer} calico/node:${calicoVer}
fi
if [ ! -f "$imageDir/coredns_${corednsVer}.tar" ];then
docker pull coredns/coredns:$corednsVer
docker save -o $imageDir/coredns_${corednsVer}.tar coredns/coredns:$corednsVer
if [[ ! -f "$imageDir/coredns_$corednsVer.tar" ]];then
docker pull coredns/coredns:${corednsVer} && \
docker save -o ${imageDir}/coredns_${corednsVer}.tar coredns/coredns:${corednsVer}
fi
if [ ! -f "$imageDir/dashboard_${dashboardVer}.tar" ];then
docker pull mirrorgooglecontainers/kubernetes-dashboard-amd64:$dashboardVer
docker save -o $imageDir/dashboard_${dashboardVer}.tar mirrorgooglecontainers/kubernetes-dashboard-amd64:$dashboardVer
if [[ ! -f "$imageDir/dashboard_$dashboardVer.tar" ]];then
docker pull mirrorgooglecontainers/kubernetes-dashboard-amd64:${dashboardVer} && \
docker save -o ${imageDir}/dashboard_${dashboardVer}.tar mirrorgooglecontainers/kubernetes-dashboard-amd64:${dashboardVer}
fi
if [ ! -f "$imageDir/flannel_${flannelVer}.tar" ];then
docker pull easzlab/flannel:$flannelVer
docker save -o $imageDir/flannel_${flannelVer}.tar easzlab/flannel:$flannelVer
if [[ ! -f "$imageDir/flannel_$flannelVer.tar" ]];then
docker pull easzlab/flannel:${flannelVer} && \
docker save -o ${imageDir}/flannel_${flannelVer}.tar easzlab/flannel:${flannelVer}
fi
if [ ! -f "$imageDir/heapster_${heapsterVer}.tar" ];then
docker pull mirrorgooglecontainers/heapster-amd64:$heapsterVer
docker save -o $imageDir/heapster_${heapsterVer}.tar mirrorgooglecontainers/heapster-amd64:$heapsterVer
if [[ ! -f "$imageDir/heapster_$heapsterVer.tar" ]];then
docker pull mirrorgooglecontainers/heapster-amd64:${heapsterVer} && \
docker save -o ${imageDir}/heapster_${heapsterVer}.tar mirrorgooglecontainers/heapster-amd64:${heapsterVer}
fi
if [ ! -f "$imageDir/metrics-server_${metricsVer}.tar" ];then
docker pull mirrorgooglecontainers/metrics-server-amd64:$metricsVer
docker save -o $imageDir/metrics-server_${metricsVer}.tar mirrorgooglecontainers/metrics-server-amd64:$metricsVer
if [[ ! -f "$imageDir/metrics-server_$metricsVer.tar" ]];then
docker pull mirrorgooglecontainers/metrics-server-amd64:${metricsVer} && \
docker save -o ${imageDir}/metrics-server_${metricsVer}.tar mirrorgooglecontainers/metrics-server-amd64:${metricsVer}
fi
if [ ! -f "$imageDir/pause_${pauseVer}.tar" ];then
docker pull mirrorgooglecontainers/pause-amd64:$pauseVer
docker save -o $imageDir/pause_${pauseVer}.tar mirrorgooglecontainers/pause-amd64:$pauseVer
if [[ ! -f "$imageDir/pause_$pauseVer.tar" ]];then
docker pull mirrorgooglecontainers/pause-amd64:${pauseVer} && \
docker save -o ${imageDir}/pause_${pauseVer}.tar mirrorgooglecontainers/pause-amd64:${pauseVer}
fi
if [ ! -f "$imageDir/traefik_${traefikVer}.tar" ];then
docker pull traefik:$traefikVer
docker save -o $imageDir/traefik_${traefikVer}.tar traefik:$traefikVer
if [[ ! -f "$imageDir/traefik_$traefikVer.tar" ]];then
docker pull traefik:${traefikVer} && \
docker save -o ${imageDir}/traefik_${traefikVer}.tar traefik:${traefikVer}
fi
}
function download_all() {
install_docker
get_kubeasz
get_k8s_bin
get_ext_bin
install_docker && \
get_kubeasz && \
get_k8s_bin && \
get_ext_bin && \
get_offline_image
}
function start_kubeasz_docker() {
if [ ! -d "/etc/ansible/roles/kube-node" ];then
echo "[ERROR] not initialized. try 'easzup -D' first."
exit 1
fi
[[ -d "/etc/ansible/roles/kube-node" ]] || { echo "[ERROR] not initialized. try 'easzup -D' first."; exit 1; }
# 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)
host_ip=$(ip a|grep "$host_if"|awk 'NR==2{print $2}'|cut -d'/' -f1)
echo "[INFO] get host IP: $host_ip"
# run kubeasz docker container
@ -182,13 +202,13 @@ function start_kubeasz_docker() {
docker run --detach \
--name kubeasz \
--restart always \
--env HOST_IP=$host_ip \
--env HOST_IP="$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 \
easzlab/kubeasz:$KUBEASZ_VER
easzlab/kubeasz:${KUBEASZ_VER}
}
function clean_container() {
@ -212,35 +232,18 @@ see more at https://github.com/kubeasz/dockerfiles
EOF
}
function process_cmd() {
echo -e "[INFO] \033[33mAction begin\033[0m : $ACTION"
$ACTION || { echo -e "[ERROR] \033[31mAction failed\033[0m : $ACTION"; return 1; }
echo -e "[INFO] \033[32mAction successed\033[0m : $ACTION"
}
### Main Lines ##################################################
if readlink /proc/$$/exe | grep -q "dash"; then
echo "[ERROR] This script needs to be run with bash, not sh"
exit 1
fi
function main() {
# check if use bash shell
readlink /proc/$$/exe|grep -q "dash" && { echo "[ERROR] you should use bash shell, not sh"; exit 1; }
# check if use with root
[[ "$EUID" -ne 0 ]] && { echo "[ERROR] you should run this script as root"; exit 1; }
if [[ "$EUID" -ne 0 ]]; then
echo "[ERROR] Sorry, you need to run this as root"
exit 1
fi
[ "$#" -gt 0 ] || { usage >&2; exit 2; }
# default version, can be overridden by cmd line options
export DOCKER_VER=18.09.6
export KUBEASZ_VER=1.3.0
export K8S_BIN_VER=v1.14.3
export EXT_BIN_VER=0.3.0
[[ "$#" -eq 0 ]] && { usage >&2; exit 1; }
ACTION=""
while getopts "CDSd:e:k:z:" OPTION; do
case $OPTION in
case "$OPTION" in
C)
ACTION="clean_container"
;;
@ -251,16 +254,16 @@ while getopts "CDSd:e:k:z:" OPTION; do
ACTION="start_kubeasz_docker"
;;
d)
export DOCKER_VER=$OPTARG
export DOCKER_VER="$OPTARG"
;;
e)
export EXT_BIN_VER=$OPTARG
export EXT_BIN_VER="$OPTARG"
;;
k)
export K8S_BIN_VER=$OPTARG
export K8S_BIN_VER="$OPTARG"
;;
z)
export KUBEASZ_VER=$OPTARG
export KUBEASZ_VER="$OPTARG"
;;
?)
usage
@ -269,4 +272,12 @@ while getopts "CDSd:e:k:z:" OPTION; do
esac
done
process_cmd
[[ "$ACTION" == "" ]] && { echo "[ERROR] illegal option"; usage; exit 1; }
# excute cmd "$ACTION"
echo -e "[INFO] \033[33mAction begin\033[0m : $ACTION"
${ACTION} || { echo -e "[ERROR] \033[31mAction failed\033[0m : $ACTION"; return 1; }
echo -e "[INFO] \033[32mAction successed\033[0m : $ACTION"
}
main "$@"