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

 更新时间:2021年1月15日 13:19  点击:1942

Redis实例安装

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

配置文件模板

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

参数1:basedir,redis安装包路径

参数2:安装实例路径

参数3:安装包名称

参数4:安装实例的端口号

#!/bin/bash
set -e
if [ $# -lt 4 ]; then
    echo "$(basename $0): Missing script argument"
    echo "$(installdir $0) [installfilename] [port] "
    exit 9
fi
PotInUse=`netstat -anp | awk '{print $4}' | grep $4 | wc -l`
if [ $PotInUse -gt 0 ];then
 echo "ERROR" $4 "Port is used by another process!"
 exit 9
fi
basedir=$1
installdir=$2
installfilename=$3
port=$4
cd $basedir
tar -zxvf $installfilename.tar.gz >/dev/null 2>&1 &
cd $installfilename
mkdir -p $installdir
make PREFIX=$installdir install
sleep 1s 
cp $basedir/redis.conf $installdir

sed -i "s/instance_port/$port/g" $installdir/redis.conf
sleep 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 ip
port instance_port
tcp-backlog 511
timeout 0
tcp-keepalive 300
################################# GENERAL #####################################
daemonize yes
supervised no
pidfile ./redis_instance_port.pid
loglevel notice
logfile ./redis_log.log
databases 16
always-show-logo yes
################################ SNAPSHOTTING ################################
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
################################# REPLICATION #################################
# masterauth <master-password>
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-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 no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
############################## APPEND ONLY MODE ###############################
appendonly no
appendfilename "appendonly.aof"
# appendfsync always
appendfsync everysec
# appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-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 10000
slowlog-max-len 128
################################ LATENCY MONITOR ##############################
latency-monitor-threshold 0
############################# EVENT NOTIFICATION ##############################
notify-keyspace-events ""
############################### ADVANCED CONFIG ###############################
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
# client-query-buffer-limit 1gb
# proto-max-bulk-len 512mb
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-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

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

示例

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

总结

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

[!--infotagslink--]

相关文章

  • PHP7快速编译安装的步骤

    编译安装非常的简单了我们现在的php版本已经到了php7了,下文小编来为各位介绍一篇关于PHP7快速编译安装的步骤,希望文章能够帮助到各位。 一、安装必要一些依赖 yum...2016-11-25
  • 详解如何清理redis集群的所有数据

    这篇文章主要介绍了详解如何清理redis集群的所有数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-18
  • Redis连接池配置及初始化实现

    这篇文章主要介绍了Redis连接池配置及初始化实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
  • Rstudio中安装package出现的问题及解决

    这篇文章主要介绍了Rstudio中安装package出现的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-06
  • 详解redis desktop manager安装及连接方式

    这篇文章主要介绍了redis desktop manager安装及连接方式,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-15
  • PHP编译安装后PHP-FPM使用笔记

    PHP-FPM我们相信各位用高版本的php经常使用到了,下面整理了一些关于PHP-FPM的笔记,有兴趣的可进来看看。 今天赶上了123System OPenVZ VPS全场半价的机会,购入了一...2016-11-25
  • 浅谈redis key值内存消耗以及性能影响

    这篇文章主要介绍了浅谈redis key值内存消耗以及性能影响,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-07
  • lua读取redis数据的null判断示例代码

    最近在工作中遇到了一个问题,通过查找相关资料才得知原因是因为返回结果的问题,下面这篇文章主要给大家介绍了关于lua读取redis数据的null判断的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下...2020-06-30
  • 安装和使用percona-toolkit来辅助操作MySQL的基本教程

    一、percona-toolkit简介 percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括: 检查master和slave数据的一致性 有效地对记录进行归档 查找重复的索...2015-11-24
  • Linux安装Pytorch1.8GPU(CUDA11.1)的实现

    这篇文章主要介绍了Linux安装Pytorch1.8GPU(CUDA11.1)的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-25
  • SpringBoot集成Redis实现消息队列的方法

    这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
  • vscode安装git及项目开发过程

    这篇文章主要介绍了vscode安装git及项目开发过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-05-19
  • redis setIfAbsent和setnx的区别与使用说明

    这篇文章主要介绍了redis setIfAbsent和setnx的区别与使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-04
  • Redis的Expire与Setex区别说明

    这篇文章主要介绍了Redis的Expire与Setex区别说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-15
  • Visual Studio 2015下载和安装图文教程

    这篇文章主要为大家详细介绍了Visual Studio 2015下载和安装图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • Node调试工具JSHint的安装及配置教程

    现在我们介绍一种在Node下检查简单错误的JS代码验证工具JSHint。  JSHint的具体介绍参考http://www.jshint.com/about/,说直白点儿,JSHint就是一个检查JS代码规范与否的工具,它可以用来检查任何(包括server端和client端...2014-05-31
  • Centos中彻底删除Mysql(rpm、yum安装的情况)

    我用的centos6,mysql让我整出了各种问题,我想重装一个全新的mysql,yum remove mysql-server mysql之后再install并不能得到一个干净的mysql,原来的/etc/my.cnf依然没变,datadir里面的数据已没有任何变化,手动删除/etc/my.cn...2015-03-15
  • Ubuntu20.04安装cuda10.1的步骤(图文教程)

    这篇文章主要介绍了Ubuntu20.04安装cuda10.1的步骤(图文教程),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-07-30
  • 在PyCharm中安装PaddlePaddle的方法

    这篇文章主要介绍了在PyCharm中安装PaddlePaddle的方法,本文给大家介绍的非常想详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-05
  • Postman安装与使用详细教程 附postman离线安装包

    这篇文章主要介绍了Postman安装与使用详细教程 附postman离线安装包,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-05