單臺服務器部署集羣3臺node
安裝依賴包redis
[root@oldboy ~]# yum -y install zlib ruby rubygems
vim
[root@oldboy ~]# gem install redisruby
安裝服務器
[root@oldboy ~]# wget http://download.redis.io/releases/redis-3.0.0.tar.gzapp
[root@oldboy ~]# tar xf redis-3.0.0.tar.gz less
[root@oldboy redis-3.0.0]# make PREFIX=/root/redissocket
[root@oldboy redis-3.0.0]# make PREFIX=/root/redis installtcp
配置文件ide
[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"