在 CentOS 上扩展 Kubernetes 集群的实用指南
一 规划与准备
swapoff -a 并在 /etc/fstab 注释 swap 行;/etc/hosts 配置 IP↔主机名映射;setenforce 0;二 扩展工作节点 Worker
kubeadm token create --print-join-command;kubeadm token list 查看 token 与过期时间,再获取 CA 证书哈希 并拼装 kubeadm join ... --token <token> --discovery-token-ca-cert-hash sha256:<hash>。kubeadm join 命令。kubectl get nodes,确认新节点状态为 Ready。kubeadm join 失败:检查 token 有效期、CA 哈希、6443/10250 连通性、系统时间同步;/var/lib/kubelet/config.yaml 配置、容器运行时状态与镜像拉取;三 扩展控制平面 Master(高可用)
kubectl get nodes 可见新节点;kubectl get pods -n kube-system 检查 kube-apiserver、kube-controller-manager、kube-scheduler、etcd 等 Pod 就绪;kubectl taint nodes <new-master-name> node-role.kubernetes.io/master-(或 control-plane 污点)。四 扩展工作负载副本与自动扩缩容
kubectl scale deployment <name> --replicas=<N>;kubectl edit deployment <name> 修改 spec.replicas。kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml--kubelet-insecure-tls、--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname);kubectl top nodes、kubectl top pods。apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
kubectl get hpa,压测观察副本数随 CPU 利用率 变化。五 验证与运维建议
kubectl get nodes -o wide 检查新节点 Ready 与 InternalIP;kubectl get pods -o wide 确认 Pod 已调度到新节点;