[Windows] Redis cluster deployment

How clusters work

Redis uses hash slots to handle the mapping relationship between data and nodes. A cluster has a total of 16384  hash slots. Each key calculates a 16-bit value through the CRC16 algorithm, and then modulo 16384 to obtain the corresponding hash slot , the cluster obtains the key mapping relationship by maintaining the relationship between the hash slot and the node. The cluster nodes communicate with each other and pass the hash slot allocation information of the current node to each other.

When the client connects to the node, it will obtain the mapping relationship between the hash slot and the node, and cache it locally. If the mapping relationship between the node hash slot and the node changes, when the client uses the key to calculate and access the node, if the node does not have the hash slot, the latest hash slot mapping relationship will be returned to the client, the client Update the local cache, and access the corresponding node operation again according to the latest mapping relationship.

Redis cluster installation

Building a Redis cluster under Windows requires 4 components:

  • Redis
  • Ruby language runtime environment
  • Redis Ruby driver redis-xxxx.gem
  • A tool for creating Redis clusters redis-trib.rb

Install Redis and run 3 instances (Redis cluster requires at least 3 or more nodes, less than 3 nodes cannot be created).

Use the redis-trib.rb tool to create a Redis cluster. Since the file is written in ruby ​​language, you need to install the Ruby development environment and drive redis-xxxx.gem.

Download and install Redis

Redis provides download files in msi and zip format, download version 3.0.504 in zip format here.

Just decompress the downloaded Redis-x64-3.0.504.zip. For convenience, it is recommended to put it in the root directory of the drive letter, and change the directory name to Redis, such as: C:\Redis or D:\Redis.

Start three different Redis instances through configuration files. Since the default port of Redis is 6379, 6380, 6381, and 6382 are used here to run three Redis instances.

Note: In order to avoid unnecessary errors, the configuration file should be saved in utf8 format as much as possible, and do not contain comments

There are two ways to save logs in the configuration file (save in file, save in System Log), please choose one of them according to your needs:

loglevel notice                       #日志的记录级别,notice是适合生产环境的
logfile "D:/Redis/Logs/redis6380_log.txt"      #指定log的保持路径,默认是创建在Redis安装目录下,如果有子目录需要手动创建,如此处的Logs目录
syslog-enabled yes       #是否使用系统日志   
syslog-ident redis6380   #在系统日志的标识名

The method of saving in a file is used here, so first create a new Logs folder under the redis directory D:/Redis.

The content of redis.6380.conf is as follows:

port 6380      
loglevel notice    
logfile "D:/Redis/Logs/redis6380_log.txt"       
appendonly yes
appendfilename "appendonly.6380.aof"   
cluster-enabled yes                                    
cluster-config-file nodes.6380.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes

The content of redis.6381.conf is as follows:

port 6381       
loglevel notice   
logfile "D:/Redis/Logs/redis6381_log.txt"       
appendonly yes
appendfilename "appendonly.6381.aof"    
cluster-enabled yes                                    
cluster-config-file nodes.6381.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes

The content of redis.6382.conf is as follows:

port 6382       
loglevel notice    
logfile "D:/Redis/Logs/redis6382_log.txt"         
appendonly yes
appendfilename "appendonly.6382.aof"    
cluster-enabled yes                                    
cluster-config-file nodes.6382.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes

The configuration content is explained as follows:

#端口号
port 6380
#日志的记录级别,notice是适合生产环境的
loglevel notice 
#指定log的保持路径,默认是创建在Redis安装目录下,如果有子目录需要手动创建,如此处的Logs目录
logfile "Logs/redis6380_log.txt"
#是否使用系统日志    
syslog-enabled yes  
#在系统日志的标识名                       
syslog-ident redis6380
#数据的保存为aof格式                   
appendonly yes
#数据保存文件                            
appendfilename "appendonly.6380.aof"
#是否开启集群    
cluster-enabled yes
#记录集群信息,不用手动维护,Redis Cluster 会自动维护                                    
cluster-config-file nodes.6380.conf
集群节点连接超时时间,如果集群规模小,都在同一个网络环境下,可以配置的短些,更快的做故障转移。 
cluster-node-timeout 15000
#设置为0,如果master slave都挂掉, slave跟master失联又超过这个数值*timeout的数值, 
#就不会发起选举了。如果设置为0,就是永远都会尝试发起选举,尝试从slave变为mater
cluster-slave-validity-factor 10
#只有当旧主服务器仍有至少给定数量的其他工作副本时,副本才会迁移到孤立主服务器。这个数字就是“移民壁垒”。
#迁移屏障为1意味着只有当主副本至少有一个其他工作副本时,副本才会迁移,以此类推。它通常反映集群中每个主节点所需的副本数量。
#默认值为1(副本仅在其主副本至少保留一个副本时迁移)。要禁用迁移,只需将其设置为一个非常大的值。
#可以设置值0,但该值仅用于调试和生产中的危险。
cluster-migration-barrier 1
#设置为no,默认为yes,故障发现到自动完成转移期间整个集群是不可用状态,
#对于大多数业务无法容忍这种情况,因此要设置为no,当主节点故障时只影 响它负责槽的相关命令执行,不会影响其他主节点的可用性。
#只要有结点宕机导致16384个槽没全被覆盖,整个集群就全部停止服务,所以一定要改为no
cluster-require-full-coverage yes

Save the above configuration files to the redis directory, and use these configuration files to install 3 redis services, the command is as follows:

Note: It is best to use the full path for configuration files such as redis.6380.conf to avoid problems restarting the Redis cluster

D:/Redis/redis-server.exe --service-install D:/Redis/redis.6380.conf --service-name redis6380
D:/Redis/redis-server.exe --service-install D:/Redis/redis.6381.conf --service-name redis6381
D:/Redis/redis-server.exe --service-install D:/Redis/redis.6382.conf --service-name redis6382

To start these three services, the command is as follows:

D:/Redis/redis-server.exe --service-start --service-name redis6380
D:/Redis/redis-server.exe --service-start --service-name redis6381
D:/Redis/redis-server.exe --service-start --service-name redis6382

download and install ruby

1. Download Ruby

Address: Download Archives

After downloading, double-click to install. Similarly, for the convenience of operation, it is also recommended to install it in the root directory of the drive letter, such as: D:\ruby22-x64 , select the last two options here during installation, which means adding ruby ​​to the system In the environment variable, ruby ​​commands can be used directly in the cmd command.

2. Download the Redis driver

Download the Redis driver in the ruby ​​environment. Considering compatibility, the version 3.2.2 is downloaded here.

redis | RubyGems.org | your community gem host

Note: The download is in the relevant link item in the lower right corner of the page

To install the driver, the command is as follows:

gem install --local path_to_gem/filename.gem  

3. Download redis-trib.rb

Download the official Ruby script file redis-trib.rb provided by Redis to create a Redis cluster. The path is as follows:

https://raw.githubusercontent.com/MSOpenTech/redis/3.0/src/redis-trib.rb

Open the link If you do not download, but open a page, then save the page as redis-trib.rb

It is recommended to save it in the redis directory.

Note: Because redis-trib.rb is a ruby ​​code, it must be opened with ruby. If redis-trib.rb cannot be recognized, you need to manually select the opening method of the file:

After selecting ruby ​​as the opening method, the logo of redis-trib.rb will change, as shown in the figure below:

Create a Redis cluster

Switch to the Redis directory under cmd, and use redis-trib.rb to create a Redis cluster:

redis-trib.rb create --replicas 0 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 

Results of the:

When prompted, you need to manually enter yes. After the input, when the following content appears, it means that the Redis cluster has been created:

To verify that it was created successfully, enter the following command:

redis-trib.rb check 127.0.0.1:6380

The following information appears, indicating that the created Redis cluster is OK:

Use the Redis client Redis-cli.exe to view the number of data records and cluster-related information:

D:/Redis/redis-cli.exe -c -p 6380

-c means cluster

-p represents the port port number

Enter dbsize to query the total number of records

dbsize

Or enter the full command at once:

D:/Redis/redis-cli.exe -c -p 6380 dbsize

The result is as follows:

Enter cluster info to view the cluster information from the client:

cluster info

The result is as follows:

If the following problems occur when building a redis cluster:

solution:

  • Delete the nodes-xxx.conf configuration file corresponding to each node
  • Delete the aof and rdb files under each node
  • Turn off the process of each instance of the redis cluster and restart

Redis service command

Suppose there is a redis.windows-service.conf file in the Redis installation directory, which is the default configuration file.

If you need to modify the port number, or set a password, you need to modify the content:

The default port number is 6379, you can modify it at will

port 6379

By default, no password is set. If necessary, modify requirepass, remove the # in front, and change foobared to your password

# requirepass foobared

The Redis official website provides two installation methods: msi file format and zip package file. After installation, redis in msi format will be automatically added to the system service list, while redis in zip package needs to be manually installed.

Special attention: When using the default configuration file to install the Redis service, you must create a new Log directory under the Redis installation directory, otherwise an error will be reported, because the log save directory is set in the redis.windows-service.conf as the installation directory Log, of course this can be modified.

Redis commonly used service commands are as follows:

1) Installation service

--service-install

Full example:

redis-server.exe --service-install redis.windows-service.conf --loglevel notice

Here, redis.windows-service.conf is used as the configuration file, --loglevel notice is to set the log record, which is basically a notice.

2) Uninstall the service

--service-uninstall

Full example:

redis-server --service-uninstall

3) Start the service

--service-start

4) stop service

--service-stop

5) Other commands

--service-name YourServiceName #设置服务的名字
--port YourPortNumber #设置服务的端口号

Use as follows, switch to the Redis installation directory through cmd, and then enter the command, such as:

The first command is: use redis.windows-servie.conf as the configuration file, the name is redisTest, and the port number is 6388 to install a Redis service;

The second command is: (in the red box) is to uninstall the service named redisTest

Guess you like

Origin blog.csdn.net/vcit102/article/details/131589292