#!/bin/sh
set -e
COLOR="echo -e \\E[1;32m"
COLOR1="echo -e \\E[1;31m"
END="\\E[0m"
#ubuntu依赖包
ubuntu_page="
wget
apt-transport-https
ca-certificates
curl
software-properties-common
"
#centos依赖包
centos_page="
wget
yum-utils
device-mapper-persistent-data
lvm2
"
#centos7安装
install_docker_centos7() {
. /etc/init.d/functions $> /dev/null
${COLOR}"开始安装 Docker....."${END}
#安装依赖包
for PAGE in ${centos_page};do
rpm -q $PAGE &> /dev/null || yum -y -q install $PAGE
done
#添加源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && action "添加源信息成功!" || { action "添加源信息失败,请检查命令!" false ; exit; }
#更新安装docker-ce
yum makecache fast &> /dev/null
${COLOR}"正在安装Docker,请稍等!"${END}
yum -y install docker-ce-19.03.9-3.el7.x86_64 containerd.io-1.3.7-3.1.el7.x86_64 docker-ce-cli-19.03.9-3.el7.x86_64 &> /dev/null
#配置镜像加速器
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
#重载配置文件
systemctl daemon-reload
#启动服务
${COLOR}"正在启动Docker,请稍等!"${END}
sleep 2
systemctl enable --now docker && action "docker启动成功!" || action "docker启动失败,请检查配置文件!" false
}
#centos8安装
install_docker_centos8() {
. /etc/init.d/functions $> /dev/null
${COLOR}"开始安装Docker,请稍等!"${END}
#安装依赖包
for PAGE in ${centos_page};do
rpm -q $PAGE &> /dev/null || yum -y -q install $PAGE
done
yum -y install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.13-3.1.el7.x86_64.rpm &> /dev/null
#添加源信息
cat > /etc/yum.repos.d/docker.repo <<EOF
[docker]
name=docker
gpgcheck=0
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
EOF
#更新安装docker-ce
yum clean all &> /dev/null
yum -y install docker-ce-19.03.9-3.el7.x86_64 containerd.io-1.3.7-3.1.el7.x86_64 docker-ce-cli-19.03.9-3.el7.x86_64 &> /dev/null && action "docker安装完成!" || action "docker按照失败!请检查网络及Yum源!" false
#配置镜像加速器
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
#重载配置文件
systemctl daemon-reload
#启动服务
${COLOR}"正在启动Docker,请稍等!"${END}
sleep 2
systemctl enable --now docker && action "docker启动成功!" || action "docker启动失败,请检查配置文件!" false
}
#ubuntu安装
install_docker_ubuntu() {
${COLOR}"开始安装 Docker....."${END}
#更新源及安装依赖包
apt-get update &> /dev/null
for PAGE in ${ubuntu_page};do
dpkg -s $PAGE &> /dev/null || apt -y install $PAGE &> /dev/null
done
#安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
#写入软件源信息
echo 'deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic stable' >> /etc/apt/sources.list
#更新源信息
apt -y update &> /dev/null
#配置镜像加速器
mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"]
}
EOF
#安装docker-ce
${COLOR}"正在安装Docker,请稍等...."${END}
apt-get -y install docker-ce &> /dev/null && ${COLOR}"docker启动成功!"${END} || ${COLOR1}"docker启动失败,请检查配置文件!"${END}
}
install_k8s_centos() {
#添加k8s源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
#关闭交换分区(临时),永久需注释掉/etc/fastb文件中与交换分区相关内容
swapoff -a
#设置selinux
setenforce 0
#安装
${COLOR}"正在安装k8s相关软件,请稍等!"$END
yum install -y kubelet kubeadm kubectl && ${COLOR}"k8S相关软件已安装完成!"$END || ${COLOR1}"k8S相关软件安装失败,请检查!"$END
#设置开机启动
systemctl enable --now kubelet && ${COLOR}"k8S启动成功!"$END || ${COLOR}"k8S启动失败,请检查配置文件!"$END
}
install_k8s_ubuntu() {
${COLOR}"开始安装k8s,请稍等!"$END
#关闭交换分区(临时),永久需注释掉/etc/fastb文件中与交换分区相关内容
swapoff -a
#添加k8s源
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
#更新源
apt-get update
#安装
apt-get install -y kubelet kubeadm kubectl && ${COLOR}"k8S启动成功!"$END || ${COLOR}"k8S启动失败,请检查配置文件!"$END
}
#系统类型
ostype1=`awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release`
ostype2=`awk -F'"' '/^NAME/{print $2}' /etc/os-release`
if [[ $ostype2 == "CentOS Linux" ]];then
if [ $ostype1 = 8 ];then
rpm -q docker-ce &> /dev/null && { ${COLOR}"Docker已安装,开始安装k8s!"${END};install_k8s_centos; } || { install_docker_centos8;install_k8s_centos; }
elif [ $ostype1 = 7 ];then
rpm -q docker-ce &> /dev/null && { ${COLOR}"Docker已安装,开始安装k8s!"${END};install_k8s_centos; } || { install_docker_centos8;install_k8s_centos; }
fi
elif [[ $ostype2 == "Ubuntu" ]];then
dpkg -s docker-ce &> /dev/null && { ${COLOR}"Docker已安装,开始安装k8s!"${END};install_k8s_ubuntu; } || { install_docker_ubuntu;install_k8s_ubuntu; }
fi