Redis multi-server master and slave configuration method detailed

Source: Internet
Author: User
Tags hash redis server memory redis server

As a result of business needs, 6 servers are configured today with Redis, 4 as Master and 2 as slave. One main one from, master-slave structure.
According to the division of business, the number of Redis services mounted by different machines is also different, distributed as follows.

The code is as follows Copy Code

#master
10.0.8.4
10.0.8.5
10.0.8.6
10.0.8.7

#master Service Port
A service 2001
B Service 2002
C Service 2003

#redis服务分布
10.0.8.4:2001
10.0.8.4:2002
10.0.8.5:2002
10.0.8.6:2002
10.0.8.7:2002
10.0.8.4:2003
10.0.8.5:2003
10.0.8.6:2003
10.0.8.7:2003

#slave
10.0.8.2
10.0.8.3

#slave分布
10.0.8.2:3001
10.0.8.2:3002
10.0.8.2:3003
10.0.8.2:3004
10.0.8.2:3005
10.0.8.3:3006
10.0.8.3:3007
10.0.8.3:3008
10.0.8.3:3009

#salve:p ort-> master:port (service) control
10.0.8.2:3001-> 10.0.8.4:2001 (a service)
10.0.8.2:3002-> 10.0.8.4:2002 (b service)
10.0.8.2:3003-> 10.0.8.5:2002 (b service)
10.0.8.2:3004-> 10.0.8.6:2002 (b service)
10.0.8.2:3005-> 10.0.8.7:2002 (b service)
10.0.8.3:3006-> 10.0.8.4:2003 (c service)
10.0.8.3:3007-> 10.0.8.5:2003 (c service)
10.0.8.3:3008-> 10.0.8.6:2003 (c service)
10.0.8.3:3009-> 10.0.8.7:2003 (c service)

General Deployment & Startup
1. Download the source package from Redis official website. I'm using redis-2.6.3.tar.gz.
2. Extract the tar zxvf redis-2.6.3.tar.gz
3. Compile Make & make install
4. Start Redis-server redis.conf (redis.conf can be found in the download source package)

Default Redis boot mode is not daemon, boot to master

Master Modify Configuration

The code is as follows Copy Code

##### #Master config
# # #General Configure the
Daemonize yes     #使用daemon Run the program, default to run not daemon
pidfile/tmp/redis.pid  #pid文件位置
Port 2001   #根据服务规定的端口填写 (default 6379)
Timeout 30   # c Lient End Idle disconnect time
LogLevel warning  #日志记录级别, the default is notice, I use warning here, is to monitor the log convenience. After using warning, only the alarm will generate the log, which is very convenient to monitor the alarm by determining whether the log file is empty.
logfile/opt/logs/redis/redis.log   #日志产生的位置
Databases 16   #默认是0, which is only 1 db, I set it to 16, Convenient for multiple applications using the same Redis server. Use the Select N command to confirm the use of Redis db, so that different applications will not have a problem if they use the same key.

# # #下面是SNAPSHOTTING持久化方式的策略. In order to ensure data relative security, in the following settings, the more frequent changes, snapshotting more frequent, that is, the greater the pressure, instead of spending on the persistence of resources will be more. So I chose the master-slave mode and turned off the snapshotting in master.
#save 900 1 #在900秒之内, Redis at least 1 changes are taken Redis snaps to disk
#save #在300秒之内, Redis at least 100 changes Redis take snapshots to disk
#save 10000 #在60秒之内, Redis at least 10,000 changes Redis snap to disk
Rdbcompression Yes #使用压缩
Dbfilename Dump.rdb #SNAPSHOTTING的文件名
dir/opt/data/redis/#SNAPSHOTTING文件的路径

# # #REPLICATION Settings,
#slaveof #如果这台机器是台redis Slave, you can open this setting. If I use Master-slave mode, I will turn the snapshotting off on master so that I can do it on the slave instead of on master, which can greatly improve master memory usage and system performance.
#slave-serve-stale-data Yes #如果slave cannot sync with master, can also read

### Security Settings
#requirepass aaaaaaaaa #redis性能太好, it doesn't make much sense to use a passwd.
#rename-command Flushall "" #可以用这种方式关掉非常危险的命令, such as the Flushall command, which empties the entire Redis server's data and does not have to be confirmed and never fail

# # #LIMIT Settings
MaxClients 0 #无client连接数量限制
MaxMemory 14GB #redis最大可使用的内存量, my server memory is 16G, if you use Redis snapshotting copy-on-write lasting write way, will use extra memory, in order to make the persistence operation will not use the system VM, Redis server performance degradation, it is recommended to retain Redis maximum use of half 8G of memory to be left to the persistence of use, I personally feel very wasteful. I don't do persistence on master, use master-slave method
Maxmemory-policy Volatile-lru #使用LRU算法删除设置了过期时间的key, but if the program does not write the time to write key expiration time, it is recommended to use ALLKEYS-LRU, so that at least ensure that Redis will not be writable.

# # #APPEND only MODE setting
AppendOnly no #不使用AOF, AoF is another way to persist, and I am not using it because this way does not guarantee data availability in the event of server or disk corruption.
Appendfsync everysec
No-appendfsync-on-rewrite No
Auto-aof-rewrite-percentage 100
Auto-aof-rewrite-min-size 64MB

# # #SLOW LOG settings
Slowlog-log-slower-than 10000 #如果操作时间大于0.001 seconds, log slow log, this log is recorded in memory, you can use REDIS-CLI slowlog get command to view
Maximum length of Slowlog-max-len 1024 #slow log

# # #VIRTUAL MEMORY settings
Vm-enabled no #不使用虚拟内存, in the Redis 2.4 version, the author has been very not recommended to use VMS.
Vm-swap-file/tmp/redis.swap
Vm-max-memory 0
Vm-page-size 32
Vm-pages 134217728
Vm-max-threads 4

# # #ADVANCED CONFIG settings, the following settings are mainly used to save memory, I did not modify them
Hash-max-zipmap-entries 512
Hash-max-zipmap-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
activerehashing Yes

# # #INCLUDES settings, use the following configuration, you can configure some other settings, such as slave configuration
#include/path/to/local.conf
#include/path/to/other.conf
#include/opt/redis/etc/slave.conf if it's slave server, open this note

After a Redis master service is finished, deploy the same steps to the rest of master, and according to the service different copy multiple redis.conf, modify the corresponding port can be
To facilitate the management of configuration filenames called

The code is as follows Copy Code

Master-redis-2001.conf
Master-redis-2002.conf
Master-redis-2003.conf

Then according to the distribution of services, the relevant environment will be set up, in the following servers to run their respective business can be

The code is as follows Copy Code

#10.0.8.4
Redis-server/redis/config/master-redis-2001.conf
Redis-server/redis/config/master-redis-2002.conf
Redis-server/redis/config/master-redis-2003.conf

#10.0.8.5, 10.0.8.6, 10.0.8.7
Redis-server/redis/config/master-redis-2002.conf
Redis-server/redis/config/master-redis-2003.conf

Test master server

The code is as follows Copy Code

Redis-cli-p 2001
Redis 10.0.8.4:2001> Set Testredis 4-2001
Ok
Redis 10.0.8.4:2001> Get Testredis
"4-2001"
Redis 10.0.8.4:2001> del Testredis
(integer) 1
Redis 10.0.8.4:2001> Get Testredis
(nil)
Redis 10.0.8.4:6489> exit

Like deploying master, deploy the slave, copy one copy of the master server's configuration file to the server, modify port, and the following settings

The code is as follows Copy Code

##### #slave Config
# # #REPLICATION
Slaveof 10.0.8.2 3001
Slave-serve-stale-data no #如果slave无法与master同步, slave not readable
# # #APPEND only MODE setting
AppendOnly Yes #在slave上使用了AOF to ensure data availability.

Slave configuration file naming

The code is as follows Copy Code

Slave-redis-3001.conf
Slave-redis-3002.conf
Slave-redis-3003.conf
Slave-redis-3004.conf
Slave-redis-3005.conf
Slave-redis-3006.conf
Slave-redis-3007.conf
Slave-redis-3008.conf
Slave-redis-3009.conf
[/bash]

Start slave.

The code is as follows Copy Code

1
#10.0.8.2
Redis-server/redis/config/master-redis-3001.conf
Redis-server/redis/config/master-redis-3002.conf
Redis-server/redis/config/master-redis-3003.conf
Redis-server/redis/config/master-redis-3004.conf
Redis-server/redis/config/master-redis-3005.conf

#10.0.8.3
Redis-server/redis/config/master-redis-3006.conf
Redis-server/redis/config/master-redis-3007.conf
Redis-server/redis/config/master-redis-3008.conf
Redis-server/redis/config/master-redis-3009.conf

After starting the slave, the slave corresponding to the master set a value, slave can get OK

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.