Redis集群安装和设置

1、解压redis安装包及编译
tar -zxvf redis-3.0.4.tar.gz -C /usr/local/
cd /usr/local/redis-3.0.4
make install PREFIX=/usr/local/redis
2、复制redis的配置文件到redis的安装目录
cp redis.conf /usr/local/redis/bin
3、创建redis集群需要用到的redis安装目录(redis集群最少需要3主3从共6个节点,3台服务器,每台服务器上启用6379和6380两个实例。单机版不需要这样操作)
mv …/redis …/redis6379
cd …
cp -a redis6379 redis6380
4、修改redis.conf配置文件
以6379实例为例,打开配置文件 redis.conf,修改以下地方
#37 daemonize yes #以后台方式运行
#45 port 6379 #侦听端口
#64 bind w.x.y.z #侦听地址
#98 loglevel notice #日志级别
#103 logfile “/mnt/redis/6379/redis.log” #日志文件名称
#177 dbfilename dump6379.rdb #持久化文件名称
#187 dir /mnt/redis/6379 #实例主目录
#212 masterauth passwordthis #(集群)主实例认证密码
#391 requirepass passwordthis #访问密码
#448 maxmemory 4gb #redis最大可用内存
#471 maxmemory-policy volatile-lru #内存清理规则
#504 appendonly yes #启用aof
#556 no-appendfsync-on-rewrite yes #提高rewrite时的性能
#632 cluster-enabled yes #启用集群模式
#640 cluster-config-file /mnt/redis/6379/nodes-6379.conf #集群状态文件
#646 cluster-node-timeout 500 #集群节点掉线超时时间

注意:如果不配置masterauth或者配置的密码不同于requirepass的设置,主从切换会失败
5、增加常用的redis命令的软连接
ln -s /usr/local/redis6379/bin/redis-cli /usr/local/bin/
ln …/redis-3.0.4/src/redis-trib.rb /usr/local/bin
6、启动redis(以redis的6379实例为例)
cd /usr/local/redis6379/bin
./redis-server redis.conf
6、安装ruby的redis组件redis-4.1.2.gem
注意:redis requires Ruby version >= 2.3.0,centos默认安装的版本是2.0.14.1,要进行升级,注意需要先通过yum安装openssl-devel后在源码编译ruby
6.1 移除系统自带的老版本的ruby(2.0.14.1版本)
yum remove ruby
6.2 手工安装2.5.8版本的ruby
cd /tmp
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.8.tar.gz
tar -zxvf ruby-2.5.8.tar.gz
cd ruby-2.5.8
./configure --with-openssl-dir=/usr/include/openssl
make
make install
6.3 安装ruby gems2.3.0
cd /tmp
curl -sSL https://rubygems.org/rubygems/rubygems-2.3.0.tgz -o ./rubygems.tar.gz
tar -xzvf rubygems.tar.gz
cd rubygems-2.3.0/
ruby setup.rb
gem -v #查看gem版本
6.4 安装ruby扩展组件
cd /tmp/ruby-2.5.8/ext/zlib/
ruby extconf.rb
cp Makefile Makefile2023061
vi Makefi将其中的zlib.o: $(top_srcdir)/include/ruby.h改为
zlib.o: …/…/include/ruby.hle

make&&make install
6.5 使用rubygem安装redis插件,以便运行集群创建脚本
先上传redis-4.1.2.gem到/tmp目录
cd /tmp
gem install redis-4.1.2.gem
6.6 验证ruby脚本是否可以正常运行
/usr/local/redis-3.0.4/src/redis-trib.rb help
6.7 创建redis集群
./redis-trib.rb create 192.168.0.21:6379 192.168.0.75:6379 192.168.0.81:6379
或者:
/usr/local/redis-3.0.4/src/redis-trib.rb create --replicas 0 192.168.0.21:6379 192.168.0.75:6379 192.168.0.81:6379

Creating cluster
Connecting to node 192.168.0.21:6379: OK
Connecting to node 192.168.0.75:6379: OK
Connecting to node 192.168.0.81:6379: OK
Performing hash slots allocation on 3 nodes…
Using 3 masters:
192.168.0.21:6379
192.168.0.75:6379
192.168.0.81:6379
M: ac8a8b8fc6575e3a7143fcdf46d9ee18257e912e 192.168.0.21:6379
slots:0-5460 (5461 slots) master
M: 0a465b20cda57b5b4cffd3855cfa99249b2d46a7 192.168.0.75:6379
slots:5461-10922 (5462 slots) master
M: 707cf2d384f9cf0a375c58e4144e20281ec5b150 192.168.0.81:6379
slots:10923-16383 (5461 slots) master
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…
Performing Cluster Check (using node 192.168.0.21:6379)
M: ac8a8b8fc6575e3a7143fcdf46d9ee18257e912e 192.168.0.21:6379
slots:0-5460 (5461 slots) master
M: 0a465b20cda57b5b4cffd3855cfa99249b2d46a7 192.168.0.75:6379
slots:5461-10922 (5462 slots) master
M: 707cf2d384f9cf0a375c58e4144e20281ec5b150 192.168.0.81:6379
slots:10923-16383 (5461 slots) master
[OK] All nodes agree about slots configuration.
Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.
[root@sql-02 src]# ./redis-trib.rb add-node --slave --master-id ac8a8b8fc6575e3a7143fcdf46d9ee18257e912e 192.168.0.75:6380 192.168.0.75:6379
Adding node 192.168.0.75:6380 to cluster 192.168.0.75:6379
Connecting to node 192.168.0.75:6379: OK
Connecting to node 192.168.0.81:6379: OK
Connecting to node 192.168.0.21:6379: OK
Performing Cluster Check (using node 192.168.0.75:6379)
M: 0a465b20cda57b5b4cffd3855cfa99249b2d46a7 192.168.0.75:6379
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: 707cf2d384f9cf0a375c58e4144e20281ec5b150 192.168.0.81:6379
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: ac8a8b8fc6575e3a7143fcdf46d9ee18257e912e 192.168.0.21:6379
slots:0-5460 (5461 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.
Connecting to node 192.168.0.75:6380: OK
Send CLUSTER MEET to node 192.168.0.75:6380 to make it join the cluster.
Waiting for the cluster to join…
Configure node as replica of 192.168.0.21:6379.
[OK] New node added correctly.

[root@sql-02 src]# ./redis-trib.rb add-node --slave --master-id 0a465b20cda57b5b4cffd3855cfa99249b2d46a7 192.168.0.81:6380 192.168.0.75:6379

Adding node 192.168.0.81:6380 to cluster 192.168.0.75:6379
Connecting to node 192.168.0.75:6379: OK
Connecting to node 192.168.0.81:6379: OK
Connecting to node 192.168.0.21:6379: OK
Connecting to node 192.168.0.75:6380: OK
Performing Cluster Check (using node 192.168.0.75:6379)
M: 0a465b20cda57b5b4cffd3855cfa99249b2d46a7 192.168.0.75:6379
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: 707cf2d384f9cf0a375c58e4144e20281ec5b150 192.168.0.81:6379
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: ac8a8b8fc6575e3a7143fcdf46d9ee18257e912e 192.168.0.21:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 084b155f3a639d761cecacbf5d666bdfeceac17e 192.168.0.75:6380
slots: (0 slots) slave
replicates ac8a8b8fc6575e3a7143fcdf46d9ee18257e912e
[OK] All nodes agree about slots configuration.
Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.
Connecting to node 192.168.0.81:6380: OK
Send CLUSTER MEET to node 192.168.0.81:6380 to make it join the cluster.
Waiting for the cluster to join…
Configure node as replica of 192.168.0.75:6379.
[OK] New node added correctly.
[root@sql-02 src]# ./redis-trib.rb add-node --slave --master-id 707cf2d384f9cf0a375c58e4144e20281ec5b150 192.168.0.21:6380 192.168.0.75:6379
Adding node 192.168.0.21:6380 to cluster 192.168.0.75:6379
Connecting to node 192.168.0.75:6379: OK
Connecting to node 192.168.0.81:6380: OK
Connecting to node 192.168.0.75:6380: OK
Connecting to node 192.168.0.81:6379: OK
Connecting to node 192.168.0.21:6379: OK
Performing Cluster Check (using node 192.168.0.75:6379)
M: 0a465b20cda57b5b4cffd3855cfa99249b2d46a7 192.168.0.75:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 64b286df97d8d835fd2a82775ede10298b5950ed 192.168.0.81:6380
slots: (0 slots) slave
replicates 0a465b20cda57b5b4cffd3855cfa99249b2d46a7
S: 084b155f3a639d761cecacbf5d666bdfeceac17e 192.168.0.75:6380
slots: (0 slots) slave
replicates ac8a8b8fc6575e3a7143fcdf46d9ee18257e912e
M: 707cf2d384f9cf0a375c58e4144e20281ec5b150 192.168.0.81:6379
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: ac8a8b8fc6575e3a7143fcdf46d9ee18257e912e 192.168.0.21:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.
Connecting to node 192.168.0.21:6380: OK
Send CLUSTER MEET to node 192.168.0.21:6380 to make it join the cluster.
Waiting for the cluster to join.
Configure node as replica of 192.168.0.81:6379.
[OK] New node added correctly.
[root@sql-02 src]# redis-cli -h 192.168.0.21
192.168.0.21:6379> auth yourredis_password
OK
192.168.0.21:6379> cluster nodes
64b286df97d8d835fd2a82775ede10298b5950ed 192.168.0.81:6380 slave 0a465b20cda57b5b4cffd3855cfa99249b2d46a7 0 1686841565811 2 connected
8d8971db58cf51acb2eb034ec48a321842927ab1 192.168.0.21:6380 slave 707cf2d384f9cf0a375c58e4144e20281ec5b150 0 1686841561803 3 connected
707cf2d384f9cf0a375c58e4144e20281ec5b150 192.168.0.81:6379 master - 0 1686841564810 3 connected 10923-16383
084b155f3a639d761cecacbf5d666bdfeceac17e 192.168.0.75:6380 slave ac8a8b8fc6575e3a7143fcdf46d9ee18257e912e 0 1686841566812 1 connected
0a465b20cda57b5b4cffd3855cfa99249b2d46a7 192.168.0.75:6379 master - 0 1686841567815 2 connected 5461-10922
ac8a8b8fc6575e3a7143fcdf46d9ee18257e912e 192.168.0.21:6379 myself,master - 0 0 1 connected 0-5460
192.168.0.21:6379>
注意事项:
a、 Redis动态配置参数的命令
redis>config set appendonly yes
b、 使用cluster模式时,必须带-c参数
[root@sql-02 src]# redis-cli -h 192.168.0.21 -c

了解更多内容 关注公众号
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值