当前位置:   article > 正文

K8S集群重新初始化--详细过程_kubectl 重新初始化

kubectl 重新初始化

在引导k8s集群的过程时可能因为这个或那个的原因导致需要重新引导集群 。

下面整理了我在实际工作中初始化k8s集群的详细过程。

0、当前环境

k8s环境部署总览

ip地址类型操作系统服务配置
192.168.162.31Master01Centos7.62核CPU 2G内存 20G硬盘
192.168.162.41node1Centos7.62核CPU 2G内存 20G硬盘
192.168.162.42node2Centos7.62核CPU 2G内存 20G硬盘

1、master节点

1.1、在master节点执行下面reset命令:

kubeadm reset

//过程会询问是否重置,输入y然后回车

1.2、手动清除配置信息,这一步很关键:

cd ~ 进入根目录

ll -a 查看是否存在.kube文件
rm -rf /root/.kube

systemctl restart docker ## 重启docker 
systemctl restart kubelet ## 重启kubelet

rm -rf /etc/cni/net.d

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

1.3、重新引导集群

kubeadm init xxxxxx
  • 1

例如,具体按你的为准:

kubeadm init \
--apiserver-advertise-address=192.168.162.31 \
--control-plane-endpoint=k8s-master01-31 \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

初始化master节点成功后,拷贝出关键信息:(注意是你自己的信息)

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join k8s-master01-31:6443 --token m7xgx0.h8m6si6ws2p0r9n6 \
    --discovery-token-ca-cert-hash sha256:f4d3af365aeabab83cf124e5a84aeb8260ae6c0d54a7f5d87928fab37a519aea \
    --control-plane 

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join k8s-master01-31:6443 --token m7xgx0.h8m6si6ws2p0r9n6 \
    --discovery-token-ca-cert-hash sha256:f4d3af365aeabab83cf124e5a84aeb8260ae6c0d54a7f5d87928fab37a519aea 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

1.4、创建配置目录,并复制权限配置文件到用户目录下:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • 1
  • 2
  • 3

1.5 查看集群状态

此时就可以在master节点使用kubectl get node 查看集群状态,如果出现状态为notready

kubectl get node 
  • 1

请添加图片描述

查看pod状态

请添加图片描述

1.6 安装Calico网络插件

curl https://docs.projectcalico.org/manifests/calico.yaml -O ## 或者

curl https://docs.projectcalico.org/v3.20/manifests/calico.yaml -O

kubectl apply -f calico.yaml
  • 1
  • 2
  • 3
  • 4
  • 5

2、work节点

2.1、重置工作节点

kubeadm reset
//过程会询问是否重置,输入y然后回车

2.2、手动删除目录

rm -rf /root/.kube
rm -rf /etc/cni/net.d
rm -rf /etc/kubernetes/*
  • 1
  • 2
  • 3

2.3、重新加入集群

kubeadm join xxx #获取该指令可以通过在master节点上执行:kubeadm token create --print-join-command

自己生成的命令:

kubeadm join k8s-master01-31:6443 --token m7xgx0.h8m6si6ws2p0r9n6 \
    --discovery-token-ca-cert-hash sha256:f4d3af365aeabab83cf124e5a84aeb8260ae6c0d54a7f5d87928fab37a519aea
  • 1
  • 2

在master节点重新获取token

kubeadm token create --print-join-command
  • 1

3、检验效果

在master节点查看各个node

kubectl get node
  • 1

请添加图片描述

在master节点查看pod状态

kubectl get pod -A
  • 1

请添加图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/706201
推荐阅读
相关标签
  

闽ICP备14008679号