0

0

Redis自动化安装以及集群实现

蓮花仙者

蓮花仙者

发布时间:2025-07-09 11:52:24

|

765人浏览过

|

来源于php中文网

原创

redis实例安装与自动化集群实现

安装说明:自动解压安装包,并在指定路径编译安装。将配置文件模板复制到Redis实例的目录下,并根据端口号修改配置文件。

所需文件:配置文件、当前shell脚本、安装包。

参数说明:

  • 参数1(basedir):Redis安装包路径
  • 参数2(installdir):安装实例路径
  • 参数3(installfilename):安装包名称
  • 参数4(port):安装实例的端口号
#!/bin/bash
set -e
if [ $# -lt 4 ]; then
  echo "$(basename $0): Missing script argument"
  echo "$(basename $0) [installfilename] [port]"
  exit 9
fi
PortInUse=`netstat -anp | awk '{print $4}' | grep $4 | wc -l`
if [ $PortInUse -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 1
scp $basedir/redis.conf $installdir
sed -i "s/instance_port/$port/g" $installdir/redis.conf
sleep 1
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 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_password

################################### CLIENTS ##################################### maxclients 10000

############################## MEMORY MANAGEMENT ################################# maxmemory

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

安装示例:

企业网站系统的升级版本eetoday2.0.3 GBK
企业网站系统的升级版本eetoday2.0.3 GBK

企业网站系统的升级版本eetoday 现能智能化安装且带有小型论坛的企业网站系统,其具有的功能包括:在线聊天,成功案例,公司新闻, 公司产品(公司产品目录可无限级扩展), 公司活动, 行业文章, 企业招聘, 资料下载, 公司论坛,MVC的购物车功能,其用了smarty模板,而且集成了淘宝的即时结算功能,产品搜索功能,及后台管理及添加次级目录功能。且全部可以在后台进行管理。 现又增加了即时聊天,无限

下载
sh redis_install.sh /usr/local/redis/ /usr/local/redis5/redis9008/ redis-5.0.4 9008

Redis自动化安装以及集群实现

Redis实例的目录结构:

Redis自动化安装以及集群实现

基于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: redis_conn_1.execute_command('cluster addslots {0}'.format(i)) elif i < 10922: redis_conn_2.execute_command('cluster addslots {0}'.format(i)) else: redis_conn_3.execute_command('cluster addslots {0}'.format(i))

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自动化安装以及集群实现

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

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

760

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

639

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

762

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

619

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1285

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

549

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

579

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

709

2023.08.11

PHP WebSocket 实时通信开发
PHP WebSocket 实时通信开发

本专题系统讲解 PHP 在实时通信与长连接场景中的应用实践,涵盖 WebSocket 协议原理、服务端连接管理、消息推送机制、心跳检测、断线重连以及与前端的实时交互实现。通过聊天系统、实时通知等案例,帮助开发者掌握 使用 PHP 构建实时通信与推送服务的完整开发流程,适用于即时消息与高互动性应用场景。

3

2026.01.19

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 4.7万人学习

Django 教程
Django 教程

共28课时 | 3.2万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号