
View Code
基于Python的自动化集群实现,初始化节点为node_1~node_6,节点实例需要为集群模式,三主三从,自动化集群,分配slots,加入从节点,3秒钟左右完成
- import redis
- #master
- node_1 = {'host': '127.0.0.1', 'port': 9001, 'password': '***'}
- node_2 = {'host': '127.0.0.1', 'port': 9002, 'password': '***'}
- node_3 = {'host': '127.0.0.1', 'port': 9003, 'password': '***'}
- #slave
- node_4 = {'host': '127.0.0.1', 'port': 9004, 'password': '***'}
- node_5 = {'host': '127.0.0.1', 'port': 9005, 'password': '***'}
- node_6 = {'host': '127.0.0.1', 'port': 9006, 'password': '***'}
- redis_conn_1 = redis.StrictRedis(host=node_1["host"], port=node_1["port"], password=node_1["password"])
- redis_conn_2 = redis.StrictRedis(host=node_2["host"], port=node_2["port"], password=node_2["password"])
- redis_conn_3 = redis.StrictRedis(host=node_3["host"], port=node_3["port"], password=node_3["password"])
- # cluster meet
- redis_conn_1.execute_command("cluster meet {0} {1}".format(node_2["host"],node_2["port"]))
- redis_conn_1.execute_command("cluster meet {0} {1}".format(node_3["host"],node_3["port"]))
- print('#################flush slots #################')
- redis_conn_1.execute_command('cluster flushslots')
- redis_conn_2.execute_command('cluster flushslots')
- redis_conn_3.execute_command('cluster flushslots')
- print('#################add slots#################')
- for i in range(0,16383+1):
- if i <= 5461:
- try:
- redis_conn_1.execute_command('cluster addslots {0}'.format(i))
- except:
- print('cluster addslots {0}'.format(i) +' error')
- elif 5461 < i and i <= 10922:
- try:
- redis_conn_2.execute_command('cluster addslots {0}'.format(i))
- except:
- print('cluster addslots {0}'.format(i) + ' error')
- elif 10922 < i:
- try:
- redis_conn_3.execute_command('cluster addslots {0}'.format(i))
- except:
- print('cluster addslots {0}'.format(i) + ' error')
- print()
- print('#################cluster status#################')
- print()
- print('##################'+str(node_1["host"])+':'+str(node_1["port"])+'##################')
- print(str(redis_conn_1.execute_command('cluster info'), encoding = "utf-8").split("\n")[0])
- print('##################'+str(node_2["host"])+':'+str(node_2["port"])+'##################')
- print(str(redis_conn_1.execute_command('cluster info'), encoding = "utf-8").split("\n")[0])
- print('##################'+str(node_3["host"])+':'+str(node_3["port"])+'##################')
- print(str(redis_conn_1.execute_command('cluster info'), encoding = "utf-8").split("\n")[0])
- #slave cluster meet
- redis_conn_1.execute_command("cluster meet {0} {1}".format(node_4["host"],node_4["port"]))
- redis_conn_2.execute_command("cluster meet {0} {1}".format(node_5["host"],node_5["port"]))
- redis_conn_3.execute_command("cluster meet {0} {1}".format(node_6["host"],node_6["port"]))
- #cluster nodes
- print(str(redis_conn_1.execute_command('cluster nodes'), encoding = "utf-8"))