Linux CentOS 下 Redis 的安裝部署 + Redis 僞集羣的搭建

redis下載 : http://download.redis.io/releases/redis-4.0.10.tar.gznode

  • 解壓 編譯 安裝
//解壓到local目錄
# tar -xvf redis-4.0.10.tar.gz -C /usr/local/

//定位到解壓目錄
# cd /usr/local/redis-4.0.10/

//執行編譯和安裝,PREFIX必須大寫
# make install PREFIX=/usr/local/redis
  • /usr/local/redis/bin目錄下會生成以下文件,這時候就能夠啓動redis-server了
  • 這裏寫圖片描述
# cd /usr/local/redis/bin
//在bin目錄下執行命令
# ./redis-server
23154:C 24 Aug 05:23:47.865 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23154:C 24 Aug 05:23:47.865 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23154, just started 23154:C 24 Aug 05:23:47.865 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf 23154:M 24 Aug 05:23:47.867 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.10 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 23154 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 23154:M 24 Aug 05:23:47.871 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 23154:M 24 Aug 05:23:47.871 # Server initialized 23154:M 24 Aug 05:23:47.872 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 23154:M 24 Aug 05:23:47.874 * Ready to accept connections 
  • 這時候啓動用的是默認的配置
  • bind 127.0.0.1
  • port 6379
  • daemonize nolinux

  • 另開一個遠程客戶端,進行客戶端鏈接git

# cd /usr/local/redis-4.0.10/bin/
# cd ./redis-cli
127.0.0.1:6379> shutdown

//執行shutdown關閉redis服務
not connected>
  • 服務端能夠經過CTRL+C 退出, 也能夠在客戶端執行shutdown退出
  • 設置服務後臺啓動方式
//拷貝一份配置文件到redis目錄
# cp /usr/local/redis-4.0.10/redis.conf /usr/local/reids

//vi命令 修改守護進程 daemonize爲yes
daemonized yes
//進入vi命令後能夠經過 /daemonize快速定位

//在bin目錄下帶配置文件啓動redis服務
# ./redis-server ../redsi.conf
23345:C 24 Aug 05:52:05.153 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23345:C 24 Aug 05:52:05.153 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23345, just started
23345:C 24 Aug 05:52:05.153 # Configuration loaded
#
  • 經過Redis Desktop Manager 0.9.5遠程鏈接redis服務github

  • 首先確認你的Linux系統已經開放了6379端口web

  • 修改配置文件,打開requirepass的註釋或者直接添加一行
requirepass 你想要的驗證碼
//添加 bind 服務器地址
 bind 120.0.0.1
 bind 192.168.25.120(這裏寫你服務器的ip地址)

//啓動服務,測試遠程鏈接
# ./redis-server ../redsi.conf

//由於配置文件添加了驗證,關閉服務的時候也須要驗證
# ./redis-cli -a 驗證碼 shutdown

這裏寫圖片描述


Redis僞集羣搭建

1.使用Ruby Version Manager (RVM) 部署Rubyredis

//在線安裝RVM
# \curl -sSL https://get.rvm.io | bash -s stable
//在線部署Ruby,列出全部版本
# rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]
ruby-head

# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2

# JRuby
jruby-1.6[.8]
jruby-1.7[.27]
jruby-9.1[.17.0]
jruby[-9.2.0.0]
jruby-head

# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx-2[.5.8]
rbx-3[.100]
rbx-head

# TruffleRuby
truffleruby[-1.0.0-rc2]

# Opal
opal

# Minimalistic ruby implementation - ISO 30170:2012
mruby-1.0.0
mruby-1.1.0
mruby-1.2.0
mruby-1.3.0
mruby-1[.4.0]
mruby[-head]

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# Topaz
topaz

# MagLev
maglev-1.0.0
maglev-1.1[RC1]
maglev[-1.2Alpha4]
maglev-head

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# IronRuby
ironruby[-1.1.3]
ironruby-head
//安裝選定的版本
# rvm install ruby-2.5
//驗證版本
# ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

2.安裝RubyGems
安裝包下載 : https://rubygems.org/rubygems/rubygems-2.7.7.tgzsql

//解壓rubygems.tgz後,在解壓目錄內執行
# ruby setup.rb

//驗證版本
# gem -v
2.7.7

3.在線安裝redis.gemruby

//會安裝最新版本的redis.gem
# gem install redis

4.配置多個redis服務端bash

//新建redis-cluster文件夾
# mkdir /usr/local/redis-cluster

//刪除bin目錄下生成的RDB快照文件和AOF文件
# rm -rf /usr/local/redis/bin/dump.rdb appendonly.aof

/* 修改redis.conf配置文件 1.註釋掉bind 127.0.0.1 2.註釋掉requirepass 驗證碼 3.開啓cluster-enabled yes的註釋 4.修改protected-mode 爲 no */

# bind 127.0.0.1
# requirepass 驗證碼
cluster-enabled yes
protected-mode no

//拷貝6份redis目錄到redis-cluster *集羣至少須要3個redis服務,每一個reids還須要一個備份.因此須要6份*
# cp /usr/local/redis /usr/local/redis-cluster/redis01 -r
# cp /usr/local/redis /usr/local/redis-cluster/redis02 -r
# cp /usr/local/redis /usr/local/redis-cluster/redis03 -r
# cp /usr/local/redis /usr/local/redis-cluster/redis04 -r
# cp /usr/local/redis /usr/local/redis-cluster/redis05 -r
# cp /usr/local/redis /usr/local/redis-cluster/redis06 -

//修改redis01-06目錄中配置文件redis.conf,更改端口號port
redis01/reids.conf  port 7001
redis02/reids.conf  port 7002
redis03/reids.conf  port 7003
redis04/reids.conf  port 7004
redis05/reids.conf  port 7005
redis01/reids.conf  port 7006

//拷貝redis解壓目錄src下的redis-trib.rb到redis-cluster目錄下
# cp /usr/local/redis-4.0.10/src/redis-trib.rb /usr/local/redis-cluster

//新建批執行文件
# mkdir /usr/local/redis-cluster/redis_start_all.sh

//修改批執行
# vi /usr/local/redis-cluster/redis_start_all.sh
d /usr/local/redis-cluster/redis01/bin
./redis-server ../redis.conf

cd /usr/local/redis-cluster/redis02/bin
./redis-server ../redis.conf

cd /usr/local/redis-cluster/redis03/bin
./redis-server ../redis.conf

cd /usr/local/redis-cluster/redis04/bin
./redis-server ../redis.conf

cd /usr/local/redis-cluster/redis05/bin
./redis-server ../redis.conf

cd /usr/local/redis-cluster/redis06/bin
./redis-server ../redis.conf

//修改redis_start_all.sh的可執行權限
# chmod u+x /usr/local/redis-cluster/redis_start_all.sh

//啓動多個redis服務,在redis-cluster目錄下執行
# ./redis_start_all.sh
23928:C 24 Aug 07:09:37.156 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23928:C 24 Aug 07:09:37.156 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23928, just started
23928:C 24 Aug 07:09:37.156 # Configuration loaded
23930:C 24 Aug 07:09:37.173 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23930:C 24 Aug 07:09:37.174 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23930, just started
23930:C 24 Aug 07:09:37.174 # Configuration loaded
23932:C 24 Aug 07:09:37.188 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23932:C 24 Aug 07:09:37.188 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23932, just started
23932:C 24 Aug 07:09:37.188 # Configuration loaded
23934:C 24 Aug 07:09:37.203 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23934:C 24 Aug 07:09:37.203 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23934, just started
23934:C 24 Aug 07:09:37.203 # Configuration loaded
23936:C 24 Aug 07:09:37.217 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23936:C 24 Aug 07:09:37.217 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23936, just started
23936:C 24 Aug 07:09:37.217 # Configuration loaded
23938:C 24 Aug 07:09:37.230 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
23938:C 24 Aug 07:09:37.231 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=23938, just started
23938:C 24 Aug 07:09:37.231 # Configuration loaded

5.使用ruby腳本搭建集羣服務器

//在redis-cluster目錄下執行

# ./redis-trib.rb create --replicas 1 192.168.25.120:7001 192.168.25.120:7002 192.168.25.120:7003 192.168.25.120:7004 192.168.25.120:7005 192.168.25.120:7006 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.25.120:7001 192.168.25.120:7002 192.168.25.120:7003 Adding replica 192.168.25.120:7005 to 192.168.25.120:7001 Adding replica 192.168.25.120:7006 to 192.168.25.120:7002 Adding replica 192.168.25.120:7004 to 192.168.25.120:7003 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 46a3cc0665de4b8cedd32559bb5b0846972f5ab6 192.168.25.120:7001 slots:0-5460 (5461 slots) master M: d10e7c79b3083f5726167e164f73469b84574609 192.168.25.120:7002 slots:5461-10922 (5462 slots) master M: 700db85f5944275988477093c84099ddccc6ffb3 192.168.25.120:7003 slots:10923-16383 (5461 slots) master S: fc11396097f8337ad17bde9496a1869932e8cbda 192.168.25.120:7004 replicates 700db85f5944275988477093c84099ddccc6ffb3 S: 57c5e3cf86738ed017668389e659edf7ba5f4fe4 192.168.25.120:7005 replicates 46a3cc0665de4b8cedd32559bb5b0846972f5ab6 S: c56a9bb7135073740229af070ab96ad500817c7d 192.168.25.120:7006 replicates d10e7c79b3083f5726167e164f73469b84574609 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.25.120:7001) M: 46a3cc0665de4b8cedd32559bb5b0846972f5ab6 192.168.25.120:7001 slots:0-5460 (5461 slots) master 1 additional replica(s) S: c56a9bb7135073740229af070ab96ad500817c7d 192.168.25.120:7006 slots: (0 slots) slave replicates d10e7c79b3083f5726167e164f73469b84574609 M: 700db85f5944275988477093c84099ddccc6ffb3 192.168.25.120:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: d10e7c79b3083f5726167e164f73469b84574609 192.168.25.120:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 57c5e3cf86738ed017668389e659edf7ba5f4fe4 192.168.25.120:7005 slots: (0 slots) slave replicates 46a3cc0665de4b8cedd32559bb5b0846972f5ab6 S: fc11396097f8337ad17bde9496a1869932e8cbda 192.168.25.120:7004 slots: (0 slots) slave replicates 700db85f5944275988477093c84099ddccc6ffb3 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 

大功告成

可能遇到的錯誤 [ERR] Sorry, can’t connect to node 192.168.X.X 檢查redis.conf配置文件 bind是否爲本身的服務器地址 protected-mode 是否爲no requirepass是否是註釋掉了