手動搭建redis集群(3台)


安裝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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM