新节点设置 hostname
hostnamectl set-hostname [name]
环境配置 - env.sh
#!/bin/bash
#关闭防火墙,禁用防火墙开机自启动
systemctl stop firewalld
systemctl disable firewalld
# 临时禁用SeLinux,重启失效
setenforce 0
# 修改SeLinux配置,永久禁用
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
# 临时关闭Swap
swapoff -a
# 修改 /etc/fstab 删除或者注释掉swap的挂载,可永久关闭swap
sed -i '/swap/s/^/#/' /etc/fstab
#修改k8s.conf
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
# 时间配置
echo 'export TZ="Asia/Shanghai"' >> /etc/profile
yum install ntp -y
echo 'server ntp.aliyun.com' >> /etc/ntp.conf
echo 'server ntp1.aliyun.com' >> /etc/ntp.conf
echo 'server ntp2.aliyun.com' >> /etc/ntp.conf
echo 'server ntp3.aliyun.com' >> /etc/ntp.conf
systemctl restart ntpd
systemctl enable ntpd
源配置 - yum_env.sh
#!/bin/bash
# 安装部分依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加docker yum源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 添加kubernetes yum源
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=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
# 对新添加的源的软件包信息cache一下
yum makecache --nogpgcheck
容器配置 - container.sh
#!/bin/bash
#安装docker
yum install -y --nogpgcheck docker-ce
#启动docker并设置开机自启动
systemctl enable docker
systemctl start docker
#安装kubernetes相关服务
yum install -y --nogpgcheck kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
# 设置kuberlet为开机自启动
systemctl enable kubelet
systemctl start kubelet
#输出docker和kubernetes的信息
docker version
kubectl version
新节点执行 sh 脚本
sh env.sh
sh yum_env.sh
sh container.sh
配置镜像加速和 harbor 仓库
vim /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {"max-size": "10m", "max-file": "3"},
"insecure-registries": ["http://harbor.com"]
}
重启 docker
systemctl restart docker
若使用了nfs外部存储需要添加nfs服务
yum -y install nfs-utils
systemctl start nfs && systemctl enable nfs
showmount -e [nfs服务器地址]
mount -t nfs [nfs服务器地址]:/data/nfs /mnt
主节点生成 token
kubeadm token create --print-join-command --ttl=0
在节点上执行 join 命令
kubeadm join 192.168.180.30:6443 --token qzog84.xdqr3jah7rnuq6y6 --discovery-token-ca-cert-hash sha256:b2028231f9a2bad5d616a5b138a92f388462480b62e2d0dfa63b08bc80615ee9
重置节点可以参考:K8S 重置节点