kubernetes 中部署redis集群

reids 集群简介

Redis Cluster 是一组 Redis 实例,旨在通过对数据库进行分区来扩展数据库,从而使其更具弹性。
群集中的每个成员(无论是主副本还是辅助副本)都管理哈希槽的子集。
如果主机无法访问,则其从机将升级为主机。在由三个主节点组成的最小 Redis 群集中,每个主节点都有一个从节点(以实现最小的故障转移),
每个主节点都分配有一个介于 0 到 16,383 之间的哈希槽范围。
节点 A 包含从 0 到 5000 的哈希槽,节点 B 从 5001 到 10000,节点 C 从 10001 到 16383。群集内部的通信是通过内部总线进行的,
使用协议传播有关群集的信息或发现新节点。

部署 redis 集群

建立应用

由yaml文件创建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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
## configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-cluster-configmap
namespace: project
data:
update-node.sh: |
#!/bin/sh
REDIS_NODES="/data/nodes.conf"
sed -i -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/${POD_IP}/" ${REDIS_NODES}
exec "$@"
redis.conf: |+
cluster-enabled yes
cluster-require-full-coverage no
cluster-node-timeout 15000
cluster-config-file /data/nodes.conf
cluster-migration-barrier 1
appendonly yes
protected-mode no
---

## sts
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis-cluster
namespace: project
spec:
serviceName: redis-cluster
replicas: 6
selector:
matchLabels:
app: redis-cluster
template:
metadata:
labels:
app: redis-cluster
spec:
containers:
- name: redis
image: redis:5
ports:
- containerPort: 6379
name: client
- containerPort: 16379
name: gossip
command: ["/conf/update-node.sh", "redis-server", "/conf/redis.conf"]
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
volumeMounts:
- name: conf
mountPath: /conf
readOnly: false
- name: data
mountPath: /data
readOnly: false
volumes:
- name: conf
configMap:
name: redis-cluster-configmap
defaultMode: 0755
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 5Gi
storageClassName: local
---

## svc
apiVersion: v1
kind: Service
metadata:
name: redis-cluster-svc
namespace: project
spec:
type: ClusterIP
ports:
- port: 6379
targetPort: 6379
name: client
- port: 16379
targetPort: 16379
name: gossip
selector:
app: redis-cluster

初始化集群

运行以下命令并键入yes以接受配置(前三个节点成为主节点,后三个节点成为从节点)

1
kubectl exec -n project -it redis-cluster-0 -- redis-cli --cluster create --cluster-replicas 1 $(kubectl get pods -n project -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 ')

验证集群

使用redis-cli cluster info 查看集群信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
127.0.0.1:6379> 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:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1250
cluster_stats_messages_pong_sent:1211
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:2462
cluster_stats_messages_ping_received:1207
cluster_stats_messages_pong_received:1251
cluster_stats_messages_meet_received:4
cluster_stats_messages_received:2462

使用 redis-cli -c 进行数据写入和读写

1
2
3
4
5
6
7
8
9
10
11
## 写
root@redis-cluster-2:/data# redis-cli -c
127.0.0.1:6379> set hello world
-> Redirected to slot [866] located at 10.233.90.231:6379
OK

## 读
root@redis-cluster-5:/data# redis-cli -c
127.0.0.1:6379> get hello
-> Redirected to slot [866] located at 10.233.90.231:6379
"world"