溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Redis集群模式部署(包含聯(lián)網(wǎng)和離線安裝)

發(fā)布時(shí)間:2020-07-31 22:08:28 來(lái)源:網(wǎng)絡(luò) 閱讀:2311 作者:898009427 欄目:數(shù)據(jù)庫(kù)

系統(tǒng)環(huán)境

系統(tǒng)版本:CentOS 6.8 x86_64(適用于CentOS 7.*)
gcc 版本 4.4.7 
redis版本:4.0.8
安裝方式:源碼安裝redis
軟件包路徑:/opt/tools/
安裝路徑:/usr/local/

依賴環(huán)境:

Ruby環(huán)境(集群搭建需要用ruby創(chuàng)建, ruby環(huán)境在2.2以上。)
rubygems:ruby的一個(gè)包管理工具
redis.gem :redis集群需要的ruby插件,通過(guò)rubygems安裝redis-4.0.1.gem。

1 Redis單節(jié)點(diǎn)部署

1.1 下載和編譯

[root@redis_1 ~]# cd /opt/tools/
[root@redis_1 tools ]# wget http://download.redis.io/releases/redis-4.0.8.tar.gz
[root@redis_1 tools ]# tar xf redis-4.0.8.tar.gz -C /usr/local/
[root@redis_1 tools ]# cd /usr/local/redis-4.0.8/
[root@redis_1 redis-4.0.8 ]# make
[root@redis_1 redis-4.0.8 ]# make install
編譯完成后會(huì)在src目錄下生成Redis服務(wù)端程序redis-server和客戶端程序redis-cli。

1.2 前臺(tái)啟動(dòng)服務(wù)

[root@redis_1 redis-4.0.8 ]# redis-server
該方式啟動(dòng)默認(rèn)為前臺(tái)方式運(yùn)行,使用默認(rèn)配置。

1.3 后臺(tái)啟動(dòng)服務(wù)

可以修改redis.conf文件的daemonize參數(shù)為yes,指定配置文件啟動(dòng),例如:
[root@redis_1 redis-4.0.8 ]# cp -a redis.conf{,_$(date +%F)}
[root@redis_1 redis-4.0.8 ]# vim redis.conf
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

或者執(zhí)行sed替換命令:
[root@redis_1 redis-4.0.8 ]# sed -i 's#daemonize no#daemonize yes#g' redis.conf
[root@redis_1 redis-4.0.8 ]# grep 'daemonize yes' redis.conf
daemonize yes
指定配置文件啟動(dòng)。
[root@redis_1 redis-4.0.8 ]# redis-server redis.conf

停止后臺(tái)啟動(dòng)的redis-server進(jìn)程:
[root@redis_1 redis-4.0.8 ]# redis-cli -h $(hostname -i) -c -p ${端口}
192.168.1.101:7000> SHUTDOWN
not connected> exit



例如:

# 指定配置文件后臺(tái)啟動(dòng)
[root@redis_1 redis-4.0.8]# src/redis-server redis.conf
95778:C 30 Jan 00:44:37.633 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
95778:C 30 Jan 00:44:37.634 # Redis version=4.0.7, bits=64, commit=00000000, modified=0, pid=95778, just started
95778:C 30 Jan 00:44:37.634 # Configuration loaded

# 查看Redis進(jìn)程
[root@redis_1 redis-4.0.8]# ps aux|grep redis
root      95779  0.0  0.0 145268   468 ?        Ssl  00:44   0:00 src/redis-server 127.0.0.1:6379
更多啟動(dòng)參數(shù)如下:
[root@redis_1 src]# ./redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
       ./redis-server - (read config from stdin)
       ./redis-server -v or --version
       ./redis-server -h or --help
       ./redis-server --test-memory <megabytes>

Examples:
       ./redis-server (run the server with default conf)
       ./redis-server /etc/redis/6379.conf
       ./redis-server --port 7777
       ./redis-server --port 7777 --slaveof 127.0.0.1 8888
       ./redis-server /etc/myredis.conf --loglevel verbose

Sentinel mode:
       ./redis-server /etc/sentinel.conf --sentinel

1.4 客戶端測(cè)試

  [root@redis_1 redis-4.0.8]# src/redis-cli 
    redis> set foo bar
    OK
    redis> get foo
    "bar"

2. Redis集群配置文件設(shè)置及啟動(dòng)實(shí)例

2.1 設(shè)置配置文件

Redis的集群部署需要在每臺(tái)集群部署的機(jī)器上安裝Redis;可參考上述的Redis部署,然后修改配置以集群的方式啟動(dòng)。
我們要在單臺(tái)機(jī)器上搭建Redis集群,方式是通過(guò)不同的TCP端口啟動(dòng)多個(gè)實(shí)例,然后組成集群。
需要注意的是:必須要3個(gè)或以上的主節(jié)點(diǎn),否則在創(chuàng)建集群時(shí)會(huì)失敗,并且當(dāng)存活的主節(jié)點(diǎn)數(shù)小于總節(jié)點(diǎn)數(shù)的一半時(shí),整個(gè)集群就無(wú)法提供服務(wù)了。
最小集群模式需要三個(gè)master實(shí)例,一般建議起六個(gè)實(shí)例,即三主三從。因此我們創(chuàng)建6個(gè)以端口號(hào)命名的目錄存放實(shí)例的配置文件和其他信息。

[root@redis_1 redis-4.0.8]# mkdir -pv redis-cluster/{7000,7001,7002,7003,7004,7005}
[root@redis_1 redis-4.0.8]# cd redis-cluster/7000


在對(duì)應(yīng)端口號(hào)的目錄中創(chuàng)建redis.conf的文件,配置文件的內(nèi)容可參考如下的集群模式配置。每個(gè)配置文件中的端口號(hào)port參數(shù)改為對(duì)應(yīng)目錄的端口號(hào)。
修改配置文件redis.conf,集群模式的配置文件如下:
[root@redis_1 7000]# vim redis.conf
daemonize yes                         //redis后臺(tái)運(yùn)行;
port 7000                             //端口7000,7001,7002,7003,7004,7005;
timeout 600                           // 客戶端無(wú)響應(yīng)連接超時(shí)時(shí)間;默認(rèn)關(guān)閉
bind 192.168.1.91                     //默認(rèn)ip為127.0.0.1 需要改為其他節(jié)點(diǎn)機(jī)器可訪問(wèn)的ip 否則創(chuàng)建集群時(shí)無(wú)法訪問(wèn)對(duì)應(yīng)的端口,無(wú)法創(chuàng)建集群;
pidfile  /var/run/redis_7000.pid      //pidfile文件對(duì)應(yīng)7000,7001,7002,7003,7004,7005;
cluster-enabled yes                   //開(kāi)啟集群  把注釋#去掉;
cluster-config-file nodes_7000.conf   //集群的配置  配置文件首次啟動(dòng)自動(dòng)生成 7000,7001,7002,7003,7004,7005 ;
cluster-node-timeout 15000            //請(qǐng)求超時(shí)  默認(rèn)15秒,可自行設(shè)置;
appendonly yes                        //aof日志開(kāi)啟  有需要就開(kāi)啟,每次寫(xiě)操作都記錄一條日志;

# 查看
[root@redis_1 7000]# egrep "^daemonize|^port|^timeout|^bind|^pidfile|^cluster-|^appendonly" redis.conf

更多集群配置參數(shù)可參考默認(rèn)配置文件redis.conf中Cluster模塊的說(shuō)明。


2.2 啟動(dòng)實(shí)例

復(fù)制redis-server的二進(jìn)制文件到redis-cluster目錄中,通過(guò)指定配置文件的方式啟動(dòng)redis服務(wù),例如:
[root@redis_1 7000]# pwd
/usr/local/redis-4.0.8/redis-cluster/7000
[root@redis_1 7000]# redis-server ./redis.conf       # 需在指定實(shí)例目錄中執(zhí)行文件,會(huì)在命令當(dāng)前目錄生成文件。
2497:C 18 May 13:42:24.279 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2497:C 18 May 13:42:24.279 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=2497, just started
2497:C 18 May 13:42:24.279 # Configuration loaded

如果是以前臺(tái)方式運(yùn)行,則會(huì)在控制臺(tái)輸出以下信息:
[82462] 26 Nov 11:56:55.329 * No cluster configuration found, I'm 97a3a64667477371c4479320d683e4c8db5858b1
每個(gè)實(shí)例都會(huì)生成一個(gè)Node ID,類似97a3a64667477371c4479320d683e4c8db5858b1,用來(lái)作為Redis實(shí)例在集群中的唯一標(biāo)識(shí),而不是通過(guò)IP和Port,IP和Port可能會(huì)改變,該Node ID不會(huì)改變。

目錄結(jié)構(gòu)可參考:

    redis-cluster/
    ├── 7000
    │   ├── appendonly.aof
    │   ├── dump.rdb
    │   ├── nodes.conf
    │   └── redis.conf
    ├── 7001
    │   ├── appendonly.aof
    │   ├── dump.rdb
    │   ├── nodes.conf
    │   └── redis.conf
    ├── 7002
    │   ├── appendonly.aof
    │   ├── dump.rdb
    │   ├── nodes.conf
    │   └── redis.conf
    ├── 7003
    │   ├── appendonly.aof
    │   ├── dump.rdb
    │   ├── nodes.conf
    │   └── redis.conf
    ├── 7004
    │   ├── appendonly.aof
    │   ├── dump.rdb
    │   ├── nodes.conf
    │   └── redis.conf
    ├── 7005
    │   ├── appendonly.aof
    │   ├── dump.rdb
    │   ├── nodes.conf
    │   └── redis.conf
    ├── redis-cli
    └── redis-server


注:其中dump.rdb為數(shù)據(jù)存放文件,第一次啟動(dòng)集群時(shí)沒(méi)有。
Redis的實(shí)例全部運(yùn)行之后,還需要redis-trib.rb工具來(lái)完成集群的創(chuàng)建,redis-trib.rb二進(jìn)制文件在Redis包主目錄下的src目錄中,運(yùn)行該工具依賴Ruby環(huán)境和gem。

說(shuō)明:下面步驟3和步驟4針對(duì)不同環(huán)境,服務(wù)器可以連外網(wǎng)推薦步驟3,無(wú)法連外網(wǎng)推薦步驟4源碼安裝依賴包,安裝一個(gè)即可。


3 在線安裝Redis集群依賴(Ruby和RubyGems和rvm和gem)

3.1 安裝Ruby

[root@redis_1 src]# yum install ruby rubygems -y
查看Ruby版本信息。

[root@redis_1 src]# ruby --version
ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]
由于centos6系統(tǒng)默認(rèn)支持Ruby版本為1.8.7(centos7系統(tǒng)默認(rèn)支持Ruby版本為2.0.0),因此執(zhí)行g(shù)em install redis命令時(shí)會(huì)報(bào)以下錯(cuò)誤。

[root@redis_1 src]# gem install redis
Fetching: redis-4.0.1.gem (100%)
ERROR:  Error installing redis:
    redis requires Ruby version >= 2.2.2.

解決方法是先安裝rvm,再升級(jí)ruby版本。

3.2 安裝rvm

curl -L get.rvm.io | bash -s stable
如果遇到以下報(bào)錯(cuò),則執(zhí)行報(bào)錯(cuò)中的gpg2 --recv-keys的命令。
[root@redis_1 ~]# curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    335      0 --:--:-- --:--:-- --:--:--   335
100 24090  100 24090    0     0  17421      0  0:00:01  0:00:01 --:--:-- 44446
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: 于 2017年09月11日 星期一 04時(shí)59分21秒 CST 創(chuàng)建的簽名,使用 RSA,鑰匙號(hào) BF04FF17
gpg: 無(wú)法檢查簽名:沒(méi)有公鑰
Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).

GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.3.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

    gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
    or if it fails:

    command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
the key can be compared with:

    https://rvm.io/mpapis.asc
    https://keybase.io/mpapis
NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.
執(zhí)行報(bào)錯(cuò)中的gpg2 --recv-keys的命令。
例如:

[root@redis_1 ~]# gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
gpg: 鑰匙環(huán)‘/root/.gnupg/secring.gpg’已建立
gpg: 下載密鑰‘D39DC0E3’,從 hkp 服務(wù)器 keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg:建立了信任度數(shù)據(jù)庫(kù)
gpg: 密鑰 D39DC0E3:公鑰“Michal Papis (RVM signing) <mpapis@gmail.com>”已導(dǎo)入
gpg: 沒(méi)有找到任何絕對(duì)信任的密鑰
gpg: 合計(jì)被處理的數(shù)量:1
gpg:           已導(dǎo)入:1  (RSA: 1)

再次執(zhí)行命令curl -L get.rvm.io | bash -s stable。
[root@redis_1 ~]# curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    310      0 --:--:-- --:--:-- --:--:--   309
100 24090  100 24090    0     0  18230      0  0:00:01  0:00:01 --:--:--  103k
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: 于 2017年09月11日 星期一 04時(shí)59分21秒 CST 創(chuàng)建的簽名,使用 RSA,鑰匙號(hào) BF04FF17
gpg: 完好的簽名,來(lái)自于“Michal Papis (RVM signing) <mpapis@gmail.com>”
gpg:               亦即“Michal Papis <michal.papis@toptal.com>”
gpg:               亦即“[jpeg image of size 5015]”
gpg: 警告:這把密鑰未經(jīng)受信任的簽名認(rèn)證!
gpg:       沒(méi)有證據(jù)表明這個(gè)簽名屬于它所聲稱的持有者。
主鑰指紋: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3
子鑰指紋: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17
GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'
Creating group 'rvm'

Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,    
and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`    
in all your open shell windows, in rare cases you need to reopen all shell windows.

以上表示執(zhí)行成功,然后執(zhí)行以下命令。


[root@redis_1 ~]# source /usr/local/rvm/scripts/rvm

查看rvm庫(kù)中已知的ruby版本
rvm list known

例如:


    [root@redis_1 ~]# 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[.7]
    [ruby-]2.3[.4]
    [ruby-]2.4[.1]
    ruby-head
    ...

3.3 升級(jí)Ruby

#安裝ruby


rvm install  2.4.0


#使用新版本
rvm use  2.4.0

#移除舊版本


rvm remove 1.8.7
#查看當(dāng)前版本
ruby --version

例如:


  [root@redis_1 ~]# rvm install  2.4.0
    Searching for binary rubies, this might take some time.
    Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/7/x86_64/ruby-2.4.0.tar.bz2
    Checking requirements for centos.
    Installing requirements for centos.
    Installing required packages: autoconf, automake, bison, bzip2, gcc-c++, libffi-devel, libtool, readline-devel, sqlite-devel, zlib-devel, libyaml-devel, openssl-devel................................
    Requirements installation successful.
    ruby-2.4.0 - #configure
    ruby-2.4.0 - #download
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14.0M  100 14.0M    0     0   852k      0  0:00:16  0:00:16 --:--:--  980k
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.4.0 - #validate archive
ruby-2.4.0 - #extract
ruby-2.4.0 - #validate binary
ruby-2.4.0 - #setup
ruby-2.4.0 - #gemset created /usr/local/rvm/gems/ruby-2.4.0@global
ruby-2.4.0 - #importing gemset /usr/local/rvm/gemsets/global.gems..............................
ruby-2.4.0 - #generating global wrappers........
ruby-2.4.0 - #gemset created /usr/local/rvm/gems/ruby-2.4.0
ruby-2.4.0 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.4.0 - #generating default wrappers........

[root@redis_1 ~]# rvm use  2.4.0
Using /usr/local/rvm/gems/ruby-2.4.0

[root@redis_1 ~]# rvm remove 1.8.7
ruby-1.8.7-p648 - #already gone
Using /usr/local/rvm/gems/ruby-2.4.0

[root@redis_1 ~]# ruby --version
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]

3.4 安裝gem


[root@redis_1 ~]# gem install redis
Fetching: redis-4.0.1.gem (100%)
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 2 seconds
1 gem installed

4 源碼安裝Redis集群依賴(Ruby和RubyGems和OpenSSL和Zlib-devel和gem)

4.1 安裝Zlib-devel

官網(wǎng):
https://pkgs.org/download/zlib
https://pkgs.org/download/zlib-devel
選擇對(duì)應(yīng)系統(tǒng)版本的zlib和zlib-devel包;
本實(shí)驗(yàn)為CentOS 6 64位,選擇el6的zlib-1.2.3-29.el6.x86_64.rpm和zlib-devel-1.2.3-29.el6.x86_64.rpm包

有yum源:
[root@redis_1 tools]# yum install zlib zlib-devel -y

沒(méi)有yum源:
[root@redis_1 tools]# wget http://mirror.centos.org/centos/6/os/x86_64/Packages/zlib-1.2.3-29.el6.x86_64.rpm
[root@redis_1 tools]# wget http://mirror.centos.org/centos/6/os/x86_64/Packages/zlib-devel-1.2.3-29.el6.x86_64.rpm  
[root@redis_1 tools]# rpm -ivh zlib-1.2.3-29.el6.x86_64.rpm zlib-devel-1.2.3-29.el6.x86_64.rpm

4.2 安裝Ruby:

到Ruby官網(wǎng):https://www.ruby-lang.org/en/downloads/
找到2.2版本以上Ruby的穩(wěn)定版,本實(shí)驗(yàn)用ruby-2.3.6
注意:ruby-2.5.0版本在CentOS6.*上安裝需升級(jí)GCC(4.8)
進(jìn)入linux,選擇一個(gè)位置存放安裝包,我的位置是/opt/tools
輸入命令wget + 剛剛復(fù)制的安裝包URL

[root@redis_1 tools]# pwd
/opt/tools
[root@redis_1 tools]# wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.6.tar.gz
[root@redis_1 tools]# tar zxf ruby-2.3.6.tar.gz -C /usr/local/
[root@redis_1 tools]# cd /usr/local/ruby-2.3.6
[root@redis_1 ruby-2.3.6]# ./configure      # 可以指定安裝路徑./configure --prefix=/usr/local/ruby
[root@redis_1 ruby-2.3.6]# make
[root@redis_1 ruby-2.3.6]# make install

驗(yàn)證:

[root@redis_1 ruby-2.3.6]# source /etc/profile
[root@redis_1 ruby-2.3.6]# ruby -v
ruby 2.3.6p384 (2017-12-14 revision 61254) [x86_64-linux]

[root@redis_1 ruby-2.3.6]# cd ext/zlib/
[root@redis_1 zlib]# pwd
/usr/local/ruby-2.3.6/ext/zli
[root@redis_1 zlib]# ruby ./extconf.rb
checking for deflateReset() in -lz... yes
checking for zlib.h... yes
checking for crc32_combine() in zlib.h... yes
checking for adler32_combine() in zlib.h... yes
checking for z_crc_t in zlib.h... no
creating Makefile
[root@redis_1 zlib]# make
compiling zlib.c
linking shared-object zlib.so
[root@redis_1 zlib]# make install
/usr/bin/install -c -m 0755 zlib.so /usr/local/lib/ruby/site_ruby/2.3.0/x86_64-linux
[root@redis_1 zlib]#

4.3 安裝RubyGems:

官網(wǎng) https://rubygems.org/pages/download
wget + URL,在線下載安裝包:
[root@redis_1 tools]# wget https://rubygems.org/rubygems/rubygems-2.7.6.tgz
[root@redis_1 tools]# tar xf rubygems-2.7.6.tgz -C /usr/local/
[root@redis_1 tools]# cd /usr/local/rubygems-2.7.6/
[root@redis_1 rubygems-2.7.6]# ruby setup.rb
Bundler 1.16.1 installed
RubyGems 2.7.6 installed
... ...
RubyGems installed the following executables:
    /usr/local/bin/gem
    /usr/local/bin/bundle

Ruby Interactive (ri) documentation was installed. ri is kind of like man
pages for Ruby libraries. You may access it like this:
  ri Classname
  ri Classname.class_method
  ri Classname#instance_method
If you do not wish to install this documentation in the future, use the
--no-document flag, or set it as the default in your ~/.gemrc file. See
'gem help env' for details.

驗(yàn)證:

[root@redis_1 rubygems-2.7.6]# gem -v
2.7.6

4.4 安裝OpenSSL:

官網(wǎng): https://www.openssl.org/source/
wget 下載安裝包:


[root@redis_1 tools]# wget https://www.openssl.org/source/openssl-1.1.1-pre6.tar.gz
[root@redis_1 tools]# tar xf openssl-1.1.1-pre6.tar.gz -C /usr/local/
[root@redis_1 tools]# cd /usr/local/openssl-1.1.1-pre6/
[root@redis_1 openssl-1.1.1-pre6]# mkdir -v /usr/local/openssl
mkdir: 已創(chuàng)建目錄 "/usr/local/openssl"

[root@redis_1 openssl-1.1.1-pre6]# ./config --prefix=/usr/local/openssl --shared
[root@redis_1 openssl-1.1.1-pre6]# make
[root@redis_1 openssl-1.1.1-pre6]# make install

添加“/usr/local/openssl/lib”到系統(tǒng)類包:
[root@redis_1 openssl-1.1.1-pre6]# echo -e "/usr/local/openssl/lib" > /etc/ld.so.conf.d/openssl.conf
[root@redis_1 openssl-1.1.1-pre6]# cat /etc/ld.so.conf.d/openssl.conf
/usr/local/openssl/lib					
[root@redis_1 openssl-1.1.1-pre6]# ldconfig		# 重新加載類

添加到環(huán)境變量:
[root@redis_1 openssl-1.1.1-pre6]# echo -e "# openssl\nexport OPENSSL=/usr/local/openssl/bin\nexport PATH=\$OPENSSL:\$PATH:\$HOME/bin" > /etc/profile.d/openssl.sh
[root@redis_1 openssl-1.1.1-pre6]# cat /etc/profile.d/openssl.sh
	# openssl
	export OPENSSL=/usr/local/openssl/bin
	export PATH=$OPENSSL:$PATH:$HOME/bin
[root@redis_1 openssl-1.1.1-pre6]# source /etc/profile    # 重新加載環(huán)境變量

# 驗(yàn)證:
[root@redis_1 openssl-1.1.1-pre6]# echo $OPENSSL
/usr/local/openssl/bin
[root@redis_1 openssl-1.1.1-pre6]#

4.5 安裝redis.gem

官網(wǎng):https://rubygems.org/gems/redis
[root@redis_1 redis-4.0.8]# cd /usr/local/redis-4.0.8/
[root@redis_1 redis-4.0.8]# pwd
/usr/local/redis-4.0.8
[root@redis_1 redis-4.0.8]# wget https://rubygems.org/downloads/redis-4.0.1.gem
[root@redis_1 redis-4.0.8]# gem install -l ./redis-4.0.1.gem
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Done installing documentation for redis after 3 seconds
1 gem installed

5 redis-trib創(chuàng)建集群

#執(zhí)行redis-trib.rb命令


[root@redis_1 redis-4.0.8]# cd /usr/local/redis-4.0.8/src/
[root@redis_1 src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
> 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

參數(shù)create表示創(chuàng)建一個(gè)新的集群,--replicas 1表示為每個(gè)master創(chuàng)建一個(gè)slave。

如果創(chuàng)建成功會(huì)顯示以下信息


[OK] All 16384 slots covered

例如:


  [root@redis_1 src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
    > 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
    >>> Creating cluster
    >>> Performing hash slots allocation on 6 nodes...
    Using 3 masters:
    127.0.0.1:7000
    127.0.0.1:7001
    127.0.0.1:7002
    Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
    Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
    Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
    >>> Trying to optimize slaves allocation for anti-affinity
    [WARNING] Some slaves are in the same host as their master
    M: d5a834d075fd93eefab877c6ebb86efff680650f 127.0.0.1:7000
       slots:0-5460 (5461 slots) master
    M: 13d0c397604a0b2644244c37b666fce83f29faa8 127.0.0.1:7001
       slots:5461-10922 (5462 slots) master
    M: be2718476eba4e56f696e56b75e67df720b7fc24 127.0.0.1:7002
       slots:10923-16383 (5461 slots) master
    S: 3d02f59b34047486faecc023685379de7b38076c 127.0.0.1:7003
       replicates 13d0c397604a0b2644244c37b666fce83f29faa8
    S: dedf672f0a75faf37407ac4edd5da23bc4651e25 127.0.0.1:7004
       replicates be2718476eba4e56f696e56b75e67df720b7fc24
    S: 99c07119a449a703583019f7699e15afa0e41952 127.0.0.1:7005
       replicates d5a834d075fd93eefab877c6ebb86efff680650f
    Can I set the above configuration? (type 'yes' to accept): yes        # 同意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 127.0.0.1:7000)
    M: d5a834d075fd93eefab877c6ebb86efff680650f 127.0.0.1:7000
       slots:0-5460 (5461 slots) master   1 additional replica(s)
    M: be2718476eba4e56f696e56b75e67df720b7fc24 127.0.0.1:7002
       slots:10923-16383 (5461 slots) master   1 additional replica(s)
    M: 13d0c397604a0b2644244c37b666fce83f29faa8 127.0.0.1:7001
       slots:5461-10922 (5462 slots) master   1 additional replica(s)
    S: 3d02f59b34047486faecc023685379de7b38076c 127.0.0.1:7003
       slots: (0 slots) slave
       replicates 13d0c397604a0b2644244c37b666fce83f29faa8
    S: 99c07119a449a703583019f7699e15afa0e41952 127.0.0.1:7005
       slots: (0 slots) slave
       replicates d5a834d075fd93eefab877c6ebb86efff680650f
    S: dedf672f0a75faf37407ac4edd5da23bc4651e25 127.0.0.1:7004
       slots: (0 slots) slave
       replicates be2718476eba4e56f696e56b75e67df720b7fc24
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.

6 集群部署結(jié)果驗(yàn)證

6.1 客戶端驗(yàn)證

使用客戶端redis-cli二進(jìn)制訪問(wèn)某個(gè)實(shí)例,執(zhí)行set和get的測(cè)試。語(yǔ)法:redis-cli -h $IP -c -p $PORT -a $PASSWORD
[root@redis_1 src]#  redis-cli -c -p 7000
redis 127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
redis 127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7000> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"

6.2 集群狀態(tài)

使用cluster info命令查看集群狀態(tài)。


127.0.0.1:7000> cluster info
cluster_state:ok                       #集群狀態(tài)
cluster_slots_assigned:16384           #被分配的槽位數(shù)
cluster_slots_ok:16384                 #正確分配的槽位
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6                  #當(dāng)前節(jié)點(diǎn)
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:48273
cluster_stats_messages_pong_sent:49884
cluster_stats_messages_sent:98157
cluster_stats_messages_ping_received:49879
cluster_stats_messages_pong_received:48273
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:98157

6.3 節(jié)點(diǎn)狀態(tài)

使用cluster nodes命令查看節(jié)點(diǎn)狀態(tài)。

127.0.0.1:7000> cluster nodes
be2718476eba4e56f696e56b75e67df720b7fc24 127.0.0.1:7002@17002 master - 0 1517303607000 3 connected 10923-16383
13d0c397604a0b2644244c37b666fce83f29faa8 127.0.0.1:7001@17001 master - 0 1517303606000 2 connected 5461-10922
3d02f59b34047486faecc023685379de7b38076c 127.0.0.1:7003@17003 slave 13d0c397604a0b2644244c37b666fce83f29faa8 0 1517303606030 4 connected
d5a834d075fd93eefab877c6ebb86efff680650f 127.0.0.1:7000@17000 myself,master - 0 1517303604000 1 connected 0-5460
99c07119a449a703583019f7699e15afa0e41952 127.0.0.1:7005@17005 slave d5a834d075fd93eefab877c6ebb86efff680650f 0 1517303607060 6 connected
dedf672f0a75faf37407ac4edd5da23bc4651e25 127.0.0.1:7004@17004 slave be2718476eba4e56f696e56b75e67df720b7fc24 0 1517303608082 5 connected

7 參考:

https://redis.io/download
https://redis.io/topics/cluster-tutorial
http://blog.csdn.net/huwh_/article/details/79242625
http://blog.csdn.net/michaelehome/article/details/79533496

8 附:redis集群密碼設(shè)置

8.1 密碼設(shè)置(推薦)

方式一:修改所有Redis集群中的redis.conf文件加入:
masterauth passwd123 
requirepass passwd123
說(shuō)明:這種方式需要重新啟動(dòng)各節(jié)點(diǎn)

方式二:進(jìn)入各個(gè)實(shí)例進(jìn)行設(shè)置:redis-cli -h $IP -c -p 端口
./redis-cli -c -p 7000 
config set masterauth passwd123 
config set requirepass passwd123 
config rewrite
之后分別使用./redis-cli -c -p 7001,./redis-cli -c -p 7002…..命令給各節(jié)點(diǎn)設(shè)置上密碼。

注意:各個(gè)節(jié)點(diǎn)密碼都必須一致,否則Redirected就會(huì)失敗, 推薦這種方式,這種方式會(huì)把密碼寫(xiě)入到redis.conf里面去,且不用重啟。

用方式二修改密碼,./redis-trib.rb check 10.104.111.174:6379執(zhí)行時(shí)可能會(huì)報(bào)[ERR] Sorry, can't connect to node 10.104.111.174:6379,因?yàn)?379的redis.conf沒(méi)找到密碼配置。

8.2 設(shè)置密碼之后如果需要使用redis-trib.rb的各種命令

如:./redis-trib.rb check 127.0.0.1:7000,則會(huì)報(bào)錯(cuò)ERR] Sorry, can’t connect to node 127.0.0.1:7000 
解決辦法:
vim /usr/local/rvm/gems/ruby-2.3.0/gems/redis-4.0.1/lib/redis/client.rb         # 聯(lián)網(wǎng)安裝路徑
vim /usr/local/lib/ruby/gems/2.3.0/gems/redis-4.0.1/lib/redis/client.rb         # 源碼安裝路徑
注意:client.rb路徑可以通過(guò)find命令查找:find / -name 'client.rb'
然后修改passord
class Client
    DEFAULTS = {
      :url => lambda { ENV["REDIS_URL"] },
      :scheme => "redis",
      :host => "127.0.0.1",
      :port => 6379,
      :path => nil,
      :timeout => 5.0,
      :password => "passwd123",
      :db => 0,
      :driver => nil,
      :id => nil,
      :tcp_keepalive => 0,
      :reconnect_attempts => 1,
      :inherit_socket => false
    }
帶密碼訪問(wèn)集群
./redis-cli -c -p 7000 -a passwd123
END
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI