一键部署k8s
Github参考网址:https://github.com/luckman666?after=Y3Vyc29yOnYyOpK0MjAxOS0xMS0yNlQwMjo0Mjo0MlrODVtKbQ%3D%3D&tab=repositories
·
Github参考网址:
脚本自动部署k8s
参考网址:https://www.yangxinghui.com/1381.html
此脚本使用的是kubeadm安装
系统环境:
操作系统:Centos 7.8
脚本功能:
1、支持kubernetes版本选择
2、支持Dcoker-ce版本选择
3、支持Master、Node节点安装
注意:Node需要跟Master做好免密钥哦
脚本内容如下:
[root@localhost ~]# vim auto_install_kubernetes.sh
#!/bin/bash
#Date:2020-9-8 09:41:00
#Author Blog:
# https://www.yangxingzhen.com
# https://www.i7ti.cn
#Author WeChat:
# 微信公众号:小柒博客
#Author mirrors site:
# https://mirrors.yangxingzhen.com
#About the Author
# BY:YangXingZhen
# Mail:xingzhen.yang@yangxingzhen.com
# QQ:675583110
#Auto Install Kubernetes
source /etc/init.d/functions
SYS_VERSION=$(cat /etc/redhat-release)
IPADDR=$(hostname -I |awk '{print $1}')
Uptime_day=$(uptime |awk '{print $3,$4}')
CPU_NUM=$(grep -c 'processor' /proc/cpuinfo)
Uptime=$(uptime -p |awk '{print $6,$7,$8,$9}')
MEM_INFO=$(free -m |awk '/Mem/ {print "内存容量:",$2"M"}')
CPU_Model=$(awk -F: '/name/ {print $NF}' /proc/cpuinfo |uniq)
MEM_Avail=$(free -m |awk '/Mem/ {print "内存可用容量:",$4"M"}')
DISK_INFO=$(df -h |grep -w "/" |awk '{print "磁盘容量:",$1,$2}')
DISK_Avail=$(df -h |grep -w "/" |awk '{print "磁盘可用容量:",$1,$4}')
LOAD_INFO=$(uptime |awk '{print "CPU负载: "$(NF-2),$(NF-1),$NF}'|sed 's/\,//g')
if [ ${UID} -ne 0 ];then
action "请使用ROOT用户运行Kubernetes安装脚本..." /bin/false
exit 1
fi
function Kubernetes_Version (){
echo -e "\033[32mVersion:1.19.0 Available...\033[0m"
echo -e "\033[32mVersion:1.18.8 Available...\033[0m"
echo -e "\033[32mVersion:1.18.6 Available...\033[0m"
echo -e "\033[32mVersion:1.18.5 Available...\033[0m"
echo -e "\033[32mVersion:1.18.4 Available...\033[0m"
echo -e "\033[32mVersion:1.18.3 Available...\033[0m"
echo -e "\033[32mVersion:1.18.2 Available...\033[0m"
echo -e "\033[32mVersion:1.18.1 Available...\033[0m"
echo -e "\033[32mVersion:1.18.0 Available...\033[0m"
echo -e "\033[32mVersion:1.17.11 Available...\033[0m"
echo -e "\033[32mVersion:1.17.9 Available...\033[0m"
echo -e "\033[32mVersion:1.17.8 Available...\033[0m"
echo -e "\033[32mVersion:1.17.7 Available...\033[0m"
echo -e "\033[32mVersion:1.17.6 Available...\033[0m"
echo -e "\033[32mVersion:1.17.5 Available...\033[0m"
echo -e "\033[32mVersion:1.17.4 Available...\033[0m"
echo -e "\033[32mVersion:1.17.3 Available...\033[0m"
echo -e "\033[32mVersion:1.17.2 Available...\033[0m"
echo -e "\033[32mVersion:1.17.1 Available...\033[0m"
echo -e "\033[32mVersion:1.17.0 Available...\033[0m"
}
function system_info () {
echo -e "\033[32m-------------System Infomation-------------\033[0m"
echo -e "\033[32m>>>>>> 系统运行时间:${Uptime_day}${Uptime} \033[0m"
echo -e "\033[32m>>>>>> 操作系统: ${SYS_VERSION} \033[0m"
echo -e "\033[32m>>>>>> 内网IP: ${IPADDR} \033[0m"
echo -e "\033[32m>>>>>> CPU型号:${CPU_Model} \033[0m"
echo -e "\033[32m>>>>>> CPU核数: ${CPU_NUM} \033[0m"
echo -e "\033[32m>>>>>> ${DISK_INFO} \033[0m"
echo -e "\033[32m>>>>>> ${DISK_Avail} \033[0m"
echo -e "\033[32m>>>>>> ${MEM_INFO} \033[0m"
echo -e "\033[32m>>>>>> ${MEM_Avail} \033[0m"
echo -e "\033[32m>>>>>> ${LOAD_INFO} \033[0m"
}
function Install_Kubernetes_Master (){
system_info
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 正在安装Kubernetes版本:${Version}\033[0m"
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 一键安装Kubernetes集群_Master端\033[0m"
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 配置网易yum源\033[0m"
mkdir -p /etc/yum.repos.d/bak
\mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
[ -f $(which wget) ] || yum -y install wget >/dev/null
wget -q -P /etc/yum.repos.d http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum clean all >/dev/null
yum makecache >/dev/null
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 更新系统\033[0m"
yum -y update >/dev/null
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 关闭防火墙、selinux\033[0m"
SYSTEM_VERSION=$(awk -F. '{print $1}' /etc/redhat-release |awk '{print $NF}')
if [ ${SYSTEM_VERSION} -eq 6 ];then
service iptables stop
chkconfig iptables off
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0 >/dev/null
else
systemctl stop firewalld.service
systemctl disable firewalld.service
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0 >/dev/null
fi
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 配置主机名\033[0m"
hostnamectl set-hostname master
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 配置hosts\033[0m"
echo "master ${IPADDR}" >>/etc/hosts
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 关闭swap,注释swap分区\033[0m"
swapoff -a
sed -i '/swap/s/^/#/g' /etc/fstab
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 配置内核参数,将桥接的IPv4流量传递到iptables的链\033[0m"
cat >/etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
echo "1" > /proc/sys/net/ipv4/ip_forward
echo -e "\033[32m================================================\033[0m"
echo ">>>>>> 安装Docker-ce、配置开机自启动、启动Docker\033[0m"
rpm -qa |grep docker |grep -v grep >/dev/null
if [ $? -ne 0 ];then
#curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
yum -y install yum-utils device-mapper-persistent-data lvm2 >/dev/null
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
if [ ${Version} = "1.18.8" -o ${Version} = "1.19.0" ];then
version="19.03.12"
else
version="18.09.9"
fi
yum -y install docker-ce-${version} docker-ce-cli-${version} containerd.io >/dev/null
systemctl enable docker
systemctl start docker
if [ $? -eq 0 ];then
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> Docker Start Success...\033[0m"
else
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> Docker Start Failed...\033[0m"
exit 1
fi
else
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> Docker版本:$(docker --version |awk -F ',' '{print $1}') \033[0m"
fi
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 添加阿里云docker仓库加速器\033[0m"
mkdir -p /etc/docker
cat >/etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://fl791z1h.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl daemon-reload
systemctl restart docker
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 添加阿里kubernetes源\033[0m"
cat >/etc/yum.repos.d/kubernetes.repo <<EOF
[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
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 安装kubectl、kubelet、kubeadm\033[0m"
yum -y install kubectl-${Version} kubelet-${Version} kubeadm-${Version} >/dev/null
rpm -qa |grep kubelet >/dev/null
if [ $? -eq 0 ];then
systemctl enable kubelet
systemctl start kubelet
if [ $? -eq 0 ];then
echo -e "\033[32m================================================\033[0m"
action "kubelet-${Version} Start Success..." /bin/true
else
echo -e "\033[32m================================================\033[0m"
action "kubelet-${Version} Start Failed..." /bin/false
exit 1
fi
else
action "kubelet-${Version} Install Failed..." /bin/false
exit 1
fi
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 初始化k8s集群,这里指定版本为${Version}\033[0m"
#echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables
kubeadm init --kubernetes-version=${Version} \
--apiserver-advertise-address=${IPADDR} \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=172.16.0.0/16 --pod-network-cidr=172.17.0.0/16
mkdir -p ${HOME}/.kube
sudo cp -i /etc/kubernetes/admin.conf ${HOME}/.kube/config
sudo chown $(id -u):$(id -g) ${HOME}/.kube/config
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> kubectl命令补全功能\033[0m"
rpm -qa |grep bash-completion >/dev/null
if [ $? -ne 0 ];then
yum -y install bash-completion >/dev/null
source /etc/profile.d/bash_completion.sh
fi
#grep "kubectl completion bash" ~/.bashrc >/dev/null
#if [ $? -ne 0 ];then
# echo "# kubectl命令补全功能" >> ~/.bashrc
# echo "source <(kubectl completion bash)" >> ~/.bashrc
#fi
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 安装calico网络\033[0m"
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 安装kubernetes-dashboard\033[0m"
wget -q https://mirrors.yangxingzhen.com/kubernetes/recommended.yaml
kubectl create -f recommended.yaml
sleep 60 >/dev/null
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 查看凭证,获取token\033[0m"
kubectl -n kubernetes-dashboard get secret
Token=$(kubectl -n kubernetes-dashboard get secret |awk '/kubernetes-dashboard-token/ {print $1}')
#echo "Token:$(kubectl describe secrets -n kubernetes-dashboard ${Token} |grep token |awk 'NR==3 {print $2}')"
sleep 60 >/dev/null
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 查看node\033[0m"
kubectl get node
sleep 60 >/dev/null
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 查看pod\033[0m"
kubectl get pod --all-namespaces
sleep 60 >/dev/null
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 查看service\033[0m"
kubectl get svc -n kubernetes-dashboard
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 授权kubernetes-dashboard,防止找不到namespace资源\033[0m"
kubectl create clusterrolebinding serviceaccount-cluster-admin --clusterrole=cluster-admin --user=system:serviceaccount:kubernetes-dashboard:kubernetes-dashboard
# 避免时间过短导致kubernetes-dashboard正在创建中,预计运行所有任务完毕时间为:4m40s(1.19.0)
sleep 60 >/dev/null
echo -e "\033[32m==========================================================================\033[0m"
echo -e "\033[32mKubernetes ${Version} 部署完成...\033[0m"
echo -e "\033[32m通过浏览器输入https://${IPADDR}:30000访问kubernetes-dashboard页面\033[0m"
echo -e "\033[32mToken获取:kubectl describe secrets -n kubernetes-dashboard ${Token} |grep token |awk 'NR==3 {print \$2}'\033[0m"
echo -e "\033[32m==========================================================================\033[0m"
}
function Install_Kubernetes_Node (){
IPADDR=$(hostname -I |awk '{print $1}')
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 正在安装Kubernetes版本:${Version}\033[0m"
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 一键安装Kubernetes集群_Node端\033[0m"
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 配置网易yum源\033[0m"
mkdir -p /etc/yum.repos.d/bak
\mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
[ -f $(which wget) ] || yum -y install wget >/dev/null
wget -q -P /etc/yum.repos.d http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum clean all >/dev/null
yum makecache >/dev/null
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 更新系统\033[0m"
yum -y update >/dev/null
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 关闭防火墙、selinux\033[0m"
SYSTEM_VERSION=$(awk -F. '{print $1}' /etc/redhat-release |awk '{print $NF}')
if [ ${SYSTEM_VERSION} -eq 6 ];then
service iptables stop
chkconfig iptables off
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0 >/dev/null
else
systemctl stop firewalld.service
systemctl disable firewalld.service
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0 >/dev/null
fi
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 配置主机名\033[0m"
hostnamectl set-hostname node
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 配置hosts\033[0m"
echo "node ${IPADDR}" >>/etc/hosts
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 关闭swap,注释swap分区\033[0m"
swapoff -a
sed -i '/swap/s/^/#/g' /etc/fstab
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 配置内核参数,将桥接的IPv4流量传递到iptables的链\033[0m"
cat >/etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
echo "1" > /proc/sys/net/ipv4/ip_forward
echo -e "\033[32m================================================\033[0m"
echo ">>>>>> 安装Docker-ce、配置开机自启动、启动Docker\033[0m"
rpm -qa |grep docker |grep -v grep >/dev/null
if [ $? -ne 0 ];then
#curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
yum -y install yum-utils device-mapper-persistent-data lvm2 >/dev/null
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
if [ ${Version} = "1.18.8" -o ${Version} = "1.19.0" ];then
version="19.03.12"
else
version="18.09.9"
fi
yum -y install docker-ce-${version} docker-ce-cli-${version} containerd.io >/dev/null
systemctl enable docker
systemctl start docker
if [ $? -eq 0 ];then
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> Docker Start Success...\033[0m"
else
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> Docker Start Failed...\033[0m"
exit 1
fi
else
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> Docker版本:$(docker --version |awk -F ',' '{print $1}') \033[0m"
fi
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 添加阿里云docker仓库加速器\033[0m"
mkdir -p /etc/docker
cat >/etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://fl791z1h.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl daemon-reload
systemctl restart docker
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 添加阿里kubernetes源\033[0m"
cat >/etc/yum.repos.d/kubernetes.repo <<EOF
[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
echo -e "\033[32m================================================\033[0m"
echo -e "\033[32m>>>>>> 安装kubectl、kubelet、kubeadm\033[0m"
yum -y install kubectl-${Version} kubelet-${Version} kubeadm-${Version} >/dev/null
rpm -qa |grep kubelet >/dev/null
if [ $? -eq 0 ];then
systemctl enable kubelet
systemctl start kubelet
if [ $? -eq 0 ];then
echo -e "\033[32m================================================\033[0m"
action "kubelet-${Version} Start Success..." /bin/true
else
echo -e "\033[32m================================================\033[0m"
action "kubelet-${Version} Start Failed..." /bin/false
exit 1
fi
else
action "kubelet-${Version} Install Failed..." /bin/false
exit 1
fi
# 加入集群
TOKEN=$(ssh root@${Master_IP} "kubeadm token list |grep token |awk '{print \$1}' |sed -n '1p'")
Hash=$(ssh root@${Master_IP} "openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'")
kubeadm join ${Master_IP}:6443 --token ${TOKEN} --discovery-token-ca-cert-hash sha256:${Hash}
}
function Main (){
stty erase '^H'
Code=""
while true
do
read -p "$(echo -e "\033[32m请输入需要安装的节点名称[master/node]:\033[0m")" Name
if [ -z ${Name} ];then
echo -e "\033[31m输入错误,节点名称不能为空...\033[0m"
elif [ ${Name} = "master" ];then
while true
do
read -p "$(echo -e "\033[32m请输入需要安装的Kubernetes版本号[Default:1.19.0],输入s/S查看可用版本:\033[0m")" Version
if [ -z ${Version} ];then
Version="1.19.0"
Install_Kubernetes_Master
Code="break"
elif [ "${Version}" = "q" -o "${Version}" = "Q" ];then
exit 1
elif [ "${Version}" = "s" -o "${Version}" = "S" ];then
Kubernetes_Version
else
Install_Kubernetes_Master
Code="break"
fi
${Code}
done
elif [ ${Name} = "node" ];then
while true
do
read -p "$(echo -e "\033[32m请输入需要安装的Kubernetes版本号[Default:1.19.0],输入s/S查看可用版本:\033[0m")" Version
if [ -z ${Version} ];then
Version="1.19.0"
Code="break"
elif [ "${Version}" = "q" -o "${Version}" = "Q" ];then
exit 1
elif [ "${Version}" = "s" -o "${Version}" = "S" ];then
Kubernetes_Version
else
while true
do
read -p "$(echo -e "\033[32m请输入需要Kubernetes的Master节点IP:\033[0m")" Master_IP
if [ -z ${Master_IP} ];then
echo -e "\033[31m输入错误,Master节点IP不能为空...\033[0m"
else
Install_Kubernetes_Node
Code="break"
fi
${Code}
done
fi
${Code}
done
else
echo -e "\033[31m输入错误,节点名称不存在...\033[0m"
fi
${Code}
done
}
Main
脚本执行方式:
Master端
[root@Edu_1 ~]# sh auto_install_kubernetes.sh
Node端
[root@Test ~]# sh auto_install_kubernetes.sh
Master端执行
更多推荐
已为社区贡献1条内容
所有评论(0)