Linux环境下Redis集群实践
生活随笔
收集整理的這篇文章主要介紹了
Linux环境下Redis集群实践
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
環(huán)境:centos 7
一、編譯及安裝redis源碼
源碼地址:redis版本發(fā)布列表
cd redis-3.2.8 sudo make && make install二、創(chuàng)建節(jié)點(diǎn)
?創(chuàng)建Redis-Cluster 文件夾,并創(chuàng)建7000-7005共6個(gè)文件夾
?
復(fù)制源碼中的redis.conf文件并修改以下內(nèi)容后分別放入7000-7005文件夾
【7000】示例
bind 192.168.1.105 port 7000 cluster-enabled yes cluster-config-file /home/jabben/Redis-Cluster/7000/nodes-7000.confpidfile /home/jabben/Redis-Cluster/7000/redis_7000.pid cluster-node-timeout 5000 daemonize yes
分別啟動(dòng)6個(gè)Redis節(jié)點(diǎn):
redis-server 7000/redis.conf redis-server 7001/redis.conf redis-server 7002/redis.conf redis-server 7003/redis.conf redis-server 7004/redis.conf redis-server 7005/redis.conf查看redis進(jìn)程是否已經(jīng)啟動(dòng):
ps -ef | grep redis三、創(chuàng)建集群
?復(fù)制redis-trib.rb文件到bin目錄下
cd /home/jabben/Redis-Cluster/Source/redis-3.2.8/src sudo cp redis-trib.rb /usr/local/bin/安裝ruby環(huán)境
yum install ruby安裝rubygems組件
yum install rubygems安裝gem-redis
gem install redis創(chuàng)建集群
redis-trib.rb create --replicas 1 192.168.1.105:7000 192.168.1.105:7001 192.168.1.105:7002 192.168.1.105:7003 192.168.1.105:7004 192.168.1.105:7005 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.1.105:7000 192.168.1.105:7001 192.168.1.105:7002 Adding replica 192.168.1.105:7003 to 192.168.1.105:7000 Adding replica 192.168.1.105:7004 to 192.168.1.105:7001 Adding replica 192.168.1.105:7005 to 192.168.1.105:7002 M: 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd 192.168.1.105:7000slots:0-5460 (5461 slots) master M: 7bd0b20971d5fc929f8454c6ff388f780cc70e77 192.168.1.105:7001slots:5461-10922 (5462 slots) master M: aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c 192.168.1.105:7002slots:10923-16383 (5461 slots) master S: e731c1918fad61f972fbca4ab6523da1d9f827af 192.168.1.105:7003replicates 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd S: c16b520a5075b0fee0840cc26cf1f7f27bad8e45 192.168.1.105:7004replicates 7bd0b20971d5fc929f8454c6ff388f780cc70e77 S: 990a4e7a2831d8d9ba8f9835557be6a394d393a4 192.168.1.105:7005replicates aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c 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.105:7000) M: 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd 192.168.1.105:7000slots:0-5460 (5461 slots) master1 additional replica(s) S: 990a4e7a2831d8d9ba8f9835557be6a394d393a4 192.168.1.105:7005slots: (0 slots) slavereplicates aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c S: e731c1918fad61f972fbca4ab6523da1d9f827af 192.168.1.105:7003slots: (0 slots) slavereplicates 339aafedd6bd7f6bd5cd98407f7ebe8cb0c4efbd M: aaa4b5109c5bcf1aa868621f59b51fb4d6feef9c 192.168.1.105:7002slots:10923-16383 (5461 slots) master1 additional replica(s) S: c16b520a5075b0fee0840cc26cf1f7f27bad8e45 192.168.1.105:7004slots: (0 slots) slavereplicates 7bd0b20971d5fc929f8454c6ff388f780cc70e77 M: 7bd0b20971d5fc929f8454c6ff388f780cc70e77 192.168.1.105:7001slots:5461-10922 (5462 slots) master1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.至此集群搭建完畢
四、測(cè)試
使用redis-cli客戶端連接到剛創(chuàng)建的集群
redis-cli -c -h 192.168.1.105 -p 7000查詢集群相關(guān)信息
192.168.1.105: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:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_sent:2645 cluster_stats_messages_received:2645【數(shù)據(jù)寫入測(cè)試】
192.168.1.105:7000> set name zhangsan -> Redirected to slot [5798] located at 192.168.1.105:7001 OK 192.168.1.105:7001> get name "zhangsan"【集群線性擴(kuò)展】
待完善
【失效轉(zhuǎn)移】
待完善
五、集群相關(guān)常用命令?
鏈接:https://redis.io/commands
CLUSTER INFO 打印集群的信息 CLUSTER NODES 列出集群當(dāng)前已知的所有節(jié)點(diǎn)(node),以及這些節(jié)點(diǎn)的相關(guān)信息。 //節(jié)點(diǎn) CLUSTER MEET <ip> <port> 將 ip 和 port 所指定的節(jié)點(diǎn)添加到集群當(dāng)中,讓它成為集群的一份子。 CLUSTER FORGET <node_id> 從集群中移除 node_id 指定的節(jié)點(diǎn)。 CLUSTER REPLICATE <node_id> 將當(dāng)前節(jié)點(diǎn)設(shè)置為 node_id 指定的節(jié)點(diǎn)的從節(jié)點(diǎn)。 CLUSTER SAVECONFIG 將節(jié)點(diǎn)的配置文件保存到硬盤里面。 CLUSTER ADDSLOTS <slot> [slot ...] 將一個(gè)或多個(gè)槽(slot)指派(assign)給當(dāng)前節(jié)點(diǎn)。 CLUSTER DELSLOTS <slot> [slot ...] 移除一個(gè)或多個(gè)槽對(duì)當(dāng)前節(jié)點(diǎn)的指派。 CLUSTER FLUSHSLOTS 移除指派給當(dāng)前節(jié)點(diǎn)的所有槽,讓當(dāng)前節(jié)點(diǎn)變成一個(gè)沒(méi)有指派任何槽的節(jié)點(diǎn)。 CLUSTER SETSLOT <slot> NODE <node_id> 將槽 slot 指派給 node_id 指定的節(jié)點(diǎn)。 CLUSTER SETSLOT <slot> MIGRATING <node_id> 將本節(jié)點(diǎn)的槽 slot 遷移到 node_id 指定的節(jié)點(diǎn)中。 CLUSTER SETSLOT <slot> IMPORTING <node_id> 從 node_id 指定的節(jié)點(diǎn)中導(dǎo)入槽 slot 到本節(jié)點(diǎn)。 CLUSTER SETSLOT <slot> STABLE 取消對(duì)槽 slot 的導(dǎo)入(import)或者遷移(migrate)。 //鍵 CLUSTER KEYSLOT <key> 計(jì)算鍵 key 應(yīng)該被放置在哪個(gè)槽上。 CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的鍵值對(duì)數(shù)量。 CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 個(gè) slot 槽中的鍵。 //新增 CLUSTER SLAVES node-id 返回一個(gè)master節(jié)點(diǎn)的slaves 列表?
轉(zhuǎn)載于:https://www.cnblogs.com/Jabben_Yi/p/6696837.html
總結(jié)
以上是生活随笔為你收集整理的Linux环境下Redis集群实践的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 4.27下午
- 下一篇: Flume的Collector