介绍
Redis(代表REmote DIctionary Server)是一种开源的内存中数据存储,通常用作数据库,缓存或消息代理。它可以存储和操作高级数据类型,例如列表,地图,集合和排序集合。由于Redis接受多种格式的密钥,因此可以在服务器上执行操作,从而减少了客户端的工作量。它仅将磁盘用于持久性,而将数据库完全保存在内存中。Redis是一种流行的数据存储解决方案,并被GitHub,Pinterest,Snapchat,Twitter,StackOverflow,Flickr等技术巨头所使用。
为什么要使用Redis?
- 它的速度非常快。它是用ANSI C编写的,并且可以在POSIX系统上运行,例如Linux,Mac OS X和Solaris。
- Redis通常被排名为最流行的键/值数据库和最流行的与容器一起使用的NoSQL数据库。
- 其缓存解决方案减少了对云数据库后端的调用次数。
- 应用程序可以通过其客户端API库对其进行访问。
- 所有流行的编程语言都支持Redis。
- 它是开源且稳定的。
什么是Redis群集?
Redis Cluster是一组Redis实例,旨在通过对数据库进行分区来扩展数据库,从而使其更具弹性。群集中的每个成员(无论是主副本还是辅助副本)都管理哈希槽的子集。如果主机无法访问,则其从机将升级为主机。在由三个主节点组成的最小Redis群集中,每个主节点都有一个从节点(以实现最小的故障转移),每个主节点都分配有一个介于0到16,383之间的哈希槽范围。节点A包含从0到5000的哈希槽,节点B从5001到10000,节点C从10001到16383。群集内部的通信是通过内部总线进行的,使用八卦协议传播有关群集的信息或发现新节点。
在Kubernetes中部署Redis集群
在Kubernetes
中部署Redis集群面临挑战,因为每个Redis实例都依赖于一个配置文件,该文件可以跟踪其他集群实例及其角色。为此,我们需要结合使用Kubernetes
StatefulSets
和PersistentVolumes
。
部署Redis
我把资源清单上传到了我的Github上
$ git clone https://github.com/jwangkun/redis-sts.git
- 创建pv
$ kubectl apply -f redis-pv.yml
persistentvolume/redis-pv1 created
persistentvolume/redis-pv2 created
persistentvolume/redis-pv3 created
persistentvolume/redis-pv4 created
persistentvolume/redis-pv5 created
persistentvolume/redis-pv6 created
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
redis-pv1 5Gi RWO Recycle Available redis-cluster 71s
redis-pv2 5Gi RWO Recycle Available redis-cluster 71s
redis-pv3 5Gi RWO Recycle Available redis-cluster 71s
redis-pv4 5Gi RWO Recycle Available redis-cluster 71s
redis-pv5 5Gi RWO Recycle Available redis-cluster 71s
redis-pv6 5Gi RWO Recycle Available redis-cluster 71s
- 创建statefulset
$ kubectl apply -f redis-sts.yml
configmap/redis-cluster created
statefulset.apps/redis-cluster created
$ kubectl get pods -l app=redis-cluster
NAME READY STATUS RESTARTS AGE
redis-cluster-0 1/1 Running 0 53s
redis-cluster-1 1/1 Running 0 49s
redis-cluster-2 1/1 Running 0 46s
redis-cluster-3 1/1 Running 0 42s
redis-cluster-4 1/1 Running 0 38s
redis-cluster-5 1/1 Running 0 34s
- 创建service
$ kubectl apply -f redis-svc.yml
service/redis-cluster created
$ kubectl get svc redis-cluster
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis-cluster ClusterIP 10.1.0.106 <none> 6379/TCP,16379/TCP 35s
部署Redis Cluster
下一步是形成Redis集群。为此,我们运行以下命令并键入yes
以接受配置。前三个节点成为主节点,后三个节点成为从节点。
$ kubectl exec -it redis-cluster-0 -- redis-cli --cluster create --cluster-replicas 1 $(kubectl get pods -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 ')
$ kubectl exec -it redis-cluster-0 -- redis-cli --cluster create --cluster-replicas 1 $(kubectl get pods -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 ')
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.2.1.16:6379 to 10.2.0.32:6379
Adding replica 10.2.2.17:6379 to 10.2.1.15:6379
Adding replica 10.2.0.33:6379 to 10.2.2.16:6379
M: 6ceaf0ac8ea5f0db95a9c0ad73ee27f9eb9f87aa 10.2.0.32:6379
slots:[0-5460] (5461 slots) master
M: 71635c5b9476582d7a0ba851785ce8b25c09b7a0 10.2.1.15:6379
slots:[5461-10922] (5462 slots) master
M: 526011f63b5196fcdefda225f031f05e1ae25de3 10.2.2.16:6379
slots:[10923-16383] (5461 slots) master
S: e66ccd638f128e870109bef35ea851322a7d9593 10.2.0.33:6379
replicates 526011f63b5196fcdefda225f031f05e1ae25de3
S: 8d0df4ad828cfb85a7d424fd59d0a21edaf3c476 10.2.1.16:6379
replicates 6ceaf0ac8ea5f0db95a9c0ad73ee27f9eb9f87aa
S: e46feec2e68aa297c22cbb90739a8334219b6f4c 10.2.2.17:6379
replicates 71635c5b9476582d7a0ba851785ce8b25c09b7a0
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.....
>>> Performing Cluster Check (using node 10.2.0.32:6379)
M: 6ceaf0ac8ea5f0db95a9c0ad73ee27f9eb9f87aa 10.2.0.32:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 71635c5b9476582d7a0ba851785ce8b25c09b7a0 10.2.1.15:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: e46feec2e68aa297c22cbb90739a8334219b6f4c 10.2.2.17:6379
slots: (0 slots) slave
replicates 71635c5b9476582d7a0ba851785ce8b25c09b7a0
M: 526011f63b5196fcdefda225f031f05e1ae25de3 10.2.2.16:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: e66ccd638f128e870109bef35ea851322a7d9593 10.2.0.33:6379
slots: (0 slots) slave
replicates 526011f63b5196fcdefda225f031f05e1ae25de3
S: 8d0df4ad828cfb85a7d424fd59d0a21edaf3c476 10.2.1.16:6379
slots: (0 slots) slave
replicates 6ceaf0ac8ea5f0db95a9c0ad73ee27f9eb9f87aa
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
验证集群部署
检查群集详细信息以及每个成员的角色。
$ kubectl exec -it redis-cluster-0 -- redis-cli 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:69
cluster_stats_messages_pong_sent:79
cluster_stats_messages_sent:148
cluster_stats_messages_ping_received:74
cluster_stats_messages_pong_received:69
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:148
$ for x in $(seq 0 5); do echo "redis-cluster-$x"; kubectl exec redis-cluster-$x -- redis-cli role; echo; done
redis-cluster-0
master
112
10.2.1.16
6379
112
redis-cluster-1
master
112
10.2.2.17
6379
112
redis-cluster-2
master
112
10.2.0.33
6379
112
redis-cluster-3
slave
10.2.2.16
6379
connected
112
redis-cluster-4
slave
10.2.0.32
6379
connected
112
redis-cluster-5
slave
10.2.1.15
6379
connected
112
测试Redis集群
我们想使用集群,然后模拟节点的故障。对于前一项任务,我们将部署一个简单的Python应用程序,而对于后者,我们将删除一个节点并观察集群行为。
部署点击计数器应用
我们将一个简单的应用程序部署到集群中,并在其前面放置一个负载平衡器。此应用程序的目的是在将计数器值作为HTTP响应返回之前,增加计数器并将其存储在Redis集群中。
$ kubectl apply -f app-deployment-service.yml
service/hit-counter-lb created
deployment.apps/hit-counter-app created
在此过程中,如果我们继续加载页面,计数器将继续增加,并且在删除Pod
之后,我们看到没有数据丢失。
$ curl `kubectl get svc hit-counter-lb -o json|jq -r .spec.clusterIP`
I have been hit 20 times since deployment.
$ curl `kubectl get svc hit-counter-lb -o json|jq -r .spec.clusterIP`
I have been hit 21 times since deployment.
$ curl `kubectl get svc hit-counter-lb -o json|jq -r .spec.clusterIP`
I have been hit 22 times since deployment.
$ kubectl delete pods redis-cluster-0
pod "redis-cluster-0" deleted
$ kubectl delete pods redis-cluster-1
pod "redis-cluster-1" deleted
$ curl `kubectl get svc hit-counter-lb -o json|jq -r .spec.clusterIP`
I have been hit 23 times since deployment.