歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux集群 >> Redis linux集群部署

Redis linux集群部署

日期:2017/3/3 12:18:31   编辑:Linux集群

Redis linux集群部署

剛接觸Redis不久,就像動手搭個集群玩玩,使用linux系統也不是很熟練,但是強大的好奇心還是推著自己去嘗試,還好找度娘不是很坑,大神們寫的文章很詳細,用了一下午的時間總算成功了,感謝那些無私分享的人。接下來分享一下我的搭建過程。

環境介紹

linux系統虛擬機ubuntu 15.0.4
redis 3.2.0

准備工作

終端下載redis 壓縮包(默認下載到root根目錄)
wget http://download.redis.io/releases/redis-3.2.0.tar.gz 也可以直接訪問
http://download.redis.io/releases/ 查看有哪些版本 我下載最新版本是3.3.0 不想下載最新的有強迫症
系統一些依賴包
安裝gcc:apt-get install gcc 安裝zlib:yum install zib(這個我沒裝成功但是好像沒影響)
安裝ruby:apt-get install ruby 安裝rubygems:yum install rubygems 安裝gem
redis:(下載:http://rubygems.org/gems/redis/versions/) gem install redis
Successfully installed redis-3.3.0 1 gem installed Installing ri
documentation for redis-3.3.0… Installing RDoc documentation for
redis-3.3.0… (安裝會默認下載最新版本的 想下redis-3.2.0的還沒有,悲哀呀!但是不影響)
系統參數配置(完全抄別人的 我沒配置以後用到在配置)
[code]修改open files:# ulimit -n  10032 (默認1024)
添加vm.overcommit_memory=1:
vi /etc/sysctl.conf
sysctl vm.overcommit_memory=1
關閉hugepage:
 echo never > /sys/kernel/mm/transparent_hugepage/enabled
修改somaxconn :
 echo 511 >/proc/sys/net/core/somaxconn
關閉防火牆:# service iptables stop
關閉selinux:# vi /etc/sysconfig/selinux  改“SELINUX=disabled”

開始安裝

[code]解壓redis-3.2.0.tar.gz
tar -zxvf redis-3.2.0.tar.gz
/root/根目錄下回默認生成文件夾redis-3.2.0
#cd /redis/redis-3.2.0
#make (編譯)
#make install
 (貌似我編譯完了就已經安裝了,在執行install這個的時候沒啥用,糾結是不是有問題,結果執行了一下make test 時間很長,我就去吃了點東西,回來確實沒問題,如果有類似情況的小伙伴不用test了應該沒問題直接往下走)

復制命令到 /usr/local/bin/ (為了快捷執行,復制前先看一下文件bin目錄下 我下邊已經有前三個的命令了 就只cp了redis-trib.rb)
[code] # cp /redis/redis-3.0.0/src/redis-trib.rb /usr/local/bin/
 # cp redis-cli /usr/local/bin/
 # cp redis-server /usr/local/bin/
 # which redis-trib.rb
 /usr/local/bin/redis-trib.rb

配置集群配置文件

要新建7個配置文件 其中一個是通用的配置 其他6個是集群3個master節點 3個slave 節點 推薦配置至少是6個節點 3主3從。
首先創建一個config配置目錄
mkdir /root/redis-3.2.0/config
nano /root/redis-3.2.0/config/redis-common.conf
配置信息如下:
[code] #GENERAL 
daemonize yes 
tcp-backlog 511 
timeout 0 
tcp-keepalive 0 
loglevel notice 
databases 16 
dir /redis/redis-3.0.0/data 
slave-serve-stale-data yes 
slave-read-only yes 
#not use default 
repl-disable-tcp-nodelay yes 
slave-priority 100 
appendonly yes 
appendfsync everysec 
no-appendfsync-on-rewrite yes 
auto-aof-rewrite-min-size 64mb 
lua-time-limit 5000 
cluster-enabled yes 
cluster-node-timeout 15000 
cluster-migration-barrier 1 
slowlog-log-slower-than 10000 
slowlog-max-len 128 
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 
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
#這倆個配置是我遇到的問題 第一個是關閉保護模式如果不配置在其他機器無法用jedis客戶端連接時會報錯並提示你關閉或者綁定訪問者的ip地址還有設置密碼,我是測試選擇最簡單的,綁定密碼或ip是靠譜的
protected-mode no
#這個不是綁定訪問者的ip地址 是對自己集群綁定的ip地址默認是127.0.0.1 如果不改變客戶端在配置多個集群節點時會出現Too many Cluster redirections? 異常
bind 192.168.1.181 (虛擬機的外網地址)

創建6個端口號命名的文件 只需要把文件裡的所有端口號的數字替換就行
redis-6379.conf
redis-6389.conf
redis-6399.conf
redis-7379.conf
redis-7389.conf
redis-7399.conf
[code]include /root/redis-3.2.0/config/redis-common.conf
port 7379
logfile "/root/redis-3.2.0/log/redis-7379.log"
maxmemory 100m
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
maxmemory-policy allkeys-lru
appendfilename "appendonly-7379.aof"
dbfilename dump-7379.rdb
#dir /root/redis-3.2.0/data
cluster-config-file nodes-7379.conf
auto-aof-rewrite-percentage 80-100

啟動redis節點
[code]# redis-server redis-6379.conf
# redis-server redis-6389.conf
# redis-server redis-6399.conf
# redis-server redis-7379.conf
# redis-server redis-7389.conf
# redis-server redis-7399.conf

# ps -ef | grep redis
root 4746  1261  0 21:33 ?  00:00:04 redis-server 192.168.1.181:6379 [cluster]
root 4750  1261  0 21:33 ?  00:00:04 redis-server 192.168.1.181:6389 [cluster]
root 4754  1261  0 21:33 ? 00:00:04 redis-server 192.168.1.181:6399 [cluster]
root 4758  1261  0 21:33 ? 00:00:04 redis-server 192.168.1.181:7379 [cluster]
root 4762  1261  0 21:33 ? 00:00:04 redis-server 192.168.1.181:7399 [cluster]
root 4766  1261  0 21:33 ? 00:00:04 redis-server 192.168.1.181:7389 [cluster]

通過redis-trib創建cluster
–replicas 則指定了為Redis Cluster中的每個Master節點配備幾個Slave 節點
[code]# redis-trib.rb create --replicas 1 192.168.1.181:6379 192.168.1.181:6389 192.168.1.181:6399 192.168.1.181:7379 192.168.1.181:7389 192.168.1.181:7399
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.181:6379
192.168.1.181:6389
192.168.1.181:6399
Adding replica 192.168.1.181:7379 to 192.168.1.181:6379
Adding replica 192.168.1.181:7389 to 192.168.1.181:6389
Adding replica 192.168.1.181:7399 to 192.168.1.181:6399
M: 05fe758161e2cbe23240697f47f1cd2c937a675b 192.168.1.181:6379
   slots:0-5460 (5461 slots) master
M: d1d124d35c848e9c8e726b59af669c9196557869 192.168.1.181:6389
   slots:5461-10922 (5462 slots) master
M: d64223d6695fcc7e1030f219f09d7488c438cf39 192.168.1.181:6399
   slots:10923-16383 (5461 slots) master
S: 7f77ec03e40d0cc9f343d783a293ae8aa6c6e090 192.168.1.181:7379
   replicates 05fe758161e2cbe23240697f47f1cd2c937a675b
S: 98dae5126228dea54d1321eeb357d8773bd2ee11 192.168.1.181:7389
   replicates d1d124d35c848e9c8e726b59af669c9196557869
S: d013aee7cae8163f787cb6445778ff97bf66ce17 192.168.1.181:7399
   replicates d64223d6695fcc7e1030f219f09d7488c438cf39
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.181:6379)
M: 7191b97cf9445e6e6ebaf39a4553485115ca3b97 192.168.1.181:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 9ce60bf9b8019fa55050a35cd386792e6f6bb099 192.168.1.181:7399
   slots: (0 slots) slave
   replicates 85136d72df79e0f5dda3e0efeb8bf760eb883b61
M: 85136d72df79e0f5dda3e0efeb8bf760eb883b61 192.168.1.181:6399
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 8c063f6bfd9faa655aba2c110a5c7339f5354d2d 192.168.1.181:6389
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 5a2ddc610989980701a86e0d66a9fd69b2fd8b39 192.168.1.181:7379
   slots: (0 slots) slave
   replicates 7191b97cf9445e6e6ebaf39a4553485115ca3b97
S: 4a6abe2e57fdf5809ae52a71908275ba6287335d 192.168.1.181:7389
   slots: (0 slots) slave
   replicates 8c063f6bfd9faa655aba2c110a5c7339f5354d2d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

檢查集群狀態
連接任意節點,執行redis-trib.rb
redis-trib.rb check 192.168.1.181:6379
[code]>>> Performing Cluster Check (using node 192.168.1.181:6379)
M: 7191b97cf9445e6e6ebaf39a4553485115ca3b97 192.168.1.181:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 9ce60bf9b8019fa55050a35cd386792e6f6bb099 192.168.1.181:7399
   slots: (0 slots) slave
   replicates 85136d72df79e0f5dda3e0efeb8bf760eb883b61
M: 85136d72df79e0f5dda3e0efeb8bf760eb883b61 192.168.1.181:6399
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 8c063f6bfd9faa655aba2c110a5c7339f5354d2d 192.168.1.181:6389
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 5a2ddc610989980701a86e0d66a9fd69b2fd8b39 192.168.1.181:7379
   slots: (0 slots) slave
   replicates 7191b97cf9445e6e6ebaf39a4553485115ca3b97
S: 4a6abe2e57fdf5809ae52a71908275ba6287335d 192.168.1.181:7389
   slots: (0 slots) slave
   replicates 8c063f6bfd9faa655aba2c110a5c7339f5354d2d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

到這裡集群就算配置成功了
可以啟動一個本地客戶端操作一下
一定要加-c 才是啟動集群的配置呢 指定host -h 指定端口 -p
[code]# redis-cli -c -h 192.168.1.181 -p 6379
192.168.1.181:6379> set key001 helloReids
-> Redirected to slot [12657] located at 192.168.1.181:6399
OK  //這裡默認分配槽點將此鍵值分配到了6399 的端口上 這個是自動切換
192.168.1.181:6399> get key001
"helloReids"
192.168.1.181:6399>

說明集群部署成功了!
接下來在Java客戶端 用jedis 建立集群客戶端獲取key001 是否好使
這裡我實在自己的主機上連接虛擬機相當於外網訪問。
[code] private static JedisCluster jc;  
        static {  
             //只給集群裡一個實例就可以  
              Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();  
              jedisClusterNodes.add(new HostAndPort("192.168.1.181", 6379));  
              jedisClusterNodes.add(new HostAndPort("192.168.1.181", 6389));  
              jedisClusterNodes.add(new HostAndPort("192.168.1.181", 6399));  
              jedisClusterNodes.add(new HostAndPort("192.168.1.181", 7379));  
              jedisClusterNodes.add(new HostAndPort("192.168.1.181", 7389));  
              jedisClusterNodes.add(new HostAndPort("192.168.1.181", 7399));  

              jc = new JedisCluster(jedisClusterNodes);  
          } 
        @Test
        public void RedisClusterDemo(){
            //JedisCluster jc =RedisClient.getRedisClient().getJedisCluster();
            System.out.println(jc.get("key001"));
            //輸出helloRedis
        }

再補充一點:如何關閉集群 一個一個端口的關閉
下次再啟動時依然是集群方式的啟動
[code]redis-cli -h 192.168.1.181 -p 6379 shutdown

開始的時候服務器端配置綁定的127.0.0.1 結果用本機Java jedis訪問時出錯了,浪費了些時間,查到解決方案後,同學來了一起出去撸串一番。回來還是迫不及待的就把它弄好。
到此就算集群初步成功了,有點小興奮,哈哈!
有朋友,有啤酒,有喜歡的東西,有向往的追求,足以!
參考:
http://blog.sina.com.cn/s/blog_75ad98f30102w6po.html
http://www.tuicool.com/articles/36F7faJ
Copyright © Linux教程網 All Rights Reserved