Redis集群搭建

1.Redis 节点:

创建:

$ mkdir /usr/local/redis/redis_cluster
$ cd /usr/local/redis_cluster
$ mkdir 7000 7001 7002
$ cp /usr/local/redis/redis.conf /usr/local/redis/redis_cluster/7000
$ cp /usr/local/redis/redis.conf /usr/local/redis/redis_cluster/7001
$ cp /usr/local/redis/redis.conf /usr/local/redis/redis_cluster/7002

修改配置文件内容:

port                  7000                        //修改为对应端口7000,7001,7002        
bind                  本机ip                      //需要改为其他节点机器可访问的ip
daemonize             yes                         //redis后台运行
pidfile               /var/run/redis_7000.pid     //pidfile文件对应700070017002
cluster-enabled       yes                         //开启集群,把注释#去掉
cluster-config-file   nodes_7000.conf             //集群的配置,配置文件首次启动自动生成 700070017002
cluster-node-timeout  15000                       //请求超时,默认15秒,可自行设置
appendonly            yes                         //aof日志开启,有需要就开启,它会每次写操作都记录一条日志 

启动节点:

##第一台机器上执行
$ /usr/local/redis/src/redis-server /usr/local/redis/redis_cluster/7000/redis.conf
$ /usr/local/redis/src/redis-server /usr/local/redis/redis_cluster/7001/redis.conf
$ /usr/local/redis/src/redis-server /usr/local/redis/redis_cluster/7002/redis.conf

##第二台机器上执行
$ /usr/local/redis/src/redis-server /usr/local/redis/redis_cluster/7003/redis.conf
$ /usr/local/redis/src/redis-server /usr/local/redis/redis_cluster/7004/redis.conf
$ /usr/local/redis/src/redis-server /usr/local/redis/redis_cluster/7005/redis.conf

#查看端口
[root@iZ2398vqlp1Z ~]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 120.27.147.124:7001     0.0.0.0:*               LISTEN      31204/redis-server  
tcp        0      0 120.27.147.124:7002     0.0.0.0:*               LISTEN      31209/redis-server          
tcp        0      0 120.27.147.124:17000    0.0.0.0:*               LISTEN      31199/redis-server  
tcp        0      0 120.27.147.124:17001    0.0.0.0:*               LISTEN      31204/redis-server         
tcp        0      0 120.27.147.124:17002    0.0.0.0:*               LISTEN      31209/redis-server          
tcp        0      0 120.27.147.124:7000     0.0.0.0:*               LISTEN      31199/redis-server  

2.安装集群所需Ruby环境:

$ yum -y install ruby ruby-devel rubygems rpm-build
$ gem install redis

此处如有报错,请参考:
redis requires Ruby version >= 2.2.2解决办法

3.创建集群:

$ /root/redis-4.0.2/src/redis-trib.rb create --replicas 1 120.27.147.124:7000 120.27.147.124:7001 120.27.147.124:7002 47.88.137.51:7003 47.88.137.51:7004 47.88.137.51:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
120.27.147.124:7000
47.88.137.51:7003
120.27.147.124:7001
Adding replica 47.88.137.51:7004 to 120.27.147.124:7000
Adding replica 120.27.147.124:7002 to 47.88.137.51:7003
Adding replica 47.88.137.51:7005 to 120.27.147.124:7001
M: eaa9423181df0438c7dd64c02550627de67ae374 120.27.147.124:7000
   slots:0-5460 (5461 slots) master
M: c5b37ae6ebe53fdf7d3f9646f8008c5baef35776 120.27.147.124:7001
   slots:10923-16383 (5461 slots) master
S: cfe1bed97ec52c6b26068c1bc876415d92102cb0 120.27.147.124:7002
   replicates 4e68360e9532f37c63a465223ce4c2b3e1005d51
M: 4e68360e9532f37c63a465223ce4c2b3e1005d51 47.88.137.51:7003
   slots:5461-10922 (5462 slots) master
S: 1e2a0f50e84880aa84be22b1197e48ba7a5194db 47.88.137.51:7004
   replicates eaa9423181df0438c7dd64c02550627de67ae374
S: 1967d7cfd2f8093db2859e537162c056d75914f4 47.88.137.51:7005
   replicates c5b37ae6ebe53fdf7d3f9646f8008c5baef35776
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 120.27.147.124:7000)
M: eaa9423181df0438c7dd64c02550627de67ae374 120.27.147.124:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: cfe1bed97ec52c6b26068c1bc876415d92102cb0 120.27.147.124:7002
   slots: (0 slots) slave
   replicates 4e68360e9532f37c63a465223ce4c2b3e1005d51
S: 1e2a0f50e84880aa84be22b1197e48ba7a5194db 47.88.137.51:7004
   slots: (0 slots) slave
   replicates eaa9423181df0438c7dd64c02550627de67ae374
M: c5b37ae6ebe53fdf7d3f9646f8008c5baef35776 120.27.147.124:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 1967d7cfd2f8093db2859e537162c056d75914f4 47.88.137.51:7005
   slots: (0 slots) slave
   replicates c5b37ae6ebe53fdf7d3f9646f8008c5baef35776
M: 4e68360e9532f37c63a465223ce4c2b3e1005d51 47.88.137.51:7003
   slots:5461-10922 (5462 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.

3.简单测试:

第二台机器:

[root@iZ22jn400djZ ~]# /usr/local/redis/bin/redis-cli -h 47.88.137.51 -c -p 7004              
47.88.137.51:7004> set name redis
-> Redirected to slot [5798] located at 47.88.137.51:7003
OK
47.88.137.51:7003> keys *
1) "name"

第一台机器:

[root@iZ2398vqlp1Z bin]# /usr/local/redis/bin/redis-cli -h 120.27.147.124 -c -p 7000
120.27.147.124:7000> get name
-> Redirected to slot [5798] located at 47.88.137.51:7003
"redis"

4.主从关系:

[root@iZ2398vqlp1Z ~]# cd /usr/local/redis/bin
[root@iZ2398vqlp1Z bin]# ./redis-cli -h 120.27.147.124 -p 7000
120.27.147.124:7000> info Replication 
# Replication
role:master  #主节点
connected_slaves:1  #从节点个数
slave0:ip=47.88.137.51,port=7004,state=online,offset=322518,lag=1 #从节点ip/端口相关信息
master_replid:930a63455aeb0abbb7a12b440871ccccc38a089d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:322518
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:322518
[root@iZ22jn400djZ ~]# cd /usr/local/redis/bin
[root@iZ22jn400djZ bin]# ./redis-cli -h 47.88.137.51 -p 7003
47.88.137.51:7003> info Replication 
# Replication
role:slave #从节点
master_host:120.27.147.124  #所映射的主节点ip
master_port:7002 
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:322603
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:6fd4b438b7f5735e4ff9e0c28e49b9491057834f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:322603
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:252548
repl_backlog_histlen:70056
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值