Redis集群简单搭建使用

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

1、下载并解压
cd /opt/redis
wget http:http://download.redis.io/releases/redis-4.0.1.tar.gz
tar -zxvf redis-4.0.1.tar.gz
2、编译安装

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

cd /opt/redis/redis-4.0.1
make && make install
3、创建集群节点

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

cd /opt/redis
mkdir redis-cluster
cd redis-cluster
mkdir 7000 7001 7002 7003 7004 7005
cp ../redis.conf 7000
cp ../redis.conf 7001
cp ../redis.conf 7002
cp ../redis.conf 7003
cp ../redis.conf 7004
cp ../redis.conf 7005

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

bind 127.0.0.1                  //默认为127.0.0.1 需要将其改为服务器IP,否则集群搭建后其他服务器无法访问
port 7000                       //端口,其余配置中改成相应的端口:7001、7002、7003、7004、7005
daemonize 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.conf
redis-server redis-cluster/7001/redis.conf
redis-server redis-cluster/7002/redis.conf
redis-server redis-cluster/7003/redis.conf
redis-server redis-cluster/7004/redis.conf
redis-server redis-cluster/7005/redis.conf

##查看redis启动情况
ps -ef | grep redis
root     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-build
gem install redis

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

# 1、安装curl
sudo yum install curl

# 2、安装RVM
curl -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.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: Signature made Mon Sep 11 04:59:21 2017 CST using RSA key ID BF04FF17
gpg: Can't check signature: No public key
Warning, 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 409B6B1796C275462A1703113804BB82D39DC0E3

or 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/mpapis

NOTE: 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 installer
curl -O https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer
curl -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 installer
bash 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.sh
source /etc/profile.d/rvm.sh

# 3、查看RVM库汇总已知的ruby版本
rvm list known

# 4、安装ruby 2.3.3
rvm 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值