Redis Cluster 安装篇(一)

安装方式

  1. 原生命令安装(繁琐、易错)
  2. 官方工具安装(简单、快捷)

配置文件简介

  • 最简配置
    #修改端口号为7000
      port {
           port}
      daemonize yes
      pidfile /var/run/redis_{
           port}.pid
      logfile "/usr/local/software/redis/cluster-log/{port}.log"
      dir "/usr/local/software/redis/cluster-data"
      dbfilename dump-{
           port}.rdb
      masterauth password
      appendonly yes
      appendfilename "appendonly-{port}.aof"
      # 启用cluster
      cluster-enabled yes
      # cluster节点自己的配置文件
      cluster-config-file nodes-{
           port}.conf
      # 主观下线超时时间
      cluster-node-timeout 15000
      # 不需要所有节点都提供服务集群才对外提供服务
      cluster-require-full-coverage no
      # 如果设置为0,只要发现主节点失联,那么从节点总会去尝试进行故障转移
      #如果为正数,一旦从节点与主节点失联的时间超过 cluster-node-timeout * cluster-replica-validity-factor + ping的间隔时间,那么该从节点不会进行故障转移
      # 如果为非0的值,一旦所有从节点都不可进行故障转移时,集群将不再对外提供服务,直到主节点重新加入集群
      # redis 5.0之后是 cluster-replica-validity-factor
      cluster-slave-validity-factor 10
      # 一个主节点维持的最少可用从节点数,如果集群中出现孤立主节点时,其他主节点在分配给孤立主节点一个从节点后,自身还能保持count个可用从节点,那么该主节点触发节点迁移,以保证集群的最大可用性。
      cluster-migration-barrier count(默认为1)
    

原生命令安装(理解原理)

  1. 修改配置文件(redis.conf)
    简易版,其他使用默认配置,从7000到7005端口,共生成6个配置文件。

  2. meet操作

    redis-cli -h {
           host} -p 7000 cluster meet {
           host} 7001
    redis-cli -h {
           host} -p 7000 cluster meet {
           host} 7002
    redis-cli -h {
           host} -p 7000 cluster meet {
           host} 7003
    redis-cli -h {
           host} -p 7000 cluster meet {
           host} 7004
    redis-cli -h {
           host} -p 7000 cluster meet {
           host} 7005
    cluster nodes
    
    #查看集群节点信息
    redis-cli -h {
           host} cluster nodes
    
    e0a2bcc7148770cf1b74fcc1231239b305c7dab1 127.0.0.1:7001@17001 master - 0 1564400731174 4 connected
    52bac4f927a8ef09698f96b04315705e515a8ebc 127.0.0.1:7005@17005 master - 0 1564400734180 5 connected
    06b0826a162390e4b2094e57594a40a181aa2206 127.0.0.1:7000@17000 myself,master - 0 1564400731000 1 connected
    e7148fb70e97d7fc50ec4c31565f9efd66f744fc 127.0.0.1:7004@17004 master - 0 1564400731000 0 connected
    9e924621a19f48368e13923230f7b3b0803a1b4a 127.0.0.1:7003@17003 master - 0 1564400733177 3 connected
    d23b903f7342a96c7f447eb413a6a2c09195d183 127.0.0.1:7002@17002 master - 0 1564400732176 2 connected
    
  3. 分配槽

    # 我们使用三主三从,因此16384个槽分为三份,分配下去
    redis-cli -h {
           host} -p 7000 cluster addslots {
           0...5461}
    redis-cli -h {
           host} -p 7001 cluster addslots {
           5462...10922}
    redis-cli -h {
           host} -p 7002 cluster addslots {
           10923...16383}
    

    一条一条敲命令太麻烦?写个脚本一步到位。

    vim addslots.sh
    start=$1
    end=$2
    host=$3
    port=$4
    
    for slot in `seq ${
            start} ${
            end}`
    do
        echo "slot:${slot}"
        ../redis-cli -h ${host} -p ${port} cluster addslots ${slot}
    done
    
    # 为7000节点分配槽
    sh addslots.sh 0 5461 127.0.0.1 7000
    

    连接7000节点,查看cluster nodes信息

    127.0.0.1:7000> cluster info
    # 集群已成功启动
    cluster_state:ok
    # 很清楚的看到,已分配给7000节点5462个槽
    cluster_slots_assigned:5462
    cluster_slots_ok:5462
    cluster_slots_pfail:0
    cluster_slots_fail:0
    cluster_known_nodes:6
    cluster_size:1
    cluster_current_epoch:5
    cluster_my_epoch:1
    cluster_stats_messages_ping_sent:1872
    cluster_stats_messages_pong_sent:1961
    cluster_stats_messages_meet_sent:21
    cluster_stats_messages_sent:3854
    cluster_stats_messages_ping_received:1961
    cluster_stats_messages_pong_received:1877
    cluster_stats_messages_received:3838
    # 查看集群node信息
    127.0.0.1:7000> cluster nodes
    e0a2bcc7148770cf1b74fcc1231239b305c7dab1 127.0.0.1:7001@17001 master - 0 1564402784041 4 connected
    52bac4f927a8ef09698f96b04315705e515a8ebc 127.0.0.1:7005@17005 master - 0 1564402783000 5 connected
    # 7000节点已经被分配了0~5461个槽
    06b0826a162390e4b2094e57594a40a181aa2206 127.0.0.1:7000@17000 myself,master - 0 1564402782000 1 connected 0-5461
    e7148fb70e97d7fc50ec4c31565f9efd66f744fc 127.0.0.1:7004@17004 master - 0 1564402782035 0 connected
    9e924621a19f48368e13923230f7b3b0803a1b4a 127.0.0.1:7003@17003 master - 0 1564402780033 3 connected
    d23b903f7342a96c7f447eb413a6a2c09195d183 127.0.0.1:7002@17002 master - 0 1564402783039 2 connected
    #同理我们登录任意一个节点查看cluster 节点信息
    127.0.0.1:7005> cluster nodes
    52bac4f927a8ef09698f96b04315705e515a8ebc 127.0.0.1:7005@17005 myself,master - 0 1564402934000 5 connected
    # 同样的,7005也发现了,7000节点已经被分配了槽
    06b0826a162390e4b2094e57594a40a181aa2206 127.0.0.1:7000@17000 master - 0 1564402934000 1 connected 0-5461
    e7148fb70e97d7fc50ec4c31565f9efd66f744fc 127.0.0.1:7004@17004 master - 0 1564402933000 0 connected
    9e924621a19f48368e13923230f7b3b0803a1b4a 127.0.0.1:7003@17003 master - 0 1564402936182 3 connected
    e0a2bcc7148770cf1b74fcc1231239b305c7dab1 127.0.0.1:7001@17001 master - 0 1564402933177 4 connected
    d23b903f7342a96c7f447eb413a6a2c09195d183 127.0.0.1:7002@17002 master - 0 1564402935180 2 connected
    

    为什么只分配了一部分槽,集群状态就显示ok呢?这主要是因为我们配置了cluster-require-full-coverage no不需要所有节点都提供服务集群才对外提供服务。
    同理,给7001节点分配槽5462~10922 ,给7002节点分配槽10923~16383

    sh addslots.sh 5642 10922 127.0.0.1 7001
    sh addslots.sh 10922 16383 127.0.0.1 7002 
    

    再次查看集群信息

    127.0.0.1:7002> cluster info
    # 集群已启动
    cluster_state:ok
    # 共分配了16384个节点
    cluster_slots_assigned:16384
    cluster_slots_ok:16384
    cluster_slots_pfail:0
    cluster_slots_fail:0
    # 已知节点6个
    cluster_known_nodes:6
    # cluster分配槽的节点3个
    cluster_size:3
    cluster_current_epoch:5
    cluster_my_epoch:2
    cluster_stats_messages_ping_sent:3100
    cluster_stats_messages_pong_sent:3036
    cluster_stats_messages_meet_sent:2
    cluster_stats_messages_sent:6138
    cluster_stats_messages_ping_received:3033
    cluster_stats_messages_pong_received:3102
    cluster_stats_messages_meet_received:3
    cluster_stats_messages_received:6138
    
  4. 主从配置

    redis-cli -h {
           host} -p 7003 cluster replicate ${node-id-7000}
    redis-cli -h {
           host} -p 7004 cluster replicate ${node-id-7001}
    redis-cli -h {
           host} -p 7005 cluster replicate ${node-id-7002}
    
    # 查看redis集群节点信息
    127.0.0.1:7000> cluster nodes
    # 三主三从
    e0a2bcc7148770cf1b74fcc1231239b305c7dab1 127.0.0.1:7001@17001 master - 0 1564483672000 4 connected 5462-10922
    52bac4f927a8ef09698f96b04315705e515a8ebc 127.0.0.1:7005@17005 slave d23b903f7342a96c7f447eb413a6a2c09195d183 0 1564483673878 5 connected
    06b0826a162390e4b2094e57594a40a181aa2206 127.0.0.1:7000@17000 myself,master - 0 1564483671000 1 connected 0-5461
    e7148fb70e97d7fc50ec4c31565f9efd66f744fc 127.0.0.1:7004@17004 slave e0a2bcc7148770cf1b74fcc1231239b305c7dab1 0 1564483674000 4 connected
    9e924621a19f48368e13923230f7b3b0803a1b4a 127.0.0.1:7003@17003 slave 06b0826a162390e4b2094e57594a40a181aa2206 0 1564483672876 3 connected
    d23b903f7342a96c7f447eb413a6a2c09195d183 127.0.0.1:7002@17002 master - 0 1564483674880 2 connected 10923-16383
    
  5. 测试集群是否可用

    [root@iz2zechwdfwvcm1rr3upjvz redis] ./redis-cli -c -p 7000
    127.0.0.1:7000> set hello world
    OK
    
    

到此原生命令安装redis-cluster就圆满完成了,但是在实际生产中我们不会用这种方式去安装redis集群,因为费时费力还极容易出错。并且redis官方也为我们提供了安装和维护工具,接下来就来介绍下redis官方工具安装集群的方式。

你可能感兴趣的:(java,java,redis,redis,Redis,Cluster)