Redis集群简单搭建使用

来源:互联网 发布:统计局70城房价数据 编辑:程序博客网 时间:2024/06/05 03:53

环境:一台Linux虚机
Redis版本:redis-4.0.1

1、下载并解压

cd /opt/rediswget http:http://download.redis.io/releases/redis-4.0.1.tar.gztar -zxvf redis-4.0.1.tar.gz

2、编译安装

一定要进入解压目录下使用命令make,不然会报错:make: * No targets specified and no makefile found.

cd /opt/redis/redis-4.0.1make && make install

3、创建集群节点

为方便将6个节点部署在一台服务器上,3主3从,一般会将主从分别部署在两台服务器上。将redis.conf拷贝到这6个文件夹里。

cd /opt/redismkdir redis-clustercd redis-clustermkdir 7000 7001 7002 7003 7004 7005cp ../redis.conf 7000cp ../redis.conf 7001cp ../redis.conf 7002cp ../redis.conf 7003cp ../redis.conf 7004cp ../redis.conf 7005

redis.conf配置如下,其余的文件夹相应改下

bind 127.0.0.1                  //默认为127.0.0.1 需要将其改为服务器IP,否则集群搭建后其他服务器无法访问port 7000                       //端口,其余配置中改成相应的端口:7001、7002、7003、7004、7005daemonize yes                   //redis后台运行cluster-enabled yes             //开启集群cluster-config-file nodes_7000.conf //集群的配置, 启动后自动生成在7000文件夹下cluster-node-timeout 15000      //请求超时,默认15秒appendonly yes                  //开启aof日志,每次写操作都记录一条日志

4、启动redis各个节点

先将redis-tribe.rb复制到/usr/local/bin下,redis-trib.rb具有以下功能
1. create:创建集群
2. check:检查集群
3. info:查看集群信息
4. fix:修复集群
5. reshard:在线迁移slot
6. rebalance:平衡集群节点slot数量
7. add-node:将新节点加入集群
8. del-node:从集群中删除节点
9. set-timeout:设置集群节点间心跳连接的超时时间
10. call:在集群全部节点上执行命令
11. import:将外部redis数据导入集群

redis-server redis-cluster/7000/redis.confredis-server redis-cluster/7001/redis.confredis-server redis-cluster/7002/redis.confredis-server redis-cluster/7003/redis.confredis-server redis-cluster/7004/redis.confredis-server redis-cluster/7005/redis.conf##查看redis启动情况ps -ef | grep redisroot     31510     1  0 16:20 ?        00:00:13 redis-server 127.0.0.1:7000 [cluster]root     31517     1  0 16:20 ?        00:00:13 redis-server 127.0.0.1:7001 [cluster]root     31523     1  0 16:20 ?        00:00:13 redis-server 127.0.0.1:7002 [cluster]root     31528     1  0 16:20 ?        00:00:13 redis-server 127.0.0.1:7003 [cluster]root     31533     1  0 16:20 ?        00:00:13 redis-server 127.0.0.1:7004 [cluster]root     31538     1  0 16:20 ?        00:00:13 redis-server 127.0.0.1:7005 [cluster]

5、安装Ruby

redis-trib.rb是用ruby实现的,所以需要安装ruby以及ruby和redis的连接

yum -y install ruby ruby-devel rubygems rpm-buildgem install redis

其中 gem install redis执行会报错:redis requires Ruby version >= 2.2.2
网上查看资料发现centos默认支持ruby是2.0.0,所以需要安装rvm将ruby版本升至2.2.3

# 1、安装curlsudo yum install curl# 2、安装RVMcurl -L get.rvm.io | bash -s stable## 或者curl -sSL https://get.rvm.io | bash -s stable### 安装RVM可能会失败,访问rvm.io站点时可能会被墙Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gzDownloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.ascgpg: Signature made Mon Sep 11 04:59:21 2017 CST using RSA key ID BF04FF17gpg: Can't check signature: No public keyWarning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.3.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:    gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3or if it fails:    command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -the key can be compared with:    https://rvm.io/mpapis.asc    https://keybase.io/mpapisNOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.### 可以先从git仓库下载并安装RVM,然后执行下面命令#### Install mpapis public key (might need `gpg2` and or `sudo`)gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3#### Download the installercurl -O https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installercurl -O https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer.asc#### Verify the installer signature (might need `gpg2`), and if it validates...gpg --verify rvm-installer.asc &&#### Run the installerbash rvm-installer stable#### 如果出现:gpg -keyserver,用如下命令替代(我没出现过,所以没使用)curl -sSL https://rvm.io/mpapis.asc | gpg --import -### RVM安装好了,提示  * First you need to add all users that will be using rvm to 'rvm' group,    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`    in all your open shell windows, in rare cases you need to reopen all shell windows.## 执行source /etc/profile.d/rvm.shsource /etc/profile.d/rvm.sh# 3、查看RVM库汇总已知的ruby版本rvm list known# 4、安装ruby 2.3.3rvm install 2.3.3# 5、使用新版本rvm use 2.3.3# 6、设备默认版本rvm remove 2.0.2# 7、卸载已知版本rvm --version# 8、重新安装redis,建立和ruby的连接gem install redis

6、创建集群

redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

创建集群覆盖默认配置
输入yes,覆盖默认配置,安装成功。

7、集群测试

连接7001节点
测试7001节点
Redirected to slot [1151] located at 127.0.0.1:7000 是指集群部署成功,可在7000节点get到在7001节点set的值。

但在其他节点会出现如下错误:(error) MOVED 5798 127.0.0.1:7001
因为使用连接命令redis-cli时没有加上-c,不能连接到集群。

集群测试出错

关闭redis命令或者kill
redis-cli -h 127.0.0.1 -p 7000 shutdown

参考资料:
Redis集群搭建与简单使用
redis requires Ruby version >= 2.2.2问题
linux下安装ruby版本管理器RVM

原创粉丝点击