redis-config.yaml (配置文件)
bash
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
data:
redis.conf: |
# Redis general configuration
bind 0.0.0.0
protected-mode no
port 6379
dir /data
appendonly yes
sentinel.conf: |
sentinel monitor mymaster redis-master 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1
redis-master-statefulset.yaml (主)
bash
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis-master
spec:
serviceName: "redis-master"
replicas: 1
selector:
matchLabels:
app: redis
role: master
template:
metadata:
labels:
app: redis
role: master
spec:
containers:
- name: redis
image: redis:5.0.7
command: ["redis-server", "/data/redis.conf"]
volumeMounts:
- name: redis-data
mountPath: /data
- name: config
mountPath: /data/redis.conf
subPath: redis.conf
volumes:
- name: config
configMap:
name: redis-config
volumeClaimTemplates:
- metadata:
name: redis-data
spec:
storageClassName: "nfs-client-storageclass"
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
redis-slave-statefulset.yaml (从)
bash
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis-slave
spec:
serviceName: "redis-slave"
replicas: 2
selector:
matchLabels:
app: redis
role: slave
template:
metadata:
labels:
app: redis
role: slave
spec:
containers:
- name: redis
image: redis:5.0.7
command: ["redis-server", "/data/redis.conf", "--slaveof", "redis-master", "6379"]
volumeMounts:
- name: redis-data
mountPath: /data
- name: config
mountPath: /data/redis.conf
subPath: redis.conf
volumes:
- name: config
configMap:
name: redis-config
volumeClaimTemplates:
- metadata:
name: redis-data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "nfs-client-storageclass"
resources:
requests:
storage: 1Gi
redis-service.yaml (端口)
bash
apiVersion: v1
kind: Service
metadata:
name: redis-master
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: master
---
apiVersion: v1
kind: Service
metadata:
name: redis-slave
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
role: slave
---
apiVersion: v1
kind: Service
metadata:
name: redis-sentinel
spec:
ports:
- port: 26379
targetPort: 26379
selector:
app: redis
role: sentinel
redis-sentinel-statefulset.yaml (哨兵)
bash
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-sentinel
spec:
replicas: 3
selector:
matchLabels:
app: redis
role: sentinel
template:
metadata:
labels:
app: redis
role: sentinel
spec:
# 使用 initContainer 复制并修改权限
initContainers:
- name: copy-config
image: busybox:1.27
command: ['sh', '-c', 'cp /data/sentinel.conf /tmp/sentinel.conf && chmod 644 /tmp/sentinel.conf']
volumeMounts:
- name: config
mountPath: /data
- name: sentinel-config
mountPath: /tmp
# Redis Sentinel 容器
containers:
- name: sentinel
image: redis:5.0.7
command: ["redis-sentinel", "/tmp/sentinel.conf"]
volumeMounts:
- name: sentinel-config
mountPath: /tmp
# 卷定义
volumes:
- name: config
configMap:
name: redis-config
- name: sentinel-config
emptyDir: {}