Redis单机版搭建上一篇已经基本介绍了,下面讨论Redis集群搭建方案和示例。redis
一、关于Redis经常使用的集群方案(三种):数据库
a、一主多从,如一个Master、两个Slavecentos
b、薪火相传,即集群中的从节点(Slave)同时也是主节点(Master),相似于链式传递同样缓存
c、反客为主,主节点down掉后从节点升级为主节点,经过人工干预 或者 经过Sentinel 哨兵模式来实现(下篇介绍)app
二、模拟测试(以一主多从为例)测试
模拟主机信息(在同一台主机经过不一样端口模拟):ui
角色 | IP | 端口 |
Master | 127.0.0.1 | 6379 |
Salve | 127.0.0.1 | 6380 |
Salve | 127.0.0.1 | 6381 |
进入Redis目录,复制redis配置文件,给Slave使用:spa
1 [root@VM_0_14_centos redis]# ls -lrt 2 total 12700 3 -rwxr-xr-x 1 root root 8100759 Mar 20 16:12 redis-server 4 -rwxr-xr-x 1 root root 4805624 Mar 20 16:13 redis-cli 5 -rw-r--r-- 1 root root 62156 Mar 20 17:17 redis.conf 6 [root@VM_0_14_centos redis]# cp redis.conf ./redis.6380.conf 7 [root@VM_0_14_centos redis]# cp redis.conf ./redis.6381.conf 8 [root@VM_0_14_centos redis]#
编辑Master配置文件redis.conf文件,主要配置如下参数:线程
daemonize yes #开启守护进程debug
pidfile /var/run/redis_6379.pid #开启守护进程后会将进程ID写入该文件
logfile "/var/log/redis.6379.log" #配置日志文件
masterauth funnyboy0128 #master验证密码
requirepass funnyboypass #Redis登陆密码
编辑Slave配置文件redis.6380.conf 和redis.6381.conf ,修改pidfile和logfile的值,并在最后追加 slaveof 配置项,修改端口分贝为6380和6380
port 6380 和 port 6381
slaveof 127.0.0.1 6379 #Master的I配合端口
启动Master节点:
1 [root@VM_0_14_centos redis]# 2 [root@VM_0_14_centos redis]# 3 [root@VM_0_14_centos redis]# 4 [root@VM_0_14_centos redis]# ./redis-server ./redis.conf
启动Slave节点:
1 [root@VM_0_14_centos redis]# 2 [root@VM_0_14_centos redis]# 3 [root@VM_0_14_centos redis]# ./redis-server ./redis.6380.conf 4 [root@VM_0_14_centos redis]# ./redis-server ./redis.6381.conf 5 [root@VM_0_14_centos redis]#
客户端链接测试:
1 [root@VM_0_14_centos redis]# 2 [root@VM_0_14_centos redis]# ./redis-cli -h 127.0.0.1 -p 6379 -a funnyboypass 3 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 4 127.0.0.1:6379> keys * 5 (empty list or set) 6 127.0.0.1:6379> set name hello redis 7 (error) ERR syntax error 8 127.0.0.1:6379> set name "hello redis" 9 OK 10 127.0.0.1:6379> 11 127.0.0.1:6379> 12 127.0.0.1:6379> get name 13 "hello redis" 14 127.0.0.1:6379>
链接Master,set信息到Redis。而后链接Slave查看数据,
1 [root@VM_0_14_centos redis]# 2 [root@VM_0_14_centos redis]# ./redis-cli -h 127.0.0.1 -p 6380 -a funnyboypass 3 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 4 127.0.0.1:6380> 5 127.0.0.1:6380> 6 127.0.0.1:6380> 7 127.0.0.1:6380> get name 8 "hello redis" 9 127.0.0.1:6380>
6380 Slave节点数据OK。
1 [root@VM_0_14_centos redis]# ./redis-cli -h 127.0.0.1 -p 6381 -a funnyboypass 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 3 127.0.0.1:6381> get name 4 "hello redis" 5 127.0.0.1:6381> 6 127.0.0.1:6381>
测试Slave是否支持写入:
1 127.0.0.1:6381> 2 127.0.0.1:6381> set sname hello 3 (error) READONLY You can't write against a read only replica. 4 127.0.0.1:6381>
结果显示Slave值只支持读操做。
补充关于redis.conf相关的配置项:
一、daemonize 若是须要在后台运行,把该项改成yes
二、pidfile 配置多个pid的地址 默认在/var/run/redis.pid
三、bind 绑定ip,设置后只接受来自该ip的请求
四、port 监听端口,默认是6379
五、loglevel 分为4个等级:debug verbose notice warning
六、logfile 用于配置log文件地址
七、databases 设置数据库个数,默认使用的数据库为0
八、save 设置redis进行数据库镜像的频率。
九、rdbcompression 在进行镜像备份时,是否进行压缩
十、dbfilename 镜像备份文件的文件名
十一、Dir 数据库镜像备份的文件放置路径
十二、Slaveof 设置数据库为其余数据库的从数据库
1三、Masterauth 主数据库链接须要的密码验证
1四、Requriepass 设置 登录时须要使用密码
1五、Maxclients 限制同时使用的客户数量
1六、Maxmemory 设置redis可以使用的最大内存
1七、Appendonly 开启append only模式
1八、Appendfsync 设置对appendonly.aof文件同步的频率(对数据进行备份的第二种方式)
1九、vm-enabled 是否开启虚拟内存支持 (vm开头的参数都是配置虚拟内存的)
20、vm-swap-file 设置虚拟内存的交换文件路径
2一、vm-max-memory 设置redis使用的最大物理内存大小
2二、vm-page-size 设置虚拟内存的页大小
2三、vm-pages 设置交换文件的总的page数量
2四、vm-max-threads 设置VM IO同时使用的线程数量
2五、Glueoutputbuf 把小的输出缓存存放在一块儿
2六、hash-max-zipmap-entries 设置hash的临界值
2七、Activerehashing 从新hash