Redis集群及结合redis的lnmp搭建

本文介绍了如何搭建Redis集群,包括主从复制和故障转移,并展示了在MySQL更新时如何通过触发器和gearman实现数据同步到Redis,以保持数据一致性。
摘要由CSDN通过智能技术生成

Redis集群介绍:

Redis 集群是一个提供在多个Redis间节点间共享数据的程序集。Redis集群并不支持处理多个keys的命令,因为这需要
在不同的节点间移动数据,从而达不到像Redis那样的性能,在高负载的情况下可能会导致不可预料的错误.
Redis 集群通过分区来提供一定程度的可用性,在实际环境中当某个节点宕机或者不可达的情况下继续处理命令.
 Redis 集群的优势:
自动分割数据到不同的节点上。
整个集群的部分节点失败或者不可达的情况下能够继续处理命令。

Redis 集群的主从复制模型:

为了使在部分节点失败或者大部分节点无法通信的情况下集群仍然可用,所以集群使用了主从复制模型,每个节点都会有N-1
个复制品. 在我们例子中具有A,B,C三个节点的集群,在没有复制模型的情况下,如果节点B失败了,那么整个集群就会以为
缺少5501-11000这个范围的槽而不可用.然而如果在集群创建的时候(或者过一段时间)我们为每个节点添加一个从节点A1,
B1,C1,那么整个集群便有三个master节点和三个slave节点组成,这样在节点B失败后,集群便会选B1为新的主节点继续
服务,整个集群便不会因为槽找不到而不可用了,不过当B和B1 都失败后,集群是不可用的.

搭建集群环境使用7001到7006搭建六个集群节点:

[root@server1 ~]# ls   使用tar zxf redis-4.0.8.tar.gz解压即可
redis-4.0.8  redis-4.0.8.tar.gz
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ls
bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@server1 local]# mkdir cluster
[root@server1 local]# cd cluster/
[root@server1 cluster]# ls
[root@server1 cluster]# mkdir 7001
[root@server1 cluster]# mkdir 7002
[root@server1 cluster]# mkdir 7003
[root@server1 cluster]# mkdir 7004
[root@server1 cluster]# mkdir 7005
[root@server1 cluster]# mkdir 7006

这里写图片描述
搭建redis:

[root@server1 cluster]# cd
[root@server1 ~]# cd redis-4.0.8
[root@server1 redis-4.0.8]# cd src/
[root@server1 src]# cp redis-trib.rb /usr/local/bin/
[root@server1 src]# cd /usr/local/bin/
[root@server1 bin]# ls
master_ip_failover       redis-check-aof  redis-sentinel
master_ip_online_change  redis-check-rdb  redis-server
redis-benchmark          redis-cli        redis-trib.rb
[root@server1 bin]# cd
[root@server1 ~]# yum install -y ruby

这里写图片描述

[root@server1 ~]# ls
redis-4.0.1.gem  redis-4.0.8.tar.gz
redis-4.0.8      rubygems-1.3.7-5.el6.noarch.rpm
[root@server1 ~]# yum install rubygems-1.3.7-5.el6.noarch.rpm 
[root@server1 ~]# redis-trib.rb 
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
	from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
	from /usr/local/bin/redis-trib.rb:25
[root@server1 ~]# ls
redis-4.0.1.gem  redis-4.0.8.tar.gz
redis-4.0.8      rubygems-1.3.7-5.el6.noarch.rpm
[root@server1 ~]# rpm -q ruby  版本限制
ruby-1.8.7.352-12.el6_4.x86_64
[root@server1 ~]# ls
redis-4.0.1.gem  redis-4.0.8.tar.gz           rubygems-1.3.7-5.el6.noarch.rpm
redis-4.0.8      ruby-2.2.3-1.el6.x86_64.rpm
[root@server1 ~]# yum install  ruby-2.2.3-1.el6.x86_64.rpm 安装高版本ruby(一种面向对象程序设计的脚本语言)
[root@server1 ~]# ls
libyaml-0.1.3-4.el6_6.x86_64.rpm  redis-4.0.8.tar.gz
redis-4.0.1.gem                   ruby-2.2.3-1.el6.x86_64.rpm
redis-4.0.8                       rubygems-1.3.7-5.el6.noarch.rpm
[root@server1 ~]# yum install libyaml-0.1.3-4.el6_6.x86_64.rpm -y

这里写图片描述

[root@server1 ~]# gem install --local redis-4.0.1.gem 
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 1 seconds
1 gem installed
[root@server1 ~]# gem list --local
[root@server1 ~]# ls
libyaml-0.1.3-4.el6_6.x86_64.rpm  redis-4.0.8.tar.gz
redis-4.0.1.gem                   ruby-2.2.3-1.el6.x86_64.rpm
redis-4.0.8                       rubygems-1.3.7-5.el6.noarch.rpm
[root@server1 ~]# cd /usr/local/cluster/
[root@server1 cluster]# ls
7001  7002  7003  7004  7005  7006
[root@server1 cluster]# cd 7001/
[root@server1 7001]# ls
redis.conf
[root@server1 7001]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped

这里写图片描述
现在我们已经有了六个正在运行中的 Redis 实例, 接下来我们需要使用这些实例来创建集群

[root@server1 7001]# ls
redis.conf
[root@server1 7001]# vim redis.conf 
[root@server1 7001]# cat redis.conf 
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /usr/local/cluster/7001/redis.pid
logfile /usr/local/cluster/7001/redis.log
[root@server1 7001]# pwd
/usr/local/cluster/7001
[root@server1 7001]# ls
redis.conf

这里写图片描述

[root@server1 7001]# redis-server redis.conf 
[root@server1 7001]# ls
appendonly.aof  nodes.conf  redis.conf  redis.log  redis.pid
[root@server1 7001]# cat redis.log    查看reid日志有很多警告
1145:C 12 Aug 10:07:28.842 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1145:C 12 Aug 10:07:28.842 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=1145, just started
1145:C 12 Aug 10:07:28.843 # Configuration loaded
1146:M 12 Aug 10:07:28.844 * Increased maximum number of open files to 10032 (it was originally set to 1024).
1146:M 12 Aug 10:07:28.845 * No cluster configuration found, I'm b517a81b4ea86301002e9c237d73a1dd42440101
1146:M 12 Aug 10:07:28.877 * Running mode=cluster, port=7001.
1146:M 12 Aug 10:07:28.877 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1146:M 12 Aug 10:07:28.877 # Server initialized
1146:M 12 Aug 10:07:28.877 # 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.
1146:M 12 Aug 10:07:28.877 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1146:M 12 Aug 10:07:28.877 * Ready to accept connections

这里写图片描述
依次解决日志出现的警告信息:

[root@server1 7001]# echo 511 > /proc/sys/net/core/somaxconn
[root@server1 7001]# sysctl -w vm.overcommit_memory=1
vm.overcommit_memory = 1
[root@server1 7001]# vim /etc/sysctl.conf 写入配置文件永久生效
[root@server1 7001]# cat /etc/sysctl.conf | tail -n 1
vm.overcommit_memory = 1
[root@server1 7001]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@server1 7001]# ls
appendonly.aof  nodes.conf  redis.conf  redis.log  redis.pid
[root@server1 7001]# cp redis.conf ../7002
[root@server1 7001]# cp redis.conf ../7003
[root@server1 7001]# cp redis.conf ../7004
[root@server1 7001]# cp redis.conf ../7005
[root@server1 7001]# cp redis.conf ../7006
[root@server1 7001]# cd ..

这里写图片描述
为每个节点编写配置文件:

[root@server1 cluster]# ls
7001  7002  7003  7004  7005  7006
[root@server1 cluster]# cd 7002
[root@server1 7002]# ls
redis.conf
[root@server1 7002]# vim redis.conf 
[root@server1 7002]# cat redis.conf 
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /usr/local/cluster/7002/redis.pid
logfile /usr/local/cluster/7002/redis.log
[root@server1 7002]# redis-server redis.conf 
[root@server1 7002]# ls
appendonly.aof  nodes.conf  redis.conf  redis.log  redis.pid

这里写图片描述

[root@server1 7002]# cd ..
[root@server1 cluster]# cd 7003
[root@server1 7003]# ls
redis.conf
[root@server1 7003]# vim redis.conf 
[root@server1 7003]# cat redis.conf 
port 7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /usr/local/cluster/7003/redis.pid
logfile /usr/local/cluster/7003/redis.log
[root@server1 7003]# redis-server redis.conf 
[root@server1 7003]# ls
appendonly.aof  nodes.conf  redis.conf  redis.log  redis.pid

这里写图片描述

[root@server1 7003]# cd ..
[root@server1 cluster]# cd 7004
[root@server1 7004]# ls
redis.conf
[root@server1 7004]# vim redis.conf 
[root@server1 7004]# cat redis.conf 
port 7004
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /usr/local/cluster/7004/redis.pid
logfile /usr/local/cluster/7004/redis.log
[root@server1 7004]# redis-server redis.conf 
[root@server1 7004]# ls
appendonly.aof  nodes.conf  redis.conf  redis.log  redis.pid

这里写图片描述

[root@server1 7004]# cd ..
[root@server1 cluster]# cd 7005
[root@server1 7005]# vim redis.conf 
[root@server1 7005]# cat redis.conf 
port 7005
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /usr/local/cluster/7005/redis.pid
logfile /usr/local/cluster/7005/redis.log
[root@server1 7005]# redis-server redis.conf 
[root@server1 7005]# ls
appendonly.aof  nodes.conf  redis.conf  redis.log  redis.pid

这里写图片描述

[root@server1 cluster]# cd 7006
[root@server1 7006]# ls
redis.conf
[root@server1 7006]# vim redis.conf 
[root@server1 7006]# cat redis.conf 
port 7006
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /usr/local/cluster/7006/redis.pid
logfile /usr/local/cluster/7006/redis.log
[root@server1 7006]# redis-server redis.conf 
[root@server1 7006]# ls
appendonly.aof  nodes.conf  redis.conf  redis.log  redis.pid

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值