K8S一键搭建集群脚本

#!/bin/bash
# Author: Nuoyo
# mail:2469329338@qq.com

RED="\033[31m"      # Error message
GREEN="\033[32m"    # Success message
YELLOW="\033[33m"   # Warning message
BLUE="\033[36m"     # Info message
PLAIN='\033[0m'

colorEcho() {
    echo -e "${1}${@:2}${PLAIN}"
}

checkSystem() {
    result=$(id | awk '{print $1}')
    if [[ $result != "uid=0(root)" ]]; then
        colorEcho $RED " 请以root身份执行该脚本"
        exit 1
    fi

    res=`which yum 2>/dev/null`
    if [[ "$?" != "0" ]]; then
        res=`which apt 2>/dev/null`
        if [[ "$?" != "0" ]]; then
            colorEcho $RED " 不受支持的Linux系统"
            exit 1
        fi
        PMT="apt"
        CMD_INSTALL="apt install -y "
        CMD_REMOVE="apt remove -y "
        CMD_UPGRADE="apt update && apt upgrade -y; apt autoremove -y"
    else
        PMT="yum"
        CMD_INSTALL="yum install -y "
        CMD_REMOVE="yum remove -y "
        CMD_UPGRADE="yum update -y"
    fi
	if [[ $PMT == "apt"   ]];then
	        colorEcho $RED " 不受支持的Linux系统"
            exit 1
	fi
    res=`which systemctl 2>/dev/null`
    if [[ "$?" != "0" ]]; then
        colorEcho $RED " 系统版本过低,请升级到最新版本"
        exit 1
    fi
}


preinstall() {
	hostnamectl set-hostname $ROLE-$HOSTNAME
    #$PMT clean all
    [[ "$PMT" = "apt" ]] && $PMT update
    #echo $CMD_UPGRADE | bash
    echo ""
    colorEcho $BLUE " 安装必要软件"
    if [[ "$PMT" = "yum" ]]; then
        $CMD_INSTALL epel-release
    fi
    $CMD_INSTALL curl vim docker
	res=`which curl 2>/dev/null`
    [[ "$?" != "0" ]] && $CMD_INSTALL curl
	res=`which docker 2>/dev/null`
    [[ "$?" != "0" ]] && $CMD_INSTALL docker
	systemctl enable --now docker

	colorEcho $BLUE "配置服务器环境"
    if [[ -s /etc/selinux/config ]] && grep 'SELINUX=enforcing' /etc/selinux/config; then
        sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config
        setenforce 0
    fi
	echo 1 > /proc/sys/net/ipv4/ip_forward
	echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
	echo 'net.ipv4.ip_forward = 1' >>  /etc/sysctl.conf
	sysctl -p
	swapoff -a
}

installk8s(){
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=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
	colorEcho $BLUE '安装k8s组件'
	$CMD_INSTALL --nogpgcheck kubelet-1.23.5-0 kubeadm-1.23.5-0 kubectl-1.23.5-0
	systemctl enable --now kubelet

}

getip(){
	colorEcho $BLUE " 请选择获取本机ip/vip的方式:"
    echo "   1)网络获取"
    echo "   2)手动输入"
    read -p " 请选择获取本机ip/vip的方式(默认联网获取):" answer
	if [[ -z "$answer" ]]; then
        METHOD="network"
    else
        case $answer in
        1)
            METHOD="network"
            ;;
		2)
            METHOD="matual"
            ;;
        *)
            colorEcho $RED " 无效的选择,使用默认角色"
            METHOD="network"
        esac
    fi
    echo ""
    colorEcho $BLUE " 获取本机ip/vip的方式: $METHOD"
	if [[ $METHOD == 'network' ]];then
		ip=`curl -s icanhazip.com`
	else
		read -p " 请输入本机ip/vip:" ip
	fi
	colorEcho $YELLOW "本机ip/vip地址为: $ip"
	read -p '确认安装请按回车键(CTRL+C退出脚本)' a
}


init(){
cat <<-EOF >/root/kubeadm_init.yaml
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
kubernetesVersion: v1.23.5
apiServer:
  certSANs:
  - "$ip"
controlPlaneEndpoint: "$ip:6443"
networking:
  podSubnet: 10.244.0.0/16
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
EOF
	kubeadm init --config=/root/kubeadm_init.yaml|tee /root/kubeinit.log
	mkdir -p $HOME/.kube
	cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
	chown $(id -u):$(id -g) $HOME/.kube/config
	kubectl apply -f 
	colorEcho $GREEN 'k8s安装完成'
}

greplog(){
        colorEcho $RED "关键命令,请注意保存"
        colorEcho $BLUE "如需部署集群"
        colorEcho $BLUE "请在其他master节点中执行下面命令"
        echo ` grep -E  -B 2 '\\-\\-control\-plane' /root/kubeinit.log|sed 's/\\\\//g'`
        colorEcho $BLUE "请在其他node节点执行完脚本后执行下面命令"
        echo `grep -E  -B 2 '\\-\\-control\-plane' /root/kubeinit.log |grep -v control|sed 's/\\\\//g'`
}


menu(){
	checkSystem
	colorEcho $BLUE " 请选择本机的角色:"
    echo "   1)master"
    echo "   2)node"
    read -p " 请选择k8s角色(默认master)" answer
	if [[ -z "$answer" ]]; then
        ROLE="master"
    else
        case $answer in
        1)
            ROLE="master"
            ;;
		2)
            ROLE="node"
            ;;
        *)
            colorEcho $RED " 无效的选择,使用默认角色"
            ROLE="master"
        esac
    fi
    echo ""
    colorEcho $BLUE " 当前k8s角色: $ROLE"
	
	if [[ $ROLE == 'master' ]];then
		colorEcho $BLUE " 检测到您设置的角色为master,请选择行为:"
		echo "   1)建立新集群"
		echo "   2)加入旧集群"
		read -p " 请选择master节点行为(默认建立新集群)" answer
		if [[ -z "$answer" ]]; then
			ACTION="new"
		else
			case $answer in
			1)
				ACTION="new"
				;;
			2)
				ACTION="old"
				;;
			*)
				colorEcho $RED " 无效的选择,使用默认行为"
				ACTION="new"
			esac
		fi
		echo ""
		colorEcho $BLUE " 当前k8s节点行为: $ACTION"
		if [[ $ACTION == "new" ]];then
			getip
			preinstall
			installk8s
			init
			greplog
		else
			preinstall
			installk8s
			colorEcho $BLUE " master节点安装完成,请在主master节点日志中拿取命令加入主节点"
			
		fi

	else
		preinstall
		installk8s
		colorEcho $BLUE " node节点安装完成,请在主master节点日志中拿取命令加入主节点"
	fi
}

main(){
	menu
}
main

K8S一键搭建集群脚本插图

阅读剩余
THE END
诺言博客