Redis-cluster快速实现
Posted knmax
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis-cluster快速实现相关的知识,希望对你有一定的参考价值。
1、环境规划
ip | node-port | os-version | redis-version |
---|---|---|---|
10.10.16.40 | 6379,6380 | CentOS7.4 | redis-4.0.7 |
10.10.16.41 | 6379,6380 | CentOS7.4 | redis-4.0.7 |
10.10.16.42 | 6379,6380 | CentOS7.4 | redis-4.0.7 |
Redis-Cluster要求至少6个节点
2、安装redis
基本安装脚本
~]# cat redis_install.sh
#!/bin/bash
cd
yum install gcc cmake make wget -y
wget http://download.redis.io/releases/redis-4.0.7.tar.gz
tar -xf redis-4.0.7.tar.gz
cd redis-4.0.7
make MALLOC=jemalloc -j 8
make PREFIX=/usr/local/redis install
echo 'PATH=/usr/local/redis/bin/:$PATH' >> /etc/profile
source /etc/profile
echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >>/etc/rc.local
sysctl -p
echo "Installed"
服务启动脚本
6379
~]# cd redis-4.0.7/utils/
utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/redis/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
6380
utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] 6380
Please select the redis config file name [/etc/redis/6380.conf]
Selected default - /etc/redis/6380.conf
Please select the redis log file name [/var/log/redis_6380.log]
Selected default - /var/log/redis_6380.log
Please select the data directory for this instance [/var/lib/redis/6380]
Selected default - /var/lib/redis/6380
Please select the redis executable path [/usr/local/redis/bin/redis-server]
Selected config:
Port : 6380
Config file : /etc/redis/6380.conf
Log file : /var/log/redis_6380.log
Data dir : /var/lib/redis/6380
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6380.conf => /etc/init.d/redis_6380
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
3、配置redis
6379
~]# egrep -v '^#|^$' /etc/redis/6380.conf
bind 10.10.16.40
protected-mode no
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6380.pid
loglevel notice
logfile /var/log/redis_6380.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis/6380
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
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
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
[[email protected] ~]# egrep -v '^#|^$' /etc/redis/6379.conf
bind 10.10.16.40
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis_6379.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis/6379
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
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
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
6380
~]# egrep -v '^#|^$' /etc/redis/6380.conf
bind 10.10.16.40
protected-mode no
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6380.pid
loglevel notice
logfile /var/log/redis_6380.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis/6380
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
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
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
基本都是默认的配置
- 其他2台服务器也这样配置
服务开机启动
systemctl enable redis_6379
systemctl enable redis_6380
服务启动脚本shutdown默认是host是127.0.0.1,需要修改为对应的bind ip
具体步骤:
HOST="10.10.16.40" // 添加一个变量,本机的ip地址
$CLIEXEC -h $HOST -p $REDISPORT shutdown // 修改
4、创建redis-cluster
使用redis-trib
,其中任意一个节点安装就行
编译安装ruby
centos 7自带的ruby版本太低,需要2.4及以上的
~]# yum install -y zlib-devel openssl-devel
~]# wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.2.tar.gz
~]# tar -xf ruby-2.4.2.tar.gz
~]# cd ruby-2.4.2
ruby-2.4.2]# ./configure && make -j 8 && make install
~]# ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
安装trib
~]# cd redis-4.0.7
redis-4.0.7]# gem install redis
# cp src/redis-trib.rb /usr/bin/redis-trib
创建集群
# redis-trib create --replicas 1 10.10.16.40:6379 10.10.16.40:6380 10.10.16.41:6379 10.10.16.41:6380 10.10.16.42:6379 10.10.16.42:6380
# redis-cli -c -h 10.10.16.40 -p 6379
10.10.16.40:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:46
cluster_stats_messages_pong_sent:57
cluster_stats_messages_sent:103
cluster_stats_messages_ping_received:52
cluster_stats_messages_pong_received:46
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:103
10.10.16.40:6379> cluster nodes
2ba99a87a1d1c5e959fcd0fddf03ee0bdfd3e130 10.10.16.41:[email protected] master - 0 1516967513049 3 connected 5461-10922
2cd2f229d0e1792deca10930a64f33567defad5a 10.10.16.40:[email protected] myself,master - 0 1516967511000 1 connected 0-5460
7773317ea089c843f148fda4cb7083cba9bdaa8e 10.10.16.42:[email protected] slave 2ba99a87a1d1c5e959fcd0fddf03ee0bdfd3e130 0 1516967510042 6 connected
603b4205533264399118ab9ba85ed284b336947f 10.10.16.41:[email protected] slave 2cd2f229d0e1792deca10930a64f33567defad5a 0 1516967510000 4 connected
8b80a3611f4057781329ac276922f775ab529b02 10.10.16.40:[email protected] slave ca112f48145059a210febf3fd01d6025f5697ff2 0 1516967512046 5 connected
ca112f48145059a210febf3fd01d6025f5697ff2 10.10.16.42:[email protected] master - 0 1516967511044 5 connected 10923-16383
以上是关于Redis-cluster快速实现的主要内容,如果未能解决你的问题,请参考以下文章