在一台机器上搭建redis集群
redis版本3.2;我这也是需要搭建然后网上找文档然最后总结的
1,解压源码包安装redis
#安装需要的一些包
yum install ruby rubygems openssl openssl-devel
#此步安装时候,是没有输出的,只要不报错慢慢等待或者更换国内源
#加 -V 参数查看过程
gem install redis --version 3.3.0
#更换国内淘宝源
gem sources --remove https://rubygems.org/
gem sources -a https://gems.ruby-china.com/
#解压安装
tar xf redis-3.2.9.tar.gz
cd redis-3.2.9
make
make install PREFIX=/usr/local/redis #把编译好的redis命令安装到这个目录下面
2,配置redis节点;redis3.x 最少需要6个节点[3主3从的模式]
#配置节点目录
mkdir -p /opt/redis/ && cd /opt/redis
mkdir 7000 7001 7002 7003 7004 7005
cp -rf /usr/local/redis/bin 7000
cp -rf /usr/local/redis/bin 7001
cp -rf /usr/local/redis/bin 7002
cp -rf /usr/local/redis/bin 7003
cp -rf /usr/local/redis/bin 7004
cp -rf /usr/local/redis/bin 7005
#节点配置文件(AOF+RDB持久化方式)
cd /opt/redis/7000
vim redis.conf
bind 192.168.16.186 127.0.0.1 #这里可以多个ip用空格分开
port 7000
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-require-full-coverage no
#aof
appendonly yes
appendfsync everysec
appendfilename "appendonly_7000.aof"
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
no-appendfsync-on-rewrite yes
#rdb
save 900 1
save 300 10
save 60 10000
dbfilename node.rdb
rdbcompression yes
rdbchecksum yes
stop-writes-on-bgsave-error yes
dir ./
pidfile /var/run/redis_7000.pid
logfile /var/log/redis/redis_7000.log
其他几个节点的配置文件和这个一样就行,需要注意需要改动对应端口和一些文件名称
#创建日志目录
mkdir /var/log/redis
#启动各节点
cd /opt/redis/7000 && redis-server ./redis.conf
cd /opt/redis/7001 && redis-server ./redis.conf
cd /opt/redis/7002 && redis-server ./redis.conf
cd /opt/redis/7003 && redis-server ./redis.conf
cd /opt/redis/7004 && redis-server ./redis.conf
cd /opt/redis/7005 && redis-server ./redis.conf
3,创建集群
#进去源码包目录下src目录下面,执行集群创建
cd redis-3.2.9/src/
./redis-trib.rb create --replicas 1 192.168.16.186:7000 192.168.16.186:7001 192.168.16.186:7002 192.168.16.186:7003 192.168.16.186:7004 192.168.16.186:7005
>>> Creating cluster
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 0 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 19 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 20 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 25 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 26 (expected array)
......此处省略......
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 104 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 105 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.16.186:7000
192.168.16.186:7001
192.168.16.186:7002
Adding replica 192.168.16.186:7003 to 192.168.16.186:7000
Adding replica 192.168.16.186:7004 to 192.168.16.186:7001
Adding replica 192.168.16.186:7005 to 192.168.16.186:7002
M: 346226a5653bfe33f6d887920e99609b9bbe94ec 192.168.16.186:7000
slots:0-5460 (5461 slots) master
M: 5dc8c8fae8addd7321b9b613123bfc4b25608bbc 192.168.16.186:7001
slots:5461-10922 (5462 slots) master
M: be357c14ac96528f86b5a6d5457086e307d05b7e 192.168.16.186:7002
slots:10923-16383 (5461 slots) master
S: 99106ae5121c1127b53374e0d678b543cd54026b 192.168.16.186:7003
replicates 346226a5653bfe33f6d887920e99609b9bbe94ec
S: 4b788f70b19969fb9f41a4b2b77265c02df3718e 192.168.16.186:7004
replicates 5dc8c8fae8addd7321b9b613123bfc4b25608bbc
S: 9e9df80f856b6dd0e54da6f1c01bc4ea9f131ff2 192.168.16.186:7005
replicates be357c14ac96528f86b5a6d5457086e307d05b7e
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join..
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 0 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 19 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 20 (expected array)
......此处省略......
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: wrong element type nil at 105 (expected array)
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: ignoring wrong elements is deprecated, remove them explicitly
/usr/local/share/gems/gems/redis-3.0.0/lib/redis.rb:182: warning: this causes ArgumentError in the next release
>>> Performing Cluster Check (using node 192.168.16.186:7000)
M: 346226a5653bfe33f6d887920e99609b9bbe94ec 192.168.16.186:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 4b788f70b19969fb9f41a4b2b77265c02df3718e 192.168.16.186:7004
slots: (0 slots) slave
replicates 5dc8c8fae8addd7321b9b613123bfc4b25608bbc
M: be357c14ac96528f86b5a6d5457086e307d05b7e 192.168.16.186:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 5dc8c8fae8addd7321b9b613123bfc4b25608bbc 192.168.16.186:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 9e9df80f856b6dd0e54da6f1c01bc4ea9f131ff2 192.168.16.186:7005
slots: (0 slots) slave
replicates be357c14ac96528f86b5a6d5457086e307d05b7e
S: 99106ae5121c1127b53374e0d678b543cd54026b 192.168.16.186:7003
slots: (0 slots) slave
replicates 346226a5653bfe33f6d887920e99609b9bbe94ec
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
安装截图
输入 yes
创建成功!
4,连接测试,配置文件里有写127.0.0.1地址,连接时候也可以不用加-h参数
[root@localhost ~]# redis-cli -c -p 7000 -h 192.168.16.186
192.168.16.186:7000> set testkey 123456
OK
192.168.16.186:7000> get testkey
"123456"
192.168.16.186:7000> exit
[root@localhost ~]# redis-cli -c -p 7005 -h 192.168.16.186
192.168.16.186:7005> get testkey
-> Redirected to slot [4757] located at 192.168.16.186:7000
"123456"
192.168.16.186:7000> exit
#查看节点信息
[root@localhost ~]# redis-cli -c -p 7000 -h 192.168.16.186
192.168.16.186:7000> CLUSTER NODES
4b788f70b19969fb9f41a4b2b77265c02df3718e 192.168.16.186:7004 slave 5dc8c8fae8addd7321b9b613123bfc4b25608bbc 0 1522393285325 5 connected
be357c14ac96528f86b5a6d5457086e307d05b7e 192.168.16.186:7002 master - 0 1522393286828 3 connected 10923-16383
5dc8c8fae8addd7321b9b613123bfc4b25608bbc 192.168.16.186:7001 master - 0 1522393284824 2 connected 5461-10922
9e9df80f856b6dd0e54da6f1c01bc4ea9f131ff2 192.168.16.186:7005 slave be357c14ac96528f86b5a6d5457086e307d05b7e 0 1522393286828 6 connected
346226a5653bfe33f6d887920e99609b9bbe94ec 192.168.16.186:7000 myself,master - 0 0 1 connected 0-5460
99106ae5121c1127b53374e0d678b543cd54026b 192.168.16.186:7003 slave 346226a5653bfe33f6d887920e99609b9bbe94ec 0 1522393286328 4 connected
5,redis 集群的启动、停止[使用脚本],如果配置文件中没有监听127.0.0.1地址,就要加上-h ip参数
# 启动脚本
[root@localhost redis]# cat start-redis.sh
#!/bin/bash
for i in {7000..7005}
do
cd /opt/redis/$i/
redis-server ./redis.conf && echo "$i 节点启动成功" || echo "$i 节点启动失败"
done
ps -ef|grep redis
#停止脚本
[root@localhost redis]# cat stop-redis.sh
#!/bin/bash
function redis_stop(){
for port in {7000..7005}
do
redis-cli -c -p $port $1 && echo "$port 节点已$2" || echo "$port 节点未$2"
done
}
echo "##### 保存节点数据"
redis_stop save "保存"
echo "##### 停止节点"
redis_stop shutdown "停止"
操作截图
2台机器搭建集群;每台3个节点;配置大同小异这里就不在贴出过程了
1,同样道理安装不变,配置节点每台为3个就行,2台机器6个节点即可;端口一样不一样都可以
2,配置文件bind的IP地址修改,两台机器要互通
3,创建集群时在2台机器任意一台即可,注意节点地址和端口的变化
本文最后记录时间 2024-03-31
文章链接地址:https://wojc.cn/archives/217.html
本站文章除注明[转载|引用|来源],均为本站原创内容,转载前请注明出处
文章链接地址:https://wojc.cn/archives/217.html
本站文章除注明[转载|引用|来源],均为本站原创内容,转载前请注明出处