redis主从+哨兵

环境

节点 IP 主机名
node1 172.25.253.7 master
node2 172.25.253.16 node

redis主从复制

配置映射和主机名

[root@node-1 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.253.7 master
172.25.253.16 node
[root@node-1 ~]# hostnamectl set-hostname master


[root@node-2 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.25.253.7 master
172.25.253.16 node                                                                                                                                                                    
[root@node-2 ~]# hostnamectl set-hostname node

 

配置YUM源

[root@master ~]# rm -rf /etc/yum.repos.d/*
[root@master ~]# vim /etc/yum.repos.d/http.repo
[centos]
name=centos
baseurl=http://172.25.253.3/centos/
gpgcheck=0
enabled=1
[gpmall]
name=gpmall
baseurl=http://172.25.253.3/colony/gpmall-repo/
gpgcheck=0
enabled=1


[root@node ~]# rm -rf /etc/yum.repos.d/*
[root@node ~]# vim /etc/yum.repos.d/http.repo
[centos]
name=centos
baseurl=http://172.25.253.3/centos/
gpgcheck=0
enabled=1
[gpmall]
name=gpmall
baseurl=http://172.25.253.3/colony/gpmall-repo/
gpgcheck=0
enabled=1

 

安装redis服务

[root@master ~]# yum install -y redis

[root@node ~]# yum install -y redis

 

修改配置文件

[root@master ~]# vim /etc/redis.conf 
bind 127.0.0.1    修改     bind 0.0.0.0           # 0.0.0.0监听所有网段
daemonize no      修改     daemonize yes          # 开启守护进程
appendonly no     修改     appendonly yes         # 开启AOF持久化功能


[root@node ~]# vim /etc/redis.conf 
bind 127.0.0.1    修改     bind 0.0.0.0
daemonize no      修改     daemonize yes
appendonly no     修改     appendonly yes
添加: slaveof 172.25.253.7 6379        # 设置master服务ip及端口,进行master进行同步设置
/etc/redis.conf 其他常用配置文件参数
logfile /var/log/redis/redis.log        # 指定日志文件目录
dir /var/lib/redis                      # 指定工作目录
requirepass  123456                     # 设置密码 123456

 

重启服务

[root@master ~]# systemctl restart redis
[root@node ~]# systemctl restart redis

 

redis主从复制验证

[root@master ~]# tail /var/log/redis/redis.log 
1369:M 06 Dec 03:27:34.481 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1369:M 06 Dec 03:27:34.482 * The server is now ready to accept connections on port 6379
1369:M 06 Dec 03:27:37.354 * Slave 172.25.253.16:6379 asks for synchronization
1369:M 06 Dec 03:27:37.354 * Full resync requested by slave 172.25.253.16:6379
1369:M 06 Dec 03:27:37.354 * Starting BGSAVE for SYNC with target: disk
1369:M 06 Dec 03:27:37.354 * Background saving started by pid 1372
1372:C 06 Dec 03:27:37.357 * DB saved on disk
1372:C 06 Dec 03:27:37.357 * RDB: 2 MB of memory used by copy-on-write
1369:M 06 Dec 03:27:37.402 * Background saving terminated with success
1369:M 06 Dec 03:27:37.402 * Synchronization with slave 172.25.253.16:6379 succeeded


[root@master ~]# redis-cli info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.25.253.16,port=6379,state=online,offset=85,lag=1
master_repl_offset:85
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:84 

 

redis哨兵

 在redis主从的基础上配置redis哨兵

修改配置文件

[root@master ~]# vim /etc/redis-sentinel.conf 
去掉注释:protected-mode no
添加:daemonize yes

[root@node ~]# vim /etc/redis-sentinel.conf 
protected-mode no
daemonize yes
sentinel monitor mymaster 172.25.253.7 6379 2

 

重启服务

[root@master ~]# cd /etc/
[root@master etc]# redis-sentinel redis-sentinel.conf &
[1] 1380
[root@master etc]# ps -aux | grep redis
redis     1369  0.0  0.4 142952  7932 ?        Ssl  00:17   0:00 /usr/bin/redis-server 0.0.0.0:6379
root      1381  0.0  0.2 142952  5472 ?        Ssl  00:23   0:00 redis-sentinel *:26379 [sentinel]
root      1385  0.0  0.0 112704   972 pts/0    S+   00:23   0:00 grep --color=auto redis
[1]+  Done                    redis-sentinel redis-sentinel.conf

[root@node ~]# cd /etc/
[root@node etc]# redis-sentinel redis-sentinel.conf &
[1] 1378
[root@node etc]# ps -axu | grep redis
redis     1367  0.0  0.3 142952  5876 ?        Ssl  00:18   0:00 /usr/bin/redis-server 0.0.0.0:6379
root      1379  0.1  0.2 142952  5516 ?        Ssl  00:24   0:00 redis-sentinel *:26379 [sentinel]
root      1383  0.0  0.0 112704   968 pts/0    S+   00:24   0:00 grep --color=auto redis
[1]+  Done                    redis-sentinel redis-sentinel.conf

注意:先master重启,其次再node

 

posted @ 2021-12-06 11:31  衡衡酱  阅读(13)  评论(0)    收藏  举报
Live2D