NoSQL介绍(六)
Redis集群
多个redis节点网络互联,数据共享。
所有的节点都是一主一从(可以是多个从),其中从不提供服务,仅作为备用
不支持同时处理多个键(如mset/mget),因为redis需要把键均匀分布在节点上,并发量很高的情况下同时创建键值会降低性能并导致不可预测的行为。
支持在线增加、删除节点
客户端可以连接任何一个主节点进行读写
Redis集群配置
- A(192.168.221.10)机器开启三个Redis服务(端口不同,端口7000,7002,7004),全部为主
vim /etc/redis_7000.conf //创建redis_7002.conf,redis_7004conf,创建dir目录,在后台运行
port 7000
bind 192.168.221.10
pidfile /var/run/redis_7000.pid
dir /data/redis_7000
logfile "/var/log/redis_7000.log"
appendonly yes
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 15000
查看三个服务redis的进程
ps aux|grep redis-server
root 70986 0.0 0.1 151404 2780 ? Ssl 11:18 0:01 redis-server 192.168.221.10:7000 [cluster]
root 71044 0.0 0.1 147308 2756 ? Ssl 11:38 0:00 redis-server 192.168.221.10:7002 [cluster]
root 71097 0.0 0.1 147308 2752 ? Ssl 11:43 0:00 redis-server 192.168.221.10:7004 [cluster]
root 71103 0.0 0.0 112676 988 pts/1 S+ 11:43 0:00 grep --color=auto redis-server
将A机器编译好的redis复制到B机器上
scp -r redis-4.0.8 192.168.221.20:/usr/local/src
- B(192.168.221.20)机器开启三个Redis服务(端口不同,端口7001,7003,7005),全部为从
cd /usr/local/src/redis-4.0.8/
make install //安装redis
ps aux|grep redis-server
root 46234 0.1 0.1 147308 2752 ? Ssl 20:35 0:00 redis-server 192.168.221.20:7001 [cluster]
root 46255 0.1 0.1 147308 2756 ? Ssl 20:38 0:00 redis-server 192.168.221.20:7003 [cluster]
root 46287 0.0 0.1 147308 2756 ? Ssl 20:41 0:00 redis-server 192.168.221.20:7005 [cluster]
root 46292 0.0 0.0 112676 984 pts/2 S+ 20:41 0:00 grep --color=auto redis-server
- 关闭A机器和B机器的防火墙和selinux
- 在A机器上安装ruby2.2
yum groupinstall "Development Tools" -y yum -y install gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-deve cd /root/ mkdir -p rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz mv ruby-2.2.3.tar.gz rpmbuild/SOURCES/ wget https://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P rpmbuild/SPECS rpmbuild -bb rpmbuild/SPECS/ruby22x.spec ls rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm //是否生成了rpm包 yum -y localinstall rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.centos.x86_64.rpm ruby -v //查看ruby的版本 gem install redis //安装与redis相关的包 cp /usr/local/src/redis-4.0.8/src/redis-trib.rb /usr/bin/ redis-trib.rb create --replicas 1 192.168.221.10:7000 192.168.221.10:7002 192.168.221.10:7004 192.168.221.20:7001 192.168.221.20:7003 192.168.221.20:7005 //自动分配了主从关系,会询问是否可以这样配置
Redis集群操作
以集群的方式登录
redis-cli -c -h 192.168.221.10 -p 7000
创建一个key
192.168.221.10:7000> set key1 zhangsan //key1被重定向到了192.168.221.20:7001上
-> Redirected to slot [9189] located at 192.168.221.20:7001
OK
检测群集状态
redis-trib.rb check 192.168.221.10:7000
列出节点,查看群集信息
redis-cli -c -h 192.168.221.10 -p 7000
192.168.221.10:7000> cluster nodes
192.168.221.10:7000> cluster info
增加一个节点到群集
在B机器上开启一个redis服务(端口号7007)
cp /etc/redis_7001.conf /etc/redis_7007.conf
vim /etc/redis_7007.conf //修改参数,创建dir目录
redis-server /etc/redis_7007.conf
redis-cli -c -h 192.168.221.10 -p 7000
192.168.221.10:7000> cluster meet 192.168.221.20 7007
192.168.221.10:7000> cluster nodes
142aadc3c6144fe787bc4346bfb7f1f1ce28e678 192.168.221.20:7001@17001 master - 0 1520839536000 4 connected 5461-10922
33f00dcecd568ada21cf4f2ce74c0a5b24bbb1a0 192.168.221.20:7005@17005 slave a111ad1e63c5da45985369c1cbfcb577813dded8 0 1520839537162 6 connected
53534241348f7e16ac44037f7b093b3cb90bcb17 192.168.221.10:7004@17004 slave 142aadc3c6144fe787bc4346bfb7f1f1ce28e678 0 1520839537000 4 connected
a111ad1e63c5da45985369c1cbfcb577813dded8 192.168.221.10:7000@17000 myself,master - 0 1520839536000 1 connected 0-5460
fd94e39a02fcb29300a0d23d4103d6fe42e24600 192.168.221.10:7002@17002 master - 0 1520839538170 2 connected 10923-16383
717d6178593cf2738882344a0b4f920225782926 192.168.221.20:7003@17003 slave fd94e39a02fcb29300a0d23d4103d6fe42e24600 0 1520839536153 5 connected
930fc737b50a0eb85d3bbc2143421666dc57ad75 192.168.221.20:7007@17007 master - 0 1520839535145 0 connected
以上可以看到7007的redis节点成功加入了群集,身份为master.
将当前节点设置为指定节点的从节点
连接到这个节点,再将这个节点设置为从
redis-cli -c -h 192.168.221.20 -p 7007
192.168.221.20:7007> CLUSTER REPLICATE fd94e39a02fcb29300a0d23d4103d6fe42e24600
可以看到192.168.221.20:7007已经变成了192.168.221.10:7002的从了
移除某个节点
cluster forget node_id这个命令不能移除master节点,当前登录的节点也不能移除,只能移除非当前登录的从节点。要移除master节点,先将master节点指定为从节点之后再移除。
redis-cli -c -h 192.168.221.10 7000
192.168.221.10:7000> CLUSTER FORGET 930fc737b50a0eb85d3bbc2143421666dc57ad75
将集群的操作保存到配置文件里
192.168.221.10:7000> cluster saveconfig
cat /data/redis_7003/nodes-7003.conf
53534241348f7e16ac44037f7b093b3cb90bcb17 192.168.221.10:7004@17004 slave 142aadc3c6144fe787bc4346bfb7f1f1ce28e678 0 1520840258397 4 connected
fd94e39a02fcb29300a0d23d4103d6fe42e24600 192.168.221.10:7002@17002 master - 0 1520840260413 2 connected 10923-16383
930fc737b50a0eb85d3bbc2143421666dc57ad75 192.168.221.20:7007@17007 slave fd94e39a02fcb29300a0d23d4103d6fe42e24600 0 1520840261420 2 connected
33f00dcecd568ada21cf4f2ce74c0a5b24bbb1a0 192.168.221.20:7005@17005 slave a111ad1e63c5da45985369c1cbfcb577813dded8 0 1520840261000 6 connected
142aadc3c6144fe787bc4346bfb7f1f1ce28e678 192.168.221.20:7001@17001 master - 0 1520840259403 4 connected 5461-10922
717d6178593cf2738882344a0b4f920225782926 192.168.221.20:7003@17003 myself,slave fd94e39a02fcb29300a0d23d4103d6fe42e24600 0 1520840256000 5 connected
a111ad1e63c5da45985369c1cbfcb577813dded8 192.168.221.10:7000@17000 master - 0 1520840259000 1 connected 0-5460
vars currentEpoch 6 lastVoteEpoch 0