Linux环境Kubernetes自动化安装脚本

2023-12-18 16:11:19 浏览数 (1)

Kubernetes 简介

Kubernetes是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效(powerful),Kubernetes提供了应用部署,规划,更新,维护的一种机制。

Kubernetes一个核心的特点就是能够自主的管理容器来保证云平台中的容器按照用户的期望状态运行着(比如用户想让apache一直运行,用户不需要关心怎么去做,Kubernetes会自动去监控,然后去重启,新建,总之,让apache一直提供服务),管理员可以加载一个微型服务,让规划器来找到合适的位置,同时,Kubernetes也系统提升工具以及人性化方面,让用户能够方便的部署自己的应用(就像canary deployments)。

此脚本使用的是kubeadm方式安装,有需要的朋友可以参考下。 {tabs-pane label="系统环境"} 操作系统:Centos 7.9 {/tabs-pane} {tabs-pane label="脚本功能"} 1、支持kubernetes版本选择

2、支持Dcoker-ce版本选择

3、支持Master、Node节点安装

注意:Node需要跟Master做好免密钥哦 {collapse-item label="[root@localhost ~]# vim auto_install_kubernetes.sh" open}

代码语言:javascript复制
#!/bin/bash
#Date:2020-05-08 19:23:00
#Author Blog:
#    https://blog.ayou.space/
#About the Author
#    BY:废柴阿尤
#    Mail:llswdhh@163.com
#    QQ:1878302194
#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 "33[32mVersion:1.19.0 Available...33[0m"
echo -e "33[32mVersion:1.18.8 Available...33[0m"
echo -e "33[32mVersion:1.18.6 Available...33[0m"
echo -e "33[32mVersion:1.18.5 Available...33[0m"
echo -e "33[32mVersion:1.18.4 Available...33[0m"
echo -e "33[32mVersion:1.18.3 Available...33[0m"
echo -e "33[32mVersion:1.18.2 Available...33[0m"
echo -e "33[32mVersion:1.18.1 Available...33[0m"
echo -e "33[32mVersion:1.18.0 Available...33[0m"
echo -e "33[32mVersion:1.17.11 Available...33[0m"
echo -e "33[32mVersion:1.17.9 Available...33[0m"
echo -e "33[32mVersion:1.17.8 Available...33[0m"
echo -e "33[32mVersion:1.17.7 Available...33[0m"
echo -e "33[32mVersion:1.17.6 Available...33[0m"
echo -e "33[32mVersion:1.17.5 Available...33[0m"
echo -e "33[32mVersion:1.17.4 Available...33[0m"
echo -e "33[32mVersion:1.17.3 Available...33[0m"
echo -e "33[32mVersion:1.17.2 Available...33[0m"
echo -e "33[32mVersion:1.17.1 Available...33[0m"
echo -e "33[32mVersion:1.17.0 Available...33[0m"
}
function system_info () {
echo -e "33[32m-------------System Infomation-------------33[0m"                                
echo -e "33[32m>>>>>>    系统运行时间:${Uptime_day}${Uptime} 33[0m"
echo -e "33[32m>>>>>>    操作系统: ${SYS_VERSION} 33[0m"                                          
echo -e "33[32m>>>>>>    内网IP: ${IPADDR} 33[0m"                          
echo -e "33[32m>>>>>>    CPU型号:${CPU_Model} 33[0m"                       
echo -e "33[32m>>>>>>    CPU核数: ${CPU_NUM} 33[0m"                        
echo -e "33[32m>>>>>>    ${DISK_INFO} 33[0m"                               
echo -e "33[32m>>>>>>    ${DISK_Avail} 33[0m"                      
echo -e "33[32m>>>>>>    ${MEM_INFO} 33[0m"                                
echo -e "33[32m>>>>>>    ${MEM_Avail} 33[0m"                               
echo -e "33[32m>>>>>>    ${LOAD_INFO} 33[0m"                               
}
 
function Install_Kubernetes_Master (){
system_info
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    正在安装Kubernetes版本:${Version}33[0m"
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    一键安装Kubernetes集群_Master端33[0m"
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    配置腾讯yum源33[0m"
[ -f $(which wget) ] || yum -y install wget >/dev/null
wget -q http://mirrors.cloud.tencent.com/repo/centos7_base.repo
mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
mv centos7_base.repo /etc/yum.repos.d
yum clean all >/dev/null
yum makecache >/dev/null
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    更新系统33[0m"
yum -y update >/dev/null
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    关闭防火墙、selinux33[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 "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    配置主机名33[0m"
hostnamectl set-hostname master
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    配置hosts33[0m"
echo "${IPADDR} master" >>/etc/hosts
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    关闭swap,注释swap分区33[0m"
swapoff -a
sed -i '/swap/s/^/#/g' /etc/fstab
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    配置内核参数,将桥接的IPv4流量传递到iptables的链33[0m"
cat >/etc/sysctl.d/k8s.conf </dev/null
echo "1" > /proc/sys/net/ipv4/ip_forward
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    安装Docker-ce、配置开机自启动、启动Docker33[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 "33[32m================================================33[0m"
        echo -e "33[32m>>>>>>    Docker Start Success...33[0m"
    else
        echo -e "33[32m================================================33[0m"
        echo -e "33[32m>>>>>>    Docker Start Failed...33[0m"
        exit 1
    fi
else
    echo -e "33[32m================================================33[0m"
    echo -e "33[32m>>>>>>    Docker版本:$(docker --version |awk -F ',' '{print $1}') 33[0m"
fi
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    添加阿里云docker仓库加速器33[0m"
mkdir -p /etc/docker
cat >/etc/docker/daemon.json <>>>>>    添加阿里kubernetes源33[0m"
cat >/etc/yum.repos.d/kubernetes.repo <>>>>>    安装kubectl、kubelet、kubeadm33[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 "33[32m================================================33[0m"
        action "kubelet-${Version} Start Success..." /bin/true
    else
        echo -e "33[32m================================================33[0m"
        action "kubelet-${Version} Start Failed..." /bin/false
        exit 1
    fi
else
    action "kubelet-${Version} Install Failed..." /bin/false
    exit 1
fi
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    初始化k8s集群,这里指定版本为${Version}33[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 "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    kubectl命令补全功能33[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 "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    安装calico网络33[0m"
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    安装kubernetes-dashboard33[0m"
kubectl create -f https://mirrors.yangxingzhen.com/kubernetes/recommended.yaml
sleep 60 >/dev/null
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    查看凭证,获取token33[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 "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    查看node33[0m"
kubectl get node
sleep 60 >/dev/null
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    查看pod33[0m"
kubectl get pod --all-namespaces
sleep 60 >/dev/null 
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    查看service33[0m"
kubectl get svc -n kubernetes-dashboard
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    授权kubernetes-dashboard,防止找不到namespace资源33[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 "33[32m==========================================================================33[0m"
echo -e "33[32mKubernetes ${Version} 部署完成...33[0m"
echo -e "33[32m通过浏览器输入https://${IPADDR}:30000访问kubernetes-dashboard页面33[0m"
echo -e "33[32mToken获取:kubectl describe secrets -n kubernetes-dashboard ${Token} |grep token |awk 'NR==3 {print $2}'33[0m"
echo -e "33[32m==========================================================================33[0m"
}
function Install_Kubernetes_Node (){
IPADDR=$(hostname -I |awk '{print $1}')
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    正在安装Kubernetes版本:${Version}33[0m"
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    一键安装Kubernetes集群_Node端33[0m"
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    配置网易yum源33[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 "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    更新系统33[0m"
yum -y update >/dev/null
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    关闭防火墙、selinux33[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 "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    配置主机名33[0m"
NUM=$(ssh root@${Master_IP} "awk '/node/ {print $2}' /etc/hosts |sed -n '$p' |awk -F 'node' '{print $2}'")
[ -z ${NUM} ] && NUM="0"
hostnamectl set-hostname node$(expr ${NUM}   1)
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    配置Hosts33[0m"
echo "${Master_IP} master" >>/etc/hosts
echo "${IPADDR} node$(expr ${NUM}   1)" >>/etc/hosts
# 添加Hosts到Master端
ssh root@${Master_IP} "echo "${IPADDR} node$(expr ${NUM}   1)" >>/etc/hosts"
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    关闭swap,注释swap分区33[0m"
swapoff -a
sed -i '/swap/s/^/#/g' /etc/fstab
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    配置内核参数,将桥接的IPv4流量传递到iptables的链33[0m"
cat >/etc/sysctl.d/k8s.conf </dev/null
echo "1" > /proc/sys/net/ipv4/ip_forward
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    安装Docker-ce、配置开机自启动、启动Docker33[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 "33[32m================================================33[0m"
        echo -e "33[32m>>>>>>    Docker Start Success...33[0m"
    else
        echo -e "33[32m================================================33[0m"
        echo -e "33[32m>>>>>>    Docker Start Failed...33[0m"
        exit 1
    fi
else
    echo -e "33[32m================================================33[0m"
    echo -e "33[32m>>>>>>    Docker版本:$(docker --version |awk -F ',' '{print $1}') 33[0m"
fi
echo -e "33[32m================================================33[0m"
echo -e "33[32m>>>>>>    添加阿里云docker仓库加速器33[0m"
mkdir -p /etc/docker
cat >/etc/docker/daemon.json <>>>>>    添加阿里kubernetes源33[0m"
cat >/etc/yum.repos.d/kubernetes.repo <>>>>>    安装kubectl、kubelet、kubeadm33[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 "33[32m================================================33[0m"
        action "kubelet-${Version} Start Success..." /bin/true
    else
        echo -e "33[32m================================================33[0m"
        action "kubelet-${Version} Start Failed..." /bin/false
        exit 1
    fi
else
    action "kubelet-${Version} Install Failed..." /bin/false
    exit 1
fi
# 加入集群
ssh root@${Master_IP} "kubeadm token list" >/dev/null
if [ $? -eq 0 ];then
    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}
else
    ssh root@${Master_IP} "kubeadm token create" >/dev/null
    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}
fi
}
function Main (){
stty erase '^H'
Code=""
while true
do
    read -p "$(echo -e "33[32m请输入需要安装的节点名称[master/node]:33[0m")" Name
    if [ -z ${Name} ];then
        echo -e "33[31m输入错误,节点名称不能为空...33[0m"
    elif [ ${Name} = "master" ];then 
        while true
        do
            read -p "$(echo -e "33[32m请输入需要安装的Kubernetes版本号[Default:1.19.0],输入s/S查看可用版本:33[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 "33[32m请输入需要安装的Kubernetes版本号[Default:1.19.0],输入s/S查看可用版本:33[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
                Code="break"
            fi
        ${Code}
        done
        Code=""
        while true
        do
            read -p "$(echo -e "33[32m请输入需要Kubernetes的Master节点IP:33[0m")" Master_IP
            if [ -z ${Master_IP} ];then
                echo -e "33[31m输入错误,Master节点IP不能为空...33[0m"
            else
                Install_Kubernetes_Node
                Code="break"
            fi
        ${Code}
        done
    else
        echo -e "33[31m输入错误,节点名称不存在...33[0m"
    fi
${Code}
done
}
Main

0 人点赞