K8S部署Redis(单机、集群)的超详细步骤
目录
- 前言
- 一、部署单机Redis
- 步骤一:创建ConfigMap
- 步骤二:创建Deployment
- 步骤三:创建Service
- 步骤四:验证单机Redis
- 小结
- 二、部署6节点Redis集群
- 步骤一:创建ConfigMap
- 步骤二:创建Deployment
- 步骤三:创建Service
- 步骤四:Redis集群初始化
- 步骤五:验证Redis集群
- 小结
- 附录1:StatefulSet方式部署Redis集群(暴露1个端口)
- 附录2:StatefulSet方式部署Redis集群(暴露6个端口)
- 三、Redis集群存在的问题以及解决方案
- 步骤一:设置hostNetwork
- 步骤二:配置环境变量HOST_IP
- 步骤三:使用宿主机IP初始化Redis集群
- 步骤四:验证Redis集群
- 小结
- 附录1:Deployment方式部署Redis集群(暴露6个端口)
- 附录2:StatefulSet方式部署Redis集群(暴露6个端口)
- 结论
前言
在今天的讨论中,我们将深入研究如何将Redis数据库迁移到云端,以便更好地利用云计算的优势提高数据管理的灵活性。
Redis(Remote Dictionary Server)是一个开源的、基于内存的数据结构存储系统,它可以用作数据库、缓存和消息代理。Redis支持多种数据结构,如字符串、列表、集合、散列等,具有高性能、低延迟、持久化等特点。
在Kubernetes(K8S)中部署Redis是一项常见的任务,因为Redis是一个高性能的键值存储数据库,非常适合用于缓存、消息队列等场景。本文将分别介绍如何在K8S集群中部署单机Redis和Redis集群。
一、部署单机Redis
步骤一:创建ConfigMap
首先,我们需要创建一个ConfigMap,用来存储和管理Redis的相关配置。
apiVersion: v1 kind: ConfigMap metadata: name: redis-single-config 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 redis#single#test
步骤二:创建Deployment
接下来,我们需要创建一个Deployment,用来定义Redis的副本数量、镜像版本等相关信息。
apiVersion: apps/v1 kind: Deployment metadata: name: redis-single spec: replicas: 1 selector: matchLabels: app: redis-single template: metadata: labels: app: redis-single 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 - name: init-1 image: busybox imagePullPolicy: IfNotPresent terminationMessagePath: /dev/termination-log terminationMessagePolicy: File command: [ "sh", "-c", "echo never > /sys/kernel/mm/transparent_hugepage/enabled" ] securityContext: privileged: true containers: - name: redis-single image: redis:6.0.8 imagePullPolicy: IfNotPresent terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - name: redis-data mountPath: /data - 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-data hostPath: path: /var/lib/docker/redis/single type: DirectoryOrCreate - name: redis-config configMap: name: redis-single-config items: - key: redis.conf path: redis.conf
在这个文件中,我们定义了一个名为
redis-single
的Deployment,它使用了之前创建的ConfigMap中的配置文件,并将其挂载到容器的/usr/local/etc/redis/redis.conf
路径下。此外,我们还将容器的/data
目录挂载到宿主机的/var/lib/docker/redis/single
目录。配置initContainers
的目的是为了解决启动时出现的两个警告。
WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
步骤三:创建Service
然后,我们还需要创建一个Service,用来将K8S集群中运行的Redis实例暴露为可访问的服务。
apiVersion: v1 kind: Service metadata: name: service-redis-single labels: app: redis-single spec: selector: app: redis-single ports: - name: redis-single port: 6379 targetPort: 6379 nodePort: 30000 type: NodePort
步骤四:验证单机Redis
- 首先,使用Redis可视化工具连接到刚部署的单机Redis上,验证Redis是否正常。

- 接下来,将副本数量调整为0,模拟Redis宕机情况。此时与Redis已断开连接。


- 然后,将副本数量恢复,模拟Redis宕机后重启。此时与Redis重新建立连接,功能使用正常。

小结