重写容器化运行kubeasz的脚本

dev1
gjmzj 2019-06-12 16:04:39 +08:00
parent 3299c3beed
commit d519f55056
2 changed files with 269 additions and 72 deletions

269
tools/easzup 100755
View File

@ -0,0 +1,269 @@
#!/bin/bash
set -o nounset
set -o errexit
set -o xtrace
function install_docker() {
systemctl status docker|grep Active|grep -q running && { echo "[WARN] dockerd already running!"; return 0; }
systemctl status containerd|grep Active|grep -q running && { echo "[ERROR] containerd running, but unsupported!"; exit 1; }
mkdir -p /opt/kube/bin /etc/docker
echo "[INFO] downloading docker $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
echo "[INFO] generate docker service file"
echo '[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
Restart=on-failure
RestartSec=5
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target' > /etc/systemd/system/docker.service
echo "[INFO] generate docker config file"
echo '{
"registry-mirrors": [
"https://dockerhub.azk8s.cn",
"https://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com"
],
"max-concurrent-downloads": 10,
"log-driver": "json-file",
"log-level": "warn",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"data-root": "/var/lib/docker"
}' > /etc/docker/daemon.json
echo "[INFO] enable and start docker"
systemctl enable docker
systemctl daemon-reload && systemctl restart docker
}
function get_kubeasz() {
if [ -d "/etc/ansible/roles/kube-node" ];then
echo "[WARN] kubeasz already Initialized."
else
echo "[INFO] downloading kubeasz $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
}
function get_k8s_bin() {
if [ -f "/etc/ansible/bin/kubelet" ];then
echo "[WARN] kubernetes:$K8S_BIN_VER binaries already existed."
else
echo "[INFO] downloading kubernetes:$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 && mv /k8s_bin/* /etc/ansible/bin
echo "[INFO] stop&remove temporary container"
docker rm -f temp_k8s_bin
fi
}
function get_ext_bin() {
if [ -f "/etc/ansible/bin/etcdctl" ];then
echo "[WARN] extral binaries already existed."
else
echo "[INFO] downloading extral binaries 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 && mv /extra_bin/* /etc/ansible/bin
echo "[INFO] stop&remove temporary container"
docker rm -f temp_ext_bin
chmod +x /etc/ansible/bin/*
fi
}
function get_offline_image() {
calicoVer=v3.4.4
corednsVer=1.5.0
dashboardVer=v1.10.1
flannelVer=v0.11.0-amd64
heapsterVer=v1.5.4
metricsVer=v0.3.2
pauseVer=3.1
traefikVer=v1.7.12
imageDir=/etc/ansible/down
[ -d "$imageDir" ] || { echo "[ERROR] $imageDir not existed!"; exit 1; }
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
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
fi
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
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
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
fi
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
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
# 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 "[INFO] get host IP: $host_ip"
# run kubeasz docker container
echo "[INFO] run kubeasz in a container"
docker run --detach \
--name kubeasz \
--restart always \
--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
}
function clean_container() {
echo "[INFO] clean all running containers"
docker ps -a|awk 'NR>1{print $1}'|xargs docker rm -f
}
function usage() {
cat <<EOF
Usage: $0 [options] [args]
option: -{DdekSz}
-C stop&clean all containers
-D download all into /etc/ansible
-S start kubeasz in a container
-d <ver> set docker-ce version, default 18.09.6
-e <ver> set kubeasz-ext-bin version, default 0.3.0
-k <ver> set kubeasz-k8s-bin version, default v1.14.3
-z <ver> set kubeasz version, default 1.3.0
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
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.2.0
export K8S_BIN_VER=v1.14.3
export EXT_BIN_VER=0.3.0
ACTION=""
while getopts "CDSd:e:k:z:" OPTION; do
case $OPTION in
C)
ACTION="clean_container"
;;
D)
ACTION="download_all"
;;
S)
ACTION="start_kubeasz_docker"
;;
d)
export DOCKER_VER=$OPTARG
;;
e)
export EXT_BIN_VER=$OPTARG
;;
k)
export K8S_BIN_VER=$OPTARG
;;
z)
export KUBEASZ_VER=$OPTARG
;;
?)
usage
exit 1
;;
esac
done
process_cmd

View File

@ -1,72 +0,0 @@
#!/bin/bash
set -o nounset
set -o errexit
function usage() {
cat <<EOF
Usage:
kubeasz-docker start <kubeasz_ver> To start a kubeasz(tagged) container
kubeasz-docker clean To stop and remove all running containers
EOF
}
function start_kubeasz_docker() {
# kubeasz docker version
KUBEASZ_DOCKER_VER=jmgao1983/kubeasz:$1
echo "[INFO] VERSION $KUBEASZ_DOCKER_VER"
# init host dir
if [ -d "/etc/ansible/roles/" ];then
echo "[WARN] aleady Initialized."
else
rm -rf /etc/ansible
echo "[INFO] run a temporary container"
docker run -d --name temp_easz $KUBEASZ_DOCKER_VER
echo "[INFO] init host kubeasz directory"
docker cp temp_easz:/etc/ansible /etc/ansible
echo "[INFO] stop&remove temporary container"
docker rm -f 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 "[INFO] get host IP: $host_ip"
# run kubeasz docker container
echo "[INFO] run kubeasz in a container"
docker run --detach \
--name kubeasz \
--restart always \
--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 \
$KUBEASZ_DOCKER_VER
}
function clean_container() {
echo "[INFO] clean all running containers"
docker ps -a|awk 'NR>1{print $1}'|xargs docker rm -f
}
[ "$#" -gt 0 ] || { start_kubeasz_docker stable; exit 0; }
case "$1" in
(start)
[ "$#" -gt 1 ] || { usage >&2; exit 2; }
start_kubeasz_docker $2
;;
(clean)
clean_container
;;
(*)
usage
exit 0
;;
esac