📋 目录
部署前准备
检查存储类
在开始部署之前,确保你的 Kubernetes 集群有可用的存储类:
1
| kubectl get storageclass
|
⚠️ 重要:如果没有默认的存储类,PVC 将无法绑定,导致 Pod 启动失败
系统要求
- Kubernetes 集群(建议 1.20+)
- 可用的存储类(如 NFS、Ceph 等)
- 至少 4 个节点以获得最佳性能
- 足够的存储空间(建议每个节点 5GB+)
Kubernetes 部署架构
组件说明
| 组件 |
作用 |
特点 |
| StatefulSet |
管理有状态 Pod |
固定网络身份、有序部署 |
| Headless Service |
Pod 间服务发现 |
无 ClusterIP,提供 DNS 解析 |
| NodePort Service |
对外暴露服务 |
通过节点端口访问 |
| PVC |
持久化存储 |
自动绑定,数据持久化 |
YAML 配置文件详解
1️⃣ Headless Service
作用:为 StatefulSet 提供固定域名解析,实现 Pod 间服务发现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| apiVersion: v1 kind: Service metadata: name: minio-hl-svc namespace: satellite-micro spec: clusterIP: None publishNotReadyAddresses: true selector: app: minio ports: - name: api port: 9000 targetPort: 9000 - name: console port: 9001 targetPort: 9001
|
关键配置说明:
clusterIP: None:创建 Headless Service
publishNotReadyAddresses: true:即使 Pod 未就绪也发布地址
2️⃣ NodePort Service
作用:对外暴露 MinIO 服务,提供外部访问入口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| apiVersion: v1 kind: Service metadata: name: minio-svc namespace: satellite-micro spec: type: NodePort selector: app: minio ports: - name: api port: 9000 targetPort: 9000 nodePort: 30900 - name: console port: 9001 targetPort: 9001 nodePort: 30901
|
端口映射:
30900:MinIO API 接口
30901:MinIO 管理控制台
3️⃣ StatefulSet
作用:管理 MinIO Pod 和 PVC,确保有状态服务的稳定性
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| apiVersion: apps/v1 kind: StatefulSet metadata: name: minio namespace: satellite-micro spec: serviceName: minio-hl-svc replicas: 4 podManagementPolicy: Parallel selector: matchLabels: app: minio template: metadata: labels: app: minio spec: securityContext: fsGroup: 1000 affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchExpressions: - key: app operator: In values: ["minio"] topologyKey: kubernetes.io/hostname containers: - name: minio image: 223.2.44.251/satellite/minio:latest imagePullPolicy: IfNotPresent args: - server - --address=:9000 - --console-address=:9001 - http://minio-{0...3}.minio-hl-svc.satellite-micro.svc.cluster.local:9000/data env: - name: MINIO_ROOT_USER value: "minioadmin" - name: MINIO_ROOT_PASSWORD value: "minioadmin" ports: - name: api containerPort: 9000 - name: console containerPort: 9001 volumeMounts: - name: data mountPath: /data readinessProbe: httpGet: path: /minio/health/ready port: 9000 initialDelaySeconds: 20 periodSeconds: 10 livenessProbe: httpGet: path: /minio/health/live port: 9000 initialDelaySeconds: 120 periodSeconds: 20 volumeClaimTemplates: - metadata: name: data spec: accessModes: ["ReadWriteOnce"] storageClassName: nfs-client resources: requests: storage: 5Gi
|
关键配置解析
网络配置
1 2
| http://minio-{0...3}.minio-hl-svc.satellite-micro.svc.cluster.local:9000/data
|
健康检查
- 就绪探针:检查服务是否准备好接收流量
- 存活探针:检查服务是否正常运行
存储配置
- PVC 模板:自动为每个 Pod 创建独立的存储卷
- 存储类:根据你的集群环境选择合适的存储类
部署步骤
1. 创建配置文件
将上述 YAML 内容分别保存为:
01-minio-headless-svc.yaml
02-minio-svc.yaml
03-minio-statefulset.yaml
2. 按顺序部署
1 2 3 4 5 6 7 8
| kubectl apply -f 01-minio-headless-svc.yaml
kubectl apply -f 02-minio-svc.yaml
kubectl apply -f 03-minio-statefulset.yaml
|
3. 验证部署
1 2 3 4 5 6 7 8 9 10 11
| kubectl get pods -n satellite-micro
kubectl get svc -n satellite-micro
kubectl get pvc -n satellite-micro
kubectl logs -f minio-0 -n satellite-micro
|
4. 访问服务
部署完成后,通过以下方式访问:
- API 接口:
http://your-node-ip:30900
- 管理控制台:
http://your-node-ip:30901
Docker vs Kubernetes 对比
1️⃣ Docker 分布式模式
特点:需要手动配置网络和存储
1 2 3 4 5 6 7 8
| minio-0 10.0.0.1 minio-1 10.0.0.2 minio-2 10.0.0.3 minio-3 10.0.0.4
minio server http://minio-0/data http://minio-1/data http://minio-2/data http://minio-3/data
|
缺点:
- 需要手动管理网络配置
- 存储管理复杂
- 扩缩容困难
- 缺乏自动故障恢复
2️⃣ Kubernetes StatefulSet 模式
特点:自动化管理,高可用性
自动服务发现
1 2 3 4 5 6 7
| minio-0, minio-1, minio-2, minio-3
minio-0.minio-hl-svc.satellite-micro.svc.cluster.local minio-1.minio-hl-svc.satellite-micro.svc.cluster.local
|
优势对比
| 特性 |
Docker |
Kubernetes |
| 服务发现 |
手动配置 hosts |
DNS 自动解析 |
| 存储管理 |
手动挂载 |
PVC 自动绑定 |
| 扩缩容 |
复杂 |
简单(修改 replicas) |
| 故障恢复 |
手动处理 |
自动重启 |
| 负载均衡 |
需要 Nginx |
Service 自动处理 |
| 监控 |
需要额外工具 |
原生支持 |
常见问题与注意事项
⚠️ 重要提醒
- 节点顺序:
minio-{0...3} 中的 0-3 顺序不要随意改动
- 存储类:确保
storageClassName 与你的集群环境匹配
- 命名空间:所有资源必须在同一个命名空间中
- 网络策略:确保 Pod 间网络通信正常
🔧 故障排查
Pod 启动失败
1 2 3 4 5
| kubectl describe pod minio-0 -n satellite-micro
kubectl get events -n satellite-micro --sort-by='.lastTimestamp'
|
PVC 绑定失败
1 2 3 4 5
| kubectl get storageclass
kubectl describe pvc data-minio-0 -n satellite-micro
|
网络连接问题
1 2
| kubectl exec -it minio-0 -n satellite-micro -- nslookup minio-1.minio-hl-svc.satellite-micro.svc.cluster.local
|
📊 性能优化建议
- 节点分布:使用
podAntiAffinity 确保 Pod 分布在不同节点
- 存储优化:根据 I/O 需求选择合适的存储类
- 资源限制:为容器设置合适的 CPU 和内存限制
- 网络优化:使用 CNI 插件优化网络性能
参考资料