四、安装tcl组件包(安装Redis需要tcl支持)
yum install tcl-devel.x86_64 tcl.x86_64
五、下载redis-3.0.2.tar.gz
tar -xvzf redis-3.0.0.tar.gz
cd redis-3.0.0
make && make install
cd src
cp redis-trib.rb /usr/local/bin
mkdir /etc/redis
mkdir /var/log/redis
五、配置redis
[root@hadoop1 redis-3.0.2]# vim redis.conf
daemonize yes #---默认值no,该参数用于定制redis服务是否以守护模式运行。
port 6379
pidfile /var/run/redis-6379.pid
dbfilename dump-6379.rdb
appendfilename "appendonly-6379.aof"
cluster-config-file nodes-6379.conf
cluster-enabled yes
cluster-node-timeout 5000
appendonly yes
copy配置文件,并修改端口
cp redis.conf /etc/redis/redis-6379.conf
cp redis.conf /etc/redis/redis-6380.conf
cp redis.conf /etc/redis/redis-6381.conf
scp redis.conf 192.168.10.220:/etc/redis/redis-6382.conf
scp redis.conf 192.168.10.220:/etc/redis/redis-6383.conf
scp redis.conf 192.168.10.220:/etc/redis/redis-6384.conf
sed -i "s/6379/6380/g" /etc/redis/redis-6380.conf
sed -i "s/6379/6381/g" /etc/redis/redis-6381.conf
sed -i "s/6379/6382/g" /etc/redis/redis-6382.conf
sed -i "s/6379/6383/g" /etc/redis/redis-6383.conf
sed -i "s/6379/6384/g" /etc/redis/redis-6384.conf
由于4作为从节点,所以只需把82-84配置文件拷过去即可
启动并查看redis
redis-server redis-6379.conf
redis-server redis-6380.conf
redis-server redis-6381.conf
[root@hadoop1 redis]# netstat -tlnp | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 16852/redis-server
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 16858/redis-server
tcp 0 0 0.0.0.0:6381 0.0.0.0:* LISTEN 16862/redis-server
tcp 0 0 0.0.0.0:16379 0.0.0.0:* LISTEN 16852/redis-server
tcp 0 0 0.0.0.0:16380 0.0.0.0:* LISTEN 16858/redis-server
tcp 0 0 0.0.0.0:16381 0.0.0.0:* LISTEN 16862/redis-server
tcp 0 0 :::6379 :::* LISTEN 16852/redis-server
tcp 0 0 :::6380 :::* LISTEN 16858/redis-server
tcp 0 0 :::6381 :::* LISTEN 16862/redis-server
tcp 0 0 :::16379 :::* LISTEN 16852/redis-server
tcp 0 0 :::16380 :::* LISTEN 16858/redis-server
tcp 0 0 :::16381 :::* LISTEN 16862/redis-server
4这台主机同样要启动
redis-server redis-6382.conf
redis-server redis-6383.conf
redis-server redis-6384.conf
[root@hadoop2 redis]# netstat -tlnp | grep redis
tcp 0 0 0.0.0.0:6382 0.0.0.0:* LISTEN 10572/redis-server
tcp 0 0 0.0.0.0:6383 0.0.0.0:* LISTEN 10578/redis-server
tcp 0 0 0.0.0.0:6384 0.0.0.0:* LISTEN 10582/redis-server
tcp 0 0 0.0.0.0:16382 0.0.0.0:* LISTEN 10572/redis-server
tcp 0 0 0.0.0.0:16383 0.0.0.0:* LISTEN 10578/redis-server
tcp 0 0 0.0.0.0:16384 0.0.0.0:* LISTEN 10582/redis-server
tcp 0 0 :::6382 :::* LISTEN 10572/redis-server
tcp 0 0 :::6383 :::* LISTEN 10578/redis-server
tcp 0 0 :::6384 :::* LISTEN 10582/redis-server
tcp 0 0 :::16382 :::* LISTEN 10572/redis-server
tcp 0 0 :::16383 :::* LISTEN 10578/redis-server
tcp 0 0 :::16384 :::* LISTEN 10582/redis-server
六、创建集群
[root@hadoop1 redis]# redis-trib.rb create --replicas 1 192.168.1.3:6379 192.168.1.3:6380 192.168.1.3:6381 192.168.1.4:6382 192.168.1.4:6383 192.168.1.4:6384
>>> Creating cluster
Connecting to node 192.168.1.3:6379: OK
Connecting to node 192.168.1.3:6380: OK
Connecting to node 192.168.1.3:6381: OK
Connecting to node 192.168.1.4:6382: OK
Connecting to node 192.168.1.4:6383: OK
Connecting to node 192.168.1.4:6384: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.4:6382
192.168.1.3:6379
192.168.1.4:6383
Adding replica 192.168.1.3:6380 to 192.168.1.4:6382
Adding replica 192.168.1.4:6384 to 192.168.1.3:6379
Adding replica 192.168.1.3:6381 to 192.168.1.4:6383
M: 178b951fd08ae3250d06719509ea45258c6cef73 192.168.1.3:6379
slots:5461-10922 (5462 slots) master
S: 76ea836e326fbd4b2a242f5da01b9005c131eb46 192.168.1.3:6380
replicates d9a852afad1669adf3561a57dbfa77b250ae32bb
S: 4e54918bfc57e0895d68ad0f6bea1d104b18e0f6 192.168.1.3:6381
replicates 85ad96dbdb49fd901de6f9f1431662c7ab58a208
M: d9a852afad1669adf3561a57dbfa77b250ae32bb 192.168.1.4:6382
slots:0-5460 (5461 slots) master
M: 85ad96dbdb49fd901de6f9f1431662c7ab58a208 192.168.1.4:6383
slots:10923-16383 (5461 slots) master
S: b37d1d090fdfffdeaeccc09f7978eeedc36b342a 192.168.1.4:6384
replicates 178b951fd08ae3250d06719509ea45258c6cef73
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.1.3:6379)
M: 178b951fd08ae3250d06719509ea45258c6cef73 192.168.1.3:6379
slots:5461-10922 (5462 slots) master
M: 76ea836e326fbd4b2a242f5da01b9005c131eb46 192.168.1.3:6380
slots: (0 slots) master
replicates d9a852afad1669adf3561a57dbfa77b250ae32bb
M: 4e54918bfc57e0895d68ad0f6bea1d104b18e0f6 192.168.1.3:6381
slots: (0 slots) master
replicates 85ad96dbdb49fd901de6f9f1431662c7ab58a208
M: d9a852afad1669adf3561a57dbfa77b250ae32bb 192.168.1.4:6382
slots:0-5460 (5461 slots) master
M: 85ad96dbdb49fd901de6f9f1431662c7ab58a208 192.168.1.4:6383
slots:10923-16383 (5461 slots) master
M: b37d1d090fdfffdeaeccc09f7978eeedc36b342a 192.168.1.4:6384
slots: (0 slots) master
replicates 178b951fd08ae3250d06719509ea45258c6cef73
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
创建一个新的集群, 选项--replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
之后跟着的其他参数则是这个集群实例的地址列表,3个master,3个slave
七、查看redis集群的状态
root@hadoop1 redis]# redis-trib.rb check 192.168.1.3:6379
Connecting to node 192.168.1.3:6379: OK
Connecting to node 192.168.1.4:6383: OK
Connecting to node 192.168.1.4:6384: OK
Connecting to node 192.168.1.3:6380: OK
Connecting to node 192.168.1.4:6382: OK
Connecting to node 192.168.1.3:6381: OK
>>> Performing Cluster Check (using node 192.168.1.3:6379)
M: 178b951fd08ae3250d06719509ea45258c6cef73 192.168.1.3:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 85ad96dbdb49fd901de6f9f1431662c7ab58a208 192.168.1.4:6383
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: b37d1d090fdfffdeaeccc09f7978eeedc36b342a 192.168.1.4:6384
slots: (0 slots) slave //仔细看一下此刻它是从的
replicates 178b951fd08ae3250d06719509ea45258c6cef73
S: 76ea836e326fbd4b2a242f5da01b9005c131eb46 192.168.1.3:6380
slots: (0 slots) slave
replicates d9a852afad1669adf3561a57dbfa77b250ae32bb
M: d9a852afad1669adf3561a57dbfa77b250ae32bb 192.168.1.4:6382
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 4e54918bfc57e0895d68ad0f6bea1d104b18e0f6 192.168.1.3:6381
slots: (0 slots) slave
replicates 85ad96dbdb49fd901de6f9f1431662c7ab58a208
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
八、测试redis集群
[root@hadoop1 redis]# redis-cli -c -p 6379 -h 192.168.1.3
192.168.1.3:6379> set tank tank1 设置测试值
-> Redirected to slot [4407] located at 192.168.1.4:6382
OK //直接转向到4 6382端口,数据存到了220 6382
192.168.1.4:6382> set foo bar
-> Redirected to slot [12182] located at 192.168.1.4:6383
OK
192.168.1.4:6383> get foo //可以取到值
"bar"
192.168.1.4:6383> get tank
-> Redirected to slot [4407] located at 192.168.1.4:6382
"tank1"
[root@hadoop2 redis]# redis-cli -c -p 6383 -h 192.168.1.4 //3机器,6383端口
192.168.1.4:6383> get foo
"bar"
192.168.1.4:6383> get tank
-> Redirected to slot [4407] located at 192.168.1.4:6382 //直接转向4 6382端口
"tank1"
[root@hadoop1 redis]# ps -aux | grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 16852 0.2 1.3 137440 9532 ? Ssl 19:51 0:04 redis-server *:6379 [cluster]
root 16858 0.2 1.3 137440 9612 ? Ssl 19:51 0:04 redis-server *:6380 [cluster]
root 16862 0.2 1.0 137440 7672 ? Ssl 19:51 0:04 redis-server *:6381 [cluster]
root 17061 0.0 0.1 103256 844 pts/0 S+ 20:23 0:00 grep redis
[root@hadoop1 redis]# kill -9 16852 //关闭3 6379端口,看数据会不会丢失
九、查看集群的情况
[root@hadoop1 redis]# redis-trib.rb check 192.168.1.3:6380
Connecting to node 192.168.1.3:6380: OK
Connecting to node 192.168.1.4:6383: OK
Connecting to node 192.168.1.3:6381: OK
Connecting to node 192.168.1.4:6384: OK
Connecting to node 192.168.1.4:6382: OK
>>> Performing Cluster Check (using node 192.168.1.3:6380)
S: 76ea836e326fbd4b2a242f5da01b9005c131eb46 192.168.1.3:6380
slots: (0 slots) slave
replicates d9a852afad1669adf3561a57dbfa77b250ae32bb
M: 85ad96dbdb49fd901de6f9f1431662c7ab58a208 192.168.1.4:6383
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 4e54918bfc57e0895d68ad0f6bea1d104b18e0f6 192.168.1.3:6381
slots: (0 slots) slave
replicates 85ad96dbdb49fd901de6f9f1431662c7ab58a208
M: b37d1d090fdfffdeaeccc09f7978eeedc36b342a 192.168.1.4:6384
slots:5461-10922 (5462 slots) master //6384变成主了
0 additional replica(s)
M: d9a852afad1669adf3561a57dbfa77b250ae32bb 192.168.1.4:6382
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.
十、经测试集群是可用的
[root@hadoop1 redis]# redis-cli -c -p 6380 -h 192.168.1.3
192.168.1.3:6380> get foo
-> Redirected to slot [12182] located at 192.168.1.4:6383
"bar"
192.168.1.4:6383> get tank
-> Redirected to slot [4407] located at 192.168.1.4:6382
"tank1"
[root@hadoop2 redis]# redis-cli -c -p 6380 -h 192.168.1.3
192.168.1.3:6380> get foo
-> Redirected to slot [12182] located at 192.168.1.4:6383
"bar"
192.168.1.4:6383> get tank
-> Redirected to slot [4407] located at 192.168.1.4:6382
"tank1"