在K3s集群中部署Redis,需依托Kubernetes资源清单,完成组件配置、存储挂载、网络访问等全流程操作。本文基于实际可直接复用的YAML配置文件,详细拆解Redis在K3s环境中的部署步骤,解析核心配置要点,确保部署过程规范、可落地。
一、环境说明
本次部署基于K3s集群,选用Redis 6.0.8版本,所有相关资源统一部署在redis命名空间下(需提前执行命令kubectl create namespace redis完成命名空间创建)。部署采用StatefulSet控制器保障Redis实例的稳定运行,结合PersistentVolumeClaim(PVC)实现数据持久化,通过IngressRouteTCP暴露外部访问入口,借助ConfigMap统一管理Redis配置文件,形成完整的部署闭环。
二、核心配置文件与部署步骤
1. 配置Redis配置文件(ConfigMap)
创建redis-config.yaml文件,通过ConfigMap存储Redis核心配置参数,涵盖网络绑定、数据持久化策略、密码认证等关键配置,确保Redis运行参数可灵活调整、统一管理,具体配置如下:
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
| apiVersion: v1 kind: ConfigMap metadata: name: redis-config namespace: redis labels: app: redis data: redis.conf: | daemonize no bind 0.0.0.0 port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 pidfile /data/redis-server.pid logfile /data/redis.log loglevel notice databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /data slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes requirepass passwd
|
配置文件创建完成后,执行以下命令部署ConfigMap:
1
| kubectl apply -f redis-config.yaml
|
2. 配置持久化存储(PV/PVC)
K3s集群默认内置local-path存储类,可满足日常Redis数据持久化需求。本次部署提供两种PVC配置方式,用户可根据实际存储场景灵活选择。
方式1:仅创建PVC(基于local-path存储类)
创建redis-pvc-local-path.yaml文件,申请2Gi存储容量,访问模式设为ReadWriteOnce(仅允许单个节点读写),适配单节点Redis部署场景,配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| apiVersion: v1 kind: PersistentVolumeClaim metadata: name: redis-pvc namespace: redis labels: app: redis spec: accessModes: - ReadWriteOnce storageClassName: local-path resources: requests: storage: 2Gi
|
执行部署命令:
1
| kubectl apply -f redis-pvc-local-path.yaml
|
方式2:手动创建PV+PVC(hostPath模式)
若需自定义存储路径,可创建redis-pvc-host-path.yaml文件,手动定义PersistentVolume(PV)和PVC。其中PV指定宿主机/mnt/redis/data路径作为存储目录,容量设为5Gi;PVC关联local-path存储类,申请2Gi存储容量,配置如下:
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
| apiVersion: v1 kind: PersistentVolume metadata: name: redis-pv namespace: redis labels: app: redis spec: storageClassName: redis-persistent-storage capacity: storage: 5Gi accessModes: - ReadWriteOnce hostPath: path: /mnt/redis/data
---
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: redis-pvc labels: app: redis spec: accessModes: - ReadWriteOnce storageClassName: local-path resources: requests: storage: 2Gi
|
执行部署命令:
1
| kubectl apply -f redis-pvc-host-path.yaml
|
3. 部署Redis StatefulSet
创建redis-deployment.yaml文件,采用StatefulSet控制器管理Redis Pod,配置包含初始化容器(调整内核参数)、存储卷挂载、配置文件挂载、启动命令指定等核心内容,确保Redis实例稳定启动,配置如下:
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
| apiVersion: apps/v1 kind: StatefulSet metadata: name: redis namespace: redis labels: app: redis spec: replicas: 1 selector: matchLabels: app: redis template: metadata: labels: app: redis spec: initContainers: - name: init-0 image: busybox imagePullPolicy: IfNotPresent terminationMessagePath: /dev/termination-log terminationMessagePolicy: File command: [ "sysctl", "-w", "net.core.somaxconn=511" ] securityContext: privileged: true
containers: - name: redis image: redis:6.0.8 imagePullPolicy: IfNotPresent terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - name: redis-persistent-storage mountPath: /var/lib/redis - name: redis-config mountPath: /usr/local/etc/redis/redis.conf subPath: redis.conf command: [ "redis-server" ,"/usr/local/etc/redis/redis.conf" ] env: - name: TZ value: "Asia/Shanghai" volumes: - name: timezone hostPath: path: /usr/share/zoneinfo/Asia/Shanghai - name: redis-persistent-storage persistentVolumeClaim: claimName: redis-pvc - name: redis-config configMap: name: redis-config
|
执行部署命令,启动Redis StatefulSet:
1
| kubectl apply -f redis-deployment.yaml
|
4. 配置Service暴露集群内访问
创建redis-service.yaml文件,定义ClusterIP类型Service,暴露Redis 6379端口,供K3s集群内其他应用程序访问Redis服务,配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| apiVersion: v1 kind: Service metadata: name: redis namespace: redis labels: app: redis spec: ports: - name: redis port: 6379 targetPort: 6379 protocol: TCP selector: app: redis type: ClusterIP
|
执行部署命令:
1
| kubectl apply -f redis-service.yaml
|
5. 配置IngressRouteTCP暴露外部访问
基于K3s默认集成的Traefik IngressController,创建redis-ingress.yaml文件,通过IngressRouteTCP暴露Redis的6379端口,实现集群外部对Redis服务的访问,配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| apiVersion: traefik.io/v1alpha1 kind: IngressRouteTCP metadata: name: redis-tcp namespace: redis labels: app: redis spec: entryPoints: - redis routes: - match: HostSNI(`*`) services: - name: redis port: 6379
|
执行部署命令:
1
| kubectl apply -f redis-ingress.yaml
|
注意:需提前在Traefik中配置名为redis的entryPoint(监听6379端口),否则IngressRouteTCP无法正常生效。
三、部署验证
Redis相关资源部署完成后,需通过以下命令检查资源状态,并验证服务可正常访问。
1. 检查资源状态
1 2 3 4 5 6
| kubectl get statefulset -n redis
kubectl get pods -n redis
kubectl get pvc -n redis
|
若所有资源状态均为Running/Bound,说明部署成功。
2. 访问Redis服务
四、关键配置说明
内核参数调整:通过初始化容器(initContainers)执行sysctl -w net.core.somaxconn=511,提升Redis网络连接上限,避免因连接数不足导致服务异常。
数据持久化:通过PVC挂载/var/lib/redis目录,结合Redis配置中的RDB和AOF双重持久化策略,确保数据不丢失;hostPath模式下,数据实际存储在宿主机/mnt/redis/data目录,local-path模式下存储在集群默认存储路径。
配置管理:通过ConfigMap挂载redis.conf配置文件,后续需修改Redis运行参数时,仅需更新ConfigMap并重启Pod即可,无需重建镜像,提升维护效率。
网络安全:采用ClusterIP类型Service限制集群内访问,通过IngressRouteTCP实现外部可控访问,同时Redis配置密码认证,多重保障服务安全。
五、总结
本文基于实际生产可用的YAML配置文件,完成了K3s集群中Redis的标准化部署,覆盖配置管理、数据持久化、网络访问、部署验证等核心环节。所有配置均保持原始内容不变,可直接复用至K3s环境,无需额外修改,能够满足中小规模业务场景下Redis的稳定运行需求,为K3s环境中Redis的部署提供了可落地的实践参考。