在引导k8s集群的过程时可能因为这个或那个的原因导致需要重新引导集群 。
下面整理了我在实际工作中初始化k8s集群的详细过程。
0、当前环境
k8s环境部署总览
ip地址 | 类型 | 操作系统 | 服务配置 |
192.168.162.31 | Master01 | Centos7.6 | 2核CPU 2G内存 20G硬盘 |
192.168.162.41 | node1 | Centos7.6 | 2核CPU 2G内存 20G硬盘 |
192.168.162.42 | node2 | Centos7.6 | 2核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.3、重新引导集群
kubeadm init xxxxxx
例如,具体按你的为准:
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
初始化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.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.5 查看集群状态
此时就可以在master节点使用kubectl get node 查看集群状态,如果出现状态为notready
kubectl get node
查看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
2、work节点
2.1、重置工作节点
kubeadm reset
//过程会询问是否重置,输入y然后回车
2.2、手动删除目录
rm -rf /root/.kube rm -rf /etc/cni/net.d rm -rf /etc/kubernetes/*
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
在master节点重新获取token
kubeadm token create --print-join-command
3、检验效果
在master节点查看各个node
kubectl get node
在master节点查看pod状态
kubectl get pod -A