redis集群部署

Redis 是一个开源的 key-value 存储系统,由于出众的性能,大部分互联网企业都用来做服务器端缓存。Redis 在3.0版本前只支持单实例模式,虽然支持主从模式、哨兵模式部署来解决单点故障,但是现在互联网企业动辄大几百G的数据,可完全是没法满足业务的需求,所以,Redis 在 3.0 版本以后就推出了集群模式。
Redis 集群采用了P2P的模式,完全去中心化。Redis 把所有的 Key 分成了 16384 个 slot,每个 Redis 实例负责其中一部分 slot 。集群中的所有信息(节点、端口、slot等),都通过节点之间定期的数据交换而更新。
Redis 客户端可以在任意一个 Redis 实例发出请求,如果所需数据不在该实例中,通过重定向命令引导客户端访问所需的实例。

资源准备:cluster的前提条件是必须要有6个nodes,但是这里并没有这么多机器,所以起了两个机器,每个机器各起三个实例,使用端口来区分,端口分别是6379,6380,6381

redis下载地址:https://redis.io/download,这里使用redis的版本是:redis-4.0.11

1. 创建存放目录

mkdir /usr/local/redis-cluster
cd /usr/local/redis-cluster
mkdir 6379/data  6380/data 6381/data -pv

2.安装redis

安装依赖包:yum install gcc tcl -y
mv redis-4.0.11.tar.gz  /usr/local/
cd /usr/local/
tar xf redis-4.0.11.tar.gz 
cd redis-4.0.11/
make
make PREFIX=/usr/local/redis install

测试:
cd src/
make test

  编译产生的错误

错误处理:
make[1]: Entering directory `/usr/local/redis-4.0.11/src'
    LINK redis-server
cc: error: ../deps/jemalloc/lib/libjemalloc.a: No such file or directory
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/usr/local/redis-4.0.11/src'
make: *** [all] Error 2
解决:
yum install jemalloc gcc jemalloc-devel -y
cd redis-4.0.11/deps/jemalloc/
./configure 
make

cc: error: ../deps/hiredis/libhiredis.a: No such file or directory
cc: error: ../deps/lua/src/liblua.a: No such file or directory
cc: error: ../deps/jemalloc/lib/libjemalloc.a: No such file or directory
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/usr/local/redis-4.0.11/src'
make: *** [all] Error 2
则进入redis下的deps下的运行如下命令,就OK了。
[root@web]# cd redis-4.0.11/deps/
[root@web deps]# make lua hiredis linenoise


collect2: error: ld returned 1 exit status
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/usr/local/redis-4.0.11/src'
make: *** [all] Error 2
解决:
编辑src/.make-settings里的OPT,改为OPT=-O2 -march=x86-64
[root@web redis-4.0.11]# grep OPT src/.make-settings 
OPT=-O2 -march=x86-64

3.分发配置文件

cd  /usr/local/redis-4.0.11
cp redis.conf /usr/local/redis-cluster/6379/
cp redis.conf /usr/local/redis-cluster/6380/
cp redis.conf /usr/local/redis-cluster/6381/

以下操作:在不同的机器修改ip地址
cat /usr/local/redis-cluster/6379/redis.conf
port 6379
daemonize yes
bind 10.0.1.4
dir /usr/local/redis-cluster/6379/data/
pidfile /var/run/redis_6379.pid
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
appendonly yes

cat /usr/local/redis-cluster/6380/redis.conf
port 6380
daemonize yes
bind 10.0.1.4
dir /usr/local/redis-cluster/6380/data/
pidfile /var/run/redis_6380.pid
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
appendonly yes

cat /usr/local/redis-cluster/6381/redis.conf
port 6381 
daemonize yes
bind 10.0.1.4
dir /usr/local/redis-cluster/6381/data/
pidfile /var/run/redis_6381.pid
cluster-enabled yes
cluster-config-file nodes-6381.conf
cluster-node-timeout 15000
appendonly yes

4. 启动redis

/usr/local/redis/bin/redis-server /usr/local/redis-cluster/6379/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis-cluster/6380/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis-cluster/6381/redis.conf

5. 检查

检查是否有三个实例启动
ps aux|grep redis
netstat -ant|grep 6379
netstat -ant|grep 6380
netstat -ant|grep 6381

6.部署ruby环境(redis-trib.rb是ruby写的)

 yum install centos-release-scl-rh centos-release-scl -y
 yum install rh-ruby22 rubygems  -y
 scl enable rh-ruby22 bash
 ruby -v         #注意ruby的版本,可能要求大于2.2.2的

gem install redis

7.创建集群(redis-trib.rb是redis官方推出的管理redis集群的工具,集成在redis的源码src目录下,是基于redis提供的集群命令封装成简单、便捷、实用的操作工具。)

把redis-trib脚本放在系统路径上
cp /usr/local/redis-4.0.11/src/redis-trib.rb /usr/local/bin/redis-trib
把相应的命令放在系统路径下
cp /usr/local/redis/bin/* /usr/local/bin/

创建集群
 redis-trib create --replicas 1 10.0.1.4:6379 10.0.1.4:6380 10.0.1.4:6381 10.0.1.6:6379 10.0.1.6:6380 10.0.1.6:6381 
Can I set the above configuration? (type 'yes' to accept):yes

8.检查

redis-cli -c -h 10.0.1.4 -p 6379
CLUSTER NODES  #把相关的主从列出来
CLUSTER INFO  ---》看状态

9.添加节点

添加节点:前面是要创建的节点,后面是集群中的节点
redis-trib add-node 10.0.1.4:6382 10.0.1.4:6383

  

posted @ 2018-08-28 12:08  Reid21  阅读(400)  评论(0编辑  收藏  举报