--- apiVersion: v1 data: redis.conf: |- # 绑定地址(容器内监听所有接口) bind 0.0.0.0 # 端口 port 6379 # 后台运行(容器中必须设为 no) daemonize no # 日志级别(日志输出到 stdout,由 Docker/K8s 收集) loglevel notice # 数据库数量 databases 16 # 是否启用 AOF 持久化(推荐开启) appendonly yes # AOF 文件名 appendfilename "appendonly.aof" # AOF 同步策略:每秒同步一次(性能与安全平衡) appendfsync everysec # AOF 重写时是否忽略 fsync(提升性能) no-appendfsync-on-rewrite no # 自动触发 AOF 重写的条件 auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb # RDB 快照(可选,若只用 AOF 可关闭) save 900 1 save 300 10 save 60 10000 # 工作目录(必须匹配 volumeMount 的 /data) dir /data # 密码认证(请替换为你的实际密码) requirepass K4l2kC8Y0Nsep7eS # 客户端空闲超时(0 表示永不超时) timeout 0 # TCP keepalive tcp-keepalive 300 # 最大内存限制(根据你的 Pod 资源调整,例如 2gb) maxmemory 2gb # 内存淘汰策略(推荐 allkeys-lru 或 volatile-lru) maxmemory-policy allkeys-lru # 保护模式(在 bind 0.0.0.0 且无密码时会拒绝连接) # 因为你设置了 requirepass,可保持 yes protected-mode yes # 是否禁止 CONFIG 命令(增强安全) # rename-command CONFIG "" kind: ConfigMap metadata: name: redis-config namespace: tools --- apiVersion: apps/v1 kind: StatefulSet metadata: name: redis namespace: tools spec: replicas: 1 serviceName: redis-headless selector: matchLabels: app: redis template: metadata: labels: app: redis spec: affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - preference: matchExpressions: - key: mid operator: In values: - '1' weight: 1 containers: - name: redis #image: redis:8.0-alpine image: registry.cn-hangzhou.aliyuncs.com/zhengyu1992/redis:8.0-alpine imagePullPolicy: IfNotPresent command: ["redis-server", "/etc/redis/redis.conf"] env: - name: TZ value: "Asia/Shanghai" volumeMounts: - name: config mountPath: /etc/redis/redis.conf subPath: redis.conf - name: data mountPath: /data tolerations: - key: mid operator: Equal value: '1' effect: NoSchedule volumes: - name: timezone hostPath: path: /usr/share/zoneinfo/Asia/Shanghai - name: config configMap: name: redis-config items: - key: redis.conf path: redis.conf volumeClaimTemplates: - metadata: name: data spec: accessModes: - ReadWriteOnce storageClassName: nfs-sc resources: requests: storage: 20Gi --- apiVersion: v1 kind: Service metadata: name: redis-headless namespace: tools labels: app: redis spec: clusterIP: None selector: app: redis ports: - name: redis port: 6379 targetPort: 6379 --- apiVersion: v1 kind: Service metadata: name: redis namespace: tools labels: app: redis spec: selector: app: redis ports: - name: redis port: 6379 targetPort: 6379 type: NodePort