在k8s中添加和删除node节点

  1. 删除节点

    #查看所有节点
    kubectl get nodes
    #删除具体的节点
    kubectl delete node1
    
  2. 在被删除的nide节点清空集群信息

    kubeadm reset
    #遇到需确认的信息输入Y即可
    
  3. 添加新node节点到集群

    1. 准备环境

      # 关闭防火墙
      systemctl stop firewalld
      systemctl disable firewalld
      
      # 关闭selinux
      sed -i 's/enforcing/disabled/' /etc/selinux/config  # 永久
      setenforce 0  # 临时
      
      # 关闭swap
      swapoff -a  # 临时
      sed -ri 's/.*swap.*/#&/' /etc/fstab    # 永久
      
      # 根据规划设置主机名  hostname:名称,方便记忆
      hostnamectl set-hostname hostname
      
      # 将桥接的IPv4流量传递到iptables的链
      cat > /etc/sysctl.d/k8s.conf << EOF
      net.bridge.bridge-nf-call-ip6tables = 1
      net.bridge.bridge-nf-call-iptables = 1
      EOF
      sysctl --system  # 生效
      
    2. 安装docker

      $ wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
      $ yum -y install docker-ce-18.06.1.ce-3.el7
      $ systemctl enable docker && systemctl start docker
      $ docker --version
      Docker version 18.06.1-ce, build e68fc7a
      
      $ cat > /etc/docker/daemon.json << EOF
      {
        "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"],
        "exec-opts": ["native.cgroupdriver=systemd"]   //不知为什么,云服务器需添加这一行不然下几步报错报错
      }
      EOF
      
    3. 配置阿里云yum软件源

      $ 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
      
    4. 安装kubectl、kubeadm和kubelet

      $ yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
      $ systemctl enable kubelet
      
  4. master节点生成新的token令牌

    kubeadm token create --print-join-command
    
  5. 在新node节点上执行token令牌

    kubeadm join 82.156.215.56:6443 --token rs4cy0.kgm7x1x0xx7fgz1v     --discovery-token-ca-cert-hash sha256:bf5c6c47381e82d66b9b327e2be37fd7bba89ddf23b0d65c70ebd4da607428f2
    
  6. 验证是否成功

    kubectl get nodes
    

Q.E.D.


一个热爱生活的95后精神小伙