kubeadm单机版k8s测试部署

直接上脚本

#! /usr/bin/env bash

# Comment: 一键安装 kubeadm 单机版
# Date: 2020-05-25 13:26:47
# Author: 萝卜


HOSTNAME=k8s-master
IPADDR=$(curl -s ip.me)
KUBERNETES_VERSION=1.18.0
SVC_IPS=10.96.0.0/12
POD_IPS=10.244.0.0/16
LOG_FILE=./install.log

# 公网: curl ip.me
# 私网: ip addr | awk '/^[0-9]+: / {}; /inet .*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}' 
# 交互界面的黑窗口按<——Backspace删除键时出现了^H^H^H^H^H^H^H^H^H^H^H^H,

# 安装前
function install_before()
{
	stty erase ^H
	read -r -p "Please enter the hostname Default(${HOSTNAME}): " hostname
	read -r -p "Please enter the local IP address Default(${IPADDR}): " ipaddr
	read -r -p "Please enter kubernetes version Default(v${KUBERNETES_VERSION}): " kubernetes_version

	if [[ -n "$hostname" ]];then HOSTNAME=${hostname} ;fi
	if [[ -n "$ipaddr" ]];then IPADDR=${ipaddr} ;fi
	if [[ -n "$kubernetes_version" ]];then KUBERNETES_VERSION=${kubernetes_version} ;fi

	echo "============================ 信息如下:==============================="
	echo -e "HOSTNAME: \033[36;1m ${HOSTNAME} \033[0m"
	echo -e "IPADDR: \033[36;1m ${IPADDR} \033[0m"
	echo -e "KUBERNETES_VERSION: \033[36;1m v${KUBERNETES_VERSION} \033[0m"
	echo -e "SVC_IPS: \033[36;1m ${SVC_IPS} \033[0m"
	echo -e "POD_IPS: \033[36;1m ${POD_IPS} \033[0m"
	echo -e "LOG_FILE: \033[36;1m ${LOG_FILE} \033[0m"
	echo "======================================================================"

	while :
	do
		read -r -p "Are you Sure? [Y/n]" input
		case $input in
			[yY][eE][sS]|[yY])
				return 0
				break
				;;
			[nN][oO]|[nN])
				exit 1
				;;
			*)
				echo "Invalid input...please enter [Y/n]"
				;;
		esac
	done
}


# 初始化
function Init()
{
        hostnamectl set-hostname ${HOSTNAME}
        echo "${IPADDR} ${HOSTNAME}" >> /etc/hosts
        systemctl stop firewalld
        systemctl disable firewalld
        sed -i 's/enforcing/disabled/' /etc/selinux/config
        setenforce 0
        swapoff -a
        sed -ri 's/.*swap.*/#&/' /etc/fstab
        echo -e "net.bridge.bridge-nf-call-ip6tables = 1\nnet.bridge.bridge-nf-call-iptables = 1" > /etc/sysctl.d/k8s.conf
        sysctl --system 
        yum install ntpdate -y
        ntpdate time.windows.com
}



# 安装 Docker
function Install_Docker()
{
	yum install wget -y
	wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
	yum -y install docker-ce-18.06.1.ce-3.el7
	systemctl enable docker && systemctl start docker
	docker --version
	cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://c1ncp8uc.mirror.aliyuncs.com"]
}
EOF
}


# 添加阿里云YUM软件源
function Add_Yum()
{
	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=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
}


# 安装kubeadm,kubelet和kubectl
function Install_lib()
{
	yum install -y kubelet-${KUBERNETES_VERSION} kubeadm-${KUBERNETES_VERSION} kubectl-${KUBERNETES_VERSION}
	systemctl enable kubelet
}


# 集群初始化
function Cluster_Init()
{
	kubeadm init \
		--apiserver-advertise-address=${IPADDR} \
		--image-repository registry.aliyuncs.com/google_containers \
		--kubernetes-version v${KUBERNETES_VERSION} \
		--service-cidr=${SVC_IPS} \
		--pod-network-cidr=${POD_IPS}
	[[ $? != 0 ]] && return 1
	mkdir -p $HOME/.kube
	sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
	sudo chown $(id -u):$(id -g) $HOME/.kube/config
	kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
}



function main()
{
    echo -e '\033[32m [INFO] Confirm before installation...... \033[0m'
    install_before
    echo -e "\033[32m [INFO] The system is initializing...... \033[0m"
    Init &>>${LOG_FILE}
    echo -e "\033[32m [INFO] Install to Docker...... \033[0m"
    Install_Docker &>>${LOG_FILE}
    echo -e "\033[32m [INFO] Add Alibaba cloud YUM repo source...... \033[0m"
    Add_Yum &>>${LOG_FILE}
    echo -e "\033[32m [INFO] Install to kubeadm、kubelet、kubectl...... \033[0m"
    Install_lib &>>${LOG_FILE}
    echo -e "\033[32m [INFO] Cluster initialization (it takes a long time to download the image)...... \033[0m"
    Cluster_Init &>>${LOG_FILE}
    Add_Node=$(grep -A1 "kubeadm join" ${LOG_FILE} |sed -n '{N;s/\\//;s/\n//p}')
    printf "  {\n\t\"Add_Node_Token\": ${Add_Node}\n  }\n"
}


main

Last updated