Redis 多機多節點集羣搭建方案(5.0版本)

>搭建環境:

a)redis的安裝包,redis-5.0.8.tar.gz

b)gcc安裝包gcc_rpm.tar.gz

c)VM10

d)CentOS鏡像文件

>搭建步驟:

1、準備工作:

(1)在linux下新建一個存儲集羣的目錄redis-cluster:

(2)檢查是否安裝gcc:

2、解壓編譯安裝一個Redis服務

1)將redis-5.0.8.tar.gz放到目錄下

(2)解壓:tar xzf redis-5.0.8.tar.gz

3)進入目錄:cd redis-5.0.8

4)編譯安裝:make install PREFIX=/home/dareway/redis-cluster/redis-5.0.8

5)修改配置文件(說明可見:https://blog.csdn.net/AhaQianxun/article/details/107014991):

啓動服務:

[dareway@localhost redis-cluster]$ ./redis-5.0.8/bin/redis-server ./redis-5.0.8/redis.conf 
1744:C 28 Jun 2020 19:34:08.081 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1744:C 28 Jun 2020 19:34:08.081 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=1744, just started
1744:C 28 Jun 2020 19:34:08.081 # Configuration loaded

查看進程可以看到在6379默認端口:

[dareway@localhost redis-cluster]$ ps -ef|grep redis
dareway    1745      1  0 19:34 ?        00:00:00 ./redis-5.0.8/bin/redis-server *:6379
dareway    1750   1510  0 19:34 pts/0    00:00:00 grep --color=auto redis

使用redis-cli 連接測試:

[dareway@localhost redis-cluster]$ ./redis-5.0.8/bin/redis-cli
127.0.0.1:6379> 

可以使用Jdis客戶端連接:

		// jedis
		Jedis jedis = new Jedis("192.168.79.129", 6379);
		jedis.auth("123456");
		jedis.set("name", "redis-value");
		String value = jedis.get("name");
		System.out.println(value);
		jedis.close();

出現問題:連接超時

這裏綁定了本機,我們把這個備註掉;

# bind 127.0.0.1

配置完後 

[root@localhost redis]# ./bin/redis-cli shutdown

[root@localhost redis]# ./bin/redis-server ./redis.conf

要重啓下redis服務;

 

又出現問題:遠程連接Redis自我保護

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: 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.
	at redis.clients.jedis.Protocol.processError(Protocol.java:127)
	at redis.clients.jedis.Protocol.process(Protocol.java:161)
	at redis.clients.jedis.Protocol.read(Protocol.java:215)
	at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
	at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
	at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:96)
	at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
	at redis.clients.jedis.Connection.sendCommand(Connection.java:117)
	at redis.clients.jedis.BinaryClient.auth(BinaryClient.java:564)
	at redis.clients.jedis.BinaryJedis.auth(BinaryJedis.java:2138)
	at com.dareway.test.RedisConnTest.main(RedisConnTest.java:16)

有兩種方法 解決

第一種 直接去掉自我保護功能(不推薦)

[root@localhost redis]# vi /usr/local/redis/redis.conf

進入配置

找到 protected-mode yes 改成 no即可

第二種 設置redis連接密碼

進入客戶端

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> config set requirepass 123456

設置密碼 123456

 

127.0.0.1:6379> quit

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> auth 123456

OK

 

再測試一下,可以了

3、多機多節點Redis集羣搭建(3主 + 6從)

克隆三臺CentOS虛擬機,分別在每臺虛擬機上8001-8003端口創建三個節點,注意要修改配置。

在每臺機器redis-cluster目錄下,mkdir 8001 8002 8003,可以從上面安裝過的6379拷貝配置文件到文件夾下分別進行修改,也可以將整個5.0目錄分別拷貝到8001-8003。

(1)分別修改配置(參考自己實際的IP和端口進行調整配置):

port 7001  //六個節點配置文件分別是7001-7003

bind 192.168.79.129    //默認ip爲127.0.0.1 需要改爲其他節點機器可訪問的ip 否則創建集羣時無法訪,和單機集羣有區別

daemonize yes        //redis後臺運行

pidfile /var/run/redis_7001.pid   //pidfile文件對應7001-7003

cluster-enabled yes   //開啓集羣

cluster-config-file nodes_7001.conf  //保存節點配置,自動創建,自動更新對應7001-7003

cluster-node-timeout 5000    //集羣超時時間,節點超過這個時間沒反應就斷定是宕機

appendonly yes   //存儲方式,aof,將寫操作記錄保存到日誌中

dbfilename dump_7001.rdb

補充配置:masterauth xxx //設置集羣節點間訪問密碼

 

(2)分別啓動多個節點:

[dareway@localhost redis-cluster]$ ps -ef|grep redis
dareway    2146      1  0 20:03 ?        00:00:00 ./8001/redis-5.0.8/bin/redis-server 192.168.79.129:8001 [cluster]
dareway    2151      1  0 20:04 ?        00:00:00 ./8002/redis-5.0.8/bin/redis-server 192.168.79.129:8002 [cluster]
dareway    2156      1  0 20:04 ?        00:00:00 ./8003/redis-5.0.8/bin/redis-server 192.168.79.129:8003 [cluster]
dareway    2162   1510  0 20:04 pts/0    00:00:00 grep --color=auto redis

(3)開放集羣端口或者直接幹掉防火牆

systemctl stop firewalld.service

(4)通過redis-cli 命令創建集羣

./redis-5.0.8/bin/redis-cli --cluster create 192.168.79.129:8001 192.168.79.130:8001 192.168.79.131:8001 192.168.79.129:8002 192.168.79.129:8003 192.168.79.130:8002 192.168.79.130:8003 192.168.79.131:8002 192.168.79.131:8003 --cluster-replicas 2 -a 123456

2代表一個master2slave,前三個ipmaster。

補充:如果未設置密碼 無需 -a xxxxxx 命令

運行結果:

[root@localhost redis-cluster]# systemctl stop firewalld.service
[root@localhost redis-cluster]# ./redis-5.0.8/bin/redis-cli --cluster create 192.168.79.129:8001 192.168.79.130:8001 192.168.79.131:8001 192.168.79.129:8002 192.168.79.129:8003 192.168.79.130:8002 192.168.79.130:8003 192.168.79.131:8002 192.168.79.131:8003 --cluster-replicas 2
>>> Performing hash slots allocation on 9 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.79.130:8002 to 192.168.79.129:8001
Adding replica 192.168.79.131:8002 to 192.168.79.129:8001
Adding replica 192.168.79.129:8003 to 192.168.79.130:8001
Adding replica 192.168.79.131:8003 to 192.168.79.130:8001
Adding replica 192.168.79.130:8003 to 192.168.79.131:8001
Adding replica 192.168.79.129:8002 to 192.168.79.131:8001
M: 6b6fe21cb7a9eb1d39bd1124b462026018131246 192.168.79.129:8001
   slots:[0-5460] (5461 slots) master
M: 9e81870e2a4beeb3568f9fa9c85d82bc93b70e29 192.168.79.130:8001
   slots:[5461-10922] (5462 slots) master
M: 0c15b4949bdfe04b4a3d31bc4fd3d1109271d6cf 192.168.79.131:8001
   slots:[10923-16383] (5461 slots) master
S: a32f31b3df9210b0f19ba132f158257ac2ac5265 192.168.79.129:8002
   replicates 0c15b4949bdfe04b4a3d31bc4fd3d1109271d6cf
S: b687a59fc61eda12defdd477bd4b0da422eb5106 192.168.79.129:8003
   replicates 9e81870e2a4beeb3568f9fa9c85d82bc93b70e29
S: e90f0459e297e3c0714bde2d5552530749237295 192.168.79.130:8002
   replicates 6b6fe21cb7a9eb1d39bd1124b462026018131246
S: f8e4acbf4e072eb2b9ac53921af83aee4acabfc9 192.168.79.130:8003
   replicates 0c15b4949bdfe04b4a3d31bc4fd3d1109271d6cf
S: 630f84d3c456d7ec5ee90931c5efabd7e4fe2a37 192.168.79.131:8002
   replicates 6b6fe21cb7a9eb1d39bd1124b462026018131246
S: e28d453f3bc528c9eb595643f1bad69b74848698 192.168.79.131:8003
   replicates 9e81870e2a4beeb3568f9fa9c85d82bc93b70e29
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.79.129:8001)
M: 6b6fe21cb7a9eb1d39bd1124b462026018131246 192.168.79.129:8001
   slots:[0-5460] (5461 slots) master
   2 additional replica(s)
S: e90f0459e297e3c0714bde2d5552530749237295 192.168.79.130:8002
   slots: (0 slots) slave
   replicates 6b6fe21cb7a9eb1d39bd1124b462026018131246
S: e28d453f3bc528c9eb595643f1bad69b74848698 192.168.79.131:8003
   slots: (0 slots) slave
   replicates 9e81870e2a4beeb3568f9fa9c85d82bc93b70e29
M: 0c15b4949bdfe04b4a3d31bc4fd3d1109271d6cf 192.168.79.131:8001
   slots:[10923-16383] (5461 slots) master
   2 additional replica(s)
S: b687a59fc61eda12defdd477bd4b0da422eb5106 192.168.79.129:8003
   slots: (0 slots) slave
   replicates 9e81870e2a4beeb3568f9fa9c85d82bc93b70e29
S: f8e4acbf4e072eb2b9ac53921af83aee4acabfc9 192.168.79.130:8003
   slots: (0 slots) slave
   replicates 0c15b4949bdfe04b4a3d31bc4fd3d1109271d6cf
S: a32f31b3df9210b0f19ba132f158257ac2ac5265 192.168.79.129:8002
   slots: (0 slots) slave
   replicates 0c15b4949bdfe04b4a3d31bc4fd3d1109271d6cf
S: 630f84d3c456d7ec5ee90931c5efabd7e4fe2a37 192.168.79.131:8002
   slots: (0 slots) slave
   replicates 6b6fe21cb7a9eb1d39bd1124b462026018131246
M: 9e81870e2a4beeb3568f9fa9c85d82bc93b70e29 192.168.79.130:8001
   slots:[5461-10922] (5462 slots) master
   2 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

顯示分配哈希槽,啓動成功,可以用了。

(5)驗證連接:

./redis-cli -h 10.1.50.207 -c -p 8001 -a redis123-a訪問服務端密碼,-c表示集羣模式,指定ip地址和端口號)

cluster info查看集羣信息

./redis-cli --cluster info 10.1.50.208:8001 -a redis123檢查集羣狀態

[root@localhost redis-cluster]# ./redis-5.0.8/bin/redis-cli -h 192.168.79.130 -c -p 8001
192.168.79.130:8001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:3
cluster_current_epoch:9
cluster_my_epoch:2
cluster_stats_messages_ping_sent:715
cluster_stats_messages_pong_sent:275
cluster_stats_messages_fail_sent:24
cluster_stats_messages_sent:1014
cluster_stats_messages_ping_received:275
cluster_stats_messages_pong_received:216
cluster_stats_messages_received:491

lettuce客戶端連接測試:


		RedisURI redisUri = RedisURI.builder().withHost("192.168.79.129").withPort(8001)
				.withTimeout(Duration.of(60, ChronoUnit.SECONDS)).build();
		RedisClient redisClient = RedisClient.create(redisUri);

		StatefulRedisConnection<String, String> connection = redisClient.connect();
		// 創建線程安全的連接
		RedisCommands<String, String> redisCommands = connection.sync();

		String result = redisCommands.set("RedisLettuce-01", "RedisLettuce-01");

		System.out.println(result);

		System.out.println(redisCommands.get("RedisLettuce-01"));

		connection.close();
		redisClient.shutdown();

搭建完成!

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