Redis 4.0.2 集群安装
01 安装单机版
02 创建集群的配置文件
创建一个安装配置文件的目录
mkdir -p /usr/local/redis/conf
创建第一个配置文件 redis_6380.conf
bind 192.168.140.129
protected-mode yes
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /export/data/redis/6380/redis_6380.pid
loglevel notice
logfile "/export/data/redis/6380/log.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 /export/data/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 no
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
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
cluster-enabled yes
cluster-config-file /usr/local/redis/conf/nodes_6380.conf
cluster-node-timeout 5000
创建其它五个配置文件,一共有六个配置文件
cp redis_6380.conf redis_6381.conf
cp redis_6380.conf redis_6382.conf
cp redis_6380.conf redis_6383.conf
cp redis_6380.conf redis_6384.conf
cp redis_6380.conf redis_6385.conf
修改6381-6385每个配置文件中的选项
主要修改端口号,6380改成对应的端口号,比如6381,6382
port 6380
pidfile /export/data/redis/6380/redis_6380.pid
logfile "/export/data/redis/6380/log.log"
dir /export/data/redis/6380/
cluster-config-file /usr/local/redis/conf/nodes_6380.conf
mkdir /export/data/redis/6380/
02启动集群
启动每个节点
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_6380.conf
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_6381.conf
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_6382.conf
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_6383.conf
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_6384.conf
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis_6385.conf
准备配置集群
redis自带一个集群配置脚本,在src目录下。
运行这个脚本需要安装ruby相关的内容
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis
#gem sources -a https://ruby.taobao.org/
如果以上代码报错,执行下面代码
wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
gem install -l ./redis-3.2.1.gem
gem install redis
启动集群
/usr/local/src/redis/src/redis-trib.rb create --replicas 1 172.16.118.100:6380 172.16.118.100:6381 172.16.118.100:6382 172.16.118.100:6383 172.16.118.100:6384 172.16.118.100:6385
连接集群
/usr/local/redis/bin/redis-cli -h 192.168.140.129 -c -p 6380
检查集群状态
/export/servers/redis/src/redis-trib.rb check 192.168.140.129:6380
关闭集群
pkill redis
列出集群列表
/usr/local/redis/bin/redis-cli -h 172.16.118.100 -c -p 6380
cluster nodes
7d9471040f0040e27d047cf0f8a21674ccf26d96 192.168.140.129:6381@16381 master - 0 1511085796000 2 connected 5461-10922
66c691e48dad0c33bb2683a573e71fe0987c65a6 192.168.140.129:6385@16385 slave 66bf06321f17c729d18e4cdd799a72a3bcf9c0bc 0 1511085795000 6 connected
0e7cbcb9794e088cc5a5002735c0b05fe2d71bb1 192.168.140.129:6380@16380 myself,master - 0 1511085796000 1 connected 0-5460
66bf06321f17c729d18e4cdd799a72a3bcf9c0bc 192.168.140.129:6382@16382 master - 0 1511085795000 3 connected 10923-16383
a8b7d0d9d8dd8877192836ae7862902366cb7cdd 192.168.140.129:6383@16383 slave 0e7cbcb9794e088cc5a5002735c0b05fe2d71bb1 0 1511085796556 4 connected
94cd289edd0f5c7f43430090191d98867f5d65ed 192.168.140.129:6384@16384 slave 7d9471040f0040e27d047cf0f8a21674ccf26d96 0 1511085795000 5 connected
打印集群信息
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:292
cluster_stats_messages_pong_sent:302
cluster_stats_messages_sent:594
cluster_stats_messages_ping_received:302
cluster_stats_messages_pong_received:290
cluster_stats_messages_received:592
03 集群命令
语法格式
redis-cli -h hostname -c -p port
集群信息
cluster info :打印集群的信息
cluster nodes :列出集群当前已知的所有节点( node),以及这些节点的相关信息。
单个节点操作
cluster meet <ip> <port> :将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
cluster forget <node_id> :从集群中移除 node_id 指定的节点。
cluster replicate <node_id> :将当前节点设置为 node_id 指定的节点的从节点。
cluster saveconfig :将节点的配置文件保存到硬盘里面。
槽(slot)
cluster addslots <slot> [slot ...] :将一个或多个槽( slot)指派( assign)给当前节点。
cluster delslots <slot> [slot ...] :移除一个或多个槽对当前节点的指派。
cluster flushslots :移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
cluster setslot <slot> node <node_id> :将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
cluster setslot <slot> migrating <node_id> :将本节点的槽 slot 迁移到 node_id 指定的节点中。
cluster setslot <slot> importing <node_id> :从 node_id 指定的节点中导入槽 slot 到本节点。
cluster setslot <slot> stable :取消对槽 slot 的导入( import)或者迁移( migrate)。
键
cluster keyslot <key> :计算键 key 应该被放置在哪个槽上。
cluster countkeysinslot <slot> :返回槽 slot 目前包含的键值对数量。
cluster getkeysinslot <slot> <count> :返回 count 个 slot 槽中的键 。