REDIS集群搭建及管理


使用六台服务器来搭建REDIS集群,每台跑一个REDIS数据库

REDIS集群搭建

1、在其中一台安装REDIS

1、安装依赖包
    [root@ene deps]# yum install -y gcc gcc-c++ libstdc++-devel
2、进入deps执行命令
    [root@ene deps]# cd deps/
    [root@ene deps]# make hiredis jemalloc linenoise lua geohash-int
3、返回上一层执行make
4、为常用的命令创建软连接
    [root@ene src]# ln -s redis-server /bin
    [root@ene src]# ln -s redis-cli /bin
5、启动REDIS数据库:redis-server

启动REDIS客户端:redis-cli

2、将安装好的REDIS传到其他主机

	scp -r /redis/redis-5.0.5/* root@172.16.16.5:/redis/
	scp -r /redis/redis-5.0.5/* root@172.16.16.6:/redis/
	scp -r /redis/redis-5.0.5/* root@172.16.16.7:/redis/
	scp -r /redis/redis-5.0.5/* root@172.16.16.11:/redis/
	scp -r /redis/redis-5.0.5/* root@172.16.16.10:/redis/
	'复制常用命令到/bin'
	cp redis-server /bin
	cp redis-cli /bin
	cp redis-trib.rb /bin

3、各个主机创建REDIS集群目录,放入redis.conf并修改

	mkdir /redis-cluster-7000
	cd /redis-cluster-7000/
	cp /redis/redis-5.0.5/redis.conf .
	[root@redhat-1-6 redis-cluster-7000]# vim redis.conf 
		bind 172.16.16.4   '外部能访问的IP'
		port 7000   '改成相应端口号'
		daemonize yes
		cluster-enabled yes
		cluster-config-file nodes.conf
		cluster-node-timeout 5000
		appendonly yes

4、启动各个REDIS

	[root@redhat-1-6 redis-cluster-7000]# redis-server redis.conf 
	36502:C 24 Feb 2019 23:00:51.700 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
	36502:C 24 Feb 2019 23:00:51.701 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=36502, just started
	36502:C 24 Feb 2019 23:00:51.701 # Configuration loaded
	
	[root@redhat-1-6 redis-cluster-7000]# ps -elf | grep 'redis'
	5 S root      35760      1  0  80   0 - 40408 ep_pol 18:11 ?        00:00:31 redis-server 127.0.0.1:6379 
	0 S root      35765  24142  0  80   0 -  5999 n_tty_ 18:12 pts/1    00:00:00 redis-cli
	5 S root      36503      1  0  80   0 - 38104 ep_pol 23:00 ?        00:00:00 redis-server 172.16.16.4:7000 [cluster]
	0 S root      36533  36308  0  80   0 - 25813 pipe_w 23:01 pts/0    00:00:00 grep redis

5、安装ruby及其依赖包(redis-trib.rb集群管理工具依赖ruby)

这一步可选做,不影响之后对集群搭建与管理

	[root@redhat-1-6 redis-cluster-7000]# yum -y install ruby ruby-devel rubygems rpm-build
	

6、搭建集群

	[root@redhat-1-6 redis-cluster-7000]# redis-cli --cluster 
		create 172.16.16.4:7000 172.16.16.5:7001 172.16.16.6:7002 172.16.16.7:7003 172.16.16.10:7004 172.16.16.11:7005 
		--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 172.16.16.10:7004 to 172.16.16.4:7000
	Adding replica 172.16.16.11:7005 to 172.16.16.5:7001
	Adding replica 172.16.16.7:7003 to 172.16.16.6:7002
	M: a9c4f45d775c8e549264939f9a45b3b30f120498 172.16.16.4:7000
	   slots:[0-5460] (5461 slots) master
	M: 16601b6cc31e1136ebff53d293c6910b238af0b4 172.16.16.5:7001
	   slots:[5461-10922] (5462 slots) master
	M: e610e15d5d18c9e3046ad1269ffcf9f191c306dc 172.16.16.6:7002
	   slots:[10923-16383] (5461 slots) master
	S: 2cf48f5e93af3cdfd37a1e82f0cb144be7da2721 172.16.16.7:7003
	   replicates e610e15d5d18c9e3046ad1269ffcf9f191c306dc
	S: 251e7635c170cbf1bf106e989d595b7cbd91d178 172.16.16.10:7004
	   replicates a9c4f45d775c8e549264939f9a45b3b30f120498
	S: 4445aef1ae3faf44b8a851ec6ab9cf7e55f361e5 172.16.16.11:7005
	   replicates 16601b6cc31e1136ebff53d293c6910b238af0b4
	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 172.16.16.4:7000)
	M: a9c4f45d775c8e549264939f9a45b3b30f120498 172.16.16.4:7000
	   slots:[0-5460] (5461 slots) master
	   1 additional replica(s)
	S: 251e7635c170cbf1bf106e989d595b7cbd91d178 172.16.16.10:7004
	   slots: (0 slots) slave
	   replicates a9c4f45d775c8e549264939f9a45b3b30f120498
	S: 4445aef1ae3faf44b8a851ec6ab9cf7e55f361e5 172.16.16.11:7005
	   slots: (0 slots) slave
	   replicates 16601b6cc31e1136ebff53d293c6910b238af0b4
	M: e610e15d5d18c9e3046ad1269ffcf9f191c306dc 172.16.16.6:7002
	   slots:[10923-16383] (5461 slots) master
	   1 additional replica(s)
	M: 16601b6cc31e1136ebff53d293c6910b238af0b4 172.16.16.5:7001
	   slots:[5461-10922] (5462 slots) master
	   1 additional replica(s)
	S: 2cf48f5e93af3cdfd37a1e82f0cb144be7da2721 172.16.16.7:7003
	   slots: (0 slots) slave
	   replicates e610e15d5d18c9e3046ad1269ffcf9f191c306dc
	[OK] All nodes agree about slots configuration.
	>>> Check for open slots...
	>>> Check slots coverage...
	[OK] All 16384 slots covered.

查看集群状态信息

这里是我后来添加的,与上面的集群搭建信息不太相符

	[root@redhat-1-6 ~]# redis-cli --cluster check 172.16.16.4:7000
	172.16.16.4:7000 (a9c4f45d...) -> 0 keys | 6553 slots | 1 slaves.
	172.16.16.4:7006 (aea1598e...) -> 0 keys | 6555 slots | 1 slaves.
	172.16.16.7:7003 (2cf48f5e...) -> 0 keys | 3276 slots | 1 slaves.
	[OK] 0 keys in 3 masters.
	0.00 keys per slot on average.
	>>> Performing Cluster Check (using node 172.16.16.4:7000)
	M: a9c4f45d775c8e549264939f9a45b3b30f120498 172.16.16.4:7000
	   slots:[4916-6826],[7827-12287],[12927-13107] (6553 slots) master
	   1 additional replica(s)
	M: aea1598e2e496ebcc0f9b2415c5b890e400582bc 172.16.16.4:7006
	   slots:[0-4915],[6827-7826],[12288-12926] (6555 slots) master
	   1 additional replica(s)
	S: 251e7635c170cbf1bf106e989d595b7cbd91d178 172.16.16.10:7004
	   slots: (0 slots) slave
	   replicates aea1598e2e496ebcc0f9b2415c5b890e400582bc
	S: 4445aef1ae3faf44b8a851ec6ab9cf7e55f361e5 172.16.16.11:7005
	   slots: (0 slots) slave
	   replicates a9c4f45d775c8e549264939f9a45b3b30f120498
	S: e610e15d5d18c9e3046ad1269ffcf9f191c306dc 172.16.16.6:7002
	   slots: (0 slots) slave
	   replicates 2cf48f5e93af3cdfd37a1e82f0cb144be7da2721
	M: 2cf48f5e93af3cdfd37a1e82f0cb144be7da2721 172.16.16.7:7003
	   slots:[13108-16383] (3276 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、加入新的主节点

1、创建新的实例
	[root@redhat-1-6 redis-cluster-7000]# mkdir /redis-cluster-7006
	[root@redhat-1-6 redis-cluster-7000]# cp redis.conf /redis-cluster-7006
	[root@redhat-1-6 redis-cluster-7000]# cd /redis-cluster-7006
	[root@redhat-1-6 redis-cluster-7006]# vim redis.conf
	port 7006
	[root@redhat-1-6 redis-cluster-7006]# redis-server redis.conf
2、将节点加入集群
	'开启节点'
	[root@redhat-1-6 redis-cluster-7006]# redis-server redis.conf 
	36668:C 24 Feb 2019 23:50:19.723 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
	36668:C 24 Feb 2019 23:50:19.723 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=36668, just started
	36668:C 24 Feb 2019 23:50:19.723 # Configuration loaded
	'加入集群'
	[root@redhat-1-6 redis-cluster-7006]# redis-cli --cluster add-node 172.16.16.4:7006 172.16.16.4:7000
	>>> Adding node 172.16.16.4:7006 to cluster 172.16.16.4:7000
	>>> Performing Cluster Check (using node 172.16.16.4:7000)
	M: a9c4f45d775c8e549264939f9a45b3b30f120498 172.16.16.4:7000
	   slots:[0-5460] (5461 slots) master
	   1 additional replica(s)
	S: 251e7635c170cbf1bf106e989d595b7cbd91d178 172.16.16.10:7004
	   slots: (0 slots) slave
	   replicates a9c4f45d775c8e549264939f9a45b3b30f120498
	S: 4445aef1ae3faf44b8a851ec6ab9cf7e55f361e5 172.16.16.11:7005
	   slots: (0 slots) slave
	   replicates 16601b6cc31e1136ebff53d293c6910b238af0b4
	M: e610e15d5d18c9e3046ad1269ffcf9f191c306dc 172.16.16.6:7002
	   slots:[10923-16383] (5461 slots) master
	   1 additional replica(s)
	M: 16601b6cc31e1136ebff53d293c6910b238af0b4 172.16.16.5:7001
	   slots:[5461-10922] (5462 slots) master
	   1 additional replica(s)
	S: 2cf48f5e93af3cdfd37a1e82f0cb144be7da2721 172.16.16.7:7003
	   slots: (0 slots) slave
	   replicates e610e15d5d18c9e3046ad1269ffcf9f191c306dc
	[OK] All nodes agree about slots configuration.
	>>> Check for open slots...
	>>> Check slots coverage...
	[OK] All 16384 slots covered.
	>>> Send CLUSTER MEET to node 172.16.16.4:7006 to make it join the cluster.
	[OK] New node added correctly.

2、加入新的从节点

1、创建新的节点
2、加入集群
	[root@redhat-1-6 redis-cluster-7007]# redis-cli --cluster 
	    add-node 172.16.16.4:7007 172.16.16.4:7000 
	    --cluster-slave
	    --cluster-master-id 8363c8e5c25c66f23c3a88c909c8278148cec088  'master-id'

迁移slot

1、在线迁移slot

	[root@redhat-1-6 redis-cluster-7006]# redis-cli --cluster reshard 172.16.16.4:7006   '目的IP'
	How many slots do you want to move (from 1 to 16384)? 100   '迁移量'
	What is the receiving node ID? 8363c8e5c25c66f23c3a88c909c8278148cec088   '目的ID'
	'从所有节点迁移,从单独节点迁移slot详见删除有slot的节点'
	Source node #1: all       
	Do you want to proceed with the proposed reshard plan (yes/no)? yes

2、平衡slot

	[root@redhat-1-6 redis-cluster-7006]# redis-cli --cluster rebalance 172.16.16.4:7000
		'平衡时设置权重'
	[root@redhat-1-6 redis-cluster-7006]# redis-cli --cluster rebalance 172.16.16.4:7000 
	    --cluster-weight aea1598e2e496ebcc0f9b2415c5b890e400582bc=2

删除节点

1、删除空节点(没有slot)

	[root@redhat-1-6 redis-cluster-7007]# redis-cli --cluster 
	    del-node 172.16.16.4:7007 b345cf3faace4b514c734e62fc94f85dd1895456
	>>> Removing node b345cf3faace4b514c734e62fc94f85dd1895456 from cluster 172.16.16.4:7007
	>>> Sending CLUSTER FORGET messages to the cluster...
	>>> SHUTDOWN the node.

2、删除有slot的节点

1、将节点的slot迁移到其他节点
	'查看集群信息,按照信息进行slot迁移'
	[root@redhat-1-6 redis-cluster-7007]# redis-cli --cluster reshard 172.16.16.4:7000
		'要删除的master上的所有slot'
		How many slots do you want to move (from 1 to 16384)? 4096
		'slot迁移的目的ID'
		What is the receiving node ID? a9c4f45d775c8e549264939f9a45b3b30f120498
		Please enter all the source node IDs.
		  Type 'all' to use all the nodes as source nodes for the hash slots.
		  Type 'done' once you entered all the source nodes IDs.
		'要迁移的master的ID'  
		Source node #1: 8363c8e5c25c66f23c3a88c909c8278148cec088
		'仅从当前ID迁移slot'
		Source node #2: done
		Do you want to proceed with the proposed reshard plan (yes/no)? yes
		'查看迁移后的slot'
		[root@redhat-1-6 redis-cluster-7007]# redis-cli --cluster check 172.16.16.4:7000
		>>> Performing Cluster Check (using node 172.16.16.4:7000)
		M: a9c4f45d775c8e549264939f9a45b3b30f120498 172.16.16.4:7000
		   slots:[0-6826],[10923-12287] (8192 slots) master
		   1 additional replica(s)
		M: 8363c8e5c25c66f23c3a88c909c8278148cec088 172.16.16.4:7006
		   slots: (0 slots) master
2、删除空节点
	[root@redhat-1-6 redis-cluster-7007]# redis-cli --cluster 
	    del-node 172.16.16.4:7006 8363c8e5c25c66f23c3a88c909c8278148cec088
	>>> Removing node 8363c8e5c25c66f23c3a88c909c8278148cec088 from cluster 172.16.16.4:7006
	>>> Sending CLUSTER FORGET messages to the cluster...
	>>> SHUTDOWN the node.

解决 [ERR] Node xxx is not empty. Either the node already knows other no…错误

这是因为我们之前将这个节点加入过redis集群,然后又删掉了,现在我们又将他添加到集群中,所以会报错
删除掉这个节点的appendonly.aof、dump.rdb、node_xxx.conf 文件
然后用 redis-cli -h host -p port 登录后执行flushdb
再将节点添加到集群即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值