k8s上通过operator搭建redis集群

Redis Cluster Operator在Kubernetes上管理 Redis 集群。

每个主节点及其从节点由一个 statefulSet管理,为每个statefulSet创建一个 headless svc,并为所有节点创建一个clusterIP服务。

每个statefulset使用PodAntiAffinity来保证主从分散在不同的节点上。 同时,operator在每个statefulset中选择master时,会优先选择k8s不同节点的pod作为master。

具体的代码及介绍可以参考https://github.com/ucloud/redis-cluster-operator

upload-image

部署operator

部署operator可以通过helm的方式部署

1
2
3
helm repo add ucloud-operator https://ucloud.github.io/redis-cluster-operator/
helm repo update
helm install --generate-name ucloud-operator/redis-cluster-operator --namespace weixnie

部署redis集群

部署redis集群有很多种方案,具体可以参考如下crd的yaml进行配置。

自定义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
apiVersion: redis.kun/v1alpha1
kind: DistributedRedisCluster
metadata:
annotations:
# if your operator run as cluster-scoped, add this annotations
redis.kun/scope: cluster-scoped
name: example-distributedrediscluster
spec:
image: redis:5.0.4-alpine
masterSize: 3
clusterReplicas: 1
config:
activerehashing: "yes"
appendfsync: everysec
appendonly: "yes"
hash-max-ziplist-entries: "512"
hash-max-ziplist-value: "64"
hll-sparse-max-bytes: "3000"
list-compress-depth: "0"
maxmemory-policy: noeviction
maxmemory-samples: "5"
no-appendfsync-on-rewrite: "no"
notify-keyspace-events: ""
set-max-intset-entries: "512"
slowlog-log-slower-than: "10000"
slowlog-max-len: "128"
stop-writes-on-bgsave-error: "yes"
tcp-keepalive: "0"
timeout: "0"
zset-max-ziplist-entries: "128"
zset-max-ziplist-value: "64"

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
apiVersion: v1
kind: Secret
metadata:
annotations:
# if your operator run as cluster-scoped, add this annotations
redis.kun/scope: cluster-scoped
name: mysecret
type: Opaque
data:
password: MWYyZDFlMmU2N2Rm
---
apiVersion: redis.kun/v1alpha1
kind: DistributedRedisCluster
metadata:
name: example-distributedrediscluster
spec:
image: redis:5.0.4-alpine
masterSize: 3
clusterReplicas: 1
passwordSecret:
name: mysecret
resources:
limits:
cpu: 200m
memory: 200Mi
requests:
cpu: 200m
memory: 100Mi

redis集群示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: redis.kun/v1alpha1
kind: DistributedRedisCluster
metadata:
annotations:
# if your operator run as cluster-scoped, add this annotations
redis.kun/scope: cluster-scoped
name: example-distributedrediscluster
spec:
image: redis:5.0.4-alpine
masterSize: 3
clusterReplicas: 1
resources:
limits:
cpu: 200m
memory: 200Mi
requests:
cpu: 200m
memory: 100Mi

这里解释下字段说明

masterSize: 是指主节点的数量
clusterReplicas: 指每个主节点从节点的数量

验证redis集群

1
2
3
4
5
6
7
[niewx@VM-0-4-centos ~]$ k get pod | grep redis
drc-example-distributedrediscluster-0-0 1/1 Running 0 41m
drc-example-distributedrediscluster-0-1 1/1 Running 0 41m
drc-example-distributedrediscluster-1-0 1/1 Running 0 41m
drc-example-distributedrediscluster-1-1 1/1 Running 0 41m
drc-example-distributedrediscluster-2-0 1/1 Running 0 41m
drc-example-distributedrediscluster-2-1 1/1 Running 0 41m

然后用redis连接工具查看集群状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> 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:4
cluster_my_epoch:0
cluster_stats_messages_ping_sent:2443
cluster_stats_messages_pong_sent:2421
cluster_stats_messages_sent:4864
cluster_stats_messages_ping_received:2416
cluster_stats_messages_pong_received:2443
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:4864

k8s上通过operator搭建redis集群
https://www.niewx.cn/2022/03/21/2022-03-21-Operator-builds-redis-cluster-on-k8s/
作者
VashonNie
发布于
2022年3月21日
许可协议