yt-summer 发表于 2018-11-5 10:53:04

redis 集群

  单台服务器部署集群3台
  安装依赖包
  # yum -y install zlib ruby rubygems
  # gem install redis
  安装
  # wget http://download.redis.io/releases/redis-3.0.0.tar.gz
  # tar xf redis-3.0.0.tar.gz
  
  # make PREFIX=/root/redis
  # make PREFIX=/root/redis install
  配置文件
  # cp /root/redis-3.0.0/src/redis-trib.rb /root/redis/bin/
  # mkdir /root/redis/{conf,data,logs}
  # 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
  
  
  # chown redis.redis /root/redis -R
  # ll -d /root/redis
  drwxr-xr-x 6 redis redis 4096 4月13 19:41 /root/redis
  
  复制配置文件
  # cp /root/redis-3.0.0/redis.conf ./conf/redis-6380.conf
  #cp /root/redis-3.0.0/redis.conf ./conf/redis-6381.conf
  #cp /root/redis-3.0.0/redis.conf ./conf/redis-6382.conf
  修改配置文件
  # 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
  # cp ./conf/redis-6380.conf ./conf/redis-6381.conf
  cp:是否覆盖"./conf/redis-6381.conf"? y
  # cp ./conf/redis-6380.conf ./conf/redis-6382.conf
  cp:是否覆盖"./conf/redis-6382.conf"? y
  # sed -i "s#6380#6381#g" ./conf/redis-6381.conf
  #sed -i "s#6380#6382#g" ./conf/redis-6382.conf
  调整参数
  # echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
  # echo 1 > /proc/sys/vm/overcommit_memory
  # echo 511 > /proc/sys/net/core/somaxconn
  # ./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
  # ss -tnl|grep 6380
  LISTEN   0      128          192.168.145.21:6380                     *:*
  LISTEN   0      128          192.168.145.21:16380
  *:*
  # echo never > /sys/kernel/mm/transparent_hugepage/enabled
  # vim /etc/rc.local =========上面语句添加
  # ./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
  #./bin/redis-server ./conf/redis-6382.conf ;tail -f logs/redis-6382.log
  
  
  # 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
  # ./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   
  fix             host:port
  call            host:port command arg arg .. arg
  check         host:port
  import          host:port
  --from
  set-timeout   host:port milliseconds
  add-node      new_host:new_port existing_host:existing_port
  --master-id
  --slave
  reshard         host:port
  --to
  --from
  --slots
  --yes
  create          host1:port1 ... hostN:portN
  --replicas
  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所指定的节点添加到集群当中,让它成为集群的一份子。
  cluster forget===========从集群中移除 node_id 指定的节点。
  cluster replicate===========将当前节点设置为node_id指定的节点的从节点。
  cluster saveconfig ========将节点的配置文件保存到硬盘里面。
  槽(slot)
  cluster addslots ==============将一个或多个槽(slot)指派(assign)给当前节点。
  cluster delslots ====================移除一个或多个槽对当前节点的指派。
  cluster flushslots================= 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
  cluster setslotnode===================将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
  cluster setslotmigrating===============将本节点的槽 slot 迁移到 node_id 指定的节点中。
  cluster setslotimporting===================从 node_id 指定的节点中导入槽 slot 到本节点。
  cluster setslotstable======================== 取消对槽 slot 的导入(import)或者迁移(migrate)。
  键
  cluster keyslot==========================计算键 key 应该被放置在哪个槽上。=
  cluster countkeysinslot=================返回槽 slot 目前包含的键值对数量。
  cluster getkeysinslot   ==============返回 count 个 slot 槽中的键。
  # ./bin/redis-cli -c -h 192.168.145.21 -p 6380
  192.168.145.21:6380> set a 123
  -> Redirected to slot located at 192.168.145.21:6382
  OK
  192.168.145.21:6382> get a
  "123"
  192.168.145.21:6382> quit
  # ./bin/redis-cli -c -h 192.168.145.21 -p 6381
  192.168.145.21:6381> get a
  -> Redirected to slot located at 192.168.145.21:6382
  "123"
  192.168.145.21:6382> quit
  # ./bin/redis-cli -c -h 192.168.145.21 -p 6382
  192.168.145.21:6382> get a
  "123"

页: [1]
查看完整版本: redis 集群