单台服务器部署集群3台

安装依赖包

[root@oldboy ~]# yum -y install zlib ruby rubygems

[root@oldboy ~]# gem install redis

安装

[root@oldboy ~]# wget http://download.redis.io/releases/redis-3.0.0.tar.gz

[root@oldboy ~]# tar xf redis-3.0.0.tar.gz 

[root@oldboy redis-3.0.0]# make PREFIX=/root/redis

[root@oldboy redis-3.0.0]# make PREFIX=/root/redis install

配置文件

[root@oldboy redis-3.0.0]# cp /root/redis-3.0.0/src/redis-trib.rb /root/redis/bin/

[root@oldboy redis-3.0.0]# mkdir /root/redis/{conf,data,logs}

[root@oldboy redis-3.0.0]# ll /root/redis

总用量 16

drwxr-xr-x 2 root root 4096 4月  13 19:40 bin

drwxr-xr-x 2 root root 4096 4月  13 19:41 conf

drwxr-xr-x 2 root root 4096 4月  13 19:41 data

drwxr-xr-x 2 root root 4096 4月  13 19:41 logs

[root@oldboy ~]# chown redis.redis /root/redis -R

[root@oldboy ~]# ll -d /root/redis

drwxr-xr-x 6 redis redis 4096 4月  13 19:41 /root/redis

复制配置文件

[root@oldboy redis]# cp /root/redis-3.0.0/redis.conf ./conf/redis-6380.conf

[root@oldboy redis]# cp /root/redis-3.0.0/redis.conf ./conf/redis-6381.conf

[root@oldboy redis]# cp /root/redis-3.0.0/redis.conf ./conf/redis-6382.conf

修改配置文件

[root@oldboy redis]# vim ./conf/redis-6380.conf

daemonize yes

pidfile /root/redis/data/redis-6380.pid

port 6380

tcp-backlog 511

bind 192.168.145.21

unixsocket /root/redis/data/redis-6380.sock

unixsocketperm 700

timeout 300

tcp-keepalive 0

loglevel verbose

logfile "/root/redis/logs/redis-6380.log"

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump-6380.rdb

dir /root/redis/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-6380.aof"

appendfsync everysec

no-appendfsync-on-rewrite yes

auto-aof-rewrite-percentage 80-100

auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

lua-time-limit 5000

cluster-enabled yes

cluster-config-file /root/redis/data/nodes-6380.conf

cluster-node-timeout 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-entries 512

list-max-ziplist-value 64

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

[root@oldboy redis]# cp ./conf/redis-6380.conf ./conf/redis-6381.conf 

cp:是否覆盖"./conf/redis-6381.conf"? y             

[root@oldboy redis]# cp ./conf/redis-6380.conf ./conf/redis-6382.conf 

cp:是否覆盖"./conf/redis-6382.conf"? y       

[root@oldboy redis]# sed -i "s#6380#6381#g" ./conf/redis-6381.conf 

[root@oldboy redis]# sed -i "s#6380#6382#g" ./conf/redis-6382.conf

调整参数 

[root@oldboy redis]# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf

[root@oldboy redis]# echo 1 > /proc/sys/vm/overcommit_memory

[root@oldboy redis]# echo 511 > /proc/sys/net/core/somaxconn

[root@oldboy redis]# ./bin/redis-server ./conf/redis-6380.conf ;tail -f logs/redis-6380.log

5744:M 13 Apr 20:16:14.849 - 0 clients connected (0 slaves), 1158392 bytes in use

5744:M 13 Apr 20:16:19.902 - 0 clients connected (0 slaves), 1158392 bytes in use

5744:M 13 Apr 20:16:24.955 - 0 clients connected (0 slaves), 1158392 bytes in use

5744:M 13 Apr 20:16:30.012 - 0 clients connected (0 slaves), 1158392 bytes in use

5744:M 13 Apr 20:16:35.065 - 0 clients connected (0 slaves), 1158392 bytes in use

5744:M 13 Apr 20:16:40.141 - 0 clients connected (0 slaves), 1158392 bytes in use

5744:M 13 Apr 20:16:45.368 - 0 clients connected (0 slaves), 1158392 bytes in use

5744:M 13 Apr 20:16:50.421 - 0 clients connected (0 slaves), 1158392 bytes in use

5744:M 13 Apr 20:16:55.475 - 0 clients connected (0 slaves), 1158392 bytes in use

5744:M 13 Apr 20:17:00.530 - 0 clients connected (0 slaves), 1158392 bytes in use

5749:M 13 Apr 20:17:01.711 * Increased maximum number of open files to 10032 (it was originally set to 1024).

5749:M 13 Apr 20:17:01.712 # Creating Server TCP listening socket 192.168.145.21:6380: bind: Address already in use

5744:M 13 Apr 20:17:05.584 - 0 clients connected (0 slaves), 1158392 bytes in use

^C

[root@oldboy redis]# ss -tnl|grep 6380

LISTEN     0      128          192.168.145.21:6380                     *:*     

LISTEN     0      128          192.168.145.21:16380  

                  *:* 

[root@oldboy redis]# echo never > /sys/kernel/mm/transparent_hugepage/enabled

[root@oldboy redis]# vim /etc/rc.local =========上面语句添加

[root@oldboy redis]# ./bin/redis-server ./conf/redis-6381.conf ;tail -f logs/redis-6381.log

5758:M 13 Apr 20:21:01.873 - 0 clients connected (0 slaves), 1158392 bytes in use

5758:M 13 Apr 20:21:06.924 - 0 clients connected (0 slaves), 1158392 bytes in use

5758:M 13 Apr 20:21:11.978 - 0 clients connected (0 slaves), 1158392 bytes in use

5758:M 13 Apr 20:21:17.031 - 0 clients connected (0 slaves), 1158392 bytes in use

5758:M 13 Apr 20:21:22.085 - 0 clients connected (0 slaves), 1158392 bytes in use

5758:M 13 Apr 20:21:27.138 - 0 clients connected (0 slaves), 1158392 bytes in use

5758:M 13 Apr 20:21:32.192 - 0 clients connected (0 slaves), 1158392 bytes in use

5758:M 13 Apr 20:21:37.249 - 0 clients connected (0 slaves), 1158392 bytes in use

5758:M 13 Apr 20:21:42.301 - 0 clients connected (0 slaves), 1158392 bytes in use

5758:M 13 Apr 20:21:47.421 - 0 clients connected (0 slaves), 1158392 bytes in use

5768:M 13 Apr 20:21:47.685 * Increased maximum number of open files to 10032 (it was originally set to 1024).

5768:M 13 Apr 20:21:47.685 # Creating Server TCP listening socket 192.168.145.21:6381: bind: Address already in use

5758:M

[root@oldboy redis]# ./bin/redis-server ./conf/redis-6382.conf ;tail -f logs/redis-6382.log

[root@oldboy redis]# ss -tnl|grep 63

LISTEN     0      511          192.168.145.21:16381                    *:*     

LISTEN     0      511          192.168.145.21:16382                    *:*     

LISTEN     0      128          192.168.145.21:6380                     *:*     

LISTEN     0      511          192.168.145.21:6381                     *:*     

LISTEN     0      511          192.168.145.21:6382                     *:*     

LISTEN     0      128          192.168.145.21:16380                    *:* 

创建cluster

[root@oldboy redis]# ./bin/redis-trib.rb create 192.168.145.21:6380 192.168.145.21:6381 192.168.145.21:6382

注:redis-trib.rb使用参数

Usage: redis-trib <command> <options> <arguments ...>

  fix             host:port

  call            host:port command arg arg .. arg

  check           host:port

  import          host:port

                  --from <arg>

  set-timeout     host:port milliseconds

  add-node        new_host:new_port existing_host:existing_port

                  --master-id <arg>

                  --slave

  reshard         host:port

                  --to <arg>

                  --from <arg>

                  --slots <arg>

                  --yes

  create          host1:port1 ... hostN:portN

                  --replicas <arg>

  help            (show this help)

  del-node        host:port node_id

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

参数说明:

call:执行redis命令

create:======创建一个新的集群。

host1:port1 ... hostN:portN============指定了用于构建Redis Cluster的所有redis实例,节点角色由顺序决定,先master之后是slave。而--replicas 则指定了为Redis Cluster中的每个Master节点配备几个Slave节点。

add-node=========将一个节点添加到集群里面, 第一个是新节点ip:port, 第二个是任意一个已存在节点ip:port,--master-id 

reshard===============重新分片

check:===============查看集群信息

del-node:=================移除一个节点

对于客户端redis-cli要访问集群,在启动的时候需要添加一个-c参数,如查看集群所有节点:

./bin/redis-cli -c -h 192.168.36.189 -p 6380  cluster nodes

集群命令

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 槽中的键。 

[root@oldboy redis]# ./bin/redis-cli -c -h 192.168.145.21 -p 6380 

192.168.145.21:6380> set a 123

-> Redirected to slot [15495] located at 192.168.145.21:6382

OK

192.168.145.21:6382> get a

"123"

192.168.145.21:6382> quit

[root@oldboy redis]# ./bin/redis-cli -c -h 192.168.145.21 -p 6381

192.168.145.21:6381> get a

-> Redirected to slot [15495] located at 192.168.145.21:6382

"123"

192.168.145.21:6382> quit

[root@oldboy redis]# ./bin/redis-cli -c -h 192.168.145.21 -p 6382

192.168.145.21:6382> get a

"123"