1. Turn off the firewall
systemctl stop firewalld && systemctl disable firewalld
2. Switch to Alibaba source
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo yum clean all && yum makecache
3. Install gcc
yum install gcc gcc-c++
Confirm whether the installation is successful
Command: gcc -v
[root@redis1 ~]# gcc -v Built in use specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper Objectives: x86_64-redhat-linux Configured as:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux Thread model: posix gcc Version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
4. Install redis
4.1 download redis
mkdir -p software && cd software && wget https://download.redis.io/releases/redis-6.2.6.tar.gz
4.2 extract to the current directory
tar -zxvf redis-6.2.6.tar.gz
4.3 compilation
cd redis-6.2.6 && make
4.4 modify the redis configuration file conf
The configuration file of the main machine is modified as follows | Profile interpretation |
---|---|
#bind 127.0.0.1 | Just comment out this line to realize the communication between different machines |
daemonize no to yes | It means running in the background |
logfile "/usr/local/src/redis-6.2.6/log/server.log" | This line is the log file for configuring redis |
masterauth "123456" | The configuration password is 123456 |
requirepass "123456" | The configuration password is 123456 |
Modify the configuration file from the machine as follows | Profile interpretation |
---|---|
#bind 127.0.0.1 | Just comment out this line to realize the communication between different machines |
daemonize no to yes | It means running in the background |
logfile "/usr/local/src/redis-6.2.6/log/server.log" | This line is the log file for configuring redis |
masterauth "123456" | The configuration password is 123456 |
requirepass "123456" | The configuration password is 123456 |
replicaof 10.10.220.30 6379 | Dependent host IP and port number |
Note: 1 The configuration file of the slave machine redis has one more slaveof configuration than that of the master machine
2. The passwords of the master machine and the slave machine should be set to the same, otherwise the Sentinels set later will have problems
4.5 whether the communication of each node can be started depends on whether redis can be started
redis. The configuration file of conf has been changed and can be copied to / usr/local/bin / directory. Redis server and redis cli files should also be copied to / usr/local/bin directory.
Then start the redis server of the three nodes in the form of configuration file
Command: redis server redis conf
The machine of 10.10.221.30 (Master) is started directly by loading the configuration file under usr/local/bin /
redis-server redis.conf [root@redis1 bin]# tail -100f /usr/local/src/redis-6.2.6/log/server.log 105952:C 04 Mar 2022 14:52:00.427 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 105952:C 04 Mar 2022 14:52:00.427 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=105952, just started 105952:C 04 Mar 2022 14:52:00.427 # Configuration loaded 105952:M 04 Mar 2022 14:52:00.430 * Increased maximum number of open files to 10032 (it was originally set to 1024). 105952:M 04 Mar 2022 14:52:00.430 * monotonic clock: POSIX clock_gettime 105952:M 04 Mar 2022 14:52:00.432 * Running mode=standalone, port=6379. 105952:M 04 Mar 2022 14:52:00.432 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 105952:M 04 Mar 2022 14:52:00.432 # Server initialized 105952:M 04 Mar 2022 14:52:00.432 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 105952:M 04 Mar 2022 14:52:00.433 * Ready to accept connections
In the same way, start the machine of 10.10.221.31 (slave) and the machine of 10.10.221.32 (slave)
[root@redis2 bin]# redis-server redis.conf [root@redis2 bin]# tail -100f /usr/local/src/redis-6.2.6/log/server.log 105743:C 04 Mar 2022 15:03:40.464 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 105743:C 04 Mar 2022 15:03:40.464 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=105743, just started 105743:C 04 Mar 2022 15:03:40.464 # Configuration loaded 105743:S 04 Mar 2022 15:03:40.465 * Increased maximum number of open files to 10032 (it was originally set to 1024). 105743:S 04 Mar 2022 15:03:40.465 * monotonic clock: POSIX clock_gettime 105743:S 04 Mar 2022 15:03:40.468 * Running mode=standalone, port=6379. 105743:S 04 Mar 2022 15:03:40.468 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 105743:S 04 Mar 2022 15:03:40.468 # Server initialized 105743:S 04 Mar 2022 15:03:40.469 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 105743:S 04 Mar 2022 15:03:40.469 * Ready to accept connections 105743:S 04 Mar 2022 15:03:40.469 * Connecting to MASTER 10.10.220.30:6379 105743:S 04 Mar 2022 15:03:40.469 * MASTER <-> REPLICA sync started 105743:S 04 Mar 2022 15:03:40.470 * Non blocking connect for SYNC fired the event. 105743:S 04 Mar 2022 15:03:40.471 * Master replied to PING, replication can continue... 105743:S 04 Mar 2022 15:03:40.471 * Partial resynchronization not possible (no cached master) 105743:S 04 Mar 2022 15:03:40.475 * Full resync from master: 6e2ad9355ad389d41224a87527c5a82e693aaed1:0 105743:S 04 Mar 2022 15:03:40.494 * MASTER <-> REPLICA sync: receiving 175 bytes from master to disk 105743:S 04 Mar 2022 15:03:40.494 * MASTER <-> REPLICA sync: Flushing old data 105743:S 04 Mar 2022 15:03:40.494 * MASTER <-> REPLICA sync: Loading DB in memory 105743:S 04 Mar 2022 15:03:40.499 * Loading RDB produced by version 6.2.6 105743:S 04 Mar 2022 15:03:40.499 * RDB age 0 seconds 105743:S 04 Mar 2022 15:03:40.499 * RDB memory usage when created 1.83 Mb 105743:S 04 Mar 2022 15:03:40.499 # Done loading RDB, keys loaded: 0, keys expired: 0. 105743:S 04 Mar 2022 15:03:40.499 * MASTER <-> REPLICA sync: Finished with success
Start the client of connection 31 on the machine of 30
Command: redis cli - C - H 192.168.234.159 - P 6379
Because we set the password, we need to enter the password when logging in
Command: auth + password you set
[root@redis3 src]# /root/software/redis-6.2.6/src/redis-cli -c -h 10.10.220.30 -p 6379 10.10.220.30:6379> auth 123456 OK 10.10.220.30:6379> keys * 1) "kkk" 10.10.220.30:6379>
Then set a key on the master to verify whether there is synchronization under the other two.
4.6 viewing role information
10.10.220.30:6379> role 1) "master" 2) (integer) 361310 3) 1) 1) "10.10.220.31" 2) "6379" 3) "361310" 2) 1) "10.10.220.32" 2) "6379" 3) "361310" 10.10.220.30:6379> 10.10.220.30:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=10.10.220.31,port=6379,state=online,offset=361492,lag=1 slave1:ip=10.10.220.32,port=6379,state=online,offset=361492,lag=0 master_failover_state:no-failover master_replid:6e2ad9355ad389d41224a87527c5a82e693aaed1 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:361492 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:361492 10.10.220.30:6379>
Description: role: represents whether the currently connected server is the master or slave
master_host:10.10.220.30 means that the current main machine is 30
master_link_status:up means that the main machine is started
connected_slaves:0 represents the slave machine currently connected to this machine. Because the current machine is a slave machine, this number is 0
4.7 configure sentinel mode
The sentinel system of Redis is used to manage multiple Redis servers. The system performs the following three tasks:
Monitoring: sentinel will constantly check whether your Master and Slave are working properly.
Notification: when a monitored Redis has a problem, the sentinel can send a notification to the administrator or other applications through the API.
Automatic failover: when a Master cannot work normally, sentinel will start an automatic failover operation, which will upgrade one slave of the failed Master to a new Master, and change other slave of the failed Master to copy the new Master; If the repaired Master is restarted, the original Master becomes slave.
I have copied the sentry's configuration file to / usr/local/bin /
The configuration is as follows:
Protected mode no open this line so that the three sentinels can communicate
sentinel monitor mymaster 10.10.220.30 6379 2 the IP address of the master machine is configured here
Sentinel auth pass mymaster 123456 here is the password for configuring the cluster
logfile "/usr/local/src/redis-6.2.6/log/sentinel_server.log"
Then start the sentry of the three machines; In the / usr/local/bin / directory, execute the command
./redis-server sentinel.conf --sentinel [root@redis2 bin]# ./redis-server sentinel.conf --sentinel [root@redis2 bin]# tail -100f /usr/local/src/redis-6.2.6/log/sentinel_server.log 41458:X 07 Mar 2022 15:20:05.247 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 41458:X 07 Mar 2022 15:20:05.247 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=41458, just started 41458:X 07 Mar 2022 15:20:05.247 # Configuration loaded 41458:X 07 Mar 2022 15:20:05.248 * Increased maximum number of open files to 10032 (it was originally set to 1024). 41458:X 07 Mar 2022 15:20:05.248 * monotonic clock: POSIX clock_gettime 41458:X 07 Mar 2022 15:20:05.250 * Running mode=sentinel, port=26379. 41458:X 07 Mar 2022 15:20:05.250 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 41458:X 07 Mar 2022 15:20:05.262 # Sentinel ID is 6d9831fdb72a99524522d7d4b96eb30ed3a2ef2b 41458:X 07 Mar 2022 15:20:05.262 # +monitor master mymaster 10.10.220.30 6379 quorum 2 41458:X 07 Mar 2022 15:20:12.364 # +tilt #tilt mode entered
4.8 setting redis to start automatically
mkdir /etc/redis Copy parameters cp /usr/local/bin/redis.conf /etc/redis/6379.conf Copy startup script file to/etc/init.d/ Under the directory cp ~/software/redis-6.2.6/utils/redis_init_script /etc/init.d/redis modify /ect/init.d/redis Add content to support chkconfig Grammar of Add two lines stay#!/ New after bin/sh # chkconfig: 2345 90 10 # description: Redis is a persistent key-value database
And increase the closing password, otherwise there will be a problem closing redis
The effect is
Set startup and self startup
chkconfig redis on start-up systemctl start redis
4.9 set sentinel to start automatically
cat > /usr/lib/systemd/system/sentinel.service << EOF [Unit] Description=Redis persistent key-value database After=network.target After=network-online.target Wants=network-online.target [Service] Type=forking ExecStart=/usr/local/bin/redis-server /usr/local/bin/sentinel.conf --sentinel ExecStop=/usr/local/bin/redis-cli -h 127.0.0.1 -p 26379 shutdown #You need to create a user first #User=redis #Group=redis #RuntimeDirectory=redis #RuntimeDirectoryMode=0755 [Install] WantedBy=multi-user.target >EOF ```bash chkconfig sentinel on start-up systemctl start sentinel
4.9 failover test:
2059:X 09 Mar 2022 14:48:31.996 # Sentinel ID is a56132ec97143eb6c69fdb0c9c4a4033e3a3a6d2 2059:X 09 Mar 2022 14:48:31.996 # +monitor master mymaster 10.10.220.30 6379 quorum 2 2059:X 09 Mar 2022 14:48:31.998 * +slave slave 10.10.220.31:6379 10.10.220.31 6379 @ mymaster 10.10.220.30 6379 2059:X 09 Mar 2022 14:48:32.008 * +slave slave 10.10.220.32:6379 10.10.220.32 6379 @ mymaster 10.10.220.30 6379 2059:X 09 Mar 2022 14:51:48.750 * +sentinel sentinel 6d9831fdb72a99524522d7d4b96eb30ed3a2ef2b 10.10.220.31 26379 @ mymaster 10.10.220.30 6379 2059:X 09 Mar 2022 14:54:05.431 * +sentinel sentinel 36b6118b062c29c955581b29932b4d1dc661b736 10.10.220.32 26379 @ mymaster 10.10.220.30 6379 2059:X 09 Mar 2022 14:55:35.925 # +sdown master mymaster 10.10.220.30 6379 2059:X 09 Mar 2022 14:55:36.016 # +new-epoch 1 2059:X 09 Mar 2022 14:55:36.026 # +vote-for-leader 6d9831fdb72a99524522d7d4b96eb30ed3a2ef2b 1 2059:X 09 Mar 2022 14:55:36.026 # +odown master mymaster 10.10.220.30 6379 #quorum 3/2 2059:X 09 Mar 2022 14:55:36.026 # Next failover delay: I will not start a failover before Wed Mar 9 15:01:36 2022 2059:X 09 Mar 2022 14:55:36.630 # +config-update-from sentinel 6d9831fdb72a99524522d7d4b96eb30ed3a2ef2b 10.10.220.31 26379 @ mymaster 10.10.220.30 6379 2059:X 09 Mar 2022 14:55:36.630 # +switch-master mymaster 10.10.220.30 6379 10.10.220.32 6379 2059:X 09 Mar 2022 14:55:36.631 * +slave slave 10.10.220.31:6379 10.10.220.31 6379 @ mymaster 10.10.220.32 6379 2059:X 09 Mar 2022 14:55:36.631 * +slave slave 10.10.220.30:6379 10.10.220.30 6379 @ mymaster 10.10.220.32 6379 2059:X 09 Mar 2022 14:56:06.666 # +sdown slave 10.10.220.30:6379 10.10.220.30 6379 @ mymaster 10.10.220.32 6379
32 becomes the host
10.10.220.31:6379> info replication # Replication role:slave master_host:10.10.220.32 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_read_repl_offset:72953 slave_repl_offset:72953 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:088ba4af0d9e529ab7704c4019f08d99babd0993 master_replid2:b8bc8fb1668fb662d4fd4284c90293115fe82bc2 master_repl_offset:72953 second_repl_offset:48438 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:72953
reference resources:
1. redis6 sentry configuration under CentOS 7 https://www.jianshu.com/p/ac065711a672