. /etc/os-release color () { RES_COL=60 MOVE_TO_COL="echo -en \\033[${RES_COL}G" SETCOLOR_SUCCESS="echo -en \\033[1;32m" SETCOLOR_FAILURE="echo -en \\033[1;31m" SETCOLOR_WARNING="echo -en \\033[1;33m" SETCOLOR_NORMAL="echo -en \E[0m" echo -n "$1" && $MOVE_TO_COL echo -n "[" if [ $2 = "success" -o $2 = "0" ] ;then ${SETCOLOR_SUCCESS} echo -n $" OK " elif [ $2 = "failure" -o $2 = "1" ] ;then ${SETCOLOR_FAILURE} echo -n $"FAILED" else ${SETCOLOR_WARNING} echo -n $"WARNING" fi ${SETCOLOR_NORMAL} echo -n "]" echo } echo -e "\e[1;32m安装kubernetes1.27.4版本\e[0m" #echo -e "\e[1;32m配置yum源\e[0m" #[ ! -d /data/bak ] && mkdir -p /data/bak #判断文件夹是否有文件 # if [ "`ls -A /etc/yum.repos.d/`" != "" ];then # mv /etc/yum.repos.d/* /data/bak # else # echo "" # fi #[ -d /mnt/cdrom ] || mkdir /mnt/cdrom #mount /dev/sr0 /mnt/cdrom #cat > /etc/yum.repos.d/base.repo <<EOF #[base] #name=CentOS #baseurl=file:///mnt/cdrom/BaseOS # https://mirror.tuna.tsinghua.edu.cn/centos/\$releasever/os/\$basearch/ # https://mirrors.huaweicloud.com/centos/\$releasever/os/\$basearch/ # https://mirrors.cloud.tencent.com/centos/\$releasever/os/\$basearch/ # https://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/ #gpgcheck=0 #[extras] #name=extras #baseurl=https://mirror.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch # https://mirrors.huaweicloud.com/centos/\$releasever/extras/\$basearch # https://mirrors.cloud.tencent.com/centos/\$releasever/extras/\$basearch # https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch # #gpgcheck=0 #enabled=1 #[epel] #name=EPEL #baseurl=https://mirror.tuna.tsinghua.edu.cn/epel/\$releasever/\$basearch # https://mirrors.cloud.tencent.com/epel/\$releasever/\$basearch/ # https://mirrors.huaweicloud.com/epel/\$releasever/\$basearch # https://mirrors.cloud.tencent.com/epel/\$releasever/\$basearch # http://mirrors.aliyun.com/epel/\$releasever/\$basearch #gpgcheck=0 #enabled=1 #EOF # yum clean all # yum makecache # yum repolist #cat >> /etc/fstab << EOF #/dev/sr0 /mnt/cdrom iso9660 defaults 0 0 #EOF #[ $? -eq 0 ] && color 已配置 0 || { color 写入失败 1;exit; } echo -e "\e[1;35m==========================================================================================================================================================================================================================================\e[0m" echo -e "\e[1;32m关闭swap\e[0m" swapoff -a sed -i '/swap/s/^/#/' /etc/fstab [ $? -eq 0 ] && color "成功 " 0 || color "失败 " 1 #echo -e "\e[1;32m关闭防火墙\e[0m" #systemctl disable --now firewalld #echo -e "\e[1;32m时间同步\e[0m" #yum -y install ntpdate #ntpdate time2.aliyun.com # 加入到crontab #*/5 * * * * /usr/sbin/ntpdate time2.aliyun.com echo -e "\e[1;35m==========================================================================================================================================================================================================================================\e[0m" echo -e "\e[1;32m修改主机名\e[0m" hostnamectl set-hostname k8s-master01 cat <<EOF >> /etc/hosts 10.0.0.21 k8s-master01 10.0.0.22 k8s-node01 EOF echo -e "\e[1;35m==========================================================================================================================================================================================================================================\e[0m" echo -e "\e[1;32m修改Linux内核参数,添加网桥过滤器和地址转发功能\e[0m" cat >> /etc/sysctl.d/kubernetes.conf <<EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 EOF modprobe br_netfilter sysctl -p /etc/sysctl.d/kubernetes.conf [ $? -eq 0 ] && color "成功 " 0 || color "失败 " 1 echo -e "\e[1;35m==========================================================================================================================================================================================================================================\e[0m" echo -e "\e[1;32m配置ipvs功能\e[0m" yum -y install ipset ipvsadm cat > /etc/sysconfig/modules/ipvs.modules <<EOF modprobe -- ip_vs modprobe -- ip_vs_rr modprobe -- ip_vs_wrr modprobe -- ip_vs_sh modprobe -- nf_conntrack_ipv4 EOF chmod +x /etc/sysconfig/modules/ipvs.modules /etc/sysconfig/modules/ipvs.modules [ $? -eq 0 ] && color "成功 " 0 || color "失败 " 1 echo -e "\e[1;35m==========================================================================================================================================================================================================================================\e[0m" echo -e "\e[1;32m安装Docker容器\e[0m" curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum makecache yum install -y yum-utils yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install docker-ce-20.10.6 docker-ce-cli-20.10.6 -y mkdir /etc/docker cat <<EOF > /etc/docker/daemon.json { "registry-mirrors": [ "https://docker.mirrors.ustc.edu.cn", "https://hub-mirror.c.163.com", "https://reg-mirror.qiniu.com", "https://registry.docker-cn.com" ], "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "200m" }, "storage-driver": "overlay2" } EOF systemctl enable --now docker [ $? -eq 0 ] && color "成功 " 0 || color "失败 " 1 echo -e "\e[1;35m==========================================================================================================================================================================================================================================\e[0m" echo -e "\e[1;32m安装cri-dockerd-0.3.1插件\e[0m" if [ -e cri-dockerd-0.3.1-3.el7.x86_64.rpm ];then echo -e "\e[1;35m 文件已存在,开始安装\e[0m" else echo -e "\e[1;31m开始下载\e[0m" wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.1/cri-dockerd-0.3.1-3.el7.x86_64.rpm if [ $? -eq 0 ];then echo -e "\e[1;32m下载成功\e[0m" else echo -e "\e[1;32m下载失败,检查下载链接\e[0m" exit fi fi #wget https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.1/cri-dockerd-0.3.1-3.el7.x86_64.rpm rpm -ivh cri-dockerd-0.3.1-3.el7.x86_64.rpm sed -i "s/^ExecStart/#&/" /usr/lib/systemd/system/cri-docker.service sed -i '10iExecStart=/usr/bin/cri-dockerd --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.7' /usr/lib/systemd/system/cri-docker.service systemctl daemon-reload && systemctl restart docker cri-docker.socket cri-docker [ $? -eq 0 ] && color "成功 " 0 || color "失败 " 1 echo -e "\e[1;35m==========================================================================================================================================================================================================================================\e[0m" echo -e "\e[1;32m配置国内yum源,安装 kubeadm、kubelet、kubectl\e[0m" 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 EOF yum install -y kubelet-1.27.4 kubeadm-1.27.4 kubectl-1.27.4 systemctl enable kubelet.service --now [ $? -eq 0 ] && color "成功 " 0 || color "失败 " 1 echo -e "\e[1;35m==========================================================================================================================================================================================================================================\e[0m" echo -e "\e[1;32m安装runc-1.1.10\e[0m" if [ -e runc.amd64 ];then echo -e "\e[1;35m 文件已存在,开始安装\e[0m" else echo -e "\e[1;31m开始下载\e[0m" wget https://github.com/opencontainers/runc/releases/download/v1.1.10/runc.amd64 if [ $? -eq 0 ];then echo -e "\e[1;32m下载成功\e[0m" else echo -e "\e[1;32m下载失败,检查下载链接\e[0m" exit fi fi sudo install -m 755 runc.amd64 /usr/local/bin/runc [ $? -eq 0 ] && color "成功 " 0 || color "失败 " 1 runc -v echo -e "\e[1;35m==========================================================================================================================================================================================================================================\e[0m" echo -e "\e[1;32m初始化\e[0m" kubeadm init --node-name=k8s-master01 --image-repository=registry.aliyuncs.com/google_containers --cri-socket=unix:///var/run/cri-dockerd.sock --apiserver-advertise-address=10.0.0.21 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 exec bash }
https://github.com/flannel-io/flannel/releases/download/v0.22.0/kube-flannel.yml
[root@localhost~]#vim kube-flannel.yml
- args:
- --ip-masq
- --kube-subnet-mgr
- --iface=ens33 #添加
[root@localhost~]#kubectl apply -f kube-flannel.yml
node节点加入
echo -e "\e[1;32mkubeadm join 结尾加上一下命令\e[0m"
echo "--cri-socket unix:///var/run/cri-dockerd.sock"
scp /etc/kubernetes/admin.conf 10.0.0.22:/etc/kubernetes/
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile # 不要忘记将admin.conf加入环境变量,这里直接使用永久生效。
source ~/.bash_profile
标签:etc,--,32m,echo,centos7.9,版本,kubernetes1.27,cri,0m From: https://www.cnblogs.com/smlience/p/17822979.html