redis-cluster集群的概念和工作原理
<code>在說明使用和安裝之前,先大概介紹下,Redis 集群是一個可以在多個 Redis 節點之間進行數據共享的設施。通過分區(partition)來提供一定程度的可用性(availability),即使集群中有一部分節點失效或者無法進行通訊,集群也可以繼續處理命令請求。
/<code>
- 架構圖
- 工作原理
<code>1、所有的redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和帶寬.
2、節點的fail是通過集群中超過半數的節點檢測失效時才生效
3、客戶端與Redis節點直連,不需要中間Proxy層,直接連接任意一個Master節點
4、redis-cluster把所有的物理節點映射到[0-16383]slot上,根據公式HASH_SLOT=CRC16(key) mod 16384,計算出映射到哪個分片上,然後Redis會去相應的節點進行操作
/<code>
- 優點和缺點
<code>1、優點:
(1)無需Sentinel哨兵監控,如果Master掛了,Redis Cluster內部自動將Slave切換Master
(2)可以進行水平擴容
(3)支持自動化遷移,當出現某個Slave宕機了,那麼就只有Master了,這時候的高可用性就無法很好的保證了,萬一master也宕機了,咋辦呢? 針對這種情況,如果說其他Master有多餘的Slave ,集群自動把多餘的Slave遷移到沒有Slave的Master 中。
2、缺點:
(1)批量操作是個坑
(2)資源隔離性較差,容易出現相互影響的情況。
/<code>
前期準備工作和說明
- 1、主機清單
<code>10.0.0.221 cyq-redis-01
10.0.0.224 cyq-redis-02
/<code>
- 2、前提2臺機器都安裝redis
<code>mkdir -p /data/redis
chmod 755 /data/redis
mkdir -p /data/logs/redis
chmod 777 /data/logs/redis
wget http://download.redis.io/releases/redis-3.2.11.tar.gz
tar -zxvf redis-3.2.11.tar.gz
mv redis-3.2.10 /usr/local/redis
cd /usr/local/redis
make
cd src
make PREFIX=/usr/local/redis install
/<code>
一、集群環境開始安裝
- 1、在cyq-redis-01 機器上執行
<code>mkdir /usr/local/redis-cluster
cd /usr/local/redis-cluster
mkdir 7001 7002 7003
cp /usr/local/redis/ /usr/local/redis-cluster/7001/ -r
cp /usr/local/redis/ /usr/local/redis-cluster/7002/ -r
cp /usr/local/redis/ /usr/local/redis-cluster/7003/ -r
/<code>
<code>增加數據路徑
mkdir -p /data/redis/7001
mkdir -p /data/redis/7002
mkdir -p /data/redis/7003
/<code>
- 2、在cyq-redis-02機器上執行
<code>mkdir /usr/local/redis-cluster
cd /usr/local/redis-cluster
mkdir 7004 7005 7006
cp /usr/local/redis/ /usr/local/redis-cluster/7004/ -r
cp /usr/local/redis/ /usr/local/redis-cluster/7005/ -r
cp /usr/local/redis/ /usr/local/redis-cluster/7006/ -r
/<code>
<code>mkdir -p /data/redis/7004
mkdir -p /data/redis/7005
mkdir -p /data/redis/7006
/<code>
- 3、修改配置文件redis.conf(修改參數如下)
<code>port 7001 # 端口
bind 172.16.0.15 #綁定IP地址
daemonize yes #開啟後臺運行
pidfile /var/run/redis_7001.pid #pid文件
logfile "/data/logs/redis/redis7001.log" #日誌文件
dir /data/redis/7001 #數據目錄
cluster-enabled yes #啟用集群模式
cluster-config-file nodes.conf #生成的集群配置文件名稱,集群搭建成功後會自動生成,在工作目錄下
cluster-node-timeout 5000 #節點宕機發現時間,可以理解為主節點宕機後從節點升級為主節點時間
appendonly yes #開啟AOF模式
masterauth atx@7wixs
requirepass atx@7wixs
/<code>
<code>6臺redis都需要修改配置
sed -i "s#7001#7002#g" redis.conf
如果需要密碼認證,提前加入賬號:
masterauth atx@7wixs
requirepass atx@7wixs
/<code>
- 4、啟動
- 4-1、在cyq-redis-01執行
<code>/usr/local/redis-cluster/7001/bin/redis-server /usr/local/redis-cluster/7001/redis.conf
/usr/local/redis-cluster/7002/bin/redis-server /usr/local/redis-cluster/7002/redis.conf
/usr/local/redis-cluster/7003/bin/redis-server /usr/local/redis-cluster/7003/redis.conf
/<code>
- 4-2、在cyq-redis-02執行
<code>/usr/local/redis-cluster/7004/bin/redis-server /usr/local/redis-cluster/7004/redis.conf
/usr/local/redis-cluster/7005/bin/redis-server /usr/local/redis-cluster/7005/redis.conf
/usr/local/redis-cluster/7006/bin/redis-server /usr/local/redis-cluster/7006/redis.conf
/<code>
- 5、查看進程狀態
<code>[root@cyq-redis-02 ~]# ps -ef |grep redis
root 19755 1 0 16:32 ? 00:00:00 /usr/local/redis-cluster/7004/bin/redis-server 10.0.0.224:7004 [cluster]
root 19757 1 0 16:32 ? 00:00:00 /usr/local/redis-cluster/7005/bin/redis-server 10.0.0.224:7005 [cluster]
root 19763 1 0 16:32 ? 00:00:00 /usr/local/redis-cluster/7006/bin/redis-server 10.0.0.224:7006 [cluster]
/<code>
二、創建集群需要用到redis-trib這個命令,它依賴Ruby和RubyGems,因此我們要先安裝一下
- 1、安裝ruby環境和庫支持
<code>yum install ruby rubygems -y
gem install redis 或者gem install redis --version 3.3.3
/<code>
- 2、如果安裝過程中,提示ruby版本過低,可以安裝rvm管理ruby.
<code>#!/bin/bash
curl -sSL https://get.rvm.io | bash
source /usr/local/rvm/scripts/rvm
rvm list known
#rvm install 2.0.0-p247 (安裝指定版本)
rvm install 2.2.10
rvm list
rvm use
gem install redis
/<code>
- 3、設置密碼之後如果需要使用redis-trib.rb的各種命令
<code>如:./redis-trib.rb check 127.0.0.1:7000,則會報錯ERR] Sorry, can’t connect to node 127.0.0.1:7000
vim /usr/local/rvm/gems/ruby-2.2.10/gems/redis-4.1.0/lib/redis/client.rb 修改密碼password
require_relative "errors"
require "socket"
require "cgi"
class Redis
class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "atx@7wixs",
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:reconnect_delay => 0,
:reconnect_delay_max => 0.5,
:inherit_socket => false
}
/<code>
- 4、複製redis-trib.rb命令開始創建集群 cp /usr/local/redis/src/redis-trib.rb /usr/local/bin/redis-trib
<code>創建集群:
redis-trib create --replicas 1 10.0.0.221:7001 10.0.0.221:7002 10.0.0.221:7003 10.0.0.224:7004 10.0.0.224:7005 10.0.0.224:7006
命令的意義如下:
給定 redis-trib.rb 程序的命令是 create , 這表示我們希望創建一個新的集群。
選項 --replicas 1 表示我們希望為集群中的每個主節點創建一個從節點。
之後跟著的其他參數則是實例的地址列表, 我們希望程序使用這些地址所指示的實例來創建新集群。
簡單來說, 以上命令的意思就是讓 redis-trib 程序創建一個包含三個主節點和三個從節點的集群。
接著, redis-trib 會打印出一份預想中的配置給你看, 如果你覺得沒問題的話, 就可以輸入 yes , redis-trib 就會將這份配置應用到集群當中:
/<code>
<code>[root@cyq-redis-01 ~]# redis-trib create --replicas 1 10.0.0.221:7001 10.0.0.221:7002 10.0.0.221:7003 10.0.0.224:7004 10.0.0.224:7005 10.0.0.224:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.0.0.221:7001
10.0.0.224:7004
10.0.0.221:7002
Adding replica 10.0.0.224:7005 to 10.0.0.221:7001
Adding replica 10.0.0.221:7003 to 10.0.0.224:7004
Adding replica 10.0.0.224:7006 to 10.0.0.221:7002
M: 9d1ae0b77bdc3604bb604c64a957b75137517e26 10.0.0.221:7001
slots:0-5460 (5461 slots) master
M: bd79d80d1cb121094f718b7f53e8a8dfd4f2b7e1 10.0.0.221:7002
slots:10923-16383 (5461 slots) master
S: 6a1688eb39211c014d180bbbac67012a0097359a 10.0.0.221:7003
replicates bd46eddd2a3022723df19b3442c6500f51c7bbb3
M: bd46eddd2a3022723df19b3442c6500f51c7bbb3 10.0.0.224:7004
slots:5461-10922 (5462 slots) master
S: f3f2686e8240547c0c1d9c60c20cdd5b01a791b0 10.0.0.224:7005
replicates 9d1ae0b77bdc3604bb604c64a957b75137517e26
S: e5d4081e0c6e676bdfd93f5e7560ffd0cb03e5f9 10.0.0.224:7006
replicates bd79d80d1cb121094f718b7f53e8a8dfd4f2b7e1
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 10.0.0.221:7001)
M: 9d1ae0b77bdc3604bb604c64a957b75137517e26 10.0.0.221:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: bd79d80d1cb121094f718b7f53e8a8dfd4f2b7e1 10.0.0.221:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: f3f2686e8240547c0c1d9c60c20cdd5b01a791b0 10.0.0.224:7005
slots: (0 slots) slave
replicates 9d1ae0b77bdc3604bb604c64a957b75137517e26
S: 6a1688eb39211c014d180bbbac67012a0097359a 10.0.0.221:7003
slots: (0 slots) slave
replicates bd46eddd2a3022723df19b3442c6500f51c7bbb3
M: bd46eddd2a3022723df19b3442c6500f51c7bbb3 10.0.0.224:7004
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: e5d4081e0c6e676bdfd93f5e7560ffd0cb03e5f9 10.0.0.224:7006
slots: (0 slots) slave
replicates bd79d80d1cb121094f718b7f53e8a8dfd4f2b7e1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
/<code>
- 5、校驗命令
<code>[root@cyq-redis-01 ~]# redis-cli -h 10.0.0.221 -c -p 7001 -a atx@7wixs
10.0.0.221:7001> set aa "123456"
OK
10.0.0.221:7001> get aa
"123456"
10.0.0.221:7001>
[root@cyq-redis-02 ~]# redis-cli -h 10.0.0.224 -c -p 7004 -a atx@7wixs
10.0.0.224:7004> get aa
-> Redirected to slot [1180] located at 10.0.0.221:7001
"123456"
10.0.0.221:7001>
[root@cyq-redis-01 ~]# redis-cli -h 10.0.0.221 -c -p 7002 -a atx@7wixs
10.0.0.221:7002> getaa
(error) ERR unknown command 'getaa'
10.0.0.221:7002> get aa
-> Redirected to slot [1180] located at 10.0.0.221:7001
"123456"
10.0.0.221:7001>
/<code>
- 6、集群狀態
<code>[root@cyq-redis-01 ~]# redis-cli -h 10.0.0.221 -p 7001 -a atx@7wixs cluster nodes
bd79d80d1cb121094f718b7f53e8a8dfd4f2b7e1 10.0.0.221:7002 master - 0 1550657022706 2 connected 10923-16383
9d1ae0b77bdc3604bb604c64a957b75137517e26 10.0.0.221:7001 myself,master - 0 0 1 connected 0-5460
f3f2686e8240547c0c1d9c60c20cdd5b01a791b0 10.0.0.224:7005 slave 9d1ae0b77bdc3604bb604c64a957b75137517e26 0 1550657021705 5 connected
6a1688eb39211c014d180bbbac67012a0097359a 10.0.0.221:7003 slave bd46eddd2a3022723df19b3442c6500f51c7bbb3 0 1550657021204 4 connected
bd46eddd2a3022723df19b3442c6500f51c7bbb3 10.0.0.224:7004 master - 0 1550657021705 4 connected 5461-10922
e5d4081e0c6e676bdfd93f5e7560ffd0cb03e5f9 10.0.0.224:7006 slave bd79d80d1cb121094f718b7f53e8a8dfd4f2b7e1 0 1550657021204 6 connected
/<code>
- 7 redis 集群啟動和關閉腳本
<code>redis-cluster-start.sh
#!/bin/bash
source /etc/profile
/usr/local/redis-cluster/7001/bin/redis-server /usr/local/redis-cluster/7001/redis.conf
/usr/local/redis-cluster/7002/bin/redis-server /usr/local/redis-cluster/7002/redis.conf
/usr/local/redis-cluster/7003/bin/redis-server /usr/local/redis-cluster/7003/redis.conf
/usr/local/redis-cluster/7004/bin/redis-server /usr/local/redis-cluster/7004/redis.conf
/usr/local/redis-cluster/7005/bin/redis-server /usr/local/redis-cluster/7005/redis.conf
/usr/local/redis-cluster/7006/bin/redis-server /usr/local/redis-cluster/7006/redis.conf
/<code>
<code>redis-cluster-stop.sh
#!/bin/bash
source /etc/profile
/usr/local/redis-cluster/7001/bin/redis-cli -h 10.0.0.221 -p 7001 -a atx@7wixs shutdown
/usr/local/redis-cluster/7002/bin/redis-cli -h 10.0.0.221 -p 7002 -a atx@7wixs shutdown
/usr/local/redis-cluster/7003/bin/redis-cli -h 10.0.0.221 -p 7003 -a atx@7wixs shutdown
/usr/local/redis-cluster/7004/bin/redis-cli -h 10.0.0.224 -p 7004 -a atx@7wixs shutdown
/usr/local/redis-cluster/7005/bin/redis-cli -h 10.0.0.224 -p 7005 -a atx@7wixs shutdown
/usr/local/redis-cluster/7006/bin/redis-cli -h 10.0.0.224 -p 7006 -a atx@7wixs shutdown/<code>
閱讀更多 運維攻城獅 的文章