1.脚本内容
#!/bin/bash
MASTER='172.30.8.211'
MASTER_HOSTNAME='master'
NODE_LIST=(172.30.8.212-node1)
K8S_VERSION=1.24.9
NFS_SERVER=172.30.8.212
NFS_PATH=/data/nfs
HELM_VERSION=v3.12.1
SCRIPT_PATH=/usr/local/scripts
install_kernel(){
#下载内核文件
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install kernel-lt kernel-lt-devel -y
#内核切换
grub2-set-default 0
kernel_version=`cat /boot/grub2/grub.cfg |egrep "^menuentry 'CentOS Linux"|awk -F"'" 'NR==1{print $2}'`
grub2-set-default "${kernel_version}"
grub2-editenv list
}
update_kernel(){
install_kernel
for i in ${NODE_LIST[*]};do
ip=`echo $i|awk -F'-' '{print $1}'`
ssh root@${ip} "/usr/local/scripts/k8s_install.sh update_kernel && sleep 5 && reboot"
done
reboot
}
add_hosts(){
echo "$MASTER $MASTER_HOSTNAME" >>/etc/hosts
for i in ${NODE_LIST[*]};do
node_hosts=`echo $i|sed "s/-/ /g"`
echo $node_hosts >>/etc/hosts
done
}
install_basics(){
#修改内核参数
echo net.bridge.bridge-nf-call-iptables = 1 >>/etc/sysctl.conf
echo net.ipv4.ip_forward=1 >>/etc/sysctl.conf
echo net.bridge.bridge-nf-call-iptables=1 >>/etc/sysctl.conf
echo net.bridge.bridge-nf-call-ip6tables=1 >>/etc/sysctl.conf
echo vm.swappiness=0 >>/etc/sysctl.conf
sysctl -p
#关闭swap
#swapoff -a
#sed -i '/swap/s/^/#/' /etc/fstab
#关闭firewalld
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
#配置IPVS模块
cat >/etc/sysconfig/modules/ipvs.modules<<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules
bash /etc/sysconfig/modules/ipvs.modules
lsmod | grep -e ip_vs -e nf_conntrack_ipv4
#配置源
cd /etc/yum.repos.d/
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
cat>/etc/yum.repos.d/kubrenetes.repo<<EOF
[kubernetes]
name=Kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
EOF
yum makecache
#安装containerd
yum install -y containerd
#配置containerd运行环境
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
#配置containerd的config.toml文件
containerd config default | sudo tee /etc/containerd/config.toml
sed -ri 's#(sandbox_image = ")(registry.k8s.io)(.*)#\1registry.cn-hangzhou.aliyuncs.com/google_containers\3#g' /etc/containerd/config.toml
sed -i "s/SystemdCgroup = false/SystemdCgroup = true/g" /etc/containerd/config.toml
#查看
cat /etc/containerd/config.toml |egrep "sandbox_image|SystemdCgroup"
#指定crictl的containerd端点
cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///var/run/containerd/containerd.sock
image-endpoint: unix:///var/run/containerd/containerd.sock
timeout: 0
debug: false
pull-image-on-create: false
EOF
#启动containerd服务
systemctl start containerd.service
systemctl enable containerd.service
yum install -y nfs-utils
#安装kubeadm等
yum install kubelet-$K8S_VERSION kubeadm-$K8S_VERSION kubectl-$K8S_VERSION -y
#安装ipvs
yum -y install ipvsadm ipset
#启动kubelet
systemctl enable kubelet && systemctl start kubelet
}
kuneadm_init(){
kubeadm init --kubernetes-version=v${K8S_VERSION} --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers' >/root/kubeadm-init.log
###mkdir .kube
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
}
kubeadm_join(){
join_cmd=`tail -2 /root/kubeadm-init.log`
for i in ${NODE_LIST[*]};do
ip=`echo $i|awk -F'-' '{print $1}'`
ssh root@${ip} "${join_cmd}"
done
}
install_nfs(){
ssh root@${NFS_SERVER} "yum -y install nfs-utils rpcbind &&systemctl start rpcbind.service &&systemctl start nfs&& mkdir /data/nfs -p &&chown nfsnobody.nfsnobody /data/nfs && echo /data/nfs ${NFS_SERVER}\(rw,sync,no_root_squash,no_all_squash\)>> /etc/exports && exportfs -arv"
}
master_install(){
yum install -y git
cd /root
git clone https://gitee.com/zhang8042/k8s_file.git
##calico
cd /root/k8s_file/calico
kubectl apply -f calico.yaml
#nfs sc
cd /root/k8s_file/nfs
sed -i "s/10.10.10.60/${NFS_SERVER}/g" deployment.yaml
sed -i "s#/ifs/kubernetes#${NFS_PATH}#g" deployment.yaml
kubectl apply -f class.yaml
kubectl apply -f rbac.yaml
kubectl apply -f deployment.yaml
kubectl apply -f test-pvc.yaml
#helm
cd /root
wget https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz
tar xf helm-${HELM_VERSION}-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/
helm version
helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo add kaiyuanshe http://mirror.kaiyuanshe.cn/ kubernetes/charts
helm repo add azure http://mirror.azure.cn/kubernetes/charts
helm repo add dandydev https://dandydeveloper.github.io/charts
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo list
}
server_init(){
#hostname
ssh root@${MASTER} "hostnamectl set-hostname ${MASTER_HOSTNAME}"
script_file=`pwd`/$0
for i in ${NODE_LIST[*]};do
ip=`echo $i|awk -F'-' '{print $1}'`
name=`echo $i|awk -F'-' '{print $2}'`
ssh root@${ip} "hostnamectl set-hostname ${name}"
ssh root@${ip} "mkdir -p ${SCRIPT_PATH}"
scp $script_file root@${ip}:${SCRIPT_PATH}/k8s_install.sh
ssh root@${ip} "chmod +x ${SCRIPT_PATH}/k8s_install.sh "
ssh root@${ip} "${SCRIPT_PATH}/k8s_install.sh add_hosts"
done
add_hosts
update_kernel
}
install_k8s(){
install_basics
for i in ${NODE_LIST[*]};do
ip=`echo $i|awk -F'-' '{print $1}'`
ssh root@${ip} "${SCRIPT_PATH}/k8s_install.sh install_basics"
done
kuneadm_init
}
case $1 in
init)
server_init
;;
install)
install_k8s
kubeadm_join
install_nfs
master_install
;;
update_kernel)
install_kernel
;;
install_basics)
install_basics
;;
add_hosts)
add_hosts
;;
*)
echo "input error"
;;
esac
2. 脚本使用
2.1 升级内核
```
bash k8s_install.sh init
```
2.2 k8s集群安装
bash k8s_install.sh install
本文由博客一文多发平台 OpenWrite 发布!