项目作者: sobotklp

项目描述 :
Redis Cluster on Kubernetes
高级语言: Shell
项目地址: git://github.com/sobotklp/kubernetes-redis-cluster.git
创建时间: 2016-07-10T06:13:04Z
项目社区:https://github.com/sobotklp/kubernetes-redis-cluster

开源协议:

下载


Redis Cluster on Kubernetes

This module is intended to simplify the creation and operation of a Redis Cluster deployment in Kubernetes.
I don’t recommend that you run this in production - it’s just meant to be an illustrative example of a nontrivial Stateful Set deployment.

Requirements

  • Kubernetes 1.17.0+
  • Minikube to run the module locally

How it works

These directions assume some familiarity with Redis Cluster.

When you create the resources in Kubernetes, it will create a 6-member (the minimum recommended size) Stateful Set cluster where members 0-2 are master nodes and all other members are replicas.

Testing it out

To launch the cluster, have Kubernetes create all the resources in redis-cluster.yml:

  1. $ kubectl create -f redis-cluster.yml
  2. service/redis-cluster created
  3. configmap "redis-cluster-config" configured
  4. poddisruptionbudget.policy/redis-cluster-pdb created
  5. statefulset.apps/redis-cluster created

Wait a bit for the service to initialize.

Once all the pods are initialized, you can see that Pod “redis-cluster-0” became the cluster master with the other nodes as slaves.

  1. $ kubectl exec redis-cluster-0 -- redis-cli cluster nodes
  2. Defaulted container "redis-cluster" out of: redis-cluster, init-redis-cluster (init)
  3. 532505ce41c64ddf4aff143406eb90424c29c138 10.1.0.136:6379@16379 myself,master - 0 1642662784000 1 connected 0-5461
  4. 1f188afd5f2a228320cc43753e4b6a1c01c32445 10.1.0.139:6379@16379 slave 532505ce41c64ddf4aff143406eb90424c29c138 0 1642662785392 1 connected
  5. 07939f1e633cd3538f8730017aa5ce1d0f8ba680 10.1.0.140:6379@16379 slave de3b3a6d02c81f4104566828e8a523b46e31cd02 0 1642662785000 0 connected
  6. de3b3a6d02c81f4104566828e8a523b46e31cd02 10.1.0.137:6379@16379 master - 0 1642662784386 0 connected 5462-10922
  7. c53e989fa503b04ecc7c651f448a7fc07ac3c975 10.1.0.138:6379@16379 master - 0 1642662786399 2 connected 10923-16383
  8. a43a4a9d81ae095bfdd373ed2c9aebbe958d086a 10.1.0.141:6379@16379 slave c53e989fa503b04ecc7c651f448a7fc07ac3c975 0 1642662784000 2 connected

Also, you should be able to use redis-cli to connect to a cluster node we just created

  1. $ kubectl exec -it redis-cluster-0 -- redis-cli

You can also check the slot configuration here:

  1. $ kubectl exec redis-cluster-0 -- redis-cli --cluster check localhost 6379
  2. Defaulted container "redis-cluster" out of: redis-cluster, init-redis-cluster (init)
  3. localhost:6379 (55a74f86...) -> 0 keys | 5462 slots | 1 slaves.
  4. 10.1.0.126:6379 (f5b39569...) -> 0 keys | 5461 slots | 1 slaves.
  5. 10.1.0.125:6379 (742cf86e...) -> 0 keys | 5461 slots | 1 slaves.
  6. [OK] 0 keys in 3 masters.
  7. 0.00 keys per slot on average.
  8. >>> Performing Cluster Check (using node localhost:6379)
  9. M: 55a74f86cf241a90f66a94a6c1789e031adbcc0c localhost:6379
  10. slots:[0-5461] (5462 slots) master
  11. 1 additional replica(s)
  12. M: f5b39569a75fe72cb16e207f2947d22c625a39ab 10.1.0.126:6379
  13. slots:[10923-16383] (5461 slots) master
  14. 1 additional replica(s)
  15. S: ddea6fbe8baa7938504f9f1ff503f0f190b49bc3 10.1.0.127:6379
  16. slots: (0 slots) slave
  17. replicates 55a74f86cf241a90f66a94a6c1789e031adbcc0c
  18. M: 742cf86e53a93473d17d352d2100b7db9dc61b72 10.1.0.125:6379
  19. slots:[5462-10922] (5461 slots) master
  20. 1 additional replica(s)
  21. S: 054f92cfb064e5cd762e466799311fbc21228049 10.1.0.128:6379
  22. slots: (0 slots) slave
  23. replicates 742cf86e53a93473d17d352d2100b7db9dc61b72
  24. S: ccc463f1aa52c9afd12aa945d7869c2a44f81c9d 10.1.0.129:6379
  25. slots: (0 slots) slave
  26. replicates f5b39569a75fe72cb16e207f2947d22c625a39ab
  27. [OK] All nodes agree about slots configuration.
  28. >>> Check for open slots...
  29. >>> Check slots coverage...
  30. [OK] All 16384 slots covered.

To add more nodes to the cluster, you can simply use normal stateful set scaling:

  1. kubectl scale -n default statefulset redis-cluster --replicas=12

Newly-created nodes will join the cluster as replicas.

To clean this mess off your Minikube VM:

  1. $ kubectl delete service,statefulsets redis-cluster
  2. $ kubectl delete configmaps redis-cluster-config
  3. $ kubectl delete poddisruptionbudgets.policy redis-cluster-pd
  4. # To prevent potential data loss, deleting a statefulset doesn't delete the pods. Gotta do that manually.
  5. $ kubectl delete pod redis-cluster-0 redis-cluster-1 redis-cluster-2 redis-cluster-3 redis-cluster-4 redis-cluster-5

TODO

  • Add documentation for common Redis Cluster operations: adding nodes, resharding, deleting nodes
  • Test some failure scenarios
  • Use a persistentvolume to store backup files
  • Create a ScheduledJob to do automated backups once this feature is finished.
  • Make it easier to assign new masters
  • Cluster members should check whether nodes.conf exists and if so, skip pod initialization.