Redis實體安裝安裝說明:自動解壓縮安裝包,按照指定路徑編譯安裝,復制組態檔模板到Redis實體路的資料徑下,根據埠號修改組態檔模板三個必須檔案:1,組態檔,2,當前shell腳本,3,安裝包引數1:basedir,redis安裝包路徑引數2:安裝實體路徑引數3:安裝包名稱引數4:安裝實體的埠號
#!/bin/bashset -eif [ $# -lt 4 ]; then echo "$(basename $0): Missing script argument" echo "$(installdir $0) [installfilename] [port] " exit 9fiPotInUse=`netstat -anp | awk '{print $4}' | grep $4 | wc -l`if [ $PotInUse -gt 0 ];then echo "ERROR" $4 "Port is used by another process!" exit 9fibasedir=$1installdir=$2installfilename=$3port=$4cd $basedirtar -zxvf $installfilename.tar.gz >/dev/null 2>&1 &cd $installfilenamemkdir -p $installdirmake PREFIX=$installdir installsleep 1s cp $basedir/redis.conf $installdirsed -i "s/instance_port/$port/g" $installdir/redis.confsleep 1s cd $installdir./bin/redis-server redis.conf >/dev/null 2>&1 &
組態檔模板

################################## INCLUDES #################################### include /path/to/local.conf# include /path/to/other.conf################################## MODULES ###################################### loadmodule /path/to/my_module.so# loadmodule /path/to/other_module.so################################## NETWORK #####################################bind 127.0.0.1 & your ipport instance_porttcp-backlog 511timeout 0tcp-keepalive 300################################# GENERAL #####################################daemonize yessupervised nopidfile ./redis_instance_port.pidloglevel noticelogfile ./redis_log.logdatabases 16always-show-logo yes################################ SNAPSHOTTING ################################save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir ./################################# REPLICATION ################################## masterauth <master-password>replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noreplica-priority 100################################## SECURITY ###################################requirepass your_passwrod################################### CLIENTS ##################################### maxclients 10000############################## MEMORY MANAGEMENT ################################# maxmemory <bytes># maxmemory-policy noeviction# maxmemory-samples 5# replica-ignore-maxmemory yes############################# LAZY FREEING ####################################lazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush no############################## APPEND ONLY MODE ###############################appendonly noappendfilename "appendonly.aof"# appendfsync alwaysappendfsync everysec# appendfsync nono-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yes################################ LUA SCRIPTING ###############################lua-time-limit 5000################################ REDIS CLUSTER ###############################cluster-enabled yes# cluster-replica-validity-factor 10# cluster-require-full-coverage yes# cluster-replica-no-failover no########################## CLUSTER DOCKER/NAT support ########################################################## SLOW LOG ###################################slowlog-log-slower-than 10000slowlog-max-len 128################################ LATENCY MONITOR ##############################latency-monitor-threshold 0############################# EVENT NOTIFICATION ##############################notify-keyspace-events ""############################### ADVANCED CONFIG ###############################hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit replica 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60# client-query-buffer-limit 1gb# proto-max-bulk-len 512mbhz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yes########################### ACTIVE DEFRAGMENTATION ######################## Enabled active defragmentation# activedefrag yes# Minimum amount of fragmentation waste to start active defrag# active-defrag-ignore-bytes 100mb# Minimum percentage of fragmentation to start active defrag# active-defrag-threshold-lower 10# Maximum percentage of fragmentation at which we use maximum effort# active-defrag-threshold-upper 100# Minimal effort for defrag in CPU percentage# active-defrag-cycle-min 5# Maximal effort for defrag in CPU percentage# active-defrag-cycle-max 75# Maximum number of set/hash/zset/list fields that will be processed from# the main dictionary scan# active-defrag-max-scan-fields 1000View Code
安裝示例
sh redis_install.sh /usr/local/redis/ /usr/local/redis5/redis9008/ redis-5.0.4 9008

Redi實體的目錄結構

基于Python的Redis自動化集群實作
基于Python的自動化集群實作,初始化節點為node_1~node_6,節點實體需要為集群模式,三主三從,自動化集群,分配slots,加入從節點,3秒鐘左右完成
import redis#masternode_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': '***'}#slavenode_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 meetredis_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 meetredis_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 nodesprint(str(redis_conn_1.execute_command('cluster nodes'), encoding = "utf-8"))############################add salve in cluster###########################################slave cluster meetprint('################# cluster meet slave #################')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"]))sleep(5)print('################# add slave in cluster #################')# get 主節點的node_id,按照主節點的順序依次添加到主節點dict_cluster_nodes中,確保主從按照list中的順序一一對應redis_conn_1 = redis.StrictRedis(host=node_1["host"], port=node_1["port"], password=node_1["password"], decode_responses=True)dict_cluster_nodes = redis_conn_1.cluster('nodes')print(dict_cluster_nodes)dict_master_node_id = {}for m_node in master_node: for key, values in dict_cluster_nodes.items(): if key[0:key.index('@')] == str(m_node["host"])+':'+str(m_node["port"]): dict_master_node_id[key[0:key.index('@')]] = values['node_id'] # 輸出示例 ''' { '127.0.0.1:9001': '84f0c3a21ab6dd6965923915434cc62fc0f5cc2b', '127.0.0.1:9002': 'b584f695eb9c1552c25f92e28a50c9ce62ad9ee9', '127.0.0.1:9001': 'b95898c17761b448ea88bb9682bac9b69b045adc' } '''# 依次添加slave節點# 如何確保主從于list中的節點一一對應?# 在主節點上任意一個節點上get 集群的node的時候,按照master節點順序構造dict_cluster_nodes,然后遍歷dict_cluster_nodes的時候自然就一一對應了,node_index = 0for s_node in slave_node: slave_redis_conn = redis.StrictRedis(host=s_node["host"], port=s_node["port"], password=s_node["password"]) print(str(s_node["host"])+':'+str(s_node["port"]) + ' slave of----->' + str(master_node[node_index]["host"])+':'+str(master_node[node_index]["port"])) repl_command = 'cluster replicate ' + dict_master_node_id[str(master_node[node_index]["host"])+':'+str(master_node[node_index]["port"])] print(repl_command) slave_redis_conn.execute_command(repl_command) node_index = node_index + 1 ''' 127.0.0.1:9004 slave of-----> 127.0.0.1:9001 cluster replicate 84f0c3a21ab6dd6965923915434cc62fc0f5cc2b 127.0.0.1:9005 slave of-----> 127.0.0.1:9002 cluster replicate b584f695eb9c1552c25f92e28a50c9ce62ad9ee9 127.0.0.1:9006 slave of-----> 127.0.0.1:9003 cluster replicate b95898c17761b448ea88bb9682bac9b69b045adc '''
示例

這樣一個Redis的集群,從實體的安裝到集群的安裝,環境依賴本身沒有問題的話,基本上1分鐘之內可以完成這個搭建程序,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/42309.html
標籤:NoSQL
上一篇:Redis五大資料型別詳解
下一篇:Redis事務控制

