背景简介
Redis是一种开源的内存中数据结构存储系统,用作数据库、缓存和消息中间件。支持字符串、列表、集合等数据结构,提供高速读写性能。适用于会话缓存、消息队列、排行榜等场景,通过持久化功能保障数据安全。
环境信息
- Kubernetes 已安装 【Kubernetes - 安装】
详细步骤
第一步: 配置数据持久化。【Kubernetes - 配置 NFS 数据持久化】
第二步: 配置 redis.conf 配置文件
- 创建
redis-config-map.yaml
配置文件
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
data:
redis.conf: |
requirepass your_password
appendonly yes
appendfsync everysec
解析:
-
requirepass your_password
:设置Redis的密码。 -
appendonly yes
:开启Redis的AOF(Append Only File)持久化功能,确保数据安全。 -
appendfsync everysec
:设置AOF同步策略为每秒同步一次。这表示Redis会每秒将数据写入磁盘,以减少数据丢失的风险。 -
创建 config map
$ kubectl apply -f redis-config-map.yaml -n test-namespace
configmap/redis-config created
第三步: 配置 redis 服务
- 创建
redis-statefulset.yaml
配置文件
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
spec:
serviceName: redis
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:latest
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
ports:
- containerPort: 6379
volumeMounts:
- name: redis-storage
mountPath: /data
- name: redis-config-volume
mountPath: /usr/local/etc/redis/redis.conf
subPath: redis.conf
volumes:
- name: redis-config-volume
configMap:
name: redis-config
volumeClaimTemplates:
- metadata:
name: redis-storage
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: redis-storageclass
resources:
requests:
storage: 10Gi
updateStrategy:
type: RollingUpdate
- 创建 redis 服务
$ kubectl apply -f redis-statefulset.yaml -n test-namespace
statefulset.apps/redis created
第四步: 添加 Service 以供访问 【Kubernetes - Service 】
第五步: 查看完整部署
$ kubectl get pod,pv,pvc,storageclass,svc -n test-namespace
NAME READY STATUS RESTARTS AGE
pod/redis-0 1/1 Running 0 15h
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
persistentvolume/pvc-24650c14-580c-49a1-8d13-a7aa7ae166e4 10Gi RWO Retain Bound chat-robot/redis-storage- redis-0 nfs-data-redis <unset> 16h
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
persistentvolumeclaim/redis-storage-redis-0 Bound pvc-24650c14-580c-49a1-8d13-a7aa7ae166e4 10Gi RWO nfs- data-redis <unset> 16h
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
storageclass.storage.k8s.io/nfs-data-redis nfs.csi.k8s.io Retain Immediate true 16h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/redis-svc NodePort 10.100.187.49 <none> 6379:31582/TCP 12s
以上便是本文的全部内容,感谢您的阅读,如遇到任何问题,欢迎在评论区留言讨论。