redis的架構(一)

redis認證

redis的認證比較簡單,這裏簡單來講明一下怎麼設置redis的認證:html

redis的配置文件中有一個requirepass字段,在後面直接寫上對應的密碼便可。默認redis的不開啓認證的,能夠把註釋去掉,而後開啓認證。前端

requirepass foobared

redis的默認的密碼如上所示,而後啓動redis服務。node

[root@test2 redis]# redis-cli
127.0.0.1:6379> info
NOAUTH Authentication required.
127.0.0.1:6379> auth foobared             #用auth命令認證給定的密碼,也能夠在客戶端使用-a參數指定密碼,
OK
127.0.0.1:6379> keys *
1) "key1"
2) "test"
3) "key"
127.0.0.1:6379>
[root@test2 redis]# redis-cli -p 6378 -a foobared            #注意這裏與mysql的-p參數不同,這裏的-p指定的是redis連接的端口號
127.0.0.1:6378> info 
# Server
redis_version:4.0.0

redis的主從架構

這片文章僅說明一些redis的基本搭建問題和一些概念mysql

主從的一些概念

主從架構就是一個簡單的master服務器用來寫,slave服務器把master上的數據在本地保存一份的形式。linux

redis的主從配置異常簡單,從服務期所必須的選項只有一個slaveof。若是用戶在啓動redis的時候,指定了一個包含slaveof host port選項的配置文件,那麼redis服務器將根據該選項在給定的ip地址和端口號來鏈接主服務器。對於一個正在運行的redis服務器,用戶能夠經過發送slaveof no one命令來讓服務器終止複製操做,再也不接受主服務器的數據更新;也能夠經過發送slaveof host port命令來讓服務器開始複製一個新的主服務器。c++

redis開啓主從複製的過程:(來自redis實戰一書)git

redis在複製進行期間也會盡量地處理接受到的命令請求,可是,若是主從服務器之間的網絡帶寬不足,或者主服務器沒有足夠的內存來建立子進程和建立記錄寫命令的緩衝區,那麼redis處理命令請求的效率就會收到影響。所以,儘管這並非必須的,但在實際中最好仍是讓主服務器只使用50%~65%的內存,留下30%~45%的內存用於執行bgsave命令和建立記錄寫命令的緩衝區。github

從服務器在進行同步時,會清空本身的全部數據: 從服務器在與主服務器進行初始鏈接時,數據庫中原有的全部數據都將丟失,並被替換成主服務器法來的數據。正則表達式

當多個從服務器嘗試鏈接同一個主服務器的時候:會出現如下兩種狀況。redis

第一:上面表格中的第3步還沒有執行: 這時候如有多個從服務器鏈接同一個主服務器,則全部從服務器都會接受到相同的快照文件和相同的緩衝區寫命令。

第二:上面表格中第3步正在執行或已經執行:當主服務器與較早進行鏈接的從服務器執行完複製所需的5個步驟後,主服務器會與新鏈接的從服務器執行一次新的步驟1至步驟5

主從的配置

在同一個服務器上運行三個redis實例,主要修改每一個redis中配置文件的不一樣,端口的不一樣,logfile的不一樣以及數據文件存儲名的不一樣。三個實例三個配置文件,分別以配置文件的方式啓動redis實例。

redis-server  redis.conf 【一樣的方式啓動第二個,第三個,可是注意修改配置文件】

三個實例監聽端口分別爲: 6379,6378,6377.

以默認的端口爲redis的master服務器,分別在6378,6377的配置文件中設定slaveof參數,配置以下:

slaveof 127.0.0.1 6379         

【由於三個實例都在同一個服務器上,所以使用了127.0.0.1】

分別啓動兩個實例。如果三個實例沒有配置認證,則只要後面的兩個從啓動成功,這個redis的主從架構已經搭建成功。客戶端鏈接master服務器,打入info命令會查看到對應複製信息。

在主從架構中啓用認證

如果主服務器啓用了認證,在從的日誌文件中會看到以下信息:

70640:S 22 Apr 04:15:18.659 * Ready to accept connections
70640:S 22 Apr 04:15:18.659 * Connecting to MASTER 127.0.0.1:6379
70640:S 22 Apr 04:15:18.659 * MASTER <-> SLAVE sync started
70640:S 22 Apr 04:15:18.659 * Non blocking connect for SYNC fired the event.
70640:S 22 Apr 04:15:18.659 * Master replied to PING, replication can continue...
70640:S 22 Apr 04:15:18.659 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
70640:S 22 Apr 04:15:18.659 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
70640:S 22 Apr 04:15:18.659 * Partial resynchronization not possible (no cached master)
70640:S 22 Apr 04:15:18.659 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
70640:S 22 Apr 04:15:18.659 * Retrying with SYNC...
70640:S 22 Apr 04:15:18.659 # MASTER aborted replication with an error: NOAUTH Authentication required.

這時候只要在從服務器上配置對應的master密碼便可:

# masterauth <master-password>
masterauth foobared

這個參數就是指定master服務器的密碼,配置了masterauth參數以後,從新啓動從服務器。

[root@test2 redis]# redis-cli -p 6377 -a foobared           #使用密碼鏈接
127.0.0.1:6377> info Replication                            #在從上查看複製信息
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:5040
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:bcb68a14424b58d4dc49d075265bb5765c20648f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5040
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4957
repl_backlog_histlen:84

在master上查看對應的複製信息:

127.0.0.1:6379> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6378,state=online,offset=5138,lag=0
slave1:ip=127.0.0.1,port=6377,state=online,offset=5138,lag=0
master_replid:bcb68a14424b58d4dc49d075265bb5765c20648f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5138
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5138
127.0.0.1:6379> 

傳輸延遲

從節點通常部署在不一樣機器上,複製時網絡延遲就稱爲須要考慮的問題,redis提供了repl-disable-tcp-nodelay參數用於控制是否關閉TCP_NODELAY,默認關閉。

  • 當關閉時,主節點產生的命令數據不管大小都會及時地發送給從節點,這樣主從之間延遲會變很小,但增長了網絡帶寬的消耗。適用於主從之間的網絡環境良好的場景,如同機架或同機房部署。
  • 當開啓時,主節點會合並較小的TCP數據包從而節省帶寬。默認發送時間間隔取決於linux內核,通常默認是40毫秒。這種配置節省了帶寬但增大了主從之間的延遲。適用於主從網絡環境複雜或帶寬緊張的場景。

檢驗硬盤的寫入

爲了驗證主服務器是否已經將寫數據發送至從服務器,用戶須要在向主服務器寫入真正的數據以後,再向主服務器寫入一個惟一的虛構值,而後經過檢查虛構值是否存在於從服務器來判斷寫入數據是否已經到達從服務器,這個操做很容易就能夠實現。另外一方面,判斷數據是否已經被保存到硬盤裏面則要困可貴多。對於每秒同步一次AOF文件的redis服務器來講,用戶老是能夠經過等待1秒來確保數據已經被保存到硬盤裏面;但更節約的作法是,檢查info命令的輸出結果中aof_pending_bio_fsync屬性的值是否爲0,若是是的話,那麼就表示服務器已經將已知的全部數據都保存在硬盤裏面了。

redis的主從複製很簡單,可是咱們知道redis有兩種持久化方式aof和rdb持久化,做爲主從的兩個redis服務器的持久化要保持一致,否則可能會有問題吧!

 redis的哨兵機制

哨兵機制的一些基本概念,貼上兩個博客的地址:

https://www.cnblogs.com/PatrickLiu/p/8444546.html

https://www.cnblogs.com/zhoujinyi/p/5569462.html

下面咱們會在上面配置的redis的主從架構基礎上配置哨兵模式。

redis的主從架構中,當主出現問題時,須要手動的切換到指定的從上面;redis的哨兵機制經過監控master的狀態,當master出現問題時,會自動的完成切換。

上面的架構中是採用的一主兩從的模式,分別對應兩三個端口6379(master), 6378,6377是兩個從的端口!由於有三個redis服務器,這裏咱們配置兩3個sentinel服務(其實每一個sentinel服務就是一個redis服務)

在sentinel.conf的配置文件中修改以下配置:

port 26379                                      #監聽的端口,默認是26379,其他的兩個分別修改成26378,26377
dir "/tmp/redis/sentinel1"                      #
sentinel monitor mymaster 127.0.0.1 6377 2      #監聽的redis集羣,注意只要指向集羣中的master便可,(三個sentinel都只要指向master便可,會自動發現對應的從)
sentinel auth-pass mymaster foobared            #設置認證,注意這條命令必定要配置在上面一條命令的以後位置,否則會報錯下面的錯誤。
daemonize yes                                   #開啓sentinel的守護進程
[root@test2 redis]# redis-sentinel sentinel.conf 

*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 71
>>> 'sentinel auth-pass mymaster foobared'
No such master with specified name.
認證位置錯誤

三個哨兵配置文件只須要改動端口號,以及dir的目錄便可,而後啓動!

[root@test2 redis]# netstat -lntp |grep redis-sentine
tcp        0      0 0.0.0.0:26378           0.0.0.0:*               LISTEN      70864/redis-sentine 
tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      70857/redis-sentine 
tcp        0      0 0.0.0.0:26377           0.0.0.0:*               LISTEN      70869/redis-sentine 

上面說過sentinel也是一個redis服務器,那麼就能夠鏈接到sentinel上,查看對應的信息。

sentinel支持的一些命令:

sentinel支持的合法命令以下:

    PING sentinel回覆PONG.

    SENTINEL masters 顯示被監控的全部master以及它們的狀態.

    SENTINEL master <master name> 顯示指定master的信息和狀態;

    SENTINEL slaves <master name> 顯示指定master的全部slave以及它們的狀態;

    SENTINEL get-master-addr-by-name <master name> 返回指定master的ip和端口,若是正在進行failover或者failover已經完成,將會顯示被提高爲master的slave的ip和端口。

    SENTINEL reset <pattern> 重置名字匹配該正則表達式的全部的master的狀態信息,清楚其以前的狀態信息,以及slaves信息。

    SENTINEL failover <master name> 強制sentinel執行failover,而且不須要獲得其餘sentinel的贊成。可是failover後會將最新的配置發送給其餘sentinel。

 redis集羣

redis貌似有不少種集羣,這裏咱們先介紹其官方集羣redis-cluster,和上面同樣不會說明不少原理性的東西,會詳細說明搭建過程:

推薦一片博客:redis-cluster 雖然是照這片博客作的,可是每一個步驟都有實際操做!

redis版本:

[root@monitor redis_cluster]# redis-server --version
Redis server v=4.0.0 sha=00000000:0 malloc=libc bits=64 build=cc318d51eae57ee3
[root@monitor redis_cluster]# 

環境準備: 兩個虛擬機,模擬6臺redis服務:

主機: 10.9.8.222  
  端口: 7000 7001 7002 主機:
10.9.8.223
  端口: 7003 7004 7005

在redis的安裝目錄/usr/local/redis下面建立以下文件:

#主機10.9.8.222下面的文件
redis_cluster ├──
7000 │   └── redis.conf ├── 7001 │   └── redis.conf ├── 7002   └── redis.conf
#主機109.8.223下面的文件 redis_cluster ├──
7003 │   └── redis.conf ├── 7004 │   └── redis.conf ├── 7005 │   └── redis.conf

每一個配置文件中須要更改以下幾項:

port  7000                               //每一個配置文件綁定各自的端口號        
bind 本機ip                              //能夠設置爲0.0.0.0,也能夠設置爲監聽本機ip和迴環ip
daemonize    yes                         //redis後臺運行
pidfile  /var/run/redis_7000.pid         //pidfile文件對應7000,7001,7002
cluster-enabled  yes                     //開啓集羣 把註釋#去掉
cluster-config-file  nodes_7000.conf     //集羣的配置  配置文件首次啓動自動生成 7000,7001,7002
cluster-node-timeout  15000              //請求超時  默認15秒,可自行設置
appendonly  yes                          //aof日誌開啓  有須要就開啓,它會每次寫操做都記錄一條日誌

標紅的選項爲開啓集羣的配置,注意配置中的不一樣端口對應不一樣的文件。

配置文件完成以後,就是啓用redis。[下面是redis-server的幫助文檔,採用指定配置文件啓動方式]

[root@monitor redis]# 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

啓動以後,查看端口,會發現redis不只會自動監聽配置的端口,還會監聽比當前端口大10000的端口。

[root@test2 src]# netstat -lntp   【這是222這臺主機上的,223主機再也不列出】
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:17002         0.0.0.0:*               LISTEN      102600/redis-server 
tcp        0      0 10.9.8.222:17002        0.0.0.0:*               LISTEN      102600/redis-server 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1315/sshd           
tcp        0      0 127.0.0.1:7000          0.0.0.0:*               LISTEN      102595/redis-server 
tcp        0      0 10.9.8.222:7000         0.0.0.0:*               LISTEN      102595/redis-server 
tcp        0      0 127.0.0.1:7001          0.0.0.0:*               LISTEN      102605/redis-server 
tcp        0      0 10.9.8.222:7001         0.0.0.0:*               LISTEN      102605/redis-server 
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2895/master         
tcp        0      0 127.0.0.1:7002          0.0.0.0:*               LISTEN      102600/redis-server 
tcp        0      0 10.9.8.222:7002         0.0.0.0:*               LISTEN      102600/redis-server 
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      78604/zabbix_agentd 
tcp        0      0 127.0.0.1:17000         0.0.0.0:*               LISTEN      102595/redis-server 
tcp        0      0 10.9.8.222:17000        0.0.0.0:*               LISTEN      102595/redis-server 
tcp        0      0 127.0.0.1:17001         0.0.0.0:*               LISTEN      102605/redis-server 
tcp        0      0 10.9.8.222:17001        0.0.0.0:*               LISTEN      102605/redis-server 

全部的redis服務啓動以後就是建立集羣:使用redis自帶的工具:redis-trib.rb,在/usr/local/redis/src下面。

[root@monitor src]# ./redis-trib.rb create --replicas 1 10.9.8.222:7000 10.9.8.222:7001 10.9.8.222:7002 10.9.8.223:7003 10.9.8.223:7004 10.9.8.223:7005
/usr/bin/env: ruby: No such file or directory
[root@monitor src]# 

#命令中--replicas 1表示爲每一個master建立一個從。

而後安裝ruby:

[root@monitor src]# yum install -y ruby           #安裝ruby
[root@monitor src]# ./redis-trib.rb create --replicas 1 10.9.8.222:7000 10.9.8.222:7001 10.9.8.222:7002 10.9.8.223:7003 10.9.8.223:7004 10.9.8.223:7005        #繼續執行
/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 ./redis-trib.rb:25:in `<main>'
[root@monitor src]# gem install redis          #執行以後會發現仍然報錯,要求ruby>2.2.2,centos自帶的ruby好像是2.0吧!
Fetching: redis-4.1.0.gem (100%)
ERROR:  Error installing redis:
    redis requires Ruby version >= 2.2.2

升級ruby

百度有不少說怎麼升級的,這裏提供一個地址:升級ruby, 我本身是按照這個方法解決的。

[root@monitor src]# gem sources -a http://mirrors.aliyun.com/rubygems/
http://mirrors.aliyun.com/rubygems/ added to sources
[root@monitor src]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net
gpg: requesting key 39499BDB from hkp server keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <mpapis@gmail.com>" imported
gpg: key 39499BDB: public key "Piotr Kuczynski <piotr.kuczynski@gmail.com>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 2
gpg:               imported: 2  (RSA: 2)
[root@monitor src]# curl -sSL https://get.rvm.io | bash -s stable
Downloading https://github.com/rvm/rvm/archive/1.29.7.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.7/1.29.7.tar.gz.asc
gpg: Signature made Thu 03 Jan 2019 05:01:48 PM EST using RSA key ID 39499BDB
gpg: Good signature from "Piotr Kuczynski <piotr.kuczynski@gmail.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 7D2B AF1C F37B 13E2 069D  6956 105B D0E7 3949 9BDB
GPG verified '/usr/local/rvm/archives/rvm-1.29.7.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.
  * Please do NOT forget to add your users to the rvm group.
     The installer no longer auto-adds root or users to the rvm group. Admins must do this.
     Also, please note that group memberships are ONLY evaluated at login time.
     This means that users must log out then back in before group membership takes effect!
[root@monitor src]# source /etc/profile.d/rvm.sh
[root@monitor src]# rvm install 2.5
Searching for binary rubies, this might take some time.
No binary rubies available for: centos/7/x86_64/ruby-2.5.3.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: patch, autoconf, automake, bison, bzip2, gcc-c++, libffi-devel, libtool, patch, readline-devel, sqlite-devel, zlib-devel, openssl-devel.................................
Requirements installation successful.
-bash: /usr/local/rvm/scripts/functions/manage/install/centos: No such file or directory
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-2.5.3, this may take a while depending on your cpu(s)...
ruby-2.5.3 - #downloading ruby-2.5.3, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13.5M  100 13.5M    0     0  83242      0  0:02:50  0:02:50 --:--:--  123k
ruby-2.5.3 - #extracting ruby-2.5.3 to /usr/local/rvm/src/ruby-2.5.3.....
ruby-2.5.3 - #configuring...................................................................
ruby-2.5.3 - #post-configuration..
ruby-2.5.3 - #compiling.....................................................................................
ruby-2.5.3 - #installing.............................
ruby-2.5.3 - #making binaries executable..
ruby-2.5.3 - #downloading rubygems-2.7.9
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  842k  100  842k    0     0   155k      0  0:00:05  0:00:05 --:--:--  176k
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.5.3 - #extracting rubygems-2.7.9.....
ruby-2.5.3 - #removing old rubygems........
ruby-2.5.3 - #installing rubygems-2.7.9....................................
ruby-2.5.3 - #gemset created /usr/local/rvm/gems/ruby-2.5.3@global
ruby-2.5.3 - #importing gemset /usr/local/rvm/gemsets/global.gems................................................................
ruby-2.5.3 - #generating global wrappers.......
ruby-2.5.3 - #gemset created /usr/local/rvm/gems/ruby-2.5.3
ruby-2.5.3 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.5.3 - #generating default wrappers.......
ruby-2.5.3 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.5.3 - #complete 
Ruby was built without documentation, to build it run: rvm docs generate-ri
幾條命令的詳細過程

下面單列幾條命令:

gem sources -a http://mirrors.aliyun.com/rubygems/
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
source /etc/profile.d/rvm.sh
rvm install 2.5

#最後刪除原來的ruby
gem sources --remove https://rubygems.org/

#而後執行
[root@monitor src]# gem install redis #這步不執行,會報錯誤

而後執行建立集羣的命令,如果報以下錯誤:

[root@test2 src]# ./redis-trib.rb create --replicas 1 10.9.8.222:7000 10.9.8.222:7001 10.9.8.222:7002 10.9.8.223:7003 10.9.8.223:7004 10.9.8.223:7005
>>> Creating cluster
[ERR] Node 10.9.8.222:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
[root@test2 src]# 

或者:
Can I set the above configuration? (type 'yes' to accept): yes
/usr/local/rvm/scripts/gems/ruby-2.2.2/gems/redis-4.1.0/lib/redis/client.rb:124:in `call': ERR Slot 6918 is already busy (Redis::CommandError)

解決上述問題:鏈接上集羣的每個節點,執行flushdb命令解決第一個問題,執行cluster reset命令解決第二個問題。

繼續再執行上面的建立集羣命令:

[root@test2 src]# ./redis-trib.rb create --replicas 1 10.9.8.222:7000 10.9.8.222:7001 10.9.8.222:7002 10.9.8.223:7003 10.9.8.223:7004 10.9.8.223:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...                    #這裏會自動爲6個節點建立3個主從集羣的,下面是主從的信息
Using 3 masters:
10.9.8.222:7000
10.9.8.223:7003
10.9.8.222:7001
Adding replica 10.9.8.223:7004 to 10.9.8.222:7000
Adding replica 10.9.8.222:7002 to 10.9.8.223:7003
Adding replica 10.9.8.223:7005 to 10.9.8.222:7001
M: 2359d9cb8872a9adbb06a2c2c18363147006da81 10.9.8.222:7000
   slots:0-5460 (5461 slots) master
M: 15e9a0d363d8cdd50e4257237b158cf645960a0b 10.9.8.222:7001
   slots:10923-16383 (5461 slots) master
S: c40ad7c21e2b799a9289684b72c5cf88f0779be0 10.9.8.222:7002
   replicates 65f1666ceb5c383f281a5aba54e1b0ea8eb9ba45
M: 65f1666ceb5c383f281a5aba54e1b0ea8eb9ba45 10.9.8.223:7003
   slots:5461-10922 (5462 slots) master
S: ada5c0cfb0d4177cf7e4710d24f8ec3b80f5b411 10.9.8.223:7004
   replicates 2359d9cb8872a9adbb06a2c2c18363147006da81
S: c78fec0a2cd02b69bb9a2825d526cbc70890447d 10.9.8.223:7005
   replicates 15e9a0d363d8cdd50e4257237b158cf645960a0b
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 10.9.8.222:7000)
M: 2359d9cb8872a9adbb06a2c2c18363147006da81 10.9.8.222:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: c78fec0a2cd02b69bb9a2825d526cbc70890447d 10.9.8.223:7005
   slots: (0 slots) slave
   replicates 15e9a0d363d8cdd50e4257237b158cf645960a0b
M: 15e9a0d363d8cdd50e4257237b158cf645960a0b 10.9.8.222:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 65f1666ceb5c383f281a5aba54e1b0ea8eb9ba45 10.9.8.223:7003
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: c40ad7c21e2b799a9289684b72c5cf88f0779be0 10.9.8.222:7002
   slots: (0 slots) slave
   replicates 65f1666ceb5c383f281a5aba54e1b0ea8eb9ba45
S: ada5c0cfb0d4177cf7e4710d24f8ec3b80f5b411 10.9.8.223:7004
   slots: (0 slots) slave
   replicates 2359d9cb8872a9adbb06a2c2c18363147006da81
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@test2 src]#

而後能夠連上集羣查詢對應的集羣信息:

[root@monitor redis_cluster]# redis-cli -p 7004
127.0.0.1:7004> cluster slots
1) 1) (integer) 0                          #每一個集羣的slot信息
   2) (integer) 5460
   3) 1) "10.9.8.222"
      2) (integer) 7000
      3) "2359d9cb8872a9adbb06a2c2c18363147006da81"
   4) 1) "10.9.8.223"
      2) (integer) 7004
      3) "ada5c0cfb0d4177cf7e4710d24f8ec3b80f5b411"
2) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.9.8.222"
      2) (integer) 7001
      3) "15e9a0d363d8cdd50e4257237b158cf645960a0b"
   4) 1) "10.9.8.223"
      2) (integer) 7005
      3) "c78fec0a2cd02b69bb9a2825d526cbc70890447d"
3) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "10.9.8.223"
      2) (integer) 7003
      3) "65f1666ceb5c383f281a5aba54e1b0ea8eb9ba45"
   4) 1) "10.9.8.222"
      2) (integer) 7002
      3) "c40ad7c21e2b799a9289684b72c5cf88f0779be0"
127.0.0.1:7004>

redis集羣在建立的時候不能配置密碼認證,所以在集羣建立完成以後能夠配置密碼認證:

[ops@vm51 src]$ redis-cli -p 6380 -h 10.9.68.51
10.9.68.51:6380> config set requirepass helloredis
OK
10.9.68.51:6380> auth helloredis
OK
10.9.68.51:6380> config rewrite

至此集羣建立完!

相關文章
相關標籤/搜索