环境准备:

主机名 外网IP 内网IP 角色 应用
db01 10.0.0.51 172.16.1.51 master slave1 Redis7.2
db02 10.0.0.52 172.16.1.52 slave2 slave3 Redis7.2
db03 10.0.0.53 172.16.1.53 slave4 slave5 Redis7.2

下载Resis7.2(三台服务器全部执行)

db02和db03注意bind修改

# 1.下载
[root@db01 ~]# wget https://github.com/redis/redis/archive/7.2.0.tar.gz
# 2.创建目录
[root@db01 ~]# mkdir /app
# 3.解压
[root@db01 ~]# tar xf redis-7.2.0.tar.gz -C /app/
# 4.编译安装
[root@db01 ~]# cd /app/redis-7.2.0/
[root@db01 redis-7.2.0]# make && make install
# 5.软链接
[root@db01 redis-7.2.0]# ln -s /app/redis-7.2.0 /app/redis
# 6.修改redis配置
[root@db01 app]# vim /app/redis/redis.conf
daemonize yes
bind 127.0.0.1 172.16.1.51
# 7.开启内存优化
[root@db01 app]# vim /etc/sysctl.conf
vm.overcommit_memory = 1
# 8.设置密码
[root@db01 app]# vim /app/redis/redis.conf
requirepass 123
#9.启动服务
[root@db01 app]# redis-server /app/redis/redis.conf

systemd服务管理(三台服务器全部执行)

# 1.拷贝一个脚本
[root@db01 app]# cp /usr/lib/systemd/system/{sshd,redis}.service
# 2.修改启动脚本
[root@db01 app]# vim /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /app/redis/redis.conf
[Install]
WantedBy=multi-user.target
# 3.重新加载脚本
[root@db01 app]# systemctl daemon-reload
# 4.启动并加入开机自启
[root@db01 app]# systemctl enable redis --now
#端口查看
[root@db01 redis-7.2.0]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 172.16.1.51:6379        0.0.0.0:*               LISTEN      13160/redis-server  
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      13160/redis-server  
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      5984/rpcbind        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6923/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7058/master         
tcp6       0      0 :::111                  :::*                    LISTEN      5984/rpcbind        
tcp6       0      0 :::22                   :::*                    LISTEN      6923/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      7058/master         
udp        0      0 0.0.0.0:1022            0.0.0.0:*                           5984/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           5984/rpcbind        
udp6       0      0 :::1022                 :::*                                5984/rpcbind        
udp6       0      0 :::111  

创建多实例 (三台服务器全部执行)

#创建多实例目录
[root@db01 ~]# mkdir /redis-slave
#复制redis配置文件
[root@db01 ~]# cp /app/redis/redis.conf /redis-slave/
#db01修改配置文件
[root@db01 ~]# vim /redis-slave/redis.conf
port 6380
daemonize yes
pidfile /redis-slave/redis-6380.pid
logfile /redis-slave/redis-6380.log
dir /redis-slave
dbfilename dump.rdb
protected-mode no
requirepass 123
bind 127.0.0.1 172.16.1.51
#//db02和db03重复db01多示例创建即可 

多实例systemd服务管理(三台服务器全部执行)

# 1.拷贝一个脚本
[root@db01 app]# cp /usr/lib/systemd/system/{redis,redis-slave}.service
# 2.修改启动脚本
[root@db01 app]# vim /usr/lib/systemd/system/redis-slave.service
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
ExecStart=/app/redis/src/redis-server /redis-slave/redis.conf
[Install]
WantedBy=multi-user.target
# 3.重新加载脚本
[root@db01 app]# systemctl daemon-reload
# 4.启动并加入开机自启
[root@db01 app]# systemctl enable redis-slave --now
#5.端口检测
[root@db01 ~]# netstat -lntup
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:6379          0.0.0.0:*               LISTEN      13586/redis-server  
tcp        0      0 172.16.1.52:6380        0.0.0.0:*               LISTEN      16507/redis-server  
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      16507/redis-server  
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      5996/rpcbind        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6915/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7010/master         
tcp6       0      0 ::1:6379                :::*                    LISTEN      13586/redis-server  
tcp6       0      0 :::111                  :::*                    LISTEN      5996/rpcbind        
tcp6       0      0 :::22                   :::*                    LISTEN      6915/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      7010/master         
udp        0      0 0.0.0.0:610             0.0.0.0:*                           5996/rpcbind        
udp        0      0 0.0.0.0:111             0.0.0.0:*                           5996/rpcbind        
udp6       0      0 :::610                  :::*                                5996/rpcbind        
udp6       0      0 :::111                  :::*                                5996/rpcbind  

主从部署 (所有从库执行)

#修改从库配置文件
[root@db01 ~]# vim /redis-slave/redis.conf
requirepass 123
masterauth 123    #//添加修改此行

[root@db02 ~]# vim /app/redis/redis.conf
requirepass 123
masterauth 123    #//修改此行
[root@db03 ~]# vim /redis-slave/redis.conf
requirepass 123
masterauth 123    #//修改此行

[root@db03 ~]# vim /app/redis/redis.conf
requirepass 123
masterauth 123    #//修改此行
[root@db03 ~]# vim /redis-slave/redis.conf
requirepass 123
masterauth 123    #//修改此行
## 重启redis
[root@db01 ~]# systemctl restart redis redis-slave
[root@db02 ~]# systemctl restart redis redis-slave
[root@db03 ~]# systemctl restart redis redis-slave
# 开启主从
127.0.0.1:6380> SLAVEOF 172.16.1.51 6379
#主库查看从库信息
[root@db01 ~]# redis-cli -a 123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:5
slave0:ip=172.16.1.53,port=6379,state=online,offset=98,lag=0
slave1:ip=172.16.1.52,port=6379,state=online,offset=98,lag=1
slave2:ip=172.16.1.52,port=6380,state=online,offset=98,lag=1
slave3:ip=172.16.1.53,port=6380,state=online,offset=98,lag=0
slave4:ip=172.16.1.51,port=6380,state=online,offset=98,lag=1
master_failover_state:no-failover
master_replid:4306bd9c0f3dac7101641ce2203447a117c59045
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98
0