想跑一下Redis集羣,可是沒有那麼多服務器,因此使用僞分佈式模式,模擬一下,記錄一下安裝過程。node
軟件: redis-3.0.3.tar.gzredis
集羣正常工做至少須要3個主節點(本示例建立6個節點,3主3從節點)ruby
安裝前提,要有ruby、rubygems環境,先安裝一下:服務器
#安裝ruby yum --nogpgcheck -y install ruby yum --nogpgcheck -y install ruby-devel.x86_64 #安裝rubygem yum --nogpgcheck -y install rubygems #安裝Ruby的redis接口gem-redis
安裝Redis軟件app
#進入安裝目錄 cd /usr/local/ rm -rf redis rm -rf redis-3.0.3 #解壓 tar -zxvf redis-3.0.3.tar.gz mv redis-3.0.3 redis #進入源碼目錄 cd redis #編譯安裝 make && make install
建立集羣的相關配置less
#建立集羣須要的目錄 mkdir -p /usr/local/redis/cluster/16001/ mkdir -p /usr/local/redis/cluster/16002/ mkdir -p /usr/local/redis/cluster/16003/ mkdir -p /usr/local/redis/cluster/16004/ mkdir -p /usr/local/redis/cluster/16005/ mkdir -p /usr/local/redis/cluster/16006/ #修改配置文件redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16001/redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16002/redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16003/redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16004/redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16005/redis.conf #cp /usr/local/redis/redis.conf /usr/local/redis/cluster/16006/redis.conf
配置文件示例:tcp
daemonize no pidfile /var/run/redis.pid #修改端口 port 16001 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile "" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 #修改成yes appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes #=========================新增=============================== daemonize yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 #========================================================
啓動Redis實例:分佈式
#分別啓動這6個redis實例(此時,節點均以Redis Cluster的方式運行,但並無自動構建集羣,由於還處於「我不認識你,你不屬於我」的狀態,它們每一個都是孤零零的Redis節點,或者說只包含了一個節點的集羣) cd /usr/local/redis/cluster/16001 && redis-server redis.conf > redis.log 2>&1 & cd /usr/local/redis/cluster/16002 && redis-server redis.conf > redis.log 2>&1 & cd /usr/local/redis/cluster/16003 && redis-server redis.conf > redis.log 2>&1 & cd /usr/local/redis/cluster/16004 && redis-server redis.conf > redis.log 2>&1 & cd /usr/local/redis/cluster/16005 && redis-server redis.conf > redis.log 2>&1 & cd /usr/local/redis/cluster/16006 && redis-server redis.conf > redis.log 2>&1 &
##啓動以後使用命令查看redis的啓動狀況
ps -ef|grep redis測試
建立集羣,讓上面的實例互相通信lua
#建立集羣,讓上面的實例互相通信(1 表示爲每一個master分配一個salve) /usr/local/redis/src/redis-trib.rb create --replicas 1 \ 127.0.0.1:16001 \ 127.0.0.1:16002 \ 127.0.0.1:16003 \ 127.0.0.1:16004 \ 127.0.0.1:16005 \ 127.0.0.1:16006
集羣建立完成,可使用下面的命令查看相關信息:
#查看集羣目前情況 redis-cli -c -p 16001 #打印集羣的信息 cluster info #列出集羣當前已知的全部節點(node),以及這些節點的相關信息。 cluster nodes
輸出的信息:
127.0.0.1:16002 master - 0 1439345771781 2 connected 5461-10922 127.0.0.1:16003 master - 0 1439345772286 3 connected 10923-16383 127.0.0.1:16001 myself,master - 0 0 1 connected 0-5460 127.0.0.1:16004 slave 2566ea486fc30c911aafaf1b71130fd24a38dba9 0 1439345772791 4 connected 127.0.0.1:16005 slave edea1e50bd224c6895b1904bce79e83fa07d6017 0 1439345772286 5 connected 127.0.0.1:16006 slave ab06e033698627ce0ecd4c8c645585a1ae70cc84 0 1439345771276 6 connected
從上面的信息能夠明顯看到哪些是主節點、從節點。
#將節點的配置文件保存到硬盤裏
cluster saveconfig
#------------------------測試-----------------------------------
#鏈接端口(可使用其餘端口測試16001 -> 16006)
redis-cli -c -p 16001
127.0.0.1:16001> set key001 v001
127.0.0.1:16001> get key001
正常輸出信息的話,集羣創建成功,集羣搭建完成。
祝好運!!