Redis-4.X 版本 Redis Cluster集群 (一)


一 創建redis cluster 集群前提條件:

1 )  每個redis node 節點采用相同的硬件配置,相同的密碼.
2 )  每個節點必須開啟的參數:
     cluster-enabled yes                  # 必須開啟集群狀態,開啟后redis進程會有cluster顯示。
     cluster-config-file node-6379.conf   # 此文件由cluster 集群自動創建和維護,不需要任何手動操作。
3 ) 所有redis 服務器必須沒有任何數據,否則會報錯.
4 )  先啟動為單機redis且沒有任何 key value 
5 )  針對redis 3.x、4.x版本ruby和redis-trib.rb,只安裝一台機器能做集群管理即可.

  2.1 )
[root@srv-1 src]# ps -ef | grep redis
root       5378      1  0 08:43 ?        00:00:00 /usr/local/bin/redis-server 0.0.0.0:6379 [cluster]
root       5383   5199  0 08:43 pts/0    00:00:00 grep --color=auto redis

二 生產環境建議6台服務器:

主從 1 主從 2 主從 3
10.10.5.11 10.10.5.12 10.10.5.13
10.10.5.14 10.10.5.15 10.10.5.16
預留位置 預留位置 10.10.5.17

三 安裝redis-4.0.14 版本包:

3.1 )  下載並安裝redis-4.0.14:
cd /data/src/
tar -zxvf redis-4.0.14.tar.gz -C /data/soft/ && mv /data/soft/redis-4.0.14 /data/soft/redis
cd /data/soft/redis
make && make install 

3.2 ) 初始化redis(根據自己的習慣來設置即可):
 ./utils/install_server.sh 
 
 [root@srv-1 redis]# ./utils/install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /etc/redis.conf
Please select the redis log file name [/var/log/redis_6379.log] /data/logs/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] /data/soft/redis/6379data        
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis.conf
Log file       : /data/logs/redis_6379.log
Data dir       : /data/soft/redis/6379data
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.

回車確認 安裝完成.

3.3 )  系統參數修改:
echo "65535" > /proc/sys/net/core/somaxconn
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
sysctl vm.overcommit_memory=1

sysctl -p

3.4 )  重啟redis:
/etc/init.d/redis_6379 restart

3.5 )  安裝Ruby版本高於2.3.0:
yum remove ruby-2.0.0.648
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
tar -zxf ruby-2.5.5.tar.gz && cd ruby-2.5.5
./configure && make && make install 

gem install redis

# 驗證結果(執行/data/soft/redis/src/redis-trib.rb):

[root@srv-1 src]# ./redis-trib.rb 
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN    # 創建集群
                  --replicas <arg>               # 指定Master 的副本數量
  check           host:port                      # 檢查集群,跟着集群里面的任意主機+端口
  info            host:port                      # 查看集群信息
  fix             host:port                      # 修復集群
                  --timeout <arg>
  reshard         host:port                      # 在線熱遷移集群制定主機的slots數據
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port                      # 平衡集群中各主機之間的slots的數量
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
  add-node        new_host:new_port existing_host:existing_port     # 添加主機到集群
                  --slave
                  --master-id <arg>
  del-node        host:port node_id                                 # 從集群內刪除主機
  set-timeout     host:port milliseconds                            # 設置節點的超時時間
  call            host:port command arg arg .. arg                  # 在集群的所有節點上執行命令
  import          host:port                                         # 導入外部redis服務器的數據到當前集群
                  --from <arg>
                  --copy
                  --replace
  help            (show this help)

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.



四 ) redis.conf 配置文件修改:


[root@srv-1 utils]# grep -vE '^#|^$' /etc/redis.conf 
bind 0.0.0.0                                      #  綁定的IP地址
port 6379                                         #  redis綁定的端口
tcp-backlog 511                                   #
timeout 0                                         #  超時時間
tcp-keepalive 300                                 # 
daemonize yes                                     #  是否進程運行
supervised no                                     #  是否交給systemd 來運行redis,否
pidfile /var/run/redis_6379.pid                   #  PID 存放路徑
loglevel notice                                   # 日志級別:debug > verbose > notice > warning
logfile /data/logs/redis_6379.log                 #  log 日志存放路徑,如果不是默認,要有redis的寫入權限
databases 16                                      #  redis 默認開啟多少庫
always-show-logo yes                              #  是否打開logo 寫入日志. 浪費不了多少。
save 900 1                                        #  900秒(15分鍾)有1個寫入及執行bgsave命令保存Disk
save 300 10                                       #  300秒(5分鍾) 有10個寫入及執行bgsave命令保存Disk
save 60 10000                                     #  60秒(1分鍾) 有10000個寫入及執行bgsave命令保存Disk
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb                               #  數據文件名,這里只能是文件名
dir /data/soft/redis/6379data                     #  數據文件 存放目錄,不能有文件名,只能有路徑.            千萬注意
masterauth 123456                                 #  slave 連接master 時需要用到的密碼,否則無法獲取信息     千萬注意
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
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no                                     #  AOF 模式保存日志
appendfilename "appendonly.aof"                   #  AOF 日志文件名
appendfsync everysec                              #  
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
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-size -2
list-compress-depth 0
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

五 ) redis-4.x集群創建:

5.1 ) 創建1個集群:
[root@srv-1 src]# ./redis-trib.rb create --replicas 1 10.10.5.11:6379 10.10.5.12:6379 10.10.5.13:6379 10.10.5.14:6379 10.10.5.15:6379 10.10.5.16:6379 
./redis-trib.rb create --replicas 1 10.10.5.11:6379 10.10.5.12:6379 10.10.5.13:6379 10.10.5.14:6379 10.10.5.15:6379 10.10.5.16:6379 

  5.1.1 )  
    1 節點redis 有密碼需要做如下設置:
    [root@srv-1 src]# vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.4/lib/redis/client.rb 
    #修改里面的:password 選項為 redis 登錄密碼. 
   
    2 節點有數據會報錯,創建集群的節點必須沒有數據,剛開始強調過.
    # 如果有集群存在數據,可以進入redis 執行flushall和bgsave即可,但這個是有風險的會清空redis數據,慎用.
    
    
[root@srv-1 src]# ./redis-trib.rb create --replicas 1 10.10.5.11:6379 10.10.5.12:6379 10.10.5.13:6379 10.10.5.14:6379 10.10.5.15:6379 10.10.5.16:6379 
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.10.5.11:6379
10.10.5.12:6379
10.10.5.13:6379
Adding replica 10.10.5.15:6379 to 10.10.5.11:6379
Adding replica 10.10.5.16:6379 to 10.10.5.12:6379
Adding replica 10.10.5.14:6379 to 10.10.5.13:6379
M: 291faee66636effee60e8409894a4ebae40fe4a6 10.10.5.11:6379
   slots:0-5460 (5461 slots) master
M: 905e3ecb99a4723b12bb5d7fda91372ee72909be 10.10.5.12:6379
   slots:5461-10922 (5462 slots) master
M: 3a311c961450d50f6236d814106fcd0f7723e071 10.10.5.13:6379
   slots:10923-16383 (5461 slots) master
S: bad22bcf6c5c42b13c3692c4d1cfcbde7fd7ce14 10.10.5.14:6379
   replicates 3a311c961450d50f6236d814106fcd0f7723e071
S: 761fbb98a72600b8322fc34e3b2bbf0adbd3822e 10.10.5.15:6379
   replicates 291faee66636effee60e8409894a4ebae40fe4a6
S: 57e6cc3af523aa829705cd0072d6283b988a43c7 10.10.5.16:6379
   replicates 905e3ecb99a4723b12bb5d7fda91372ee72909be
Can I set the above configuration? (type 'yes' to accept):     
...... 集群創建完成......

集群創建信息


安裝ruby問題:

## 問題:
解決ruby版本較低的問題:
[root@srv-1 ~]# gem install redis
Fetching: redis-4.1.4.gem (100%)
ERROR:  Error installing redis:
	redis requires Ruby version >= 2.3.0.

# 解決方法:
yum remove ruby-2.0.0.648
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
tar -zxf ruby-2.5.5.tar.gz && cd ruby-2.5.5
./configure && make && make install 


## redis-trib.rb無法運行的問題:
[root@srv-1 ~]# /data/soft/redis/src/redis-trib.rb 
/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
	from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /data/soft/redis/src/redis-trib.rb:25:in `<main>'
	
# 解決方法:
Ruby的redis接口沒有安裝,需要安裝Redis接口,輸入命令 " gem install redis " 進行安裝:
[root@srv-1 ~]# gem install redis

## 安裝期間出現各種奇葩問題:
解決*** No rule to make target `/include/ruby.h' 報錯
https://blog.csdn.net/ck3207/article/details/90404952

此處有兩種解決方法:
1、在Makefile文檔最前面,設置變量top_srcdir的路徑
2、用絕對/相對路徑替換$(top_srcdir)該變量
來一個簡單粗暴點的方法吧:
使用 vim Makefile(因為要大批量改動,建議先備份: cp Makefile Makefile-date +%F)編輯文件,而后替換所有$(top_srcdir),輸入:

: %s/$(top_srcdir)/..\/../g

保存退出,重新執行make即可.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM