logo

轻量级K3S集群部署Redis:从基础到高可用的完整指南

作者:很酷cat2025.10.10 15:47浏览量:1

简介:本文详细介绍在K3S轻量级Kubernetes集群中部署Redis的完整流程,涵盖单节点部署、持久化存储配置、高可用集群搭建及监控优化方案,提供可落地的技术实现路径。

一、K3S与Redis的适配性分析

1.1 K3S的核心优势

K3S作为CNCF认证的轻量级Kubernetes发行版,其设计初衷是为边缘计算和资源受限环境提供完整的K8s能力。相较于标准K8s,K3S具有三大显著优势:

  • 资源占用低:单节点仅需512MB内存,适合树莓派等IoT设备
  • 部署简单:单条命令即可完成集群初始化,减少运维复杂度
  • 组件精简:默认移除存储驱动、云控制器等非必要组件

1.2 Redis的部署需求

Redis作为高性能内存数据库,其K8s部署需重点关注:

  • 持久化存储:需配置Volume保障数据持久性
  • 高可用架构:需实现主从复制和故障自动转移
  • 资源隔离:需通过ResourceQuota限制内存使用

1.3 适配场景匹配

在K3S环境中部署Redis特别适合以下场景:

二、基础环境准备

2.1 K3S集群搭建

  1. # 主节点初始化
  2. curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644
  3. # 从节点加入
  4. curl -sfL https://get.k3s.io | K3S_URL=https://<master-ip>:6443 K3S_TOKEN=<token> sh -

验证集群状态:

  1. kubectl get nodes
  2. # 应显示Ready状态,且角色为<none>或control-plane

2.2 存储类配置

推荐使用Local Path Provisioner实现轻量级持久化存储:

  1. # local-path-storage.yaml
  2. apiVersion: storage.k8s.io/v1
  3. kind: StorageClass
  4. metadata:
  5. name: local-path
  6. provisioner: rancher.io/local-path
  7. volumeBindingMode: WaitForFirstConsumer

应用配置后验证:

  1. kubectl get sc
  2. # 应显示local-path状态为Available

三、单节点Redis部署方案

3.1 基础Deployment配置

  1. # redis-single.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: redis-single
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels:
  10. app: redis
  11. template:
  12. metadata:
  13. labels:
  14. app: redis
  15. spec:
  16. containers:
  17. - name: redis
  18. image: redis:6.2-alpine
  19. ports:
  20. - containerPort: 6379
  21. volumeMounts:
  22. - name: redis-data
  23. mountPath: /data
  24. volumes:
  25. - name: redis-data
  26. persistentVolumeClaim:
  27. claimName: redis-pvc

3.2 持久化存储配置

  1. # redis-pvc.yaml
  2. apiVersion: v1
  3. kind: PersistentVolumeClaim
  4. metadata:
  5. name: redis-pvc
  6. spec:
  7. accessModes:
  8. - ReadWriteOnce
  9. resources:
  10. requests:
  11. storage: 1Gi
  12. storageClassName: local-path

3.3 服务暴露配置

  1. # redis-service.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: redis-service
  6. spec:
  7. selector:
  8. app: redis
  9. ports:
  10. - protocol: TCP
  11. port: 6379
  12. targetPort: 6379
  13. type: ClusterIP

四、高可用Redis集群部署

4.1 StatefulSet架构设计

  1. # redis-cluster.yaml
  2. apiVersion: apps/v1
  3. kind: StatefulSet
  4. metadata:
  5. name: redis-cluster
  6. spec:
  7. serviceName: redis-cluster
  8. replicas: 6
  9. selector:
  10. matchLabels:
  11. app: redis-node
  12. template:
  13. metadata:
  14. labels:
  15. app: redis-node
  16. spec:
  17. containers:
  18. - name: redis
  19. image: redis:6.2-alpine
  20. command: ["redis-server"]
  21. args: ["--cluster-enabled", "yes",
  22. "--cluster-config-file", "/data/nodes.conf",
  23. "--cluster-node-timeout", "5000",
  24. "--appendonly", "yes"]
  25. ports:
  26. - containerPort: 6379
  27. name: client
  28. - containerPort: 16379
  29. name: cluster
  30. volumeMounts:
  31. - name: redis-data
  32. mountPath: /data
  33. volumeClaimTemplates:
  34. - metadata:
  35. name: redis-data
  36. spec:
  37. accessModes: [ "ReadWriteOnce" ]
  38. resources:
  39. requests:
  40. storage: 1Gi
  41. storageClassName: local-path

4.2 集群初始化脚本

  1. # 等待所有Pod启动后执行
  2. PODS=$(kubectl get pods -l app=redis-node -o jsonpath='{.items[*].metadata.name}')
  3. HOSTS=""
  4. for pod in $PODS; do
  5. IP=$(kubectl get pod $pod -o jsonpath='{.status.podIP}')
  6. HOSTS="$HOSTS $IP:6379"
  7. done
  8. # 进入任意Pod执行集群初始化
  9. kubectl exec -it $PODS -- redis-cli --cluster create $HOSTS --cluster-replicas 1

4.3 故障转移验证

  1. # 模拟主节点故障
  2. kubectl delete pod redis-cluster-0
  3. # 验证新主节点选举
  4. kubectl exec -it redis-cluster-1 -- redis-cli cluster nodes | grep master

五、性能优化与监控

5.1 资源限制配置

  1. # 在Deployment/StatefulSet中添加resources字段
  2. resources:
  3. limits:
  4. memory: 512Mi
  5. requests:
  6. memory: 256Mi
  7. cpu: 500m

5.2 监控方案实现

使用Prometheus Operator监控Redis指标:

  1. # redis-exporter-deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: redis-exporter
  6. spec:
  7. replicas: 1
  8. template:
  9. spec:
  10. containers:
  11. - name: exporter
  12. image: oliver006/redis_exporter:latest
  13. args: ["-redis.addr", "redis-service:6379"]
  14. ports:
  15. - containerPort: 9121

5.3 水平扩展策略

基于HPA实现自动扩缩容:

  1. # redis-hpa.yaml
  2. apiVersion: autoscaling/v2
  3. kind: HorizontalPodAutoscaler
  4. metadata:
  5. name: redis-hpa
  6. spec:
  7. scaleTargetRef:
  8. apiVersion: apps/v1
  9. kind: StatefulSet
  10. name: redis-cluster
  11. minReplicas: 3
  12. maxReplicas: 10
  13. metrics:
  14. - type: Resource
  15. resource:
  16. name: memory
  17. target:
  18. type: Utilization
  19. averageUtilization: 70

六、最佳实践建议

  1. 存储选择:生产环境建议使用Longhorn或Rook-Ceph替代Local Path
  2. 网络配置:启用IPVS模式提升网络性能
    1. # 在/etc/rancher/k3s/config.yaml中添加
    2. kube-proxy-arg: "proxy-mode=ipvs"
  3. 备份策略:配置Velero实现集群级备份
  4. 安全加固:启用Redis的TLS加密和认证
    1. # 在args中添加
    2. - "--requirepass", "$(REDIS_PASSWORD)"
    3. - "--tls-port", "6379"
    4. - "--tls-cert-file", "/etc/redis/tls/redis.crt"
    5. - "--tls-key-file", "/etc/redis/tls/redis.key"

七、常见问题处理

7.1 集群节点无法加入

现象redis-cli --cluster add-node命令失败
解决方案

  1. 检查防火墙是否开放6379/16379端口
  2. 验证所有节点时间同步(kubectl exec -it <pod> -- chronyc sources

7.2 持久化数据丢失

原因:Local Path存储在节点重启后可能丢失
改进方案

  1. # 修改StorageClass配置
  2. apiVersion: storage.k8s.io/v1
  3. kind: StorageClass
  4. metadata:
  5. name: local-path
  6. provisioner: rancher.io/local-path
  7. parameters:
  8. nodePath: /mnt/disks/redis-data # 指定固定存储路径

7.3 内存不足OOM

处理流程

  1. 检查Pod事件:kubectl describe pod <redis-pod>
  2. 调整资源限制或增加节点内存
  3. 配置Redis的maxmemory策略:
    1. args: ["--maxmemory", "256mb", "--maxmemory-policy", "allkeys-lru"]

八、总结与展望

在K3S环境中部署Redis需要充分考虑资源限制与高可用需求的平衡。通过StatefulSet+持久化存储的组合,可以实现既轻量又可靠的Redis服务。未来可探索的方向包括:

  1. 使用K3S的嵌入式etcd提升集群稳定性
  2. 集成Redis Sentinel实现更精细的故障检测
  3. 开发K3S Operator实现Redis集群的自动化运维

建议生产环境采用3主3从的集群架构,并配合Prometheus+Grafana构建完整的监控体系。对于资源极度受限的场景,可考虑使用Redis的模块化架构,仅加载必要的功能模块以减少内存占用。

相关文章推荐

发表评论

活动