上篇文章介绍了《centos7下安装redis》,此次试验redis主从配置。默认已经配置好了8个redis实例。
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
[root@Cache src]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:7001 0.0.0.0:* LISTEN 1457/../bin/redis-s tcp 0 0 127.0.0.1:7002 0.0.0.0:* LISTEN 1462/../bin/redis-s tcp 0 0 127.0.0.1:7003 0.0.0.0:* LISTEN 1466/../bin/redis-s tcp 0 0 127.0.0.1:7004 0.0.0.0:* LISTEN 1470/../bin/redis-s tcp 0 0 127.0.0.1:7005 0.0.0.0:* LISTEN 1474/../bin/redis-s tcp 0 0 127.0.0.1:7006 0.0.0.0:* LISTEN 1478/../bin/redis-s tcp 0 0 127.0.0.1:7007 0.0.0.0:* LISTEN 1482/../bin/redis-s tcp 0 0 127.0.0.1:7008 0.0.0.0:* LISTEN 1489/../bin/redis-s [root@Cache src]# pwd /byrd/tools/redis-3.2.0/src [root@Cache src]# cp redis-trib.rb /usr/local/bin/redis-trib.rb [root@Cache src]# redis-trib.rb -h Unknown redis-trib subcommand '-h' [root@Cache src]# redis-trib.rb --help Unknown redis-trib subcommand '--help' [root@Cache src]# redis-trib.rb help Usage: redis-trib <command> <options> <arguments ...> [root@Cache src]# redis-trib.rb create --replicas 1.1.1.61:7001 1.1.1.61:7002 1.1.1.61:7003 1.1.1.61:7004 1.1.1.61:7005 1.1.1.61:7006 >>> Creating cluster [ERR] Sorry, can't connect to node 1.1.1.61:7002 [root@Cache conf]# kill `ps aux | grep redis | awk '{print $2}'` [root@Cache conf]# redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 Adding replica 127.0.0.1:7004 to 127.0.0.1:7001 Adding replica 127.0.0.1:7005 to 127.0.0.1:7002 Adding replica 127.0.0.1:7006 to 127.0.0.1:7003 M: 7faab7a1e9bb81f957aaf474611afb85eb7dee45 127.0.0.1:7001 slots:0-5460 (5461 slots) master M: 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 127.0.0.1:7002 slots:5461-10922 (5462 slots) master M: f7f863121b256a83a20f3d1dd8677b6a8391d4b1 127.0.0.1:7003 slots:10923-16383 (5461 slots) master S: 5787ab30edf9363d1b0bb2282fbc4843f77ea3c3 127.0.0.1:7004 replicates 7faab7a1e9bb81f957aaf474611afb85eb7dee45 S: addc600ef74067ec6703a1811edd1aab5fc303c5 127.0.0.1:7005 replicates 2f41fea0613bf01bcd4ed22b6cb33d283febb52e S: 32fc3d61390ca1d6de7652471755550311bcb922 127.0.0.1:7006 replicates f7f863121b256a83a20f3d1dd8677b6a8391d4b1 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join... >>> Performing Cluster Check (using node 127.0.0.1:7001) M: 7faab7a1e9bb81f957aaf474611afb85eb7dee45 127.0.0.1:7001 slots:0-5460 (5461 slots) master M: 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 127.0.0.1:7002 slots:5461-10922 (5462 slots) master M: f7f863121b256a83a20f3d1dd8677b6a8391d4b1 127.0.0.1:7003 slots:10923-16383 (5461 slots) master M: 5787ab30edf9363d1b0bb2282fbc4843f77ea3c3 127.0.0.1:7004 slots: (0 slots) master replicates 7faab7a1e9bb81f957aaf474611afb85eb7dee45 M: addc600ef74067ec6703a1811edd1aab5fc303c5 127.0.0.1:7005 slots: (0 slots) master replicates 2f41fea0613bf01bcd4ed22b6cb33d283febb52e M: 32fc3d61390ca1d6de7652471755550311bcb922 127.0.0.1:7006 slots: (0 slots) master replicates f7f863121b256a83a20f3d1dd8677b6a8391d4b1 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [root@Cache conf]# |
效果检验:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@Cache conf]# ../bin/redis-cli -c -p 7001 127.0.0.1:7001> set hello world OK 127.0.0.1:7001> get hello "world" 127.0.0.1:7001> quit [root@Cache conf]# ../bin/redis-cli -c -p 7003 127.0.0.1:7003> get hello -> Redirected to slot [866] located at 127.0.0.1:7001 "world" 127.0.0.1:7001> quit [root@Cache conf]# ../bin/redis-cli -c -p 7006 127.0.0.1:7006> get hello -> Redirected to slot [866] located at 127.0.0.1:7001 "world" |
增加node:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
[root@Cache conf]# redis-trib.rb add-node 127.0.0.1:7001 127.0.0.1:7007 >>> Adding node 127.0.0.1:7001 to cluster 127.0.0.1:7007 >>> Performing Cluster Check (using node 127.0.0.1:7007) M: 6d4649ce09245a997710e33da73c6f4c80323103 127.0.0.1:7007 slots: (0 slots) master 0 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [ERR] Not all 16384 slots are covered by nodes. [ERR] Node 127.0.0.1:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0. [root@Cache conf]# redis-trib.rb add-node 127.0.0.1:7007 127.0.0.1:7001 >>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7001 >>> Performing Cluster Check (using node 127.0.0.1:7001) M: 7faab7a1e9bb81f957aaf474611afb85eb7dee45 127.0.0.1:7001 slots:0-5460 (5461 slots) master 1 additional replica(s) M: f7f863121b256a83a20f3d1dd8677b6a8391d4b1 127.0.0.1:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: addc600ef74067ec6703a1811edd1aab5fc303c5 127.0.0.1:7005 slots: (0 slots) slave replicates 2f41fea0613bf01bcd4ed22b6cb33d283febb52e S: 5787ab30edf9363d1b0bb2282fbc4843f77ea3c3 127.0.0.1:7004 slots: (0 slots) slave replicates 7faab7a1e9bb81f957aaf474611afb85eb7dee45 S: 32fc3d61390ca1d6de7652471755550311bcb922 127.0.0.1:7006 slots: (0 slots) slave replicates f7f863121b256a83a20f3d1dd8677b6a8391d4b1 M: 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 127.0.0.1:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 127.0.0.1:7007 to make it join the cluster. [OK] New node added correctly. 127.0.0.1:7003> cluster nodes addc600ef74067ec6703a1811edd1aab5fc303c5 127.0.0.1:7005 slave 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 0 1463323912759 5 connected 7faab7a1e9bb81f957aaf474611afb85eb7dee45 127.0.0.1:7001 master - 0 1463323914268 1 connected 0-5460 32fc3d61390ca1d6de7652471755550311bcb922 127.0.0.1:7006 slave f7f863121b256a83a20f3d1dd8677b6a8391d4b1 0 1463323914771 6 connected f7f863121b256a83a20f3d1dd8677b6a8391d4b1 127.0.0.1:7003 myself,master - 0 0 3 connected 10923-16383 6d4649ce09245a997710e33da73c6f4c80323103 127.0.0.1:7007 master - 0 1463323913262 0 connected 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 127.0.0.1:7002 master - 0 1463323912759 2 connected 5461-10922 5787ab30edf9363d1b0bb2282fbc4843f77ea3c3 127.0.0.1:7004 slave 7faab7a1e9bb81f957aaf474611afb85eb7dee45 0 1463323913765 4 connected [root@Cache conf]# redis-trib.rb reshard 127.0.0.1:7007 >>> Performing Cluster Check (using node 127.0.0.1:7007) M: 6d4649ce09245a997710e33da73c6f4c80323103 127.0.0.1:7007 slots: (0 slots) master 0 additional replica(s) M: f7f863121b256a83a20f3d1dd8677b6a8391d4b1 127.0.0.1:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: addc600ef74067ec6703a1811edd1aab5fc303c5 127.0.0.1:7005 slots: (0 slots) slave replicates 2f41fea0613bf01bcd4ed22b6cb33d283febb52e S: 5787ab30edf9363d1b0bb2282fbc4843f77ea3c3 127.0.0.1:7004 slots: (0 slots) slave replicates 7faab7a1e9bb81f957aaf474611afb85eb7dee45 M: 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 127.0.0.1:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 7faab7a1e9bb81f957aaf474611afb85eb7dee45 127.0.0.1:7001 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 32fc3d61390ca1d6de7652471755550311bcb922 127.0.0.1:7006 slots: (0 slots) slave replicates f7f863121b256a83a20f3d1dd8677b6a8391d4b1 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)? 500 What is the receiving node ID? 6d4649ce09245a997710e33da73c6f4c80323103 Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1:all Ready to move 500 slots. Source nodes: M: f7f863121b256a83a20f3d1dd8677b6a8391d4b1 127.0.0.1:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 127.0.0.1:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 7faab7a1e9bb81f957aaf474611afb85eb7dee45 127.0.0.1:7001 slots:0-5460 (5461 slots) master 1 additional replica(s) Destination node: M: 6d4649ce09245a997710e33da73c6f4c80323103 127.0.0.1:7007 slots: (0 slots) master 0 additional replica(s) Resharding plan: Moving slot 5461 from 2f41fea0613bf01bcd4ed22b6cb33d283febb52e Moving slot 5627 from 2f41fea0613bf01bcd4ed22b6cb33d283febb52e Do you want to proceed with the proposed reshard plan (yes/no)? yes 127.0.0.1:7003> cluster nodes addc600ef74067ec6703a1811edd1aab5fc303c5 127.0.0.1:7005 slave 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 0 1463324136055 5 connected 7faab7a1e9bb81f957aaf474611afb85eb7dee45 127.0.0.1:7001 master - 0 1463324137061 1 connected 166-5460 32fc3d61390ca1d6de7652471755550311bcb922 127.0.0.1:7006 slave f7f863121b256a83a20f3d1dd8677b6a8391d4b1 0 1463324137061 6 connected f7f863121b256a83a20f3d1dd8677b6a8391d4b1 127.0.0.1:7003 myself,master - 0 0 3 connected 11089-16383 6d4649ce09245a997710e33da73c6f4c80323103 127.0.0.1:7007 master - 0 1463324137061 7 connected 0-165 5461-5627 10923-11088 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 127.0.0.1:7002 master - 0 1463324136558 2 connected 5628-10922 5787ab30edf9363d1b0bb2282fbc4843f77ea3c3 127.0.0.1:7004 slave 7faab7a1e9bb81f957aaf474611afb85eb7dee45 0 1463324136055 4 connected [root@Cache conf]# redis-trib.rb add-node 127.0.0.1:7008 127.0.0.1:7007 >>> Adding node 127.0.0.1:7008 to cluster 127.0.0.1:7007 >>> Performing Cluster Check (using node 127.0.0.1:7007) M: 6d4649ce09245a997710e33da73c6f4c80323103 127.0.0.1:7007 slots:0-165,5461-5627,10923-11088 (499 slots) master 0 additional replica(s) M: f7f863121b256a83a20f3d1dd8677b6a8391d4b1 127.0.0.1:7003 slots:11089-16383 (5295 slots) master 1 additional replica(s) S: addc600ef74067ec6703a1811edd1aab5fc303c5 127.0.0.1:7005 slots: (0 slots) slave replicates 2f41fea0613bf01bcd4ed22b6cb33d283febb52e S: 5787ab30edf9363d1b0bb2282fbc4843f77ea3c3 127.0.0.1:7004 slots: (0 slots) slave replicates 7faab7a1e9bb81f957aaf474611afb85eb7dee45 M: 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 127.0.0.1:7002 slots:5628-10922 (5295 slots) master 1 additional replica(s) M: 7faab7a1e9bb81f957aaf474611afb85eb7dee45 127.0.0.1:7001 slots:166-5460 (5295 slots) master 1 additional replica(s) S: 32fc3d61390ca1d6de7652471755550311bcb922 127.0.0.1:7006 slots: (0 slots) slave replicates f7f863121b256a83a20f3d1dd8677b6a8391d4b1 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 127.0.0.1:7008 to make it join the cluster. [OK] New node added correctly. 127.0.0.1:7003> cluster nodes addc600ef74067ec6703a1811edd1aab5fc303c5 127.0.0.1:7005 slave 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 0 1463324363445 5 connected 7faab7a1e9bb81f957aaf474611afb85eb7dee45 127.0.0.1:7001 master - 0 1463324361937 1 connected 166-5460 32fc3d61390ca1d6de7652471755550311bcb922 127.0.0.1:7006 slave f7f863121b256a83a20f3d1dd8677b6a8391d4b1 0 1463324363947 6 connected f7f863121b256a83a20f3d1dd8677b6a8391d4b1 127.0.0.1:7003 myself,master - 0 0 3 connected 11089-16383 61d02b4e030e8434093884896e2a7162cd022833 127.0.0.1:7008 master - 0 1463324363947 0 connected 6d4649ce09245a997710e33da73c6f4c80323103 127.0.0.1:7007 master - 0 1463324361937 7 connected 0-165 5461-5627 10923-11088 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 127.0.0.1:7002 master - 0 1463324363445 2 connected 5628-10922 5787ab30edf9363d1b0bb2282fbc4843f77ea3c3 127.0.0.1:7004 slave 7faab7a1e9bb81f957aaf474611afb85eb7dee45 0 1463324362439 4 connected [root@Cache conf]# ../bin/redis-cli -c -p 7008 127.0.0.1:7008> CLUSTER REPLICATE 6d4649ce09245a997710e33da73c6f4c80323103 OK 127.0.0.1:7008> CLUSTER NODES addc600ef74067ec6703a1811edd1aab5fc303c5 127.0.0.1:7005 slave 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 0 1463324450544 2 connected f7f863121b256a83a20f3d1dd8677b6a8391d4b1 127.0.0.1:7003 master - 0 1463324451046 3 connected 11089-16383 61d02b4e030e8434093884896e2a7162cd022833 127.0.0.1:7008 myself,slave 6d4649ce09245a997710e33da73c6f4c80323103 0 0 0 connected 5787ab30edf9363d1b0bb2282fbc4843f77ea3c3 127.0.0.1:7004 slave 7faab7a1e9bb81f957aaf474611afb85eb7dee45 0 1463324451550 1 connected 32fc3d61390ca1d6de7652471755550311bcb922 127.0.0.1:7006 slave f7f863121b256a83a20f3d1dd8677b6a8391d4b1 0 1463324450544 3 connected 6d4649ce09245a997710e33da73c6f4c80323103 127.0.0.1:7007 master - 0 1463324449538 7 connected 0-165 5461-5627 10923-11088 2f41fea0613bf01bcd4ed22b6cb33d283febb52e 127.0.0.1:7002 master - 0 1463324450040 2 connected 5628-10922 7faab7a1e9bb81f957aaf474611afb85eb7dee45 127.0.0.1:7001 master - 0 1463324451550 1 connected 166-5460 |
配置文件:(默认基本未修改)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
[root@Cache conf]# grep -v "^#\|^$" 7001/redis.conf bind 127.0.0.1 protected-mode yes port 7001 cluster-enabled yes cluster-config-file nodes_7001.conf cluster-node-timeout 5000 tcp-backlog 511 timeout 0 tcp-keepalive 0 daemonize yes supervised no pidfile /var/run/redis_7001.pid loglevel notice logfile "/var/log/redis_7001.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump_7001.rdb dir /usr/local/redis/cache/ 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_7001.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes 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 |
集群密码:
空密码集群配置密码:
0123 ./redis-cli -c -p 7000config set masterauth passwd123config set requirepass passwd123config rewrite
未加入集群时候配置密码:
1:redis.conf加入密码
01 masterauth passwd123requirepass passwd123
2:修改client.rb设置密码
01 :timeout => 5.0,:password => nil,
测试不成功!!! SourceByrd's Weblog-https://note.t4x.org/environment/redis-cluster-tutorial-config-test/
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# redis-trib.rb create --replicas 1.1.1.61:7001 1.1.1.61:7002 1.1.1.61:7003 1.1.1.61:7004 1.1.1.61:7005 1.1.1.61:7006 /usr/bin/env: ruby: No such file or directory yum install openssl openssl-devel wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.4.tar.gz ./configure -prefix=/opt/ruby-2.6.4 wget https://rubygems.org/rubygems/rubygems-3.0.6.tgz Traceback (most recent call last): 2: from /usr/local/bin/redis-trib.rb:25:in `<main>' 1: from /opt/ruby-2.6.4/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require' /opt/ruby-2.6.4/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- redis (LoadError) gem install redis |
参考文档:
1:http://redisdoc.com/topic/replication.html
2:http://redisdoc.com/topic/cluster-tutorial.htmlSourceByrd's Weblog-https://note.t4x.org/environment/redis-cluster-tutorial-config-test/ SourceByrd's Weblog-https://note.t4x.org/environment/redis-cluster-tutorial-config-test/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!