靳闯博客 记录是一种习惯,分享是一种态度
  • 波浪
  • 波浪
  • 波浪
  • 波浪
Redis集群搭建-centos7
发表于: | 分类: 技术积累 | 标签: Centos7 Redis | 评论:0 | 阅读: 728

在一台机器上搭建redis集群
redis版本3.2;我这也是需要搭建然后网上找文档然最后总结的

参考redis集群搭建(同机器、不同机器搭建)

1,解压源码包安装redis

#安装需要的一些包
yum install ruby rubygems openssl openssl-devel

#此步安装时候,是没有输出的,只要不报错慢慢等待或者更换国内源
#加 -V 参数查看过程
gem install redis --version 3.0.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

#节点配置文件
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
appendonly yes
pidfile /var/run/redis_7000.pid
logfile /var/log/redis/redis_7000.log
dbfilename dump_7000.rdb
appendfilename "appendonly_7000.aof"
其他几个节点的配置文件和这个一样就行,需要注意需要改动对应端口和一些文件名称

#创建日志目录
mkdir /var/log/redis

#启动各节点
cd /opt/redis/7000
./bin/redis-server ./redis.conf
cd /opt/redis/7001
./bin/redis-server ./redis.conf
cd /opt/redis/7002
./bin/redis-server ./redis.conf
cd /opt/redis/7003
./bin/redis-server ./redis.conf
cd /opt/redis/7004
./bin/redis-server ./redis.conf
cd /opt/redis/7005
./bin/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

redis-jq.png
redis-jq1.png
创建成功!

4,连接测试

[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 集群的启动、停止[使用脚本]

启动
[root@localhost redis]# cat start-redis.sh 
#!/bin/bash
#7000
cd /opt/redis/7000
./bin/redis-server ./redis.conf

#7001
cd /opt/redis/7001
./bin/redis-server ./redis.conf

#7002
cd /opt/redis/7002
./bin/redis-server ./redis.conf

#7003
cd /opt/redis/7003
./bin/redis-server ./redis.conf

#7004
cd /opt/redis/7004
./bin/redis-server ./redis.conf

#7005
cd /opt/redis/7005
./bin/redis-server ./redis.conf

#查看redis进程
ps -ef |grep redis

#停止
[root@localhost redis]# cat stop-redis.sh 
#!/bin/bash
/opt/redis/7000/bin/redis-cli -p 7000 -h 192.168.16.186 shutdown
/opt/redis/7001/bin/redis-cli -p 7001 -h 192.168.16.186 shutdown
/opt/redis/7002/bin/redis-cli -p 7002 -h 192.168.16.186 shutdown
/opt/redis/7003/bin/redis-cli -p 7003 -h 192.168.16.186 shutdown
/opt/redis/7004/bin/redis-cli -p 7004 -h 192.168.16.186 shutdown
/opt/redis/7005/bin/redis-cli -p 7005 -h 192.168.16.186 shutdown
ps -ef |grep redis
操作截图

redic-cli.png
redis-node.png


2台机器搭建集群;每台3个节点;配置大同小异这里就不在贴出过程了

1,同样道理安装不变,配置节点每台为3个就行,2台机器6个节点即可;端口一样不一样都可以
2,配置文件bind的IP地址修改,两台机器要互通
3,创建集群时在2台机器任意一台即可,注意节点地址和端口的变化


商业转载请联系作者获得授权,非商业转载请注明出处,谢谢合作。


如果这篇文章帮助到了你,我感到十分荣幸!

或许你不想写点什么·但我依旧在这里

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png2018new_aini_org.png2018new_baobao_thumb.png2018new_erha_org.png2018new_kuxiao_thumb.png2018new_yun_thumb.png2018new_hufen_thumb.png2018new_gui_org.png2018new_xiaoerbuyu_org.png2018new_heixian_thumb.png2018new_wabi_thumb.png2018new_tianping_thumb.png

站点地图 网站地图
豫ICP备17003270号 | Copyright © 2018 💖 靳闯博客
Typecho🍹Sgreen
TOP