Kubernetes 集群运维指南 目录
Kubernetes 架构概述
集群部署
kubectl 使用指南
工作负载管理
服务与网络
存储管理
配置管理
集群监控
故障排查
备份与恢复
1. Kubernetes 架构概述 1.1 核心组件 控制平面(Control Plane)
kube-apiserver : API 服务器,集群的统一入口
etcd : 分布式键值存储,保存集群状态
kube-scheduler : 负责 Pod 调度
kube-controller-manager : 运行各种控制器
cloud-controller-manager : 与云提供商交互
节点组件(Node Components)
kubelet : 节点代理,管理 Pod 生命周期
kube-proxy : 网络代理,维护网络规则
容器运行时 : Docker, containerd, CRI-O
1.2 核心概念 1 2 3 4 5 6 7 8 9 10 11 12 Cluster(集群) ├── Node(节点) │ ├── Pod(最小调度单元) │ │ └── Container(容器) ├── Namespace(命名空间) └── Resource(资源) ├── Deployment ├── StatefulSet ├── DaemonSet ├── Service ├── ConfigMap └── Secret
2. 集群部署 2.1 kubeadm 部署 Master 节点配置
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 swapoff -a sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 EOF sysctl --system apt-get update apt-get install -y containerd containerd config default > /etc/containerd/config.toml sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml systemctl restart containerd apt-get update apt-get install -y apt-transport-https ca-certificates curl curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | tee /etc/apt/sources.list.d/kubernetes.listapt-get update apt-get install -y kubelet kubeadm kubectl apt-mark hold kubelet kubeadm kubectl
初始化集群
1 2 3 4 5 6 7 8 9 10 11 12 13 kubeadm init --pod-network-cidr=10.244.0.0/16 mkdir -p $HOME /.kubecp -i /etc/kubernetes/admin.conf $HOME /.kube/configchown $(id -u):$(id -g) $HOME /.kube/configkubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml kubeadm token create --print-join-command
Worker 节点加入
1 2 kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash >
2.2 验证集群 1 2 3 4 5 6 7 8 kubectl get nodes kubectl get componentstatuses kubectl get pods -n kube-system
3. kubectl 使用指南 3.1 基本命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 kubectl config view kubectl config get-contexts kubectl config use-context <context-name> kubectl get pods kubectl get pods -n <namespace> kubectl get pods -o wide kubectl get pods -o yaml kubectl get pods -o json kubectl describe pod <pod-name> kubectl describe pod <pod-name> -n <namespace> kubectl logs <pod-name> kubectl logs -f <pod-name> kubectl logs <pod-name> -c <container-name>
3.2 资源操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 kubectl create -f manifest.yaml kubectl apply -f manifest.yaml kubectl apply -f ./directory/ kubectl delete -f manifest.yaml kubectl delete pod <pod-name> kubectl delete namespace <namespace> kubectl edit pod <pod-name> kubectl edit deployment <deployment-name> kubectl scale deployment <deployment-name> --replicas=3
3.3 进入容器 1 2 3 4 5 6 7 kubectl exec -it <pod-name> -- /bin/bash kubectl exec -it <pod-name> -c <container-name> -- /bin/sh kubectl exec <pod-name> -- ls -la kubectl exec <pod-name> -- cat /etc/hosts
3.4 端口转发 1 2 3 4 5 6 kubectl port-forward <pod-name> 8080:80 kubectl port-forward deployment/<deployment-name> 8080:80 kubectl port-forward svc/<service-name> 8080:80
3.5 常用别名 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 alias k=kubectlcomplete -o default -F __start_kubectl k alias kgp='kubectl get pods' alias kgd='kubectl get deployments' alias kgs='kubectl get services' alias kgn='kubectl get nodes' alias kgl='kubectl get pods -o wide' alias kdp='kubectl describe pod' alias kdd='kubectl describe deployment' alias kaf='kubectl apply -f' alias kdf='kubectl delete -f' alias kl='kubectl logs' alias klf='kubectl logs -f' alias ke='kubectl edit' alias kx='kubectl delete pod'
4. 工作负载管理 4.1 Deployment 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 28 29 30 31 32 33 34 35 36 37 38 39 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment namespace: default spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.21 ports: - containerPort: 80 resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m" readinessProbe: httpGet: path: / port: 80 initialDelaySeconds: 5 periodSeconds: 10 livenessProbe: httpGet: path: / port: 80 initialDelaySeconds: 15 periodSeconds: 20
4.2 StatefulSet 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 28 29 30 31 32 33 34 35 36 37 apiVersion: apps/v1 kind: StatefulSet metadata: name: mysql spec: serviceName: "mysql" replicas: 3 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql:5.7 ports: - containerPort: 3306 volumeMounts: - name: data mountPath: /var/lib/mysql env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-secret key: password volumeClaimTemplates: - metadata: name: data spec: accessModes: ["ReadWriteOnce" ] resources: requests: storage: 10Gi
4.3 DaemonSet 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 apiVersion: apps/v1 kind: DaemonSet metadata: name: node-exporter namespace: monitoring spec: selector: matchLabels: app: node-exporter template: metadata: labels: app: node-exporter spec: hostNetwork: true hostPID: true containers: - name: node-exporter image: prom/node-exporter:latest ports: - containerPort: 9100 hostPort: 9100
4.4 Job & CronJob 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 28 29 30 31 apiVersion: batch/v1 kind: Job metadata: name: backup-job spec: template: spec: containers: - name: backup image: backup-tool:latest command: ["./backup.sh" ] restartPolicy: OnFailure backoffLimit: 3 --- apiVersion: batch/v1 kind: CronJob metadata: name: backup-cronjob spec: schedule: "0 2 * * *" jobTemplate: spec: template: spec: containers: - name: backup image: backup-tool:latest restartPolicy: OnFailure
5. 服务与网络 5.1 Service 类型 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: myapp ports: - port: 80 targetPort: 8080 type: ClusterIP --- apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: myapp ports: - port: 80 targetPort: 8080 nodePort: 30080 type: NodePort --- apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: myapp ports: - port: 80 targetPort: 8080 type: LoadBalancer --- apiVersion: v1 kind: Service metadata: name: headless-service spec: clusterIP: None selector: app: myapp ports: - port: 80
5.2 Ingress 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 28 29 apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: rules: - host: example.com http: paths: - path: / pathType: Prefix backend: service: name: web-service port: number: 80 - path: /api pathType: Prefix backend: service: name: api-service port: number: 8080 tls: - hosts: - example.com secretName: tls-secret
5.3 Network Policy 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 28 29 30 31 apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: deny-all namespace: default spec: podSelector: {} policyTypes: - Ingress - Egress --- apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-web namespace: default spec: podSelector: matchLabels: app: web policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: name: ingress-nginx ports: - protocol: TCP port: 80
6. 存储管理 6.1 Volume 类型 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 28 29 volumes: - name: cache emptyDir: {} volumes: - name: log-volume hostPath: path: /var/log type: Directory volumes: - name: data-volume persistentVolumeClaim: claimName: my-pvc volumes: - name: config configMap: name: my-config volumes: - name: secret secret: secretName: my-secret
6.2 PersistentVolume 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 apiVersion: v1 kind: PersistentVolume metadata: name: pv-data spec: capacity: storage: 10Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain storageClassName: manual hostPath: path: /mnt/data --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-data spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi storageClassName: manual
6.3 StorageClass 1 2 3 4 5 6 7 8 9 10 11 apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: fast provisioner: kubernetes.io/aws-ebs parameters: type: gp2 fsType: ext4 reclaimPolicy: Retain allowVolumeExpansion: true volumeBindingMode: WaitForFirstConsumer
7. 配置管理 7.1 ConfigMap 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 28 29 30 31 apiVersion: v1 kind: ConfigMap metadata: name: app-config data: app.properties: | server.port=8080 database.host=mysql LOG_LEVEL: INFO ENVIRONMENT: production --- apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: my-container image: myapp envFrom: - configMapRef: name: app-config volumeMounts: - name: config mountPath: /etc/config volumes: - name: config configMap: name: app-config
7.2 Secret 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 28 29 30 apiVersion: v1 kind: Secret metadata: name: db-secret type: Opaque data: username: YWRtaW4= password: cGFzc3dvcmQxMjM= --- apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: my-container image: myapp env: - name: DB_USERNAME valueFrom: secretKeyRef: name: db-secret key: username - name: DB_PASSWORD valueFrom: secretKeyRef: name: db-secret key: password
8. 集群监控 8.1 Metrics Server 1 2 3 4 5 6 kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml kubectl top nodes kubectl top pods
8.2 Prometheus + Grafana 1 2 3 4 5 6 helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm install monitoring prometheus-community/kube-prometheus-stack -n monitoring --create-namespace kubectl port-forward svc/monitoring-grafana -n monitoring 3000:80
8.3 日志收集 1 2 3 4 5 6 helm repo add grafana https://grafana.github.io/helm-charts helm install loki grafana/loki-stack -n logging --create-namespace
9. 故障排查 9.1 常见问题 Pod 无法启动
1 2 3 4 5 6 7 8 9 10 kubectl get pods -o wide kubectl describe pod <pod-name> kubectl logs <pod-name> kubectl logs <pod-name> --previous kubectl get events --sort-by='.lastTimestamp'
Pod 处于 Pending 状态
1 2 3 4 5 6 kubectl describe pod <pod-name> kubectl top nodes kubectl get events | grep <pod-name>
节点 NotReady
1 2 3 4 5 6 7 8 9 kubectl describe node <node-name> systemctl status kubelet journalctl -u kubelet -f kubectl get pods -n kube-system -o wide
9.2 调试工具 1 2 3 4 5 6 7 8 kubectl debug -it <pod-name> --image=busybox kubectl run -it --rm debug --image=busybox --restart=Never kubectl run -it --rm netshoot --image=nicolaka/netshoot --restart=Never
10. 备份与恢复 10.1 etcd 备份 1 2 3 4 5 6 7 8 9 10 ETCDCTL_API=3 etcdctl snapshot save snapshot.db \ --endpoints=https://127.0.0.1:2379 \ --cacert=/etc/kubernetes/pki/etcd/ca.crt \ --cert=/etc/kubernetes/pki/etcd/server.crt \ --key=/etc/kubernetes/pki/etcd/server.key ETCDCTL_API=3 etcdctl snapshot restore snapshot.db \ --data-dir=/var/lib/etcd-restore
10.2 资源备份 1 2 3 4 5 6 7 8 9 10 kubectl get all --all-namespaces -o yaml > all-resources.yaml kubectl get all -n <namespace> -o yaml > namespace-resources.yaml velero backup create daily-backup --include-namespaces default,production velero backup get velero restore create --from-backup daily-backup
文档版本 : 1.0最后更新 : 2026-02-27适用版本 : Kubernetes 1.23+