k8s的node进行扩容,扩容的机器需要满足以下前提条件
1.无密登录配置 ssh-keygen -t rsa
2.hostsname修改,并添加集群列表到/etc/hosts
3.注意我的使用的nfs做的持久化存储,如果没有注意最后的步骤
#!/bin/bash
#脚本运行之前准备工作
#
# ??
#!/bin/bash# 集群的起节点
start=1# 集群的终节点
end=10# 主机名前缀
hostname_prefix="node"echo "升级软件和系统内核"
#for i in $(seq $start $end); do
# ?ssh "${hostname_prefix}${i}" "yum -y update"
#doneecho "关闭 Linux 防火墙"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "systemctl stop firewalld && systemctl disable firewalld"
doneecho "检查防火墙状态"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "sestatus -v"
doneecho "禁用 swap"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "echo 'vm.swappiness = 0' >> /etc/sysctl.conf && sysctl -w vm.swappiness=1"
doneecho "配置内核参数"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF"
doneecho "重新加载内核参数"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "sysctl --system"
doneecho "下载 docker-ce.repo 文件"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo"
doneecho "安装 Docker CE、Docker CLI 和 containerd.io"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6"
doneecho "启用并启动 Docker 服务"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "systemctl enable docker && systemctl start docker"
doneecho "显示 Docker 版本"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "docker --version"
done
?? ?
echo "配置镜像加速器"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "sudo mkdir -p /etc/docker && sudo tee /etc/docker/daemon.json <<-'EOF'
{
? \"registry-mirrors\": [\"https://afi5x6i2.mirror.aliyuncs.com\"],
? \"exec-opts\": [\"native.cgroupdriver=systemd\"],
? \"insecure-registries\": [\"172.25.152.2:30002\"]
}
EOF"
doneecho "重新加载并重启 Docker 服务"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "sudo systemctl daemon-reload && sudo systemctl restart docker"
doneecho "时钟同步"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "yum install ntpdate -y && ntpdate time.windows.com"
doneecho "添加 Kubernetes YUM 源"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "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"
doneecho "安装 kubeadm kubelet ?kubectl 服务"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "yum install -y kubelet-1.22.10 kubeadm-1.22.10 kubectl-1.22.10"
doneecho "启动 kubelet ?服务"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "systemctl enable kubelet"
done
# 在 Kubernetes 主控节点上执行 kubeadm token create --print-join-command 命令
join_command=$(ssh k8s-master 'kubeadm token create --print-join-command')# 打印生成的加入命令
echo "加入命令:"
echo "$join_command"
echo "集群节点加入"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "$join_command"
doneecho "安装nfs"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "yum install -y nfs-utils"
doneecho "检查nfs挂载目录"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "showmount -e 172.25.152.2"
doneecho "创建nfs客户端目录"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "mkdir -p /data/nfs/data/"
doneecho "创建nfs客户端目录"
for i in $(seq $start $end); do
? ssh "${hostname_prefix}${i}" "mount -t nfs nfs的ip地址:/data/nfs/data /data/nfs/data"
done
?