本文 redis版本3.2.9
介绍
IP | 端口 | 角色 |
---|---|---|
127.0.0.1 | 7000 | Master |
127.0.0.1 | 7001 | Master |
127.0.0.1 | 7002 | Master |
127.0.0.1 | 7003 | Slave |
127.0.0.1 | 7004 | Slave |
127.0.0.1 | 7005 | Slave |
一、安装
1.1 创建目录
mkdir -p /redis/cluster
mkdir 7000 7001 7002 7003 7004 7005
1.2 下载安装包wget http://download.redis.io/releases/redis-3.2.9.tar.gz
1.3 解压安装
tar zxvf redis-3.2.9.tar.gz
cd redis-3.2.9
make && make PREFIX=/usr/local/redis install
注:PREFIX可以根据环境需求更改存放位置
1.4 yum源
vi /etc/yum.repos.d/yum.repo
[base]
name=Red Hat Enterprise Linux
baseurl=file:///media/Server
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
资源包
yum install gcc
yum install tcl
1.5 复制和修改配置文件
cp /redis/redis-3.2.9/redis.conf /redis/cluster/7000
cp /redis/redis-3.2.9/redis.conf /redis/cluster/7001
cp /redis/redis-3.2.9/redis.conf /redis/cluster/7002
cp /redis/redis-3.2.9/redis.conf /redis/cluster/7003
cp /redis/redis-3.2.9/redis.conf /redis/cluster/7004
cp /redis/redis-3.2.9/redis.conf /redis/cluster/7005
1.6 修改配置文件
# 端口号
port 7000
#后台启动
daemonize yes
#开启集群
cluster-enabled yes
#集群节点配置文件
cluster-config-file nodes-7000.conf
#集群连接超时时间
cluster-node-timeout 5000
#进程pid的文件位置
pidfile /var/run/redis-7000.pid
#开启aof
appendonly yes
#aof文件路径
appendfilename "appendonly-7005.aof"
#rdb文件路径
dbfilename dump-7000.rdb
1.7 创建启动脚本
/redis/redis-3.2.9/bin/redis-server /redis/cluster/7000/redis.conf
/redis/redis-3.2.9/bin/redis-server /redis/cluster/7001/redis.conf
/redis/redis-3.2.9/bin/redis-server /redis/cluster/7002/redis.conf
/redis/redis-3.2.9/bin/redis-server /redis/cluster/7003/redis.conf
/redis/redis-3.2.9/bin/redis-server /redis/cluster/7004/redis.conf
/redis/redis-3.2.9/bin/redis-server /redis/cluster/7005/redis.conf
二、集群
2.1 资源包
[root@redis2 ~]#yum install ruby rubygems -y
[root@redis2 ~]# tar zxvf rubygems-2.4.6.tgz
[root@redis2 ~]# chown -R root:root rubygems-2.4.6
[root@redis2 ~]# cd rubygems-2.4.6
ruby setup.rb
gem sources --remove https://rubygems.org/
gem install redis-3.3.3.gem
2.2 创建集群redis-trib create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
命令的意义如下:
redis-trib.rb 程序的命令是 create ,集群。
选项 --replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
之后跟着的其他参数则是实例的地址列表, 我们希望程序使用这些地址所指示的实例来创建新集群。
简单来说,以上的命令的意思就是让redis-trib程序帮我们创建三个主节点和三个从节点的集群
接着, redis-trib 会打印出一份预想中的配置给你看, 如果你觉得没问题的话, 就可以输入 yes , redis-trib 就会将这份配置应用到集群当中:
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: bdcddddd3d78a866b44b68c7ae0e5ccf875c446a 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: b85519795fa42aa33d4e88d25104cbae895933a6 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: b681e1a151890cbf957d1ff08352ee48f6ae39e6 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
S: d403713ab9db48aeac5b5393b69e1201026ef479 127.0.0.1:7003
replicates bdcddddd3d78a866b44b68c7ae0e5ccf875c446a
S: b7ec92919e5bcffa76c8eee338f8ca5155293c64 127.0.0.1:7004
replicates b85519795fa42aa33d4e88d25104cbae895933a6
S: 8a0d2a3f271b349744a971e1b0a545405de2742e 127.0.0.1:7005
replicates b681e1a151890cbf957d1ff08352ee48f6ae39e6
Can I set the above configuration? (type 'yes' to accept):
2.3 集群使用
[root@centos1 redis]# ./redis-cli -c -p 7000
127.0.0.1:7000> set name leo
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get name
"leo"