前提:
redis環境默認搭建完畢,目錄結構如下:
utils,redis-cluster不用在意,後面說。
1,創建目錄(redis-cluster) 然後再下面創建6個文件夾,
mkdir 7001 7002 7003 7004 7005 7006
2,創建redis.conf文件
拷貝etc下面的redis.conf,稍作修改,分別放在對應的目錄下面(主要是根據端口號)
port 7001 #端口
cluster-enabled yes #啓用集羣模式
cluster-config-file nodes.conf
cluster-node-timeout 5000 #超時時間
appendonly yes
daemonize yes #後臺運行
protected-mode no #非保護模式
pidfile /var/run/redis_7001.pid
#如果要設置密碼需要增加如下配置:
#requirepass xxx (設置redis訪問密碼)
#masterauth xxx (設置集羣節點間訪問密碼,跟上面一致)
注意如果配置了password,後面使用redis-cli 創建集羣的時候需要添加參數./redis-cli -a xxx
3,啓動6個節點
./redis-server ../redis-cluster/7001/redis.conf
./redis-server ../redis-cluster/7002/redis.conf
……
./redis-server ../redis-cluster/7006/redis.conf
4,啓動集羣
./redis-cli -a xxx --cluster create 192.168.2.40:7001 192.168.2.40:7002 192.168.2.40:7003 192.168.2.40:7004 192.168.2.40:7005 192.168.2.40:7006 --cluster-replicas 1
啓動成功後顯示如下信息:
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.2.40:7004 to 192.168.2.40:7001
Adding replica 192.168.2.40:7005 to 192.168.2.40:7002
Adding replica 192.168.2.40:7006 to 192.168.2.40:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 191c645200a8b4d267f71e3354c8248dbb533dde 192.168.2.40:7001
slots:[0-5460] (5461 slots) master
M: 400a08d4e5a534c1b609988105d3e045395fbd12 192.168.2.40:7002
slots:[5461-10922] (5462 slots) master
M: 684f6aa0fbccda295ce6818a8c01ee7255a7b002 192.168.2.40:7003
slots:[10923-16383] (5461 slots) master
S: f2701549ae98315b432d73b49d139ee77d5685b4 192.168.2.40:7004
replicates 684f6aa0fbccda295ce6818a8c01ee7255a7b002
S: 9fdc1e375436767ab815cbddd3df674f3bc2ca99 192.168.2.40:7005
replicates 191c645200a8b4d267f71e3354c8248dbb533dde
S: e7742888ed85b37cff4a98e861e99bb16e8bae2c 192.168.2.40:7006
replicates 400a08d4e5a534c1b609988105d3e045395fbd12
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.2.40:7001)
M: 191c645200a8b4d267f71e3354c8248dbb533dde 192.168.2.40:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 684f6aa0fbccda295ce6818a8c01ee7255a7b002 192.168.2.40:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9fdc1e375436767ab815cbddd3df674f3bc2ca99 192.168.2.40:7005
slots: (0 slots) slave
replicates 191c645200a8b4d267f71e3354c8248dbb533dde
S: e7742888ed85b37cff4a98e861e99bb16e8bae2c 192.168.2.40:7006
slots: (0 slots) slave
replicates 400a08d4e5a534c1b609988105d3e045395fbd12
M: 400a08d4e5a534c1b609988105d3e045395fbd12 192.168.2.40:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: f2701549ae98315b432d73b49d139ee77d5685b4 192.168.2.40:7004
slots: (0 slots) slave
replicates 684f6aa0fbccda295ce6818a8c01ee7255a7b002
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
一些集羣操作命令:
使用集羣方式鏈接客戶端:
./redis-cli -c -h 127.0.0.1 -p 7002
在集羣中查看集羣信息:
cluster info
cluster nodes
關閉某個集羣:
./redis-cli -c -h 127.0.0.1 -p 7001 shutdown
集羣工具:create-cluster
在源碼的utils下面 有個create-cluster.可用於集羣的關閉與重啓。
但是要編寫一下里面的命令,因爲你的目錄與文件名可能和他的demo不一致。
主要修改 路徑,文件名端口等信息。