繼續上一章的學習,因爲以前安裝的是ubuntu倉庫中的redis-server,致使不少命令都沒有,因此專門卸載掉了,從官網從新下載了一個解壓版,也推薦你們使用解壓版。node
集羣的配置有兩種方式,咱們逐一學習
1、使用redis-trib.rb
首先在redis目錄下新建文件夾cluster-test,因爲搭建集羣最少須要6個節點,咱們在文件夾下建立6個文件夾,以端口命名redis
給每一個文件夾中拷貝redis-server和redis.conf文件ubuntu
其中的appendonly.aof、dump.rdb、nodes.conf文件是啓動後自動生成的bash
redis.conf文件修改以下app
port 7000 pidfile /var/run/redis_7000.pid appendonly yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000
而後將服務所有啓動學習
啓動後能夠看到以下輸出,表明配置成功測試
14287:M 06 May 11:36:01.890 * No cluster configuration found, I'm b1f810c73555ca5de844b15eb5e87d2ba304090c
接下來建立集羣,這裏用到了redis-trib.rb文件code
執行命令,等待命令執行完成,集羣就建立成功了server
./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \ 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
下面咱們測試一下,隨便連接到一個master的客戶端上,進行的操做會自動定位到對於的實例上面get
redis-cli -c -p 7000 127.0.0.1:7000> set foo bar -> Redirected to slot [12182] located at 127.0.0.1:7002 OK 127.0.0.1:7002> set hello world -> Redirected to slot [866] located at 127.0.0.1:7000 OK 127.0.0.1:7000> get foo -> Redirected to slot [12182] located at 127.0.0.1:7002 "bar" 127.0.0.1:7002> get hello -> Redirected to slot [866] located at 127.0.0.1:7000 "world"
測試下連接到7001上面,同樣執行成功
redis-cli -c -p 7001 127.0.0.1:7001> set test1 anwser1 -> Redirected to slot [4768] located at 127.0.0.1:7000 OK 127.0.0.1:7000> get test1 "anwser1" 127.0.0.1:7000> set test2 anwser2 -> Redirected to slot [8899] located at 127.0.0.1:7001 OK 127.0.0.1:7001> get test2 "anwser2" 127.0.0.1:7001> get foo -> Redirected to slot [12182] located at 127.0.0.1:7002 "bar" 127.0.0.1:7002> get hello -> Redirected to slot [866] located at 127.0.0.1:7000 "world"
2、使用create-cluster腳本建立集羣
進入redis文件夾下的utils>create-cluster目錄下,執行命令
create-cluster create
會自動建立一套集羣,默認端口是從30001-30006
一樣執行啓動和中止操做
create-cluster start create-cluster stop