redis一主二从三哨兵模式

229 阅读3分钟

在三个节点自行安装 Redis 服务并启动,配置 Redis 的访问需要密码,密码设置为 123456。然后将这三个 Redis 节点配置为 Redis 的一主二从三哨兵架构,即一个 Redis 主节点,两个从节点,三个节点均为哨兵节点。

节点规划主机名ip地址
主机1controller192.168.100.131
主机2compute192.168.100.132
主机3node3192.168.100.143

1.三台主机关闭防火墙和selinux

systemctl stop firewalld && setenforce 0

2.配置主机映射

controller:

[root@controller]# vi /etc/hosts

192.168.100.131 controller
192.168.100.132 compute
192.168.100.143 node3

compute:

[root@compute ~]# vi /etc/hosts

192.168.100.131 controller
192.168.100.132 compute
192.168.100.143 node3

node3:

[root@node3 ~]# vi /etc/hosts

192.168.100.131 controller
192.168.100.132 compute
192.168.100.143 node3

3.配置yum源

挂载镜像,redis需要用centos7.9和iaas的镜像源,先移除centos自带的yum源

controller:

[root@controller]# mkdir /opt/{centos,iaas}

[root@controller]# mount -o loop CentOS-7-x86_64-DVD-2009.iso /mnt/

[root@controller]# cp -rvf /mnt/* /opt/centos/

[root@controller]# umount /mnt/

[root@controller]# mount -o loop chinaskills_cloud_iaas_v2.0.3.iso /mnt/

[root@controller]# cp -rvf /mnt/* /opt/iaas/

[root@controller]# umount /mnt/

[root@controller]# mv /etc/yum.repos.d/* /media/

[root@controller]# /etc/yum.repos.d/http.repo

[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[iaas]
name=iaas
baseurl=file:///opt/iaas/iaas-repo
gpgcheck=0
enabled=1

compute:

[root@compute ~]# mv /etc/yum.repos.d/* /media/

[root@compute ~]# vi /etc/yum.repos.d/centos.repo

[centos]
name=centos
baseurl=ftp://192.168.100.131/centos
gpgcheck=0
enabled=1

[iaas]
name=iaas
baseurl=ftp://192.168.100.131/iaas/iaas-repo
gpgcheck=0
enabled=1

node3:

[root@node3 ~]# mv /etc/yum.repos.d/* /media/

[root@node3 ~]# vi /etc/yum.repos.d/centos.repo

[centos]
name=centos
baseurl=ftp://192.168.100.131/centos
gpgcheck=0
enabled=1

[iaas]
name=iaas
baseurl=ftp://192.168.100.131/iaas/iaas-repo
gpgcheck=0
enabled=1

4.三台节点安装redis服务

yum install redis -y

5.三台节点配置Redis的访问需要密码123456,并关闭保护模式

sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g' /etc/redis.conf

sed -i 's/protected-mode yes/protected-mode no' /etc/redis.conf

echo -e 'requirepass 123456 \nmasterauth 123456'>>/etc/redis.conf

systemctl start redis

6.配置Redis集群为一主二从

[root@compute ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> slaveof controller 6379
OK
(5.80s)
[root@node3 ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> slaveof controller 6379
OK
[root@controller ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.100.132,port=6379,state=online,offset=43,lag=1
slave1:ip=192.168.100.143,port=6379,state=online,offset=43,lag=1
master_repl_offset:43
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:42

7.配置Redis集群为三哨兵模式

controller:

[root@controller ~]# vi /etc/redis-sentinel.conf

sentinel monitor mymaster 192.168.100.131 6379 2 #127.0.0.1改为本机的ip

# sentinel parallel-syncs <master-name> <numslaves>
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel auth-pass mymaster 123456  #设置密码为123456

compute:

[root@compute ~]# vi /etc/redis-sentinel.conf

sentinel monitor mymaster 192.168.100.132 6379 2

# sentinel parallel-syncs <master-name> <numslaves>
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel auth-pass mymaster 123456

node3:

[root@node3 ~]# vi /etc/redis-sentinel.conf

sentinel monitor mymaster 192.168.100.143 6379 2

# sentinel parallel-syncs <master-name> <numslaves>
#
# How many slaves we can reconfigure to point to the new slave simultaneously
# during the failover. Use a low number if you use the slaves to serve query
# to avoid that all the slaves will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel auth-pass mymaster 123456

三台主机后台运行服务

redis-sentinel /etc/redis-sentinel.conf &

8.在controller节点,验证sentinels为3就成功

[root@controller ~]# redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.100.131:6379,slaves=2,sentinels=3

以上就成功了,这篇是学习途中根据网上资料做的一些笔记