书接上文,在上一篇文章中我们提到了创建k8s集群的第一个master节点(从0到1手动搭建k8s集群-初始化master节点-CSDN博客),这一个节点已经是一个可用的k8s集群了,那么下面我们将以集群扩容的方式重新打开,介绍如何添加其他master节点。

PS:这里有一点需要注意的是,我们的k8s集群的etcd存储是通过kubeadm采用静态pod的方式部署的,由于etcd集群节点数必须为奇数以保证可以顺利选举,故我们的master数量也需要为奇数

下面我们就正式开始介绍master2节点的添加,master3节点同理:

目录

1. 机器信息

2. 环境初始化

3. 部署k8s二进制

4. 将新节点加入集群


1. 机器信息

  • master2:192.168.56.11

2. 环境初始化

以下操作在master2节点执行

  • 关闭防火墙、虚拟交换分区、selinux

# 关闭防火墙
sudo systemctl stop firewalld && systemctl disable firewalld
sudo systemctl stop ufw && systemctl disable ufw
# 关闭虚拟交换(注释fstab中swap配置)
sudo swapoff -asudo sed -i /^[^#]*swap*/s/^/\#/g /etc/fstab
  • 设置/etc/hosts

192.168.56.10 master1
192.168.56.11 master2
192.168.56.12 master3
192.168.56.13 node1
192.168.56.14 node2
  • 部署 docker

#一键式部署docker
curl -fsSL https://get.docker.com | sudo bash -s docker --mirror Aliyunsudo
sudo systemctl enable docker && sudo systemctl restart docker
  • 安装必备软件

sudo apt-get install socat conntrack ebtables ipset ipvsadm
  • 设置 hostname

sudo hostnamectl set-hostname master2

3. 部署k8s二进制

  • 将kubelet、kubectl、kubeadm拷贝到/usr/local/bin路径下,并赋予执行权限

curl https://dl.k8s.io/v1.20.4/kubernetes-node-linux-amd64.tar.gz -o ./kubernetes-node-linux-amd64.tar.gz
tar -zxvf kubernetes-client-linux-amd64.tar.gz -C ./ 
# 部署kubeadm
sudo cp ./kubernetes/node/bin/kubeadm /usr/local/bin/ && sudo chmod +x /usr/local/bin/kubeadm
# 部署kubectl
sudo cp ./kubernetes/node/bin/kubectl /usr/local/bin/ && sudo chmod +x /usr/local/bin/kubectl
# 部署kubelet
sudo cp ./kubernetes/node/bin/kubelet /usr/local/bin/ && sudo chmod +x /usr/local/bin/kubelet
  • 生成kubelet服务/etc/systemd/system/kubelet.service

[Unit]
Description=kubelet: The Kubernetes Node Agent
Documentation=http://kubernetes.io/docs/

[Service]
CPUAccounting=true
MemoryAccounting=true
ExecStart=/usr/local/bin/kubelet
Restart=always
StartLimitInterval=0
RestartSec=10

[Install]
WantedBy=multi-user.target
  • 生成kubelet配置文件/etc/systemd/system/kubelet.service.d/10-kubeadm.conf

# Note: This dropin only works with kubeadm and kubelet v1.11+
[Service]
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
# This is a file that "kubeadm init" and "kubeadm join" generate at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
# This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
# the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
EnvironmentFile=-/etc/default/kubelet
Environment="KUBELET_EXTRA_ARGS=--node-ip=192.168.56.11 --hostname-override=master2"
ExecStart=
ExecStart=/usr/local/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS
  • 使能kubelet

sudo systemctl disable kubelet 
sudo systemctl enable kubelet 
sudo ln -snf /usr/local/bin/kubelet /usr/bin/kubelet

4. 将新节点加入集群

  • 生成token和证书(master1节点执行)

#生成token
sudo kubeadm token create
#生成证书
sudo kubeadm init phase upload-certs --upload-certs
  • 生成/etc/kubernetes/kubeadm-config.yaml,请将${TOKEN}、${CERT}的值替换成前一步获取到的值

---
apiVersion: kubeadm.k8s.io/v1beta2
kind: JoinConfiguration
discovery:
  bootstrapToken:
    apiServerEndpoint: 192.168.56.10:6443
    token: "${TOKEN}"
    unsafeSkipCAVerification: true
  tlsBootstrapToken: "${TOKEN}"
controlPlane:
  localAPIEndpoint:
    advertiseAddress: 192.168.56.11
    bindPort: 6443
  certificateKey: ${CERT}
nodeRegistration:
  kubeletExtraArgs:
    cgroup-driver: cgroupfs
  • 加入集群

#如果失败的话,执行"sudo kubeadm reset -f"命令清理环境后,再重新执行
sudo kubeadm join --config=/etc/kubernetes/kubeadm-config.yaml --ignore-preflight-errors=FileExisting-crictl,ImagePull
  • 拷贝kubeconfig配置

mkdir ~/.kube
sudo cp /etc/kubernetes/admin.conf ~/.kube/config
  • 更新kubelet config,将apiserver的地址指向自己

sudo sed -i 's#server:.*#server: https://127.0.0.1:6443#g' /etc/kubernetes/kubelet.conf
sudo systemctl daemon-reload && sudo systemctl restart kubelet

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐