侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Redis自动化安装及集群实现搭建过程

Redis  /  管理员 发布于 7年前   183

Redis实例安装

安装说明:自动解压缩安装包,按照指定路径编译安装,复制配置文件模板到Redis实例路的数据径下,根据端口号修改

配置文件模板

配置文件,当前shell脚本,安装包

参数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 1000

安装示例

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"))

示例

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

总结

以上所述是小编给大家介绍的Redis自动化安装及集群实现搭建过程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!


  • 上一条:
    redis数据库查找key在内存中的位置的方法
    下一条:
    Redis集群增加节点与删除节点的方法详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Redis中能实现的功能、常见应用介绍(0个评论)
    • 2024年Redis面试题之一(0个评论)
    • 在redis缓存常见出错及解决方案(0个评论)
    • 在redis中三种特殊数据类型:地理位置、基数(cardinality)估计、位图(Bitmap)使用场景介绍浅析(2个评论)
    • Redis 删除 key用 del 和 unlink 有啥区别?(1个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-12
    • 2020-03
    • 2020-05
    • 2021-04
    • 2022-03
    • 2022-05
    • 2022-08
    • 2023-02
    • 2023-04
    • 2023-07
    • 2024-01
    • 2024-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客