用兩臺服務器模擬6個節點,一臺機器3個節點,創建出3 master、3 salve 環境。
redis 採用 redis-3.2.6 版本。
安裝過程
1. 下載並解壓
到redis官網下載 redis-3.2.6.tar.gz
http://download.redis.io/releases/redis-3.2.6.tar.gz
[weixin@i-2-1366-VM ~]$ tar -zxvf redis-3.2.6.tar.gz
2.編譯安裝
[weixin@i-2-1366-VM ~]$ mv redis-3.2.6 redis_rb
[weixin@i-2-1366-VM ~]$ cd redis_rb
[weixin@i-2-1366-VM redis_rb]$ make && make install
3.創建 Redis 節點
首先在 10.153.68.6 機器上 redis_rb 目錄下創建 redis_cluster 目錄;
[weixin@i-2-1366-VM redis_rb]$ mkdir redis_cluster
[weixin@i-2-1366-VM redis_rb]$ cd redis_cluster/
在 redis_cluster 目錄下,創建名爲7000、7001、7002的目錄,並將 redis.conf 拷貝到這三個目錄中
[weixin@i-2-1366-VM redis_cluster]$ mkdir 7003 7004 7005
[weixin@i-2-1366-VM redis_cluster]$ vi 7003/redis.conf
[weixin@i-2-1366-VM redis_cluster]$ cp 7003/redis.conf 7004
[weixin@i-2-1366-VM redis_cluster]$ cp 7003/redis.conf 7005
[weixin@i-2-1366-VM redis_cluster]$ vi 7004/redis.conf
[weixin@i-2-1366-VM redis_cluster]$ vi 7005/redis.conf
分別修改這三個配置文件,修改如下內容
port 7000 //端口7000,7002,7003
bind 本機ip //默認ip爲127.0.0.1 需要改爲其他節點機器可訪問的ip 否則創建集羣時無法訪問對應的端口,無法創建集羣
daemonize yes //redis後臺運行
pidfile /var/run/redis_7000.pid //pidfile文件對應7000,7001,7002
cluster-enabled yes //開啓集羣 把註釋#去掉
cluster-config-file nodes_7000.conf //集羣的配置 配置文件首次啓動自動生成 7000,7001,7002
cluster-node-timeout 15000 //請求超時 默認15秒,可自行設置
appendonly yes //aof日誌開啓 有需要就開啓,它會每次寫操作都記錄一條日誌
4.啓動各個節點
[weixin@i-2-1366-VM redis_rb]$ ./src/redis-server ./redis_cluster/7003/redis.conf
[weixin@i-2-1366-VM redis_rb]$ ./src/redis-server ./redis_cluster/7004/redis.conf
[weixin@i-2-1366-VM redis_rb]$ ./src/redis-server ./redis_cluster/7005/redis.conf
接着在另外一臺機器上(10.153.68.4),的操作重複以上三步,只是把目錄改爲7003、7004、7005,對應的配置文件也按照這個規則修改即可
5.檢查啓動情況
[weixin@i-2-1366-VM redis_rb]$ netstat -tlnp | grep redis
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 10.153.86.4:7005 0.0.0.0:* LISTEN 4764/./src/redis-se
tcp 0 0 10.153.86.4:17003 0.0.0.0:* LISTEN 4742/./src/redis-se
tcp 0 0 10.153.86.4:6379 0.0.0.0:* LISTEN 40857/./src/redis-s
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 40857/./src/redis-s
tcp 0 0 10.153.86.4:17004 0.0.0.0:* LISTEN 4753/./src/redis-se
tcp 0 0 10.153.86.4:17005 0.0.0.0:* LISTEN 4764/./src/redis-se
tcp 0 0 10.153.86.4:7003 0.0.0.0:* LISTEN 4742/./src/redis-se
tcp 0 0 10.153.86.4:7004 0.0.0.0:* LISTEN 4753/./src/redis-se
[weixin@i-2-1366-VM redis_rb]$ ps -aux|grep 'redis'
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
weixin 4742 0.0 0.0 133528 2412 ? Ssl 20:06 0:00 ./src/redis-server 10.153.86.4:7003 [cluster]
weixin 4753 0.0 0.0 133528 2416 ? Ssl 20:06 0:00 ./src/redis-server 10.153.86.4:7004 [cluster]
weixin 4764 0.0 0.0 133528 2416 ? Ssl 20:06 0:00 ./src/redis-server 10.153.86.4:7005 [cluster]
6.創建集羣
Redis 官方提供了 redis-trib.rb 這個工具,就在解壓目錄的 src 目錄中,第三步中已將它複製到 /usr/local/bin 目錄中,可以直接在命令行中使用了。使用下面這個命令即可完成安裝。
[weixin@ZYC-NLKFPT-CPQDGL01 redis]$ ./src/redis-trib.rb create --replicas 1 10.153.86.6:7000 10.153.86.6:7001 10.153.86.6:7002 10.153.86.4:7003 10.153.86.4:7004 10.153.86.4:7005
/usr/bin/env: ruby: No such file or directory
[weixin@ZYC-NLKFPT-CPQDGL01 redis]$ ./src/redis-trib.rb create --replicas 1 10.153.86.6:7000 10.153.86.6:7001 10.153.86.6:7002 10.153.86.4:7003 10.153.86.4:7004 10.153.86.4:7005
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from ./src/redis-trib.rb:25
https://rubygems.org/gems/redis/versions/4.0.0.rc1
[zwq18719128666@ZYC-NLKFPT-CPQDGL01 ~]$ sudo gem install redis --pre
Successfully installed redis-4.0.0.rc1
1 gem installed
Installing ri documentation for redis-4.0.0.rc1...
Installing RDoc documentation for redis-4.0.0.rc1...
[root@ZYC-NLKFPT-CPQDGL01 redis]# ./src/redis-trib.rb create --replicas 1 10.153.86.6:7000 10.153.86.6:7001 10.153.86.6:7002 10.153.86.4:7003 10.153.86.4:7004 10.153.86.4:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.153.86.4:7003
10.153.86.6:7000
10.153.86.4:7004
Adding replica 10.153.86.6:7001 to 10.153.86.4:7003
Adding replica 10.153.86.4:7005 to 10.153.86.6:7000
Adding replica 10.153.86.6:7002 to 10.153.86.4:7004
M: e65f5f5caf941a4b74284770dbdf26ae15b40f68 10.153.86.6:7000
slots:5461-10922 (5462 slots) master
S: 0e8729a859497c59e8c0dc25b6c2c6ab49c41d11 10.153.86.6:7001
replicates 450ab47539885de9ae9875fb369390ce2d7bbea7
S: 674a1901b20246936bc64a7bd1182467e085cdf6 10.153.86.6:7002
replicates c94f817cd2f75b3ff6a5554c9c6eb5f0e631dcf5
M: 450ab47539885de9ae9875fb369390ce2d7bbea7 10.153.86.4:7003
slots:0-5460 (5461 slots) master
M: c94f817cd2f75b3ff6a5554c9c6eb5f0e631dcf5 10.153.86.4:7004
slots:10923-16383 (5461 slots) master
S: 87f4c5f0de38882c683f04e6a3dc4ecfc9537aa5 10.153.86.4:7005
replicates e65f5f5caf941a4b74284770dbdf26ae15b40f68
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 10.153.86.6:7000)
M: e65f5f5caf941a4b74284770dbdf26ae15b40f68 10.153.86.6:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 674a1901b20246936bc64a7bd1182467e085cdf6 10.153.86.6:7002
slots: (0 slots) slave
replicates c94f817cd2f75b3ff6a5554c9c6eb5f0e631dcf5
S: 87f4c5f0de38882c683f04e6a3dc4ecfc9537aa5 10.153.86.4:7005
slots: (0 slots) slave
replicates e65f5f5caf941a4b74284770dbdf26ae15b40f68
M: c94f817cd2f75b3ff6a5554c9c6eb5f0e631dcf5 10.153.86.4:7004
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 0e8729a859497c59e8c0dc25b6c2c6ab49c41d11 10.153.86.6:7001
slots: (0 slots) slave
replicates 450ab47539885de9ae9875fb369390ce2d7bbea7
M: 450ab47539885de9ae9875fb369390ce2d7bbea7 10.153.86.4:7003
slots:0-5460 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
到此,集羣已經部署完成。