将 Redis 的配置挂载到 ConfigMap
使用 ConfigMap 创建 redis.conf。
apiVersion: v1
kind: ConfigMap
metadata:
labels:
app.kubernetes.io/name: redis
app.kubernetes.io/component: database
name: redis
namespace: tools
data:
redis.conf: |-
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
#logfile "/data/redis.log"
requirepass 123456
#datadir "/data"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
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 no
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
使用 StatefulSet 创建 Redis
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app.kubernetes.io/name: redis
app.kubernetes.io/component: database
name: redis
namespace: tools
spec:
podManagementPolicy: OrderedReady
replicas: 6
selector:
matchLabels:
app.kubernetes.io/name: redis
app.kubernetes.io/component: database
serviceName: redis-cluster
template:
metadata:
labels:
app.kubernetes.io/name: redis
app.kubernetes.io/component: database
spec:
containers:
- args:
- /conf/redis.conf
image: redis:5.0.5
imagePullPolicy: IfNotPresent
name: redis
ports:
- containerPort: 6379
name: client
protocol: TCP
- containerPort: 16379
name: gossip
protocol: TCP
resources:
limits:
cpu: 250m
memory: 512Mi
requests:
cpu: 250m
memory: 512Mi
securityContext:
privileged: false
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /conf
name: conf
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- configMap:
defaultMode: 493
name: redis
name: conf
updateStrategy:
rollingUpdate:
partition: 0
type: RollingUpdate
使用 redis-cli 初始化集群
初始化 Redis 集群
redis-cli --cluster create pod1_ip:6379 pod2_ip:6379 pod3_ip:6379 pod4_ip:6379 pod5_ip:6379 pod6_ip:6379 --cluster-replicas 1
验证 Redis 集群状态
通过命令查看集群是否分配成功
>> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:9
cluster_my_epoch:7
cluster_stats_messages_ping_sent:12456796
cluster_stats_messages_pong_sent:12406231
cluster_stats_messages_update_sent:2
cluster_stats_messages_sent:24863029
cluster_stats_messages_ping_received:12406231
cluster_stats_messages_pong_received:12456024
cluster_stats_messages_fail_received:223
cluster_stats_messages_auth-req_received:2
cluster_stats_messages_update_received:3
cluster_stats_messages_received:24862483
>> cluster nodes
9416b9306b091de56a3568e15c230a5624d96bed pod1_ip:6379@16379 myself,slave 73401488a251b6d40d3b55c2c1bc4711b1cfa858 0 1662392497000 3 connected
5445d814ffbf8836991eacbdc7c8bf4dd7375419 pod2_ip:6382@16382 master - 0 1662392499000 9 connected 5461-10922
3e2405ca00ccd1160add2b63f447fd871f009c07 pod3_ip:6381@16381 master - 0 1662392499000 7 connected 0-5460
813c582e69c1862d83ebe42b9be1d34ee6de2ea7 pod4_ip:6379@16379 slave 5445d814ffbf8836991eacbdc7c8bf4dd7375419 0 1662392499267 9 connected
e7829ad2dda26f565a92d44a8f5f639785f7be85 pod5_ip:6379@16379 slave 3e2405ca00ccd1160add2b63f447fd871f009c07 0 1662392500270 7 connected
73401488a251b6d40d3b55c2c1bc4711b1cfa858 pod6_ip:6380@16380 master - 0 1662392498000 8 connected 10923-16383
使用 RedisInsight 工具查询集群效果
本文由 Ivan Dong 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Jun 13, 2023 at 10:19 am