1. 程式人生 > >Redis 叢集搭建簡述

Redis 叢集搭建簡述

環境

作業系統:CentOS 7.3
redis版本:Redis 3.2.8
兩臺主機,每臺主機上3個節點

基本步驟

下載解壓安裝包

# 下載解壓安裝包
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar -zxvf redis-3.2.8.tar.gz

編譯安裝

# 正常編譯安裝
make && make install

# 若未安裝gcc,則安裝gcc
yum install gcc

# 若未安裝jemalloc記憶體分配器,則使用如下命令編譯安裝
make MALLOC=libc  && make install

建立相應目錄,編寫配置檔案

本次搭建使用如下目錄:

  • 日誌目錄:/opt/redis/logs
  • 資料目錄:/opt/redis/db
  • 叢集檔案目錄:/opt/redis/cluster-conf/埠 ,如:/opt/redis/cluster-conf/7001

配置檔案:簡單配置檔案,注意修改各節點的IP和埠。

啟動各節點

# 啟動各節點
cd /opt/redis/src
./redis-server ../cluster-conf/7001/redis.conf
./redis-server ../cluster-conf/7002/redis.conf
./redis-server ../cluster-conf/7003
/redis.conf ./redis-server ../cluster-conf/7004/redis.conf ./redis-server ../cluster-conf/7005/redis.conf ./redis-server ../cluster-conf/7006/redis.conf

建立叢集

# 安裝ruby
yum -y install ruby ruby-devel rubygems rpm-build

# 安裝redis包 (指定相應的版本號,https://rubygems.org/gems/redis)
gem install redis -v 3.3.0

# 建立叢集
./redis-trib.rb create --replicas 1
192.168.192.128:7001 192.168.192.128:7002 192.168.192.128:7003 192.168.192.129:7004 192.168.192.129:7005 192.168.192.129:7006
  • 使用RubyGems安裝Redis包時,需指明版本號,否則會安裝最新版的,因為該版本的CentOS的Ruby預設版本是2.0,最新的Redis包需要升級Ruby的版本才能安裝。

簡單配置檔案

################################## INCLUDES ###################################


################################## NETWORK #####################################

bind 192.168.192.128
protected-mode yes
port 7001
tcp-backlog 511
timeout 0
tcp-keepalive 300

################################# GENERAL #####################################

daemonize yes
supervised no
pidfile /var/run/redis_7001.pid

loglevel notice
logfile "/opt/redis/logs/redis_7001.log"
databases 16

################################ SNAPSHOTTING  ################################

save 900 1
save 300 10
save 60 10000

stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /opt/redis/db

################################# REPLICATION #################################

slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100

################################## SECURITY ###################################


################################### LIMITS ####################################


############################## APPEND ONLY MODE ###############################

appendonly yes
appendfilename "appendonly_7001.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes

################################ LUA SCRIPTING  ###############################

lua-time-limit 5000

################################ REDIS CLUSTER  ###############################

cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 15000

################################## 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
activerehashing yes

client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

hz 10
aof-rewrite-incremental-fsync yes

參考

1.Redis 3.2.1叢集搭建
2.REDIS cluster-tutorial