k8s的 Node and Cluster实战

Node and Cluster

查看节点

bash 复制代码
# 查看节点清单
root@master30 ~ 14:58:52# kubectl get nodes 
NAME                 STATUS   ROLES           AGE   VERSION
master30.ggg.cloud   Ready    control-plane   95m   v1.30.2
worker31.ggg.cloud   Ready    <none>          28m   v1.30.2
worker32.ggg.cloud   Ready    <none>          28m   v1.30.2

# 查看特定节点详细信息
root@master30 ~ 15:21:38# kubectl describe node worker31.ggg.cloud
#重点关注Labels,Conditions,Non-terminated Pod,Allocated resources等

删除节点

以 worker31 节点为例。

bash 复制代码
# 设置节点为维护模式
root@master30 ~ 15:23:12# kubectl drain worker31.ggg.cloud --ignore-daemonsets
node/worker31.ggg.cloud already cordoned
Warning: ignoring DaemonSet-managed Pods: kube-system/calico-node-6bltt, kube-system/kube-proxy-kx6kn
node/worker31.ggg.cloud drained
root@master30 ~ 15:23:24# kubectl get nodes 
NAME                 STATUS                     ROLES           AGE   VERSION
master30.ggg.cloud   Ready                      control-plane   97m   v1.30.2
worker31.ggg.cloud   Ready,SchedulingDisabled   <none>          30m   v1.30.2
worker32.ggg.cloud   Ready                      <none>          30m   v1.30.2

# 删除 worker31 节点
root@master30 ~ 15:23:27# kubectl delete node worker31.ggg.cloud 
node "worker31.ggg.cloud" deleted
root@master30 ~ 15:23:50# kubectl get nodes 
NAME                 STATUS   ROLES           AGE   VERSION
master30.ggg.cloud   Ready    control-plane   97m   v1.30.2
worker32.ggg.cloud   Ready    <none>          31m   v1.30.2

# 重置删除的 worker31 节点
root@worker31 ~ 15:24:18# kubeadm reset -f
[preflight] Running pre-flight checks
W0805 15:24:26.048287   10253 removeetcdmember.go:106] [reset] No kubeadm config, using etcd pod spec to get data directory
[reset] Deleted contents of the etcd data directory: /var/lib/etcd
[reset] Stopping the kubelet service
[reset] Unmounting mounted directories in "/var/lib/kubelet"
[reset] Deleting contents of directories: [/etc/kubernetes/manifests /var/lib/kubelet /etc/kubernetes/pki]
[reset] Deleting files: [/etc/kubernetes/admin.conf /etc/kubernetes/super-admin.conf /etc/kubernetes/kubelet.conf /etc/kubernetes/bootstrap-kubelet.conf /etc/kubernetes/controller-manager.conf /etc/kubernetes/scheduler.conf]

The reset process does not clean CNI configuration. To do so, you must remove /etc/cni/net.d

The reset process does not reset or clean up iptables rules or IPVS tables.
If you wish to reset iptables, you must do so manually by using the "iptables" command.

If your cluster was setup to utilize IPVS, run ipvsadm --clear (or similar)
to reset your system's IPVS tables.

The reset process does not clean your kubeconfig files and you must remove them manually.
Please, check the contents of the $HOME/.kube/config file.


#重新加入 worker31 节点
root@worker31 ~ 15:24:26# kubeadm join 10.1.8.30:6443 --token cum331.6m74ji4yo7ffi5tw \
        --discovery-token-ca-cert-hash sha256:6f83aa8722974ef53128918a78da3fdcf931db14f779941343e11862cc39c1d8
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-check] Waiting for a healthy kubelet. This can take up to 4m0s
[kubelet-check] The kubelet is healthy after 501.227089ms
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

#验证
root@master30 ~ 15:24:43# kubectl get nodes 
NAME                 STATUS   ROLES           AGE   VERSION
master30.ggg.cloud   Ready    control-plane   99m   v1.30.2
worker31.ggg.cloud   Ready    <none>          22s   v1.30.2
worker32.ggg.cloud   Ready    <none>          32m   v1.30.2

删除集群

删除集群流程:

  1. 删除所有 node
  2. 删除所有 master

具体步骤:

  1. 删除所有node节点
bash 复制代码
root@master30 ~ 15:25:18#  kubectl drain worker31.ggg.cloud --ignore-daemonsets --force
root@master30 ~ 15:45:50# kubectl drain worker32.ggg.cloud --ignore-daemonsets --force
root@master30 ~ 15:46:01# kubectl delete node worker31.ggg.cloud worker32.ggg.cloud

# 重置节点,注意执行位置
root@worker31 ~ 15:24:18# kubeadm reset -f
root@worker32 ~ 14:58:56# kubeadm reset -f
  1. 删除master节点
bash 复制代码
# 删除集群前获取集群配置
root@master30 ~ 15:46:12# kubectl get cm kubeadm-config -n kube-system -o yaml > kubeadm.yml

# 修改kubeadm.yml内容如下:
root@master30 ~ 15:46:22# vim kubeadm.yml
# 删除1-3和22-28行,效果如下
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.k8s.io
kind: ClusterConfiguration
kubernetesVersion: v1.30.2
networking:
  dnsDomain: cluster.local
  podSubnet: 10.224.0.0/16
  serviceSubnet: 10.96.0.0/12
scheduler: {}

root@master30 ~ 15:46:44# kubectl delete node master30.ggg.cloud
root@master30 ~ 15:46:47# kubeadm reset -f
root@master30 ~ 15:46:53# rm -fr .kube/

重建集群

bash 复制代码
# 初始化集群
root@master30 ~ 15:53:44# kubeadm init --config kubeadm.yml

# 也可以使用之前的命令
root@master30 ~ 15:53:44# kubeadm init --kubernetes-version=v1.30.2 --pod-network-cidr=10.224.0.0/16

# 配置凭据
root@master30 ~ 15:54:17# mkdir -p $HOME/.kube
root@master30 ~ 15:54:17# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
root@master30 ~ 15:54:17# sudo chown $(id -u):$(id -g) $HOME/.kube/config

# 配置网络
root@master30 ~ 15:53:59# kubectl apply -f calico.yaml

# 加入集群
root@worker31 ~ 15:46:24# kubeadm join 10.1.8.30:6443 --token quq3fd.z2cxskllch0gqo5i \
        --discovery-token-ca-cert-hash sha256:fc8f6417071df50562346fdfb79f4459ea8ba1c8f05031607ea75794ac593dfa
root@worker32 ~ 15:46:29# kubeadm join 10.1.8.30:6443 --token quq3fd.z2cxskllch0gqo5i \
        --discovery-token-ca-cert-hash sha256:fc8f6417071df50562346fdfb79f4459ea8ba1c8f05031607ea75794ac593dfa
相关推荐
lxw202302711629 分钟前
k8s的控制器
云原生·容器·kubernetes
大大大大晴天1 小时前
大数据K8S基础:从 Pod 到 Service,看懂数据负载如何在云原生上运行
大数据·kubernetes
FoldWinCard2 小时前
K8s -- 控制器管理
云原生·容器·kubernetes
容器魔方3 小时前
云容器引擎 CCE 2026-Q2 优化升级:AI 推理负载、备份中心、Gateway API能力全新上线!
人工智能·云原生·容器·开源
无望__wsk3 小时前
k8s的pod管理
云原生·容器·kubernetes
悠然南风3 小时前
【云原生学习】Prometheus监控、GitLab与Jenkins部署实战
kubernetes·gitlab
菜鸟界的菜鸟4 小时前
k8s-Service详解(七)
云原生·容器·kubernetes
wazmlp0018873694 小时前
k8s pod 管理
docker·容器·kubernetes
Dear~yxy4 小时前
k8s的pod
云原生·容器·kubernetes
Dear~yxy4 小时前
k8s中的控制器管理
云原生·容器·kubernetes