Redis集群安装
- 架构图
- 服务器信息
- 安装
- 测试
- 总结
测试环境部署一套redis集群:
16384个hash slots,三个主节点,三个从节点,易于扩展与缩减
redis路径:/app/redissource/
集群路径:/app/redis/600x
db路径:/app/redis/db/600x
架构图如下:
2、服务器信息
ip | 内存 | 系统 |
---|---|---|
192.168.109.133 | 2c8g | centos7.4 |
192.168.109.134 | 2c8g | centos7.4 |
192.168.109.135 | 2c8g | centos7.4 |
遇过的坑:ruby自带的redis工具,gem-redis。如果redis是4.0版本,那么对ruby的版本要求也高,需要手动升级ruby版本,才能用redis4.0的工具
每台机器上:
setenforce 0
mkdir -p /app/redissource && cd /app/redissource
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
tar zxvf redis-3.2.9.tar.gz
mkdir -p /app/redis
编译:
cd /app/redissource/redis-3.2.9 && make
新环境可能遇到的错误
报错:/bin/sh: cc: command not found
解决:
缺少gcc
yum install gcc -y
报错:jemalloc/jemalloc.h: No such file or directory
解决:
make时添加参数
make MALLOC=lib
echo $?
显示0,编译成功
继续安装:
cd /app/redismkdir 6000 6001
另外两台创建 6002 6003 6004 6005,下面同理scp /app/redissource/redis-3.2.9/redis.conf /app/redis/6000/
scp /app/redissource/redis-3.2.9/redis.conf /app/redis/6001/
修改一些必要的参数,可自行决定,以修改6004为例,贴出我修改的:
sed -i "s/bind 127.0.0.1/#bind 127.0.0.1/g" /app/redis/6004/redis.conf
sed -i "s/protected-mode yes/protected-mode no/g" /app/redis/6004/redis.confsed -i "s/port 6379/port 6004/g" /app/redis/6004/redis.conf
sed -i "s/daemonize no/daemonize yes/g" /app/redis/6004/redis.confsed -i "s/pidfile \/var\/run\/redis_6379.pid/pidfile \/var\/run\/redis_6004.pid/g" /app/redis/6004/redis.confsed -i 's/logfile \"\"/logfile \"\/app\/redis\/db\/6004\/redis.log\"/g' /app/redis/6004/redis.conf
sed -i "s/dir \.\//dir \"\/app\/redis\/db\/6004\"/g" /app/redis/6004/redis.confsed -i "s/appendonly no/#appendonly no/g" /app/redis/6004/redis.conf
sed -i "s/appendfilename \"appendonly.aof\"/#appendfilename \"appendonly.aof\"/g" /app/redis/6004/redis.conf
sed -i "s/appendfsync everysec/#appendfsync everysec/g" /app/redis/6004/redis.conf
sed -i "s/# cluster-enabled yes/cluster-enabled yes/g" /app/redis/6004/redis.conf
sed -i "s/# cluster-config-file nodes-6379.conf/cluster-config-file nodes-6004.conf/g" /app/redis/6004/redis.conf
sed -i "s/# cluster-node-timeout 15000/cluster-node-timeout 5000/g" /app/redis/6004/redis.conf
sed -i "s/# cluster-slave-validity-factor 10/cluster-slave-validity-factor 0/g" /app/redis/6004/redis.confsed -i "s/# cluster-require-full-coverage yes/cluster-require-full-coverage no/g" /app/redis/6004/redis.conf
##protected-mode no:保护模式是禁止公网ip访问redis,开启的条件有两个,1是没有bind任何ip,2是没有设置密码。我是设置了密码,所以关掉保护模式
##cluster-slave-validity-factor
##daemonize yes: 后台模式打开
##appendonly no:关闭append持久化模式,采用另一种
##cluster-enabled:打开redis cluster的一些设置
创建存放db的目录
mkdir -p /app/redis/db/6000 (6000到6005都要创建)
启动redis
/app/redissource/redis-3.2.9/src/redis-server /app/redis/6000/redis.conf (6000到6005都要启动)
安装ruby
yum install ruby -y
gem install redis --version 3.2.0
/app/redissource/redis-3.2.9/src/redis-trib.rb create --replicas 1 1.1.1.24:6001 1.1.1.23:6003 1.1.1.23:6002 1.1.1.25:6004 1.1.1.25:6005 1.1.1.24:6000
4、测试 进入集群,查看节点状态,6个已知节点,并设置密码
/app/redissource/redis-3.2.9/src/redis-cli -c -p 6000
cluster info
config set masterauthpwd
config set requirepasspwd
auth pwd
config rewrite
redis丢失数据案例
背景介绍:
我们的一台redis服务器,硬件配置为4核,4G内存。redis持久话方案是RDB。前面几个月redis使用的
内存在1G左右。在一次重启之后,redis只恢复了部分数据,这时查看redis.log文件。看见了如下的错误
[23635] 25 Jul 08:30:54.059 * 10000 changes in 60 seconds. Saving...
[23635] 25 Jul 08:30:54.059 # Can't save in background: fork: Cannot allocate memory
这时,想起了redis启动时的警告
WARNING overcommit_memory is set to 0!
Background save may fail under low memory condition.
To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and
then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
翻译
警告:过量使用内存设置为0!在低内存环境下,后台保存可能失败。为了修正这个问题,
请在/etc/sysctl.conf 添加一项 'vm.overcommit_memory
5、总结 【redis集群搭建】redis集群就搭建完成了,下面应该进行性能测试和宕机模拟,再加上监控