1. 程式人生 > >redis4.0.2版本叢集搭建

redis4.0.2版本叢集搭建

redhat7下redis原始碼編譯安裝,需要先安裝pcre,openssl等依賴。

yum install openssl-devel pcre-devel

然後下載redis-4.0.2.tar.gz,解壓,進入redis-4.0.2解壓完的目錄,以root使用者執行make && make install

make install會將src下面可執行程式如redis-server,redis-cli,redis-check-aof,redis-check-rdb,redis-benchmark安裝到/usr/local/bin目錄下。

redis4.0.2cluster搭建相對於單節點搭建稍微複雜一點,就是要為每個節點單獨配置一個配置檔案,而且啟動的時候必須進入配置檔案所在的目錄,不能通過redis-server 指定配置檔案絕對路徑或者相對路徑來啟動例項,另外在配置檔案中需要開啟cluster-enabled配置,預設是不開啟的。這裡假設在一個伺服器上開啟6個節點構建叢集。6個節點對應的埠分別為6379,6380,6381,6382,6383,6384。以其中一個6387/redis.conf配置檔案為例:

bind 192.168.42.128
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
appendonly yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000

其中cluster-enabled,cluster-config-file是必須配置的,另外為了區別本機上的其他節點,port,pidfile做配置,其餘配置基本和以前版本沒有區別。

依次啟動每一個節點:

[[email protected] 6379]# /usr/local/bin/redis-server redis.conf 
5930:C 28 Nov 03:25:09.979 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5930:C 28 Nov 03:25:09.979 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5930, just started
5930:C 28 Nov 03:25:09.979 # Configuration loaded
[
[email protected]
6379]# cd ../6380/ [[email protected] 6380]# /usr/local/bin/redis-server redis.conf 5935:C 28 Nov 03:25:25.177 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5935:C 28 Nov 03:25:25.177 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5935, just started 5935:C 28 Nov 03:25:25.177 # Configuration loaded [
[email protected]
6380]# cd ../6381/ [[email protected] 6381]# /usr/local/bin/redis-server redis.conf 5940:C 28 Nov 03:25:35.654 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5940:C 28 Nov 03:25:35.655 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5940, just started 5940:C 28 Nov 03:25:35.655 # Configuration loaded [[email protected] 6381]# cd ../6382/ [[email protected] 6382]# /usr/local/bin/redis-server redis.conf 5945:C 28 Nov 03:25:44.166 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5945:C 28 Nov 03:25:44.166 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5945, just started 5945:C 28 Nov 03:25:44.166 # Configuration loaded [[email protected] 6382]# cd ../6383/ [[email protected] 6383]# /usr/local/bin/redis-server redis.conf 5950:C 28 Nov 03:25:52.197 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5950:C 28 Nov 03:25:52.197 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5950, just started 5950:C 28 Nov 03:25:52.197 # Configuration loaded [[email protected] 6383]# cd ../6484/ -bash: cd: ../6484/: No such file or directory [[email protected] 6383]# cd ../6384/ [[email protected] 6384]# /usr/local/bin/redis-server redis.conf 5957:C 28 Nov 03:26:06.756 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 5957:C 28 Nov 03:26:06.756 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=5957, just started 5957:C 28 Nov 03:26:06.756 # Configuration loaded

檢視啟動的節點

安裝rubygems

[[email protected] redis-4.0.2]# yum install rubygems
[[email protected] redis-4.0.2]# gem install redis -v 3.3.3
Fetching: redis-3.3.3.gem (100%)
Successfully installed redis-3.3.3
Parsing documentation for redis-3.3.3
Installing ri documentation for redis-3.3.3
1 gem installed

說明:如果你有多個虛擬機器,rubygems只需要在一臺機器上安裝,這裡指定gem install redis -v 3.3.3版本,因為最新的版本存在bug,最終執行redis-trib.rb命令構建叢集在哪臺機器上都可以。

構建叢集

[[email protected] redis-4.0.2]# src/redis-trib.rb create --replicas 1 192.168.42.128:6379 192.168.42.128:6380 192.168.42.128:6381 192.168.42.128:6382 192.168.42.128:6383 192.168.42.128:6384
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.42.128:6379
192.168.42.128:6380
192.168.42.128:6381
Adding replica 192.168.42.128:6382 to 192.168.42.128:6379
Adding replica 192.168.42.128:6383 to 192.168.42.128:6380
Adding replica 192.168.42.128:6384 to 192.168.42.128:6381
M: 8a3124f8fd8f9925b0202c4072fcf9fe34df4a61 192.168.42.128:6379
   slots:0-5460 (5461 slots) master
M: bfda1c760994f1805de78dfd692a19a3807431f5 192.168.42.128:6380
   slots:5461-10922 (5462 slots) master
M: 2a72906ca42ed83065f8ec8a60483ff5e6715bcd 192.168.42.128:6381
   slots:10923-16383 (5461 slots) master
S: dae96e813dc06ae7ad9b771cc4ec7887111ec32a 192.168.42.128:6382
   replicates 8a3124f8fd8f9925b0202c4072fcf9fe34df4a61
S: debbb67f323b41ee4dde48d904003340cde6507b 192.168.42.128:6383
   replicates bfda1c760994f1805de78dfd692a19a3807431f5
S: 10493890436c3a364ccbb640e137810ca4261fe1 192.168.42.128:6384
   replicates 2a72906ca42ed83065f8ec8a60483ff5e6715bcd
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.42.128:6379)
M: 8a3124f8fd8f9925b0202c4072fcf9fe34df4a61 192.168.42.128:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 10493890436c3a364ccbb640e137810ca4261fe1 192.168.42.128:6384
   slots: (0 slots) slave
   replicates 2a72906ca42ed83065f8ec8a60483ff5e6715bcd
S: dae96e813dc06ae7ad9b771cc4ec7887111ec32a 192.168.42.128:6382
   slots: (0 slots) slave
   replicates 8a3124f8fd8f9925b0202c4072fcf9fe34df4a61
S: debbb67f323b41ee4dde48d904003340cde6507b 192.168.42.128:6383
   slots: (0 slots) slave
   replicates bfda1c760994f1805de78dfd692a19a3807431f5
M: 2a72906ca42ed83065f8ec8a60483ff5e6715bcd 192.168.42.128:6381
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: bfda1c760994f1805de78dfd692a19a3807431f5 192.168.42.128:6380
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[[email protected] redis-4.0.2]#

檢驗叢集

三個主節點,三個從節點