CentOS中Redis-Cluster集群配置

我是三臺(tái)機(jī)器要做cluster集群滑频,所以每臺(tái)機(jī)器啟2個(gè)端口率挣。
服務(wù)器1:192.168.31.146
服務(wù)器2:192.168.31.147
服務(wù)器3:192.168.31.148
三臺(tái)所用端口均為 7001菠发、7002

官方推薦RedisCluster最少6個(gè)節(jié)點(diǎn),三主三從祈秕。并且物理節(jié)點(diǎn)數(shù)要為奇數(shù)像樊,可根據(jù)(n-1)/2算出來你最多可掛幾個(gè)物理節(jié)點(diǎn)保證不影響正常運(yùn)行

  1. 安裝集群環(huán)境
    1.1 安裝gcc,g++
    yum install gcc g++
    1.2 安裝ruby腳本運(yùn)行環(huán)境
    yum install ruby
    1.3 修改ruby下載源,不然國外的源下載不了
    gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
    1.4 安裝redis集群工具
    gem install redis -v 3.3.5

  2. 安裝redis
    2.1 cd /下載redis予颤、解壓
    wget http://download.redis.io/releases/redis-3.2.4.tar.gz
    tar -zxvf redis-3.2.4.tar.gz
    2.3 編譯安裝到指定目錄
    cd redis-3.2.4
    make install PREFIX=/usr/local/redis
    2.4 從redis的源碼目錄中復(fù)制redis.conf到redis的安裝目錄
    cp redis.conf /usr/local/redis/bin

  3. 集群的搭建
    現(xiàn)在才是正真的集群搭建囤官。每臺(tái)服務(wù)器創(chuàng)建2個(gè)實(shí)例,端口7001~7002
    3.1 跳到指定目錄蛤虐、創(chuàng)建目錄redis-cluster
    mkdir /usr/local/redis-cluster
    3.2 進(jìn)入redis/bin目錄党饮,把持久化文件刪掉
    cd /usr/local/redis/bin
    rm -f dump.rdb
    3.3 創(chuàng)建多個(gè)redis實(shí)例
    cp -r /usr/local/redis/bin /usr/local/redis-cluster/redis01
    cp -r /usr/local/redis/bin /usr/local/redis-cluster/redis02
    3.4 去編輯各個(gè)實(shí)例下面的配置文件
    vi /usr/local/redis-cluster/redis01/redis.conf

    修改1: port端口號(hào)(不要重復(fù)枣宫,依次修改)
    修改2: bind 的IP 127.0.0.1 改為 0.0.0.0
    修改3: 打開cluster-enable前面的注釋
    修改4: daemonize 改為yes
    修改5: maxmemory 5gb (Redis最大使用的內(nèi)存大猩摹)
    修改6: maxmemory-policy allkeys-lru (內(nèi)存滿了使用LRU算法清除key)
    

    同理,去redis02 修改這些配置

    ??注意:如果想給集群加密碼缔杉,那么不要在這時(shí)候加饲常,因?yàn)榧用芎髣?chuàng)建集群關(guān)聯(lián)就會(huì)失敗報(bào)錯(cuò)蹲堂,等集群啟動(dòng)成功后,kill掉所有redis服務(wù)再修改并啟動(dòng)各個(gè)服務(wù)贝淤。

    修改:masterauth xxxx
    修改:requirepass xxxx 
    

    3.5 把創(chuàng)建集群的ruby腳本復(fù)制到redis-cluster的目錄下
    去到redis的安裝文件
    cd /usr/local/redis-3.2.4/src
    復(fù)制腳本到redis-cluster目錄下
    cp *.rb /usr/local/redis-cluster/
    3.8 再回去redis-cluster目錄
    cd /usr/local/redis-cluster

  4. 開始創(chuàng)建集群
    這個(gè)時(shí)候開始創(chuàng)建集群柒竞,但是需要把每一個(gè)實(shí)例都啟動(dòng)起來好麻煩,所有在這時(shí)候創(chuàng)建一個(gè)腳本
    4.1 創(chuàng)建redis集群啟動(dòng)的腳本
    vim startall.sh
    內(nèi)容如下:

    #!/bin/sh
    # chkconfig: 2345 90 10
    # description: Redis is a persistent key-value database
    
    cd /usr/local/redis-cluster/redis01
    /usr/local/redis-cluster/redis01/redis-server  redis.conf
    cd  ..
    cd /usr/local/redis-cluster/redis02
    /usr/local/redis-cluster/redis02/redis-server  redis.conf
    cd  ..
    

    編輯腳本權(quán)限
    chmod +x startall.sh
    利用腳本啟動(dòng)redis集群
    ./startall.sh
    查看他們的運(yùn)行狀態(tài)
    ps aux|grep redis

  5. 創(chuàng)建集群
    5.1 我們還缺少一個(gè)東西沒有安裝(如果已安裝請忽略)
    gem install redis -v 3.3.5

    這里注意播聪,我指定版本是因?yàn)槲业腞uby是2.0.0版本的朽基,最新版本redis 4.0.0 是要求Ruby最低2.2.0版本以上的

    5.2 創(chuàng)建集群

    ./redis-trib.rb create --replicas 1 192.168.31.146:7001 192.168.31.146:7002 192.168.31.147:7001 192.168.31.147:7002  192.168.31.148:7001 192.168.31.148:7002 
    


這里需要注意,以上配置你需要在兩臺(tái)以上的機(jī)器內(nèi)都要配置离陶,并且保證各個(gè)機(jī)器上的redis服務(wù)和端口是啟動(dòng)狀態(tài)稼虎,這樣才可以成功創(chuàng)建集群。



#查看集群狀態(tài)
/usr/local/redis/bin/redis-cli -c -p 7001 cluster nodes
成功視圖
  1. 開機(jī)自啟動(dòng)redis-cluster
# 拷貝腳本到啟動(dòng)目錄
cp /usr/local/redis-cluster/startall.sh /etc/init.d/redisd
# 添加啟動(dòng)配置
chkconfig redisd on

如果要擴(kuò)展新的機(jī)器節(jié)點(diǎn)參考
提醒一點(diǎn)招刨,新加入集群的master節(jié)點(diǎn)需要重新slots霎俩,把節(jié)點(diǎn)插到分配的hash槽內(nèi)才能使用參考

如果碰巧所有redis集群內(nèi)的服務(wù)器都同時(shí)重啟了,那么需要重新創(chuàng)建集群沉眶。
那么你需要把所有redis服務(wù)停掉
ps -ef | grep redis | grep -v grep | awk '{print $2}' | xargs kill -9
并刪除各個(gè)redis-cluster目錄下各個(gè)端口文件夾里的dump.rdb 和 nodes.conf文件打却,如:

示例

下面是我以前的配置備份,作為參考

[root@localhost redis-stable]# yum install net-tools -y
[root@localhost script]# yum install wget -y
[root@localhost script]# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@localhost script]# yum install epel-release-latest-7.noarch.rpm
[root@localhost script]# yum repolist
[root@localhost script]# yum install -y gcc tcl
[root@localhost script]# mkdir /soft
[root@localhost script]# cd /soft
[root@localhost script]# wget http://download.redis.io/releases/redis-stable.tar.gz
[root@localhost script]# tar zxf redis-stable.tar.gz -C /usr/local/src/
[root@localhost script]# cd /usr/local/src/redis-stable/
[root@localhost script]# mkdir /usr/local/redis3
[root@localhost script]# make PREFIX=/usr/local/redis3 install
[root@localhost script]# mkdir -p /usr/local/redis3/cluster
[root@localhost script]# cp /usr/local/src/redis-stable/redis.conf /usr/local/redis3/cluster/redis-cluster.conf
[root@localhost script]# mkdir -p /data/redis/{data,log,var}
[root@localhost script]# vim /usr/local/redis3/cluster/redis-cluster.conf
daemonize yes
pidfile /data/redis/var/redis.pid
bind 0.0.0.0
port 7000
unixsocket /data/redis/var/redis.sock
timeout 0
tcp-keepalive 0
databases 16
cluster-enabled yes
cluster-config-file /usr/local/redis3/cluster/nodes.conf
cluster-node-timeout 15000
cluster-migration-barrier 1
cluster-require-full-coverage yes
loglevel warning
logfile /data/redis/log/redis.log
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/data
maxmemory 20000mb
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes

[root@localhost redis-stable]# yum install -y ruby rubygems  
[root@localhost redis-stable]# gem sources --remove https://rubygems.org/
[root@localhost redis-stable]# gem sources -a https://ruby.taobao.org/
[root@localhost redis-stable]# gem sources -l
[root@localhost redis-stable]# gem install redis
[root@localhost redis-stable]# cp /usr/local/src/redis-stable/src/redis-trib.rb /usr/local/redis3/bin/redis-trib
[root@localhost redis-stable]# cat >> /etc/hosts << EOF
172.17.25.120 redisCluster1.dev.inc.ubaby.cn redisCluster1
172.17.25.130 redisCluster2.dev.inc.ubaby.cn redisCluster2
172.17.25.121 redisCluster3.dev.inc.ubaby.cn redisCluster3
172.17.25.131 redisCluster4.dev.inc.ubaby.cn redisCluster4
172.17.25.122 redisCluster5.dev.inc.ubaby.cn redisCluster5
172.17.25.132 redisCluster6.dev.inc.ubaby.cn redisCluster6
EOF
[root@localhost redis-stable]# /usr/local/redis3/bin/redis-server /usr/local/redis3/cluster/redis-cluster.conf
[root@localhost redis-stable]# /usr/local/redis3/bin/redis-trib create --replicas 1 172.17.25.120:7000 172.17.25.121:7000 172.17.25.122:7000 172.17.25.130:7000 172.17.25.131:7000 172.17.25.132:7000
[root@localhost redis-stable]# /usr/local/redis3/bin/redis-cli -p 7000 cluster nodes

重新配置沦寂,清空內(nèi)容(如下)

rm -rf nodes_7000.conf *
rm -rf /data/redis/data_7000/*
rm -rf /data/redis/data_7001/*
rm -rf /data/redis/data_7002/*
rm -rf /data/redis/data_7003/*
rm -rf /data/redis/data_7004/*
rm -rf /data/redis/data_7005/*
ps -ef | grep redis | awk '{print $2}' | xargs kill
ps -ef | grep redis
ls
../bin/redis-server redis_7000.conf
ps -ef | grep redis
ls
ll -Z
chmod 777 ./*
../bin/redis-server redis_7000.conf
ps -ef | grep redis
ls /data/redis/data_7000/
vi redis_7000.conf
vi redis_7001.conf
vi redis_7002.conf
vi redis_7003.conf
vi redis_7004.conf
vi redis_7005.conf
../bin/redis-server redis_7000.conf
../bin/redis-server redis_7001.conf
../bin/redis-server redis_7002.conf
../bin/redis-server redis_7003.conf
../bin/redis-server redis_7004.conf
../bin/redis-server redis_7005.conf
 /usr/local/redis3/bin/redis-trib create --replicas 1 192.168.1.60:7000 192.168.1.60:7001 192.168.1.60:7002 192.168.1.60:7003 192.168.1.60:7004 192.168.1.60:7005 

systemctl stop firewalld.service #關(guān)閉防火墻
/usr/local/redis3/bin/redis-cli -c -p 7000   
127.0.0.1:7000> config set protected-mode "no"  #關(guān)閉保護(hù)模式

一. 問題如下

在192.168.56.57客戶端登錄192.168.56.56的redis服務(wù)器時(shí)学密,報(bào)錯(cuò)如下:  
[root@localhost src]# ./redis-cli -h  192.168.56.56  -p  6379 -a  "aabbcc"  
192.168.56.56:6379> ping  
Error: Connection reset by peer  

再telnet一下192.168.56.56的redis服務(wù)器的6379端口,提示redis服務(wù)有保護(hù)模式,需要解除  
[root@localhost src]# telnet 192.168.56.56  6379  
Trying 192.168.56.56...  
Connected to 192.168.56.56.  
Escape character is '^]'.  

-DENIED Redis is running in protected modebecause protected mode is enabled, no bind address was specified, noauthentication password is requested to clients.   
In this mode connections areonly accepted from the loopback interface. If you want to connect from externalcomputers to Redis you may adopt one of the following   
solutions: 1) Justdisable protected mode sending the command 'CONFIG SET protected-mode no' fromthe loopback interface by connecting to Redis from the same host   
the server isrunning, however MAKE SURE Redis is not publicly accessible from internet ifyou do so. Use CONFIG REWRITE to make this change permanent.   
2) Alternativelyyou can just disable the protected mode by editing the Redis configurationfile, and setting the protected mode option to 'no', and then restarting theserver.   
3) If you started the server manually just for testing, restart it withthe '--protected-mode no' option.   
4) Setup a bind address or an authenticationpassword. NOTE: You only need to do one of the above things in order for theserver to start accepting connections from the outside.  
Connection closed by foreign host.  

二. 解決方案

1、修改redis服務(wù)器的配置文件  
vi redis.conf    
  
注釋以下綁定的主機(jī)地址  
# bind 127.0.0.1  
   
2传藏、修改redis服務(wù)器的參數(shù)配置  
  
修改redis的守護(hù)進(jìn)程為no 腻暮,不啟用  
127.0.0.1:6379> config set daemonize "no"  
OK  
  
修改redis的保護(hù)模式為no彤守,不啟用  
127.0.0.1:6379> config set protected-mode "no"  
OK  

三. 問題解決

再次telnet一下192.168.56.56的redis服務(wù)器的6379端口,無問題  
[root@localhost Packages]# telnet 192.168.56.56  6379  
Trying 192.168.56.56...  
Connected to 192.168.56.56.  
Escape character is '^]'.  
  
  
再次在192.168.56.57客戶端登錄192.168.56.56的redis服務(wù)器哭靖,無問題  
[root@localhost src]# ./redis-cli -h  192.168.56.56 -p 6379 -a "aabbcc"  
192.168.56.56:6379> ping  
PONG  



修改配置具垫、添加密碼

# 修改配置  

sed -i 's/port 6379/port 7001/' /usr/local/redis-cluster/redis01/redis.conf

sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' /usr/local/redis-cluster/redis01/redis.conf

sed -i 's/# cluster-enabled yes/cluster-enabled yes/' /usr/local/redis-cluster/redis01/redis.conf

sed -i 's/daemonize no/daemonize yes/' /usr/local/redis-cluster/redis01/redis.conf

sed -i 's/# maxmemory <bytes>/maxmemory 5gb/' /usr/local/redis-cluster/redis01/redis.conf

sed -i 's/# maxmemory-policy noeviction/maxmemory-policy allkeys-lru/' /usr/local/redis-cluster/redis01/redis.conf
# 復(fù)制到其他節(jié)點(diǎn)   
\cp -v redis01/redis.conf redis02/
......
# 修改其他節(jié)點(diǎn)端口  
sed -i 's/port 7001/port 7002/' /usr/local/redis-cluster/redis02/redis.conf
......
# 殺掉所有進(jìn)程    
ps -ef | grep redis | grep -v grep | awk '{print $2}' | xargs kill -9
# 添加密碼  
sed -i 's/# masterauth <master-password>/masterauth nasoft2018/' /usr/local/redis-cluster/redis01/redis.conf

sed -i 's/# requirepass foobared/requirepass nasoft2018/' /usr/local/redis-cluster/redis01/redis.conf
    

錯(cuò)誤案例

MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.

大意為:(錯(cuò)誤)misconf redis被配置以保存數(shù)據(jù)庫快照,但misconf redis目前不能在硬盤上持久化试幽。用來修改數(shù)據(jù)集合的命令不能用筝蚕,請使用日志的錯(cuò)誤詳細(xì)信息。

這是由于強(qiáng)制停止redis快照铺坞,不能持久化引起的起宽,運(yùn)行info命令查看redis快照的狀態(tài)

解決方案如下:
連接redis后運(yùn)行 config set stop-writes-on-bgsave-error no 命令
關(guān)閉配置項(xiàng)stop-writes-on-bgsave-error解決該問題。

這只是臨時(shí)解決讓Redis忽略了這個(gè)錯(cuò)誤济榨,讓程序繼續(xù)往下運(yùn)行坯沪,但實(shí)際上數(shù)據(jù)還是會(huì)存儲(chǔ)到硬盤失敗擒滑!

修改/etc/sysctl.conf
vm.overcommit_memory=1

網(wǎng)上查了一下腐晾,有人也遇到類似的問題,并且給出了很好的分析(詳見:http://www.linuxidc.com/Linux/2012-07/66079.htm)丐一,簡單地說:Redis在保存數(shù)據(jù)到硬盤時(shí)為了避免主進(jìn)程假死藻糖,需要Fork一份主進(jìn)程,然后在Fork進(jìn)程內(nèi)完成數(shù)據(jù)保存到硬盤的操作库车,如果主進(jìn)程使用了4GB的內(nèi)存巨柒,F(xiàn)ork子進(jìn)程的時(shí)候需要額外的4GB,此時(shí)內(nèi)存就不夠了凝颇,F(xiàn)ork失敗潘拱,進(jìn)而數(shù)據(jù)保存硬盤也失敗了。

redis錯(cuò)誤:LOADING Redis is loading the dataset in memory
原因是拧略,redis使用的內(nèi)存超過操作系統(tǒng)一半的內(nèi)存

查看文件占的內(nèi)存
du -sh ./*|grep G
df -h 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市瘪弓,隨后出現(xiàn)的幾起案子垫蛆,更是在濱河造成了極大的恐慌,老刑警劉巖腺怯,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件袱饭,死亡現(xiàn)場離奇詭異,居然都是意外死亡呛占,警方通過查閱死者的電腦和手機(jī)虑乖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晾虑,“玉大人疹味,你說我怎么就攤上這事仅叫。” “怎么了糙捺?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵诫咱,是天一觀的道長。 經(jīng)常有香客問我洪灯,道長坎缭,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任签钩,我火速辦了婚禮掏呼,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘铅檩。我一直安慰自己憎夷,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布柠并。 她就那樣靜靜地躺著岭接,像睡著了一般。 火紅的嫁衣襯著肌膚如雪臼予。 梳的紋絲不亂的頭發(fā)上鸣戴,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天,我揣著相機(jī)與錄音粘拾,去河邊找鬼窄锅。 笑死,一個(gè)胖子當(dāng)著我的面吹牛缰雇,可吹牛的內(nèi)容都是我干的入偷。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼械哟,長吁一口氣:“原來是場噩夢啊……” “哼疏之!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起暇咆,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤锋爪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后爸业,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體其骄,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年扯旷,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拯爽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡钧忽,死狀恐怖毯炮,靈堂內(nèi)的尸體忽然破棺而出逼肯,到底是詐尸還是另有隱情,我是刑警寧澤否副,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布汉矿,位于F島的核電站,受9級特大地震影響备禀,放射性物質(zhì)發(fā)生泄漏洲拇。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一曲尸、第九天 我趴在偏房一處隱蔽的房頂上張望赋续。 院中可真熱鬧,春花似錦另患、人聲如沸纽乱。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鸦列。三九已至,卻和暖如春鹏倘,著一層夾襖步出監(jiān)牢的瞬間薯嗤,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工纤泵, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留骆姐,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓捏题,卻偏偏與公主長得像玻褪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子公荧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容