安装
下载,提取和编译Redis:
$ wget http://download.redis.io/releases/redis-4.0.8.tar.gz
$ tar xzf redis-4.0.8.tar.gz
$ cd redis-4.0.8
$ make
现在编译的二进制文件在src 目录中可用 。运行Redis:
$ src/redis-server
您可以使用内置客户端与Redis进行交互:
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
搭建集群
1、进入redis-4.0.8根目录
cd redis-4.0.8
mkdir redis_cluster
cd redis_cluster
mkdir 7000 7001 7002
cp redis.conf redis_cluster/7000
cp redis.conf redis_cluster/7001
cp redis.conf redis_cluster/7002
2、分别修改这三个配置文件,修改如下内容
port 7000 //端口7000,7002,7003
bind 本机ip //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes //redis后台运行
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
接着在另外一台机器上(198.216.15.201),的操作重复以上三步,只是把目录改为7003、7004、7005,对应的配置文件也按照这个规则修改即可
3、启动:
第一台机器上执行
redis-server redis_cluster/7000/redis.conf
redis-server redis_cluster/7001/redis.conf
redis-server redis_cluster/7002/redis.conf
另外一台机器上执行
redis-server redis_cluster/7003/redis.conf
redis-server redis_cluster/7004/redis.conf
redis-server redis_cluster/7005/redis.conf
4、检查 redis 启动情况
202服务器
redis 20876 1 0 10:43 ? 00:00:26 src/redis-server 198.216.15.202:7000 [cluster]
redis 20877 1 0 10:43 ? 00:00:26 src/redis-server 198.216.15.202:7001 [cluster]
redis 20878 1 0 10:43 ? 00:00:26 src/redis-server 198.216.15.202:7002 [cluster]
201服务器
redis 31696 1 0 16:12 pts/0 00:00:03 src/redis-server 198.216.15.201:7003 [cluster]
redis 31697 1 0 16:12 pts/0 00:00:03 src/redis-server 198.216.15.201:7004 [cluster]
redis 31698 1 0 16:12 pts/0 00:00:03 src/redis-server 198.216.15.201:7005 [cluster]
5、启动集群
Redis 官方提供了 redis-trib.rb (需要前面安装的ruby语言)这个工具,就在解压目录的 src 目录中
安装ruby
第一种安装方法:下载最新版的 Ruby 压缩文件。请点击这里下载。
- 下载 Ruby 之后,解压到新创建的目录下:
$ tar -xvzf ruby-2.2.3.tgz
$ cd ruby-2.2.3
- 现在,配置并编译源代码,如下所示:
$ ./configure
$ make
$ sudo make install
- 安装后,通过在命令行中输入以下命令来确保一切工作正常:
$ruby -v
ruby 2.2.3
gem install redis
第二种安装方法:如果上面方法无法安装ruby,可通过yum安装
yum install ruby
yum install rubygems
gem install redis
如果ruby版本过低会报下面错误 ,解决方法
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
再次执行上边命令
redis-trib.rb create --replicas 1 198.216.15.202:7000 198.216.15.202:7001 198.216.15.202:7002 198.216.15.201:7003 198.216.15.201:7004 198.216.15.201:7005
输出:
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
198.216.15.202:7000
198.216.15.201:7003
198.216.15.202:7001
Adding replica 198.216.15.201:7005 to 198.216.15.202:7000
Adding replica 198.216.15.202:7002 to 198.216.15.201:7003
Adding replica 198.216.15.201:7004 to 198.216.15.202:7001
M: 530cf6b659fd685ee647515805b98d89d5d8524b 198.216.15.202:7000
slots:0-5460 (5461 slots) master
M: 6d5bd829a784de772e25d79d55eae6b3d173d26a 198.216.15.202:7001
slots:10923-16383 (5461 slots) master
S: b9585f528e1b1ee8837762ccecf9ea4d84fd5b47 198.216.15.202:7002
replicates 80abbc5979277e02ba33044d47c69363ac07b909
M: 80abbc5979277e02ba33044d47c69363ac07b909 198.216.15.201:7003
slots:5461-10922 (5462 slots) master
S: 246cdd3c12fe65b434cc85da7529a8b6b405b2b3 198.216.15.201:7004
replicates 6d5bd829a784de772e25d79d55eae6b3d173d26a
S: e215c86996a0dfe4ee6a51bbc1e0240b662f3baa 198.216.15.201:7005
replicates 530cf6b659fd685ee647515805b98d89d5d8524b
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 198.216.15.202:7000)
M: 530cf6b659fd685ee647515805b98d89d5d8524b 198.216.15.202:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: e215c86996a0dfe4ee6a51bbc1e0240b662f3baa 198.216.15.201:7005
slots: (0 slots) slave
replicates 530cf6b659fd685ee647515805b98d89d5d8524b
S: 246cdd3c12fe65b434cc85da7529a8b6b405b2b3 198.216.15.201:7004
slots: (0 slots) slave
replicates 6d5bd829a784de772e25d79d55eae6b3d173d26a
M: 6d5bd829a784de772e25d79d55eae6b3d173d26a 198.216.15.202:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 80abbc5979277e02ba33044d47c69363ac07b909 198.216.15.201:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: b9585f528e1b1ee8837762ccecf9ea4d84fd5b47 198.216.15.202:7002
slots: (0 slots) slave
replicates 80abbc5979277e02ba33044d47c69363ac07b909
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
集群验证:
src/redis-cli -h 198.216.15.201 -c -p 7003
198.216.15.201:7003> set hello world
-> Redirected to slot [866] located at 198.216.15.202:7000
OK
198.216.15.202:7000> keys *
1) "hello"
198.216.15.202:7000> exit
[redis@BJNServer redis-4.0.8]$ src/redis-cli -h 198.216.15.202 -c -p 7000
198.216.15.202:7000> keys *
1) "hello"
198.216.15.202:7000> get hello
"world"