In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
One: about the current situation of redis cluster1:redis cluster
Cluster features currently supported by redis
1): node automatic discovery
2): slave- > master election, cluster fault tolerance
3): Hot resharding: online sharding
4): group management: cluster xxx
5): cluster management based on nodes-port.conf
6): ASK steering / MOVED steering mechanism.
2:redis cluster architecture
1) redis-cluster architecture diagram
Architectural details:
(1) all redis nodes are interconnected with each other (PING-PONG mechanism), and binary protocol is used internally to optimize transmission speed and bandwidth.
(2) the fail of a node takes effect only when more than half of the nodes in the cluster detect failure.
(3) the client is directly connected to the redis node and does not need an intermediate proxy layer. The client does not need to connect to all the nodes in the cluster, just connect to any available node in the cluster
(4) redis-cluster maps all physical nodes to [0-16383] slot, and cluster is responsible for maintaining nodeslotvalue.
2) redis-cluster election: fault tolerance
The main results are as follows: (1) leading the election process is the participation of all master in the cluster. If more than half of the master nodes communicate with master nodes more than (cluster-node-timeout), the current master nodes are considered dead.
(2): when the whole cluster is unavailable (cluster_state:fail), and when the cluster is unavailable, all operations on the cluster are not available. ((error) CLUSTERDOWN The cluster is down) error received
A: if any master of the cluster is down, and the current master has no slave. When the cluster enters the fail state, it can also be understood as entering the fail state when the slot mapping [0-16383] is not completed.
B: if more than half of the master is dead, no matter whether or not any slave cluster enters the fail state.
Second: the use of redis cluster 1: install redis cluster
1): install redis-cluster dependency: the dependent library of redis-cluster has compatibility problems. You will encounter all kinds of errors in reshard. Please install it according to the specified version.
(1) make sure that zlib is installed on the system, otherwise gem install will report (no such file to load-- zlib)
# download:zlib-1.2.6.tar. / configure make make install
(2) install ruby:version (1.9.2)
# ruby1.9.2 cd / path/ruby. / configure-prefix=/usr/local/ruby make make install sudo cp ruby / usr/local/bin
(3) install rubygem:version (1.8.16)
# rubygems-1.8.16.tgz cd / path/gem sudo ruby setup.rb sudo cp bin/gem / usr/local/bin
(4) install gem-redis:version (3.0.0)
Gem install redis--version 3.0.0 # if the download may fail due to the source, download it manually and install it # download address: http://rubygems.org/gems/redis/versions/3.0.0 gem install-l / data/soft/redis-3.0.0.gem
(5) install redis-cluster
Cd / path/redis make sudo cp / opt/redis/src/redis-server / usr/local/bin sudo cp / opt/redis/src/redis-cli / usr/local/bin sudo cp / opt/redis/src/redis-trib.rb / usr/local/bin
2: configure redis cluster
1) redis configuration file structure:
Use include to separate the general configuration from the special configuration for easy maintenance.
2) redis universal configuration.
# GENERAL daemonize no tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice databases 16 dir / opt/redis/data slave-serve-stale-data yes # slave read-only slave-read-only yes # not use default repl-disable-tcp-nodelay yes slave-priority # # turn on aof persistence appendonly yes # once aof write appendfsync everysec # turn off fsync no-appendfsync-on-rewrite yes auto-aof-rewrite-min-size for new writes during aof rewrite 64mb lua-time-limit 5000 # threshold for opening redis cluster cluster-enabled yes # node interconnect timeout cluster-node-timeout 15000 cluster-migration-barrier 1 slowlog-log-slower-than 10000 slowlog-max-len 128notify-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 128zset-max-ziplist-value 64 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
3) redis special configuration.
# includes general configuration include / opt/redis/redis-common.conf # listening on tcp port port 6379 # maximum available memory maxmemory 100m # elimination strategy when memory is exhausted: # volatile-lru-> remove the key with an expire set using an LRU algorithm # allkeys-lru-> remove any key accordingly to the LRU algorithm # volatile-random-> remove a random key with an expire set # allkeys-random-> remove a random key Any key # volatile-ttl-> remove the key with the nearest expire time (minor TTL) # noeviction-> don't expire at all, just return an error on write operations maxmemory-policy allkeys-lru # aof storage file appendfilename "appendonly-6379.aof" # rdb file, only used to dynamically add slave process dbfilename dump-6379.rdb # cluster configuration file (start automatic generation) cluster-config-file nodes-6379.conf # redis instances deployed on the same machine Rub the auto-aof-rewrite apart to prevent all redis processes from doing rewrite in an instant, taking up a lot of memory auto-aof-rewrite-percentage 80-100.
3:cluster operation
Cluster cluster related commands. For more redis related commands, please see the document: http://redis.readthedocs.org/en/latest/
The cluster CLUSTER INFO prints information about the cluster CLUSTER NODES lists all the nodes (node) currently known to the cluster, as well as information about these nodes. Node CLUSTER MEET adds the nodes specified by ip and port to the cluster, making it part of the cluster. CLUSTER FORGET removes the nodes specified by node_id from the cluster. CLUSTER REPLICATE sets the current node as a slave to the node specified by node_id. CLUSTER SAVECONFIG saves the configuration file of the node to the hard disk. Slot (slot) CLUSTER ADDSLOTS [slot...] Assign one or more slots (slot) to the current node (assign). CLUSTER DELSLOTS [slot...] Removes the assignment of one or more slots to the current node. CLUSTER FLUSHSLOTS removes all slots assigned to the current node, making the current node a node with no slots assigned. CLUSTER SETSLOT NODE assigns the slot slot to the node specified by node_id, and if the slot is already assigned to another node, let the other node delete the slot > before assigning it. CLUSTER SETSLOT MIGRATING migrates the slot slot of this node to the node specified by node_id. CLUSTER SETSLOT IMPORTING imports slot slot into this node from the node specified by node_id. CLUSTER SETSLOT STABLE cancels the import (import) or migration (migrate) of slot slot. The key CLUSTER KEYSLOT calculates which slot the key key should be placed on. CLUSTER COUNTKEYSINSLOT returns the number of key-value pairs currently contained in slot slot. CLUSTER GETKEYSINSLOT returns the keys in count slot slots.
4:redis cluster operation and maintenance operation
1) initialize and build the cluster
(1) # start cluster-related nodes (must be empty nodes) and specify configuration files and output logs
Redis-server / opt/redis/conf/redis-6380.conf > / opt/redis/logs/redis-6380.log 2 > & 1 & redis-server / opt/redis/conf/redis-6381.conf > / opt/redis/logs/redis-6381.log 2 > & 1 & redis-server / opt/redis/conf/redis-6382.conf > / opt/redis/logs/redis-6382.log 2 > & 1 & redis-server / opt/redis/conf/redis-7380.conf > / opt/redis/ Logs/redis-7380.log 2 > & 1 & redis-server / opt/redis/conf/redis-7381.conf > / opt/redis/logs/redis-7381.log 2 > & 1 & redis-server / opt/redis/conf/redis-7382.conf > / opt/redis/logs/redis-7382.log 2 > & 1 &
(2): build a cluster using the built-in ruby tool (redis-trib.rb)
The create subcommand of # redis-trib.rb builds #-replicas specifies how many Slave nodes are provided for each Master node in the Redis Cluster # the role of the node is determined by the order, first master then slave (for ease of identification The port of slave is 1000 larger than that of master) redis-trib.rb create-- replicas 1 10.10.34.14purl 6380 10.10.34.14 redis-trib.rb create 6381 10.10.34.14 redis-trib.rb create 6382 10.10.34.14
(3): check the cluster status
The checksub-command of # redis-trib.rb builds # ip:port can be any node of the cluster redis-trib.rb check 1 10.10.34.14 redis-trib.rb check 6380
Finally, the following information is output, without any warning or error, indicating that the cluster started successfully and is in the ok state
[OK] All nodes agree about slots configuration. > Check for open slots... > Check slots coverage... [OK] All 16384 slots covered.
(4): use the redis-cli command to enter the cluster environment
Redis-cli-c-p 7001
2): add a new master node
(1) add a master node: create an empty node (empty node), and then move some slot to the empty node, a process that currently requires human intervention
A): generate a configuration file based on the port (ps:establish_config.sh is an output configuration script I wrote myself)
Sh establish_config.sh 6386 > conf/redis-6386.conf
B): start the node
Nohup redis-server / opt/redis/conf/redis-6386.conf > / opt/redis/logs/redis-6386.log 2 > & 1 &
C): add an empty node to the cluster
Add-node adds a node to the cluster, the first is the new node ip:port, and the second is any existing node ip:port
Redis-trib.rb add-node 10.10.34.14:6386 10.10.34.14:6381
Node: the new node does not contain any data because it does not contain any slot. The newly added point is a master node, and when the cluster needs to upgrade a slave node to a new master node, the new node will not be selected
D): assign slot to the new node
Redis-trib.rb reshard 10.10.34.14 redis-trib.rb reshard 6386 # Select the number of slot to migrate (ps: select 500 here) How many slots do you want to move (from 1 to 16384)? 500 # Select the node-id What is the receiving node ID to accept these slot? F51e26b5d5ff74f85341f06f28f125b7254e61bf # Select the slot source: # all means to reassign from all master, # or the master node id of the slot to be extracted from the data, and finally end the Please enter all the source node IDs with done. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. After Source node # 1:all # prints the moved slot, type yes to start moving the slot and the corresponding data. # Do you want to proceed with the proposed reshard plan (yes/no)? Yes # end 3): add a new slave node
A): the first three steps are the same as adding master
B) step 4: connect redis-cli to the new node shell, enter the command: cluster replicate corresponds to the node-id of master
Cluster replicate 2b9ebcbd627ff0fd7a7bbcc5332fb09e72788835
Note: when adding slave online, you need to dump the whole master process and pass it to slave, and then slave loads the rdb file into memory. Master may not be able to provide services during rdb transfer, and the whole process consumes a lot of io. Be careful.
For example, the rdb file generated by this add slave operation
-rw-r--r-- 1 root root 34946 Apr 17 18:23 dump-6386.rdb-rw-r--r-- 1 root root 34946 Apr 17 18:23 dump-7386.rdb
4): online reshard data:
For the case of uniform load / data, it can be solved by online reshard slot, which is the same as adding reshard with new master, except that the master node of reshard is the old node.
5): delete a slave node # redis-trib del-node ip:port''redis-trib.rb del-node 10.10.34.14 redis-trib.rb del-node 7386' c7ee2fca17cb79fe3c9822ced1d4f6c5e169e378'
6): delete a master node
A): before deleting a master node, you must first use reshard to remove all slot of the master, and then delete the current node (currently, only the slot of the deleted master can be migrated to one node)
# migrate the current master to the redis-trib.rb reshard 10.10.34.14How many slots do you want to move 6380 on 10.10.34.14How many slots do you want to move 6380 # Select the number of slot to be migrated (ps: select 500 here) How many slots do you want to move (from 1 to 16384)? 500 (number of slot deleted) # Select the node-id (10.10.34.14How many slots do you want to move 6380) What is the receiving node ID that will accept these slot? C4a31c852f81686f6ed8bcd6d1b13accdc947fd2 (node-id of ps:10.10.34.14:6380) 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. After Source node # 1:f51e26b5d5ff74f85341f06f28f125b7254e61bf (node-id of deleted master) Source node # 2:done # prints the moved slot, type yes to start moving the slot and the corresponding data. # Do you want to proceed with the proposed reshard plan (yes/no)? Yes
B): delete empty master node
Redis-trib.rb del-node 10.10.34.14 6386 'f51e26b5d5ff74f85341f06f28f125b7254e61bf'
Three: redis cluster client (Jedis)
1: client basic operation uses public static void main (String [] args) {JedisPoolConfig config = new JedisPoolConfig (); config.setMaxTotal (20); config.setMaxIdle (2); HostAndPort hp0 = new HostAndPort ("10.8.24.112", 7001); HostAndPort hp1 = new HostAndPort ("10.8.24.112", 7002) HostAndPort hp2 = new HostAndPort ("10.8.24.112", 7003); HostAndPort hp3 = new HostAndPort ("10.8.24.112", 7004); HostAndPort hp4 = new HostAndPort ("10.8.24.112", 7005); HostAndPort hp5 = new HostAndPort ("10.8.24.112", 7006); Set hps = new HashSet (); hps.add (hp0) Hps.add (hp1); hps.add (hp2); hps.add (hp3); hps.add (hp4); hps.add (hp5); / / timeout, maximum number of retweets, maximum number of links, minimum number of links will affect the cluster JedisCluster jedisCluster = new JedisCluster (hps, 5000, 10, config) Long start = System. CurrentTimeMillis (); for (int I = 0; I < 100; iTunes +) {jedisCluster.set ("sn" + I, "n" + I);} long end = System. CurrentTimeMillis (); System. Out.println ("Simple @ Sharding Set:" + (end-start) / 10000); for (int I = 0; I < 1000; iTunes +) {System. Out.println (jedisCluster.get ("sn" + I));} try {jedisCluster.close ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.