经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
Redis自动化安装以及集群实现
来源:cnblogs  作者:MSSQL123  时间:2019/9/19 9:07:38  对本文有异议
Redis实例安装
安装说明:自动解压缩安装包,按照指定路径编译安装,复制配置文件模板到Redis实例路的数据径下,根据端口号修改配置文件模板
配置文件,当前shell脚本,安装包
参数1:basedir,redis安装包路径
参数2:安装实例路径
参数3:安装包名称
参数4:安装实例的端口号
  1. #!/bin/bash
  2. set -e
  3. if [ $# -lt 4 ]; then
  4. echo "$(basename $0): Missing script argument"
  5. echo "$(installdir $0) [installfilename] [port] "
  6. exit 9
  7. fi
  8. PotInUse=`netstat -anp | awk '{print $4}' | grep $4 | wc -l`
  9. if [ $PotInUse -gt 0 ];then
  10. echo "ERROR" $4 "Port is used by another process!"
  11. exit 9
  12. fi
  13. basedir=$1
  14. installdir=$2
  15. installfilename=$3
  16. port=$4
  17. cd $basedir
  18. tar -zxvf $installfilename.tar.gz >/dev/null 2>&1 &
  19. cd $installfilename
  20. mkdir -p $installdir
  21. make PREFIX=$installdir install
  22. sleep 1s
  23. cp $basedir/redis.conf $installdir
  24. sed -i "s/instance_port/$port/g" $installdir/redis.conf
  25. sleep 1s
  26. cd $installdir
  27. ./bin/redis-server redis.conf >/dev/null 2>&1 &

配置文件模板

  1. ################################## INCLUDES ###################################
  2. # include /path/to/local.conf
  3. # include /path/to/other.conf
  4. ################################## MODULES #####################################
  5. # loadmodule /path/to/my_module.so
  6. # loadmodule /path/to/other_module.so
  7. ################################## NETWORK #####################################
  8. bind 127.0.0.1 & your ip
  9. port instance_port
  10. tcp-backlog 511
  11. timeout 0
  12. tcp-keepalive 300
  13. ################################# GENERAL #####################################
  14. daemonize yes
  15. supervised no
  16. pidfile ./redis_instance_port.pid
  17. loglevel notice
  18. logfile ./redis_log.log
  19. databases 16
  20. always-show-logo yes
  21. ################################ SNAPSHOTTING ################################
  22. save 900 1
  23. save 300 10
  24. save 60 10000
  25. stop-writes-on-bgsave-error yes
  26. rdbcompression yes
  27. rdbchecksum yes
  28. dbfilename dump.rdb
  29. dir ./
  30. ################################# REPLICATION #################################
  31. # masterauth <master-password>
  32. replica-serve-stale-data yes
  33. replica-read-only yes
  34. repl-diskless-sync no
  35. repl-diskless-sync-delay 5
  36. repl-disable-tcp-nodelay no
  37. replica-priority 100
  38. ################################## SECURITY ###################################
  39. requirepass your_passwrod
  40. ################################### CLIENTS ####################################
  41. # maxclients 10000
  42. ############################## MEMORY MANAGEMENT ################################
  43. # maxmemory <bytes>
  44. # maxmemory-policy noeviction
  45. # maxmemory-samples 5
  46. # replica-ignore-maxmemory yes
  47. ############################# LAZY FREEING ####################################
  48. lazyfree-lazy-eviction no
  49. lazyfree-lazy-expire no
  50. lazyfree-lazy-server-del no
  51. replica-lazy-flush no
  52. ############################## APPEND ONLY MODE ###############################
  53. appendonly no
  54. appendfilename "appendonly.aof"
  55. # appendfsync always
  56. appendfsync everysec
  57. # appendfsync no
  58. no-appendfsync-on-rewrite no
  59. auto-aof-rewrite-percentage 100
  60. auto-aof-rewrite-min-size 64mb
  61. aof-load-truncated yes
  62. aof-use-rdb-preamble yes
  63. ################################ LUA SCRIPTING ###############################
  64. lua-time-limit 5000
  65. ################################ REDIS CLUSTER ###############################
  66. cluster-enabled yes
  67. # cluster-replica-validity-factor 10
  68. # cluster-require-full-coverage yes
  69. # cluster-replica-no-failover no
  70. ########################## CLUSTER DOCKER/NAT support ########################
  71. ################################## SLOW LOG ###################################
  72. slowlog-log-slower-than 10000
  73. slowlog-max-len 128
  74. ################################ LATENCY MONITOR ##############################
  75. latency-monitor-threshold 0
  76. ############################# EVENT NOTIFICATION ##############################
  77. notify-keyspace-events ""
  78. ############################### ADVANCED CONFIG ###############################
  79. hash-max-ziplist-entries 512
  80. hash-max-ziplist-value 64
  81. list-max-ziplist-size -2
  82. list-compress-depth 0
  83. set-max-intset-entries 512
  84. zset-max-ziplist-entries 128
  85. zset-max-ziplist-value 64
  86. hll-sparse-max-bytes 3000
  87. stream-node-max-bytes 4096
  88. stream-node-max-entries 100
  89. activerehashing yes
  90. client-output-buffer-limit normal 0 0 0
  91. client-output-buffer-limit replica 256mb 64mb 60
  92. client-output-buffer-limit pubsub 32mb 8mb 60
  93. # client-query-buffer-limit 1gb
  94. # proto-max-bulk-len 512mb
  95. hz 10
  96. dynamic-hz yes
  97. aof-rewrite-incremental-fsync yes
  98. rdb-save-incremental-fsync yes
  99. ########################### ACTIVE DEFRAGMENTATION #######################
  100. # Enabled active defragmentation
  101. # activedefrag yes
  102. # Minimum amount of fragmentation waste to start active defrag
  103. # active-defrag-ignore-bytes 100mb
  104. # Minimum percentage of fragmentation to start active defrag
  105. # active-defrag-threshold-lower 10
  106. # Maximum percentage of fragmentation at which we use maximum effort
  107. # active-defrag-threshold-upper 100
  108. # Minimal effort for defrag in CPU percentage
  109. # active-defrag-cycle-min 5
  110. # Maximal effort for defrag in CPU percentage
  111. # active-defrag-cycle-max 75
  112. # Maximum number of set/hash/zset/list fields that will be processed from
  113. # the main dictionary scan
  114. # active-defrag-max-scan-fields 1000
View 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秒钟左右完成

  1. import redis
  2. #master
  3. node_1 = {'host': '127.0.0.1', 'port': 9001, 'password': '***'}
  4. node_2 = {'host': '127.0.0.1', 'port': 9002, 'password': '***'}
  5. node_3 = {'host': '127.0.0.1', 'port': 9003, 'password': '***'}
  6. #slave
  7. node_4 = {'host': '127.0.0.1', 'port': 9004, 'password': '***'}
  8. node_5 = {'host': '127.0.0.1', 'port': 9005, 'password': '***'}
  9. node_6 = {'host': '127.0.0.1', 'port': 9006, 'password': '***'}
  10. redis_conn_1 = redis.StrictRedis(host=node_1["host"], port=node_1["port"], password=node_1["password"])
  11. redis_conn_2 = redis.StrictRedis(host=node_2["host"], port=node_2["port"], password=node_2["password"])
  12. redis_conn_3 = redis.StrictRedis(host=node_3["host"], port=node_3["port"], password=node_3["password"])
  13. # cluster meet
  14. redis_conn_1.execute_command("cluster meet {0} {1}".format(node_2["host"],node_2["port"]))
  15. redis_conn_1.execute_command("cluster meet {0} {1}".format(node_3["host"],node_3["port"]))
  16. print('#################flush slots #################')
  17. redis_conn_1.execute_command('cluster flushslots')
  18. redis_conn_2.execute_command('cluster flushslots')
  19. redis_conn_3.execute_command('cluster flushslots')
  20. print('#################add slots#################')
  21. for i in range(0,16383+1):
  22. if i <= 5461:
  23. try:
  24. redis_conn_1.execute_command('cluster addslots {0}'.format(i))
  25. except:
  26. print('cluster addslots {0}'.format(i) +' error')
  27. elif 5461 < i and i <= 10922:
  28. try:
  29. redis_conn_2.execute_command('cluster addslots {0}'.format(i))
  30. except:
  31. print('cluster addslots {0}'.format(i) + ' error')
  32. elif 10922 < i:
  33. try:
  34. redis_conn_3.execute_command('cluster addslots {0}'.format(i))
  35. except:
  36. print('cluster addslots {0}'.format(i) + ' error')
  37. print()
  38. print('#################cluster status#################')
  39. print()
  40. print('##################'+str(node_1["host"])+':'+str(node_1["port"])+'##################')
  41. print(str(redis_conn_1.execute_command('cluster info'), encoding = "utf-8").split("\n")[0])
  42. print('##################'+str(node_2["host"])+':'+str(node_2["port"])+'##################')
  43. print(str(redis_conn_1.execute_command('cluster info'), encoding = "utf-8").split("\n")[0])
  44. print('##################'+str(node_3["host"])+':'+str(node_3["port"])+'##################')
  45. print(str(redis_conn_1.execute_command('cluster info'), encoding = "utf-8").split("\n")[0])
  46. #slave cluster meet
  47. redis_conn_1.execute_command("cluster meet {0} {1}".format(node_4["host"],node_4["port"]))
  48. redis_conn_2.execute_command("cluster meet {0} {1}".format(node_5["host"],node_5["port"]))
  49. redis_conn_3.execute_command("cluster meet {0} {1}".format(node_6["host"],node_6["port"]))
  50. #cluster nodes
  51. print(str(redis_conn_1.execute_command('cluster nodes'), encoding = "utf-8"))

示例

这样一个Redis的集群,从实例的安装到集群的安装,环境依赖本身没有问题的话,基本上1分钟之内可以完成这个搭建过程。

 

原文链接:http://www.cnblogs.com/wy123/p/11543882.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号