Redis3集群安装
1. 什么是Redis
Redis是目前一个非常优秀的key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set有序集合)和hash(哈希类型)。
2. 为什么要安装Redis3集群
Redis3.x支持集群模式,更加可靠!
3. 安装Redis3集群(6台Linux)
参考文章:http://blog.csdn.net/myrainblues/article/details/25881535
1.下载redis3的稳定版本,下载地址http://download.redis.io/releases/redis-3.2.10.tar.gz
2.上传redis-3.2.10.tar.gz到服务器
3.解压redis源码包
tar -zxvf redis-3.2.10.tar.gz -C /usr/local/src/
4.进入到源码包中,编译并安装redis
cd /usr/local/src/redis-3.2.10/
make && make install
5.报错,缺少依赖的包
6.配置本地YUM源并安装redis依赖的rpm包
yum -y install gcc
7.编译并安装
make && make install
8.报错,原因是没有安装jemalloc内存分配器,可以安装jemalloc或直接输入
9.重新编译安装
make MALLOC=libc && make install
10.用同样的方式在其他的机器上编译安装redis
11.在所有机器的/usr/local/下创建一个redis目录,然后拷贝redis自带的配置文件redis.conf到/usr/local/redis
mkdir /usr/local/redis
cp /usr/local/src/redis-3.2.10/redis.conf /usr/local/redis
12.修改所有机器的配置文件redis.conf
daemonize yes #redis后台运行 cluster-enabled yes #开启集群把注释去掉 appendonly yes #开启aof日志,它会每次写操作都记录一条日志 |
sed -i 's/daemonize no/daemonize yes/' /usr/local/redis/redis.conf
sed -i "s/bind 127.0.0.1/ bind $HOST/" /usr/local/redis/redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/' /usr/local/redis/redis.conf
sed -i 's/appendonly no/appendonly yes/' /usr/local/redis/redis.conf
sed -i 's/# cluster-node-timeout 15000/cluster-node-timeout 5000/' /usr/local/redis/redis.conf
13.启动所有的redis节点
cd /usr/local/redis
redis-server redis.conf
14.查看redis进程状态
ps -ef | grep redis
15.(只要在一台机器上安装即可)配置集群:安装ruby和ruby gem工具(redis3集群配置需要ruby的gem工具,类似yum)
yum -y install ruby rubygems
16.使用gem下载redis集群的配置脚本
gem install redis
ruby --version
17.安装RVM
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
curl -L get.rvm.io | bash -s stable
source /usr/local/rvm/scripts/rvm
rvm list known
rvm install 2.3.4
#用ruby的工具安装reids
gem install redis
18.使用脚本配置redis集群
cd /usr/local/src/redis-3.2.10/src/
#service iptables stop
#在第一机器上执行下面的命令
./redis-trib.rb create --replicas 1 192.168.10.101:6379 192.168.10.102:6379 192.168.10.103:6379 192.168.10.104:6379 192.168.10.105:6379 192.168.10.106:6379
19.测试(别忘加-c参数)
redis-cli -c -h 192.168.1.13
4. Redis3伪分布式安装(1台Linux)
1.下载redis3的稳定版本,下载地址http://download.redis.io/releases/redis-3.2.10.tar.gz
2.上传redis-3.2.10.tar.gz到服务器
3.解压redis源码包
tar -zxvf redis-3.2.10.tar.gz -C /usr/local/src/
4.进入到源码包中,编译并安装redis
cd /usr/local/src/redis-3.2.10/
make && make install
5.在/usr/local/下创建一个redis目录,然后分别在/usr/local/redis目录创建6个文件夹7000,7001,7002,7003,7004,7005然后拷贝redis自带的配置文件redis.conf到这六个目录中
mkdir /usr/local/redis
mkdir /usr/local/redis/{7000,7001,7002,7003,7004,7005}
cp /usr/local/src/redis-3.2.10/redis.conf /usr/local/redis/7000
cp /usr/local/src/redis-3.2.10/redis.conf /usr/local/redis/7001
cp /usr/local/src/redis-3.2.10/redis.conf /usr/local/redis/7002
cp /usr/local/src/redis-3.2.10/redis.conf /usr/local/redis/7003
cp /usr/local/src/redis-3.2.10/redis.conf /usr/local/redis/7004
cp /usr/local/src/redis-3.2.10/redis.conf /usr/local/redis/7005
6.分别修改这六个目录中的配置文件
port 7000 #端口要与其所在的文件名一致 pidfile /var/run/redis-7000.pid #pid要与其所在的文件名一致 daemonize yes cluster-enabled yes appendonly yes |
7.分别进入到这六个目录启动redis进程
cd /usr/local/redis/7000
redis-server redis.conf
cd /usr/local/redis/7001
redis-server redis.conf
cd /usr/local/redis/7002
redis-server redis.conf
cd /usr/local/redis/7003
redis-server redis.conf
cd /usr/local/redis/7004
redis-server redis.conf
cd /usr/local/redis/7005
redis-server redis.conf
HOST=`ifconfig eth1 | grep "inet addr" | awk -F : '{print $2}' | awk '{print $1}'`
sed -i "s/bind 127.0.0.1/bind $HOST/" /usr/local/redis/redis.conf
分别进入几个redis的客户端中设置redis密码
config set masterauth username config set requirepass passwd auth username config rewrite
连接到创建的集群中(-c:参数一定要添加)
/usr/local/src/redis-3.2.10/src/redis-cli -p 7005 -c
停止redis服务
redis-cli -h 192.168.100.101 -a 密码 shutdown
创建集群的命令(初始化执行一次,以后只需要将redis启动就可以了)
/usr/local/src/redis-3.2.10/src/redis-trib.rb start --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
查看集群运行状态命令
/usr/local/src/redis-3.2.10/src/redis-trib.rb check 127.0.0.1:7001
运行状态截图
批量启动集群中机器
1 #!/bin/bash 2 3 echo "start redis on port 7000~7005" 4 for i in {7000..7005} 5 do 6 echo "[$i start now]" 7 cd /usr/local/redis/$i 8 /usr/local/src/redis-3.2.10/src/redis-server redis.conf 9 sleep 1 10 done 11 12 for i in {7000..7005} 13 do 14 REDIS_ID=$( ps -ef | grep redis | grep $i | grep -v 'grep' | head -1 | awk '{print $2}' ) 15 sleep 1 16 if [ -z "$REDIS_ID" ]; then 17 echo "port [$i] pid [$REDIS_ID] not start" 18 else 19 echo "port [$i] pid [$REDIS_ID] been start" 20 fi 21 done 22 echo "start success redis port 7000~7005"
批量停止集群机器
1 #!/bin/bash 2 3 for i in {7000..7005} 4 do 5 REDIS_ID=$( ps -ef | grep redis | grep $i | grep -v 'grep' | head -1 | awk '{print $2}' ) 6 if [ -z "$REDIS_ID" ]; then 7 echo "[$REDIS_ID]... " 8 echo "port [$i] pid [$REDIS_ID] has been stop" 9 else 10 echo "port [$i] pid [$REDIS_ID] will stop" 11 kill -9 $REDIS_ID 12 sleep 2 13 fi 14 done
参考博客
https://www.cnblogs.com/xuliangxing/p/7146868.html
集群测试参考博客
https://www.cnblogs.com/shihaiming/p/5984006.html
原文件位置
链接:https://pan.baidu.com/s/12MBLADPKLfHk_2z5qX48OA
提取码:43hl