我是三臺(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)行
安裝集群環(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
安裝redis
2.1cd /
下載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
-
集群的搭建
現(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
-
開始創(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
-
創(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
- 開機(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