Redis-Cluster集群配置
Posted 会飞的小冉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis-Cluster集群配置相关的知识,希望对你有一定的参考价值。
1、Redis主机准备(本次为了减少创建虚拟机的麻烦使用一台主机(Ubuntu16.4)进行配置)
(1)先保证系统有基本编译环境:apt-get install -y build-essential
(2)使用wget命令下载redis:wget http://download.redis.io/releases/redis-4.0.10.tar.gz
(3)使用解压命令解压缩到/usr/local/src目录下:tar -zxvf redis-4.0.10.tar.gz -C /usr/local/src/
(4)进入到/usr/local/src/redis-4.0.10目录下执行make命令:make
(5)编译完成后进行Redis安装:make install
(6)Redis是属于内存缓存数据库,所以应该把内存都交于Redis,所以执行此命令:echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
(7)将以上的配置写入到内核中:/sbin/sysctl -p
(8)为了方便建立一个Redis命令工具目录: mkdir -p /usr/local/redis/{bin,conf}
(9)拷贝Redis服务启动程序:cp /usr/local/src/redis-4.0.10/src/redis-server /usr/local/redis/bin/
(10)拷贝Redis命令行客户端:cp /usr/local/src/redis-4.0.10/src/redis-cli /usr/local/redis/bin/
(11)拷贝Redis测试工具:cp /usr/local/src/redis-4.0.10/src/redis-benchmark /usr/local/redis/bin/
(12)拷贝一个配置文件:cp /usr/local/src/redis-4.0.10/redis.conf /usr/local/redis/conf/
(13)建立一个目录用于存放数据:mkdir -p /usr/data/redis/{run,logs,dbcache}
(14)修改redis.conf的配置文件:vi /usr/local/redis/conf/redis.conf
配置Redis运行端口:port 6379 配置Redis是否后台运行:daemonize yes 设置进程保存路径:pidfile /usr/data/redis/run/redis_6379.pid 设置日志保存路径:logfile /usr/data/redis/logs/redis.log 该Redis数据库支持的数据库个数:databases 16 保存数据文件目录:dir /usr/data/redis/dbcache
(15)启动服务(要明确的指定redis.conf配置文件):/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
(16)使用此命令查看端口:netstat -nptl
(17)启用Redis客户端:/usr/local/redis/bin/redis-cli
*此时就连接上本机6379端口的Redis
(18)远程连接Reids:/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379
(19)关闭Reids服务:killall redis-server或者kill -9 pid
2、配置集群主机(使用八台主机)
(1)主机名称都是:redis-cluster
(2)主机IP地址和端口:192.168.180.138:6379、192.168.180.138:6380、192.168.180.138:6381、192.168.180.138:6382、192.168.180.138:6383、192.168.180.138:6384、192.168.180.138:6385、192.168.180.138:6386
(3)建立各自的数据保存目录:mkdir -p /usr/data/redis/{redis-6379,redis-6380,redis-6381,redis-6382,redis-6383,redis-6384,redis-6385,redis-6386}/{run,logs,dbcache}
(4)创建多个redis.conf配置文件,先以redis-6379为例: vi /usr/local/redis/conf/redis-6379.conf
取消本机IP绑定:#bind 127.0.0.1 在配置之前取消保护模式:protected-mode no 监听端口:port 6379 后台运行:daemonize yes 定义pid保存目录:pidfile /usr/data/redis/redis-6379.pid 定义日志保存路径:logfile /usr/data/redis/redis-6379/logs/redis.log 数据保存路径:dir /usr/data/redis/redis-6379/dbcache 从属服务器不需要配置:# slaveof <masterip> <masterport> 主服务器不需要配置:# masterauth <master-password> 密码也不需要配置:# requirepass foobared 打开RedisCluster集群:cluster-enabled yes 定义cluster配置的保存文件:cluster-config-file nodes-6379.conf 定义节点的超时时间:cluster-node-timeout 15000
(5)将redis-6379.conf配置文件进行复制以及操作的变更:
a)复制文件:cp /usr/local/redis/conf/redis-6379.conf /usr/local/redis/conf/redis-6380.conf
b)整版替换:
①打开文件: vi /usr/local/redis/conf/redis-6380.conf
②整版替换::1,$s/6379/6380/g
* 按照如上命令把其他的主机也配置完成
cp /usr/local/redis/conf/redis-6379.conf /usr/local/redis/conf/redis-6381.conf cp /usr/local/redis/conf/redis-6379.conf /usr/local/redis/conf/redis-6382.conf cp /usr/local/redis/conf/redis-6379.conf /usr/local/redis/conf/redis-6383.conf cp /usr/local/redis/conf/redis-6379.conf /usr/local/redis/conf/redis-6384.conf cp /usr/local/redis/conf/redis-6379.conf /usr/local/redis/conf/redis-6385.conf cp /usr/local/redis/conf/redis-6379.conf /usr/local/redis/conf/redis-6386.conf vi /usr/local/redis/conf/redis-6381.conf 1,$s/6379/6381/g vi /usr/local/redis/conf/redis-6382.conf 1,$s/6379/6382/g vi /usr/local/redis/conf/redis-6383.conf 1,$s/6379/6383/g vi /usr/local/redis/conf/redis-6384.conf 1,$s/6379/6384/g vi /usr/local/redis/conf/redis-6385.conf 1,$s/6379/6385/g vi /usr/local/redis/conf/redis-6386.conf 1,$s/6379/6386/g
(6)启动多所有的Redis服务:
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6379.conf /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6380.conf /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6381.conf /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6382.conf /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6383.conf /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6384.conf /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6385.conf /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6386.conf
(7)之后使用命令确定redis cluster集群的运行:ps -ef|grep redis
* 此时可以确保所有的Redis主机可以运行
*未完待续
以上是关于Redis-Cluster集群配置的主要内容,如果未能解决你的问题,请参考以下文章