redis3.0集群搭建
1. 部署规划
依据官网介绍,部署6个redis节点,为3主3从。3台物理机每台都创建2个redis节点。
IP | 实例 |
192.168.56.21 | 7000 |
7001 | |
192.168.56.22 | 7002 |
7003 | |
192.168.56.23 | 7004 |
7005 |
2. 系统优化
2.1修改内存分配控制
修改vm.overcommit_memory值,默认为0,需要修改为1
操作步骤:
echo vm.overcommit_memory = 1 >> /etc/sysctl.conf
sysctl -p
2.2 TCP backlog
修改net.core.somaxconn 值,默认为128,需要修改为65535
echo net.core.somaxconn = 65535 >> /etc/sysctl.conf
sysctl -p
2.3 Transparent Huge Pages(需要重启服务器)
<ac:structured-macro ac:name=”unmigrated-wiki-markup” ac:schema-version=”1″ ac:macro-id=”abe25705-1e30-4851-8f5f-9a1e6e8d3d06″><ac:plain-text-body><![CDATA[ | 首先检查THP的启用状态:[root@localhost ~]# cat /sys/kernel/mm/transparent_hugepage/defrag[always] madvise never[root@localhost ~]# cat /sys/kernel/mm/transparent_hugepage/enabled[always] madvise never 这个状态就说明都是启用的。 我们这个时候当然可以逐个修改上述两文件,来禁用THP,但要想一劳永逸的令其永久生效,还是参考下列的步骤。 编辑rc.local文件:[root@localhost ~]# vim /etc/rc.d/rc.local 增加下列内容:if test -f /sys/kernel/mm/transparent_hugepage/enabled; thenecho never > /sys/kernel/mm/transparent_hugepage/enabledfiif test -f /sys/kernel/mm/transparent_hugepage/defrag; thenecho never > /sys/kernel/mm/transparent_hugepage/defragfi 保存退出,然后赋予rc.local文件执行权限:[root@localhost ~]# chmod +x /etc/rc.d/rc.local 最后重启系统,以后再检查THP应该就是被禁用了[root@localhost ~]# cat /sys/kernel/mm/transparent_hugepage/enabledalways madvise [never][root@localhost ~]# cat /sys/kernel/mm/transparent_hugepage/defrag always madvise [never] | ]]></ac:plain-text-body></ac:structured-macro> |
3. 安装redis实例
3.1安装redis所需的依赖包
yum install gcc make
3.2安装redis实例
3.2.1 下载redis-3.2.9.tar.gz
wget http://download.redis.io/releases/redis-3.2.9.tar.gz

3.2.2 解压并安装redis于/opt目录
tar -zxvf redis-3.2.9.tar.gz
3.2.3 编译安装redis
cd redis-3.2.9
make && make install
输出结尾如下

输入redis-server –version 出现版本信息则redis安装成功

4. redis集群部署
4.1配置并启动redis实例
4.1.2 创建必要的目录结构
192.168.56.21:
[root@cx_redis1 redis-3.2.9]# mkdir -p /opt/redis_cluster/7000/data
[root@cx_redis1 redis-3.2.9]# mkdir -p /opt/redis_cluster/7000/log
[root@cx_redis1 redis-3.2.9]# mkdir -p /opt/redis_cluster/7001/log
[root@cx_redis1 redis-3.2.9]# mkdir -p /opt/redis_cluster/7001/data
192.168.56.22:
[root@cx_redis2 redis-3.2.9]# mkdir -p /opt/redis_cluster/7002/data
[root@cx_redis2 redis-3.2.9]# mkdir -p /opt/redis_cluster/7002/log
[root@cx_redis2 redis-3.2.9]# mkdir -p /opt/redis_cluster/7003/log
[root@cx_redis2 redis-3.2.9]# mkdir -p /opt/redis_cluster/7003/data
192.168.56.23:
[root@cx_redis3 redis-3.2.9]# mkdir -p /opt/redis_cluster/7004/data
[root@cx_redis3 redis-3.2.9]# mkdir -p /opt/redis_cluster/7004/log
[root@cx_redis3 redis-3.2.9]# mkdir -p /opt/redis_cluster/7005/log
[root@cx_redis3 redis-3.2.9]# mkdir -p /opt/redis_cluster/7005/data
4.1.3 拷贝并修改redis.conf
192.168.56.21:
[root@cx_redis1 redis-3.2.9]# cp /opt/redis-3.2.9/redis.conf /opt/redis_cluster/7000/
[root@cx_redis1 redis-3.2.9]# cp /opt/redis-3.2.9/redis.conf /opt/redis_cluster/7001/
192.168.56.22:
[root@cx_redis2 redis-3.2.9]# cp /opt/redis-3.2.9/redis.conf /opt/redis_cluster/7002/
[root@cx_redis2 redis-3.2.9]# cp /opt/redis-3.2.9/redis.conf /opt/redis_cluster/7003/
192.168.56.23:
[root@cx_redis3 redis-3.2.9]# cp /opt/redis-3.2.9/redis.conf /opt/redis_cluster/7004/
[root@cx_redis3 redis-3.2.9]# cp /opt/redis-3.2.9/redis.conf /opt/redis_cluster/7005/
修改redis.conf配置文件如下,以下已7000节点为例,每个节点只需将7000替换为对应的端口号:
bind 0.0.0.0 protected-mode no port 7000 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile “/var/run/redis_7000.pid” loglevel notice logfile “/opt/redis_cluster/7000/log/run.log” databases 16 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename “dump_7000.rdb” dir “/opt/redis_cluster/7000/data” slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly yes appendfilename “appendonly_7000.aof” appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 500mb aof-load-truncated yes lua-time-limit 5000 cluster-enabled yes cluster-config-file “nodes-7000.conf” cluster-node-timeout 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events Ex hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes |
4.1.4 分别启动6个节点,验证启动成功
192.168.56.21:
[root@cx_redis1 7000]# redis-server /opt/redis_cluster/7000/redis.conf
[root@cx_redis1 7000]# redis-server /opt/redis_cluster/7001/redis.conf

192.168.56.22:
[root@cx_redis2 7003]# redis-server /opt/redis_cluster/7002/redis.conf
[root@cx_redis2 7003]# redis-server /opt/redis_cluster/7003/redis.conf

192.168.56.23:
[root@cx_redis3 7005]# redis-server /opt/redis_cluster/7004/redis.conf
[root@cx_redis3 7005]# redis-server /opt/redis_cluster/7005/redis.conf

4.2创建和启动redis集群
4.2.1 安装ruby环境
yum install ruby rubygems -y
4.2.2 下载并安装redis-3.2.2.gem
下载
wget https://rubygems.org/downloads/redis-3.2.2.gem
安装
gem install -l redis-3.2.2.gem

4.2.3 使用redis-trib.rb工具创建启动集群
cd /opt/redis-3.2.9/src
./redis-trib.rb create –replicas 1 192.168.56.21:7000 192.168.56.21:7001 192.168.56.22:7002 192.168.56.22:7003 192.168.56.23:7004 192.168.56.23:7005
<ac:structured-macro ac:name=”unmigrated-wiki-markup” ac:schema-version=”1″ ac:macro-id=”feb7a0ac-4854-4179-8a3b-2dd212ae864a”><ac:plain-text-body><![CDATA[ | [root@cx_redis1 src]# ./redis-trib.rb create –replicas 1 192.168.56.21:7000 192.168.56.21:7001 192.168.56.22:7002 192.168.56.22:7003 192.168.56.23:7004 192.168.56.23:7005 ]]></ac:plain-text-body></ac:structured-macro> >>> Creating cluster >>> Performing hash slots allocation on 6 nodes… Using 3 masters: 192.168.56.21:7000 192.168.56.22:7002 192.168.56.23:7004 Adding replica 192.168.56.22:7003 to 192.168.56.21:7000 Adding replica 192.168.56.21:7001 to 192.168.56.22:7002 Adding replica 192.168.56.23:7005 to 192.168.56.23:7004 M: 4b9f1831c9e302b5be0744265271c5b9634cd1d4 192.168.56.21:7000 slots:0-5460 (5461 slots) master S: 7189a33d6d19db6f41591d793cc81b34ce258d62 192.168.56.21:7001 replicates 34aa8f81f059bdfa69bd34f65e91476ccb6b4f18 M: 34aa8f81f059bdfa69bd34f65e91476ccb6b4f18 192.168.56.22:7002 slots:5461-10922 (5462 slots) master S: e992dcfeeb396b0c05b1f3e4883e251e4644d8f8 192.168.56.22:7003 replicates 4b9f1831c9e302b5be0744265271c5b9634cd1d4 M: baa46a38248e5e600b333ca9b702fe3a2b8e8354 192.168.56.23:7004 slots:10923-16383 (5461 slots) master S: 4888a985af5b0910449ec67a42af93ae9bf0d5c9 192.168.56.23:7005 replicates baa46a38248e5e600b333ca9b702fe3a2b8e8354 Can I set the above configuration? (type ‘yes’ to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join… >>> Performing Cluster Check (using node 192.168.56.21:7000) M: 4b9f1831c9e302b5be0744265271c5b9634cd1d4 192.168.56.21:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: e992dcfeeb396b0c05b1f3e4883e251e4644d8f8 192.168.56.22:7003 slots: (0 slots) slave replicates 4b9f1831c9e302b5be0744265271c5b9634cd1d4 M: 34aa8f81f059bdfa69bd34f65e91476ccb6b4f18 192.168.56.22:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 4888a985af5b0910449ec67a42af93ae9bf0d5c9 192.168.56.23:7005 slots: (0 slots) slave replicates baa46a38248e5e600b333ca9b702fe3a2b8e8354 M: baa46a38248e5e600b333ca9b702fe3a2b8e8354 192.168.56.23:7004 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 7189a33d6d19db6f41591d793cc81b34ce258d62 192.168.56.21:7001 slots: (0 slots) slave replicates 34aa8f81f059bdfa69bd34f65e91476ccb6b4f18 <ac:structured-macro ac:name=”unmigrated-wiki-markup” ac:schema-version=”1″ ac:macro-id=”1026a77c-b055-456c-ad8a-e1128dc94eda”><ac:plain-text-body><![CDATA[[OK] All nodes agree about slots configuration. ]]></ac:plain-text-body></ac:structured-macro> >>> Check for open slots… >>> Check slots coverage… <ac:structured-macro ac:name=”unmigrated-wiki-markup” ac:schema-version=”1″ ac:macro-id=”4a1174e4-982f-4c26-b722-9007b6e9e3dc”><ac:plain-text-body><![CDATA[[OK] All 16384 slots covered. | ]]></ac:plain-text-body></ac:structured-macro> |
4.2.4 检查集群状态
登陆redis查看信息
redis-cli -c -p 7000
cluster nodes

- 本文固定链接: https://www.gayj.cn/?p=632
- 转载请注明: https://www.gayj.cn/