redis-5.0.9集羣部署

1.搭建集羣需要的環境

搭建集羣需要使用到官方提供的ruby腳本。

需要安裝ruby的環境。ruby安裝2.7.1

ruby安裝文檔詳見https://blog.csdn.net/baidu_38432732/article/details/106568431

redis-trib.rb腳本需要的ruby包:

需要上傳到linux服務。

安裝ruby的包:

下載地址https://rubygems.org/gems/rubygems-update-3.1.4.gem

安裝rubygems

[root@localhost ~]# ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
[root@localhost ~]# gem install rubygems-update-3.1.4.gem 
Successfully installed rubygems-update-3.1.4
Parsing documentation for rubygems-update-3.1.4
Installing ri documentation for rubygems-update-3.1.4
Done installing documentation for rubygems-update after 45 seconds
1 gem installed

2、redis安裝

1、redis包下載http://download.redis.io/releases/redis-5.0.9.tar.gz

# tar -xf redis-5.0.9.tar.gz 
# cd redis-5.0.9
# make
# make install prifix=/usr/local/redis

2、 將redis源碼下src目錄下的以下文件拷貝到我們的redis下

# cd redis-5.0.9/src
# mkdir /data/redis01
# cp redis-cli  redis.conf  redis-sentinel  redis-server  redis-trib.rb /data/redis01

3、修改redis配置文件

注意以下五個參數:

# vim redis.conf
protected-mode no                # 關閉保護模式
port 6379                        # 不同節點不同端口號
daemonize yes                    # 開啓後臺啓動
pidfile /var/run/redis_6379.pid  # 服務id文件名稱及地址更改防止衝突
# bind 127.0.0.1                 # 註釋掉綁定的127.0.0.1,服務對外開放

4、複製多個文件,分別拷貝到不同的主機上,並啓動各個節點服務

[root@localhost redis01]# ./redis-server redis.conf 
28245:C 05 Jun 2020 11:58:18.858 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
28245:C 05 Jun 2020 11:58:18.858 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=28245, just started
28245:C 05 Jun 2020 11:58:18.858 # Configuration loaded
[root@localhost redis01]# cd ../redis02/
[root@localhost redis02]# ./redis-server redis.conf 
28250:C 05 Jun 2020 11:58:28.074 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
28250:C 05 Jun 2020 11:58:28.074 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=28250, just started
28250:C 05 Jun 2020 11:58:28.074 # Configuration loaded

5、創建一個集羣

[root@localhost redis01]# ./redis-trib.rb create --replicas 1 192.168.0.242:6379 192.168.0.242:6380 192.168.0.243:6379 192.168.0.243:6380 192.168.0.244:6379 192.168.0.244:6380
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.

All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.

Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]

Example:
redis-cli --cluster create 192.168.0.242:6379 192.168.0.242:6380 192.168.0.243:6379 192.168.0.243:6380 192.168.0.244:6379 192.168.0.244:6380 --cluster-replicas 1

To get help about all subcommands, type:
redis-cli --cluster help

因爲我下載的是redis-5.0.9上面也說了現在redis--trib.rb全部移到了redis-cli,至此上面的ruby相關的資料都可以不安裝

接下來創建集羣,此時我們必須關閉redis的保護模式

[root@localhost redis01]# ./redis-cli --cluster create --cluster-replicas 1 192.168.0.242:6379 192.168.0.242:6380 192.168.0.243:6379 192.168.0.243:6380 192.168.0.244:6379 192.168.0.244:6380
[ERR] Node 192.168.0.242:6379 DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

[root@localhost redis01]# ./redis-cli --cluster create 192.168.0.242:6379 192.168.0.242:6380 192.168.0.243:6379 192.168.0.243:6380 192.168.0.244:6379 192.168.0.244:6380 --cluster-replicas 1
[ERR] Node 192.168.0.242:6379 DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

6、正式牀架集羣 


[root@localhost redis02]# cd ../redis01
[root@localhost redis01]# ./redis-cli --cluster create 192.168.0.242:6379 192.168.0.242:6380 192.168.0.243:6379 192.168.0.243:6380 192.168.0.244:6379 192.168.0.244:6380 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.0.243:6380 to 192.168.0.242:6379
Adding replica 192.168.0.244:6380 to 192.168.0.243:6379
Adding replica 192.168.0.242:6380 to 192.168.0.244:6379
M: 2db59cce8b73eac58f81147ce505f5a3ec8ee957 192.168.0.242:6379
   slots:[0-5460] (5461 slots) master
S: af8c284dda526f2142b117627e8b62f8511babae 192.168.0.242:6380
   replicates b52d403b848a83c1b4aa2a2a20c0c5a0bab233df
M: 1ee40f4166085844b0ed16b4aa56959f693dd76a 192.168.0.243:6379
   slots:[5461-10922] (5462 slots) master
S: 2d89cad0ab2a979474b84a3fd9d011f14b8beacb 192.168.0.243:6380
   replicates 2db59cce8b73eac58f81147ce505f5a3ec8ee957
M: b52d403b848a83c1b4aa2a2a20c0c5a0bab233df 192.168.0.244:6379
   slots:[10923-16383] (5461 slots) master
S: 20682cf03c7aa01eb84ac764a9d64e9ea7d6f0bd 192.168.0.244:6380
   replicates 1ee40f4166085844b0ed16b4aa56959f693dd76a
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.0.242:6379)
M: 2db59cce8b73eac58f81147ce505f5a3ec8ee957 192.168.0.242:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: b52d403b848a83c1b4aa2a2a20c0c5a0bab233df 192.168.0.244:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 2d89cad0ab2a979474b84a3fd9d011f14b8beacb 192.168.0.243:6380
   slots: (0 slots) slave
   replicates 2db59cce8b73eac58f81147ce505f5a3ec8ee957
S: 20682cf03c7aa01eb84ac764a9d64e9ea7d6f0bd 192.168.0.244:6380
   slots: (0 slots) slave
   replicates 1ee40f4166085844b0ed16b4aa56959f693dd76a
S: af8c284dda526f2142b117627e8b62f8511babae 192.168.0.242:6380
   slots: (0 slots) slave
   replicates b52d403b848a83c1b4aa2a2a20c0c5a0bab233df
M: 1ee40f4166085844b0ed16b4aa56959f693dd76a 192.168.0.243:6379
   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.

登錄集羣1,並set一個值

[root@localhost redis01]# ./redis-cli -h 192.168.0.242 -p 6379 -c
192.168.0.242:6379> set a 1
-> Redirected to slot [15495] located at 192.168.0.244:6379
OK
192.168.0.244:6379> get a
"1"

登錄之後的其他集羣,並get值

[root@localhost redis01]# ./redis-cli -h 192.168.0.242 -p 6380 -c
192.168.0.242:6380> get a
-> Redirected to slot [15495] located at 192.168.0.244:6379
"1"
192.168.0.244:6379> 
[root@localhost redis01]# ./redis-cli -h 192.168.0.243 -p 6380 -c
192.168.0.243:6380> get a
-> Redirected to slot [15495] located at 192.168.0.244:6379
"1"
192.168.0.244:6379> 
[root@localhost redis01]# ./redis-cli -h 192.168.0.243 -p 6379 -c
192.168.0.243:6379> get a
-> Redirected to slot [15495] located at 192.168.0.244:6379
"1"
192.168.0.244:6379> 
[root@localhost redis01]# ./redis-cli -h 192.168.0.244 -p 6379 -c
192.168.0.244:6379> get a
"1"
192.168.0.244:6379> 
[root@localhost redis01]# ./redis-cli -h 192.168.0.244 -p 6380 -c
192.168.0.244:6380> get a
-> Redirected to slot [15495] located at 192.168.0.244:6379
"1"

至此所有的集羣節點都是通的,redis集羣部署成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章