Linux 服务集群管理与大规模运维:Kubernetes 基础、Operator 与 Systemd 混合架构实战
一、Kubernetes 架构原理与核心概念
Kubernetes 架构:
- Control Plane(控制平面):API Server、etcd、Scheduler、Controller Manager
- Worker Node:Kubelet、Kube-Proxy、Container Runtime(CRI-O / containerd)
- 核心对象:Pod(最小调度单元)、Deployment、StatefulSet、DaemonSet、Service、Ingress 等
关键设计理念:
- 声明式 API:描述期望状态,Kubernetes 自动协调
- 自愈能力:Pod 失败自动重启、节点 Down 自动迁移
- 水平扩展:HPA(Horizontal Pod Autoscaler)
- 服务发现:ClusterIP、NodePort、LoadBalancer
与 Systemd 的关系: Kubelet 本身作为 systemd 服务运行,Pod 内容器可通过 CRI-O 与宿主机 Systemd 深度集成。
二、生产环境 Kubernetes 集群部署实战(kubeadm)
1. 环境准备(所有节点)
# 禁用 Swap
swapoff -a && sed -i '/swap/d' /etc/fstab
# 加载内核模块
cat <<EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
# sysctl 参数
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system
2. 安装 Container Runtime(推荐 CRI-O)
# CRI-O 安装(Rocky Linux 示例)
yum install -y cri-o
systemctl enable --now crio
3. kubeadm 安装
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/repodata/repomd.xml.key
EOF
yum install -y kubelet kubeadm kubectl
systemctl enable --now kubelet
4. 初始化 Master 节点
kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.1.10
# 记录 join 命令
kubeadm token create --print-join-command
5. 安装网络插件(Flannel 示例)
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
6. Worker 节点加入
kubeadm join ...
生产高可用集群:推荐 3 个 Master 节点 + etcd 集群 + Keepalived / 云 LB。
三、Kubernetes 核心资源管理实战
1. Pod 与 Deployment 示例
apiVersion:apps/v1
kind:Deployment
metadata:
name:nginx-deploy
spec:
replicas:3
selector:
matchLabels:
app:nginx
template:
metadata:
labels:
app:nginx
spec:
containers:
-name:nginx
image:nginx:alpine
ports:
-containerPort:80
resources:
requests:
cpu:100m
memory:128Mi
limits:
cpu:500m
memory:512Mi
2. Service 与 Ingress
apiVersion:v1
kind:Service
metadata:
name:nginx-svc
spec:
selector:
app:nginx
ports:
-port:80
targetPort:80
type:ClusterIP# 或 NodePort / LoadBalancer
3. StatefulSet(有状态服务,如 MySQL) 适合数据库、Redis 等需要稳定网络标识和持久化存储的场景。
4. ConfigMap 与 Secret
# ConfigMap
kubectlcreateconfigmapnginx-config--from-file=nginx.conf
# Secret
kubectlcreatesecretgenericdb-secret--from-literal=password=StrongPass123!
四、Systemd 与 Kubernetes 混合架构实战
混合模式优势:
1. 混合监控方案
- 主机层:node_exporter + systemd_exporter
- K8s 层:kube-state-metrics + cAdvisor + Prometheus Operator
2. 日志混合采集
- Pod 日志 → Promtail DaemonSet
3. Ansible + Kubernetes 混合管理
- Helm / Kustomize 管理 K8s 应用
4. 服务互通
- 使用 CoreDNS 解析外部 Systemd 服务
- MetalLB 或 Keepalived 提供外部访问
五、Operator 模式与自定义控制器
Operator 是 Kubernetes 的高级扩展模式,实现了“自定义资源(CRD)+ 控制器”来管理复杂应用。
1. 常用 Operator
- MySQL Operator / Percona Operator
2. 简单 Operator 开发思路
- 使用 Operator SDK 或 Kubebuilder
- 控制器 Watch CRD 变化,自动创建 StatefulSet、Service、Backup 等
生产价值:
- 数据库 Operator 自动处理主从切换、备份、扩容
六、大规模集群运维最佳实践
- ResourceQuota + LimitRange
- Tekton / GitLab CI + Helm
- RBAC + Pod Security Policy / PodSecurity Admission
- ImagePolicyWebhook + Trivy 扫描
七、常见集群故障排查案例
案例1:Pod 一直 Pending 排查:
kubectl describe pod xxx
kubectl get events
原因:资源不足、节点 taint、PVC 未绑定等。
案例2:Service 无法访问 排查:kubectl get ep、DNS 检查、NetworkPolicy。
案例3:节点 NotReady 排查:journalctl -u kubelet、kubectl describe node。
案例4:Operator 控制器 Crash 排查:Operator 日志 + CRD 状态。
案例5:混合架构中服务发现失败 解决:CoreDNS 配置外部 DNS + 统一服务注册。