安装redis
- 1.搜索源中的redis包 apt-cache pkgnames | grep redis
- 2.安装redis-server apt-get install redis-server
根据端口号配置redis并开启集群配置
- 1.复制redis配置文件 cp redis.conf nodes7000.conf
- 2.搜索cluster-enabled变量 并设置为 yes 开启集群配置;
- 3.搜索cluster-config-file变量 并设置为 nodes-7000.conf 每个redis-cluster开启之后,都会有一个自己的配置文件
- 4.重复上面的步骤且修改对应的端口号为7001和7002
分别开启不同端口号的redis-server
- redis-server node7000.conf
- 查看进程是否存在
root@ubuntu:/etc/redis# ps -aux | grep redis-server
root 4886 0.1 0.8 40136 8556 ? Ssl 16:26 0:05 redis-server *:7000 [cluster]
redis-server 握手
127.0.0.1:7000> cluster meet your_IP 7001
OK
your_IP如果使用127.0.0.1的话,远程连接会有问题,如果连接的redis没有维护对应的key 则会返回正确的ip和端口号。如果设置为127.0.0.1的话 就会返回127.0.0.1,这样就连接不上远程redis-server了
cluster info命令 查看集群状态 cluster_known_nodes:3 表示有3个节点连接到集群内了
cluster_state:fail
cluster_slots_assigned:0 分配的槽位为0
127.0.0.1:7000> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:0
cluster_current_epoch:1
cluster_my_epoch:0
cluster_stats_messages_sent:1111
cluster_stats_messages_received:1095
分配槽位
redis-cli -h your_IP -p 7000 cluster addslots {0..5461}
redis-cli -h your_IP -p 7001 cluster addslots {5462..10922}
redis-cli -h your_IP -p 7002 cluster addslots {10923..16383}
查看信息
cluster_state 为ok
cluster_slots_assigned 槽位也已经分配完成
192.168.110.134:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:2
cluster_my_epoch:2
cluster_stats_messages_sent:2320
cluster_stats_messages_received:2171
建立从节点
slaveof Your_Master_IP Your_Slave_PORT
从节点日志如下
Connecting to MASTER IP:7000
MASTER <-> SLAVE sync started
Non blocking connect for SYNC fired the event.
Master replied to PING, replication can continue...
Partial resynchronization not possible (no cached master)
Full resync from master: b20678039dab9be4b9ceb58e3a4dc0679d720383:1
MASTER <-> SLAVE sync: receiving 33 bytes from master
MASTER <-> SLAVE sync: Flushing old data
MASTER <-> SLAVE sync: Loading DB in memory
MASTER <-> SLAVE sync: Finished with success
在Slave上查看同步节点状态
127.0.0.1:7000> info replication
# Replication
role:slave
master_host:192.168.232.101
master_port:7000
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:935
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0