linux-centos7-安装redis集群

1、安装redis


//安装
$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
$ tar xzf redis-5.0.5.tar.gz
$ cd redis-5.0.5
$ make

//启动服务
$ src/redis-server

//使用
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

//停止服务
$ src/redis-cli shutdown

 2、安装ruby


  • 安装rvm

创建集群的时候,要使用ruby,可以通过rvm来指定ruby版本安装,版本过低无法创建集群,先安装rvm

访问rvm官方网站:https://rvm.io/,执行如下:

$ gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

$ yum install curl

$ \curl -sSL https://get.rvm.io | bash -s stable
[root@192 ~]# \curl -sSL https://get.rvm.io | bash -s stable
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
curl: (35) SSL connect error
Could not download 'https://github.com/rvm/rvm/archive/1.29.3.tar.gz'.
  curl returned status '35'.
Downloading https://bitbucket.org/mpapis/rvm/get/1.29.3.tar.gz
Downloading https://bitbucket.org/mpapis/rvm/downloads/1.29.3.tar.gz.asc
curl: (7) couldn't connect to host
Could not download 'https://bitbucket.org/mpapis/rvm/downloads/1.29.3.tar.gz.asc'.
  curl returned status '7'.
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:
  * 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.

执行上述命令 * To start using RVM you need to run `source /etc/profile.d/rvm.sh`

[root@192~]# source /etc/profile.d/rvm.sh
  • 查看RVM可安装的ruby的版本
[root@192~]# rvm list known
[root@192~]# rvm install 2.6.3
[root@192~]# rvm use 2.6.3
[root@192~]# ruby -v

//可以设置默认
[root@192~]# rvm use 2.6.3 --default 

3、创建redis节点

在redis目录下创建文件夹redis_cluster,在redis_cluster下创建7000、7001、7002的文件夹目录,并将 redis.conf 拷贝到这三个目录中

修改redis.conf配置文件:

port  7000                                        //端口7000,7002,7003        
bind 本机ip                                       //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize    yes                               //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001,7002
cluster-enabled  yes                           //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf   //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志

如果有多台服务器,重复上面操作。

4、启动关闭服务

[root@192 start-close-server]# cat start-redis.sh 
for((i=0;i<3;i++));
do /opt/software/redis-5.0.5/src/redis-server /opt/software/redis-5.0.5/redis_cluster/700$i/redis.conf;
done
[root@192 start-close-server]# 
[root@192 start-close-server]# cat close-redis.sh 
for((i=0;i<3;i++));
do /opt/software/redis-5.0.5/src/redis-cli -c -h 192.168.6.128 -p 700$i shutdown;
done
[root@192 start-close-server]# 

需要给start-redis.sh和close-redis.sh配置可执行权限:

chmod  u+x 

查看启动情况:

[root@192 start-close-server]# ps -ef|grep redis

5、创建集群

$ cd /root/redis-5.0.5/src
$ ./src/redis-cli --cluster create 192.168.0.103:7000 192.168.0.103:7001 192.168.0.103:7002 --cluster-replicas l

 创建成功会生成nodes-7000.conf、nodes-7001.conf、nodes-7002.conf

6、集群验证

  参数 -c 连接到集群,因为 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略,-p 参数为端口号

[root@192 redis-5.0.5]# ./src/redis-cli  -c -p 7000 -h 192.168.0.103
192.168.0.103:7000> set foo hello
-> Redirected to slot [12182] located at 192.168.0.103:7002
OK
192.168.0.103:7002> 

7、远程访问

设置成桥接,远程可以直接访问虚拟机的ip地址,另外需要开放暴露出去的端口,防火墙也需要关闭。

firewall-cmd --zone=public --add-port=7000/tcp --permanent
firewall-cmd --reload 

8、报错

出现以下报错,需要重新创建集群

 [ERR] Node 192.168.15.102:6000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

将新增的aof、rdb、nodes-7000.conf、nodes-7001.conf、nodes-7002.conf文件删除,再创建集群,如果还不行,再清空数据库 

192.168.0.103:7002>  flushdb      #清空当前数据库

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值