前面的3回都是吹牛逼的,到这里才刚刚开始玩真的。
是的,这个架构,说大不大,说小不小。架构都已经画好了,先不说代码怎么实现,组件得先搭起来,否则怎么玩?
这一次,我们就先聊一下怎么搭建一个Redis集群。
由于机器数量不多,为了方便,这个Redis集群就全部搭建在同一台机器上面,如果将来真的部署在生产环境了,当然不能这样玩,肯定要3个节点放到3台机器上,否则这个集群就没有意义了。
很多人把集群和主从都搞混。主从就是其实一份数据放到两台机器,从是主的备份而已,集群不一样,集群是把数据进行分片,一个节点存一点, 如果想做到真正的高可用分布式,那必须要集群加主从,否则,如果没有主从,只有集群,A,B,C,坏掉一个,就可能不见1/3数据,在安全和成本之间,要不要主从,就看怎么折中了。
如果只是想简单了解一下,看个教程就行
https://redis.io/topics/cluster-tutorial
如果想成为高手,当然要看看规范,规范如下
https://redis.io/topics/cluster-tutorial
实在英文不行的话,百度一下也行,不过百度来的东西,很多是错的,那得自己去伪存真了。
以下就是安装的过程:
系统:CentOS 6.9
内存:8G
硬盘: 30G
CPU: 4线程
安装过程:
1. 建立一个存放目录以及集群节点目录
mkdir -p /data1/software/redis/
cd /data1/software/redis/
mkdir redis_1
mkdir redis_2
mkdir redis_3
cd ../
2. 下载redis包,解压,编译
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
tar xzvf redis-4.0.10.tar.gz
cd redis-4.0.10/src
make
make test
注:如果make test被提示“You need tcl 8.5 or newer in order to run the Redis test”
那么就安装一下tcl,再make test就没问题了,安装过程如:
cd /data1/software/redis/
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
tar xzvf tcl8.6.1-src.tar.gz -C /usr/local/
cd /usr/local/tcl8.6.1/unix/
sudo ./configure
make
make install3. 安装一个到第一个节点,把默认的配置也拷贝过去
cd /data1/software/redis/redis-4.0.10/src
make PREFIX=/data1/software/redis/redis_1 install
4.进入第一个节点的目录,如:
cd /data1/software/redis/redis_1
[root@Centos-213 redis_1]# ls -R
.:
bin
./bin:
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
5. 可见启动文件已经安装过来了,接着整理一下配置
mkdir conf
cd conf
cp /data1/software/redis/redis-4.0.10/redis.conf .
6. 启动节点
[root@Centos-213 bin]# ./redis-server ../conf/redis.conf
9129:C 22 Jun 11:23:22.454 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9129:C 22 Jun 11:23:22.454 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=9129, just started
9129:C 22 Jun 11:23:22.454 # Configuration loaded
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.10 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 9129
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
9129:M 22 Jun 11:23:22.456 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9129:M 22 Jun 11:23:22.456 # Server initialized
9129:M 22 Jun 11:23:22.456 # 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.
9129:M 22 Jun 11:23:22.456 # 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.
9129:M 22 Jun 11:23:22.456 * DB loaded from disk: 0.000 seconds
9129:M 22 Jun 11:23:22.456 * Ready to accept connections
7. 单节点是启动了,进程号,端口号什么都显示出来,不用多说,但是有几个告警。这些告警要不要理会呢?如果是在测试环境,就随意了,如果是线上环境,当然要解决。否则就成了隐患。现在,我们来看看上面那几个告警:
告警1:
WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
原因:要解释backlog,就要知道tcp的三次握手是什么回事,这个东西,如果要把来龙去脉都讲清楚,真的要讲一个礼拜,这里就先不展开了,我看了有的博客说,backlog是tcp协议里面的规范xxxxx,瞎掰!你去查查tcp协议规范里面有这个东西吗?这个东西,其实已经是tcp的系统实现的范畴了,协议是协议,实现是实现,协议只有一种,实现可不一定是一种,一个接口对应着多少个实现类?自己想想,如果想了解,可以阅读以下以下的文章,做好心理准备啊,是因为的。
http://veithen.github.io/2014/01/01/how-tcp-backlog-works-in-linux.html
解决方案:
把参数改大一点就可以,编辑 /etc/sysctl.conf文件,增加一行配置:net.core.somaxconn= 1024
vim /etc/sysctl.conf
sysctl -p
告警2: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.原因:这里需要解释一下vm.overcommit_memory这个内核参数,该参数接受三种取值:
- 0 – Heuristic overcommit handling. 这是缺省值,它允许overcommit,Heuristic的意思是“试探式的”,内核利用某种算法计算你的内存申请是否合理,它认为不合理就会拒绝overcommit。
- 1 – Always overcommit. 允许overcommit,申请多少都给。
- 2 – Don’t overcommit. 禁止overcommit。
解决方案:跟上面第一个告警是差不多的,这里不赘述。但是,个人认为,这里的参数,改不改都没有很大意义,当一个redis申请不到内存的话, 系统应该已经基本没有内存了,强硬要超过系统要内存是不会有好结果的。
告警3:
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.
解决方案:THP这个我真的不懂,以后有空在研究了。
解决方案:告警里面已经给了提示了,按照提示做就可以了。