使用sentinel搭建集群

  1. 实验拓扑图

使用sentinel搭建集群_第1张图片

1.1在mysql1上安装redis服务
[root@mysql1 ~]# bash redis-install.sh

使用sentinel搭建集群_第2张图片

1.2配置文件中的与RDB相关的参数

创建数据存放目录

[root@mysql1 ~]# mkdir -p /redis/data/

[root@mysql1 ~]# vim /usr/local/redis/etc/redis6379.conf

398 stop-writes-on-bgsave-error yes

404 rdbcompression yes

413 rdbchecksum yes

431 dbfilename dump.rdb

454 dir /redis/data

[root@mysql1 ~]# systemctl restart redis6379.service #重启服务

[root@mysql1 ~]# redis-cli -a 123123 -p 6379 -h 192.168.0.88

192.168.0.88:6379> config get dir #查看数据存放目录是否创建成功

1) "dir"

2) "/redis/data" #设置成功

1.3配置文件中的与AOF相关的参数

[root@mysql1 ~]# vim /usr/local/redis/etc/redis6379.conf

1252 appendonly no

1256 appendfilename "appendonly.aof"

1281 appendfsync always

1282 appendfsync everysec

1283 appendfsync no

1304 no-appendfsync-on-rewrite no

1323 auto-aof-rewrite-percentage 100

1324auto-aof-rewrite-min-size 64mb

[root@mysql1 ~]# systemctl restart redis6379.service

1.4在mysql1上插入数据测试

[root@mysql1 ~]# redis-cli -a 123123 -p 6379 -h 192.168.0.88

192.168.0.88:6379> set aa 123

OK

192.168.0.88:6379> set bb 456

OK

192.168.0.88:6379> get aa

"123"

192.168.0.88:6379> get bb

"456"

使用sentinel搭建集群_第3张图片

1.5部署sentinel

1.5.1修改配置文件

[root@mysql1 ~]# cp /usr/local/src/redis-6.2.6/sentinel.conf

/usr/local/redis/etc/

[root@mysql1 ~]# vim /usr/local/redis/etc/sentinel.conf

27 daemonize yes

16 bind 0.0.0.0

37 logfile "/var/log/redis/sentinel_26379.log"

85 sentinel monitor mymaster 192.168.0.88 6379 2

87 sentinel auth-pass mymaster 123123

126 sentinel down-after-milliseconds mymaster 30000

201 sentinel parallel-syncs mymaster 1

226 sentinel failover-timeout mymaster 180000

[root@mysql1 ~]# scp /usr/local/redis/etc/sentinel.conf

192.168.0.99:/usr/local/redis/etc/

[root@mysql1 ~]# scp /usr/local/redis/etc/sentinel.conf

192.168.0.111:/usr/local/redis/etc/

1.5.2启动sentinel

1.5.2.1方式1:

[root@mysql1 ~]# redis-sentinel /usr/local/redis/etc/sentinel.conf

[root@mysql1 ~]# echo "redis-sentinel

/usr/local/redis/etc/sentinel.conf" >> /etc/rc.d/rc.local

[root@mysql1 ~]# source /etc/rc.d/rc.local

[root@mysql1 ~]# ps -ef | grep sentinel

[root@mysql1 ~]# pkill redis-sentinel

使用sentinel搭建集群_第4张图片

1.5.2.2方式2:使用systemctl命令控制sentinel

[root@mysql1 ~]# vim /etc/systemd/system/sentinel.service

[Unit]

Description=sentinel

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/redis/bin/redis-sentinel

/usr/local/redis/etc/sentinel.conf

ExecReload=/usr/local/redis/bin/redis-sentinel -s reload

ExecStop=/usr/local/redis/bin/redis-sentinel -s stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target

[root@mysql1 system]# scp sentinel.service

192.168.0.99:/etc/systemd/system

[root@mysql1 system]# scp sentinel.service 192.168.0.111:/etc/systemd/system

[root@mysql1 ~]# systemctl start sentinel.service

[root@mysql1 ~]# ps -ef | grep sentinel

使用sentinel搭建集群_第5张图片

1.5登录sentinel,检查整个集群的状态

[root@mysql1 ~]# redis-cli -p 26379 -h 192.168.0.88

192.168.0.88:26379> info sentinel

192.168.0.88:26379> client list #客户端列表

使用sentinel搭建集群_第6张图片

1.6关闭master,查看主从状态

[root@mysql1 ~]# systemctl stop redis6379.service

[root@mysql1 ~]# redis-cli -p 26379 -h 192.168.0.88

192.168.0.88:26379> info sentinel

192.168.0.88:26379> sentinel masters

1) "name"

2) "mymaster"

3) "ip"

4) "192.168.0.99"

5) "port"

6) "6379"

使用sentinel搭建集群_第7张图片

1.7重启主服务器,查看主从状态

[root@mysql1 ~]# systemctl restart redis6379.service

[root@mysql1 ~]# redis-cli -a 123123 -p 6379 -h 192.168.0.88

192.168.0.88:26379> info replication

# Replication

role:slave

master_host:192.168.0.99

master_port:6379

master_link_status:down #显示down

[root@mysql1 ~]# sed -i '/# masterauth/a masterauth 123123'

/usr/local/redis/etc/redis6379.conf

[root@mysql1 ~]# systemctl restart redis6379.service

[root@mysql1 ~]# redis-cli -a 123123 -p 6379 -h 192.168.0.88

192.168.0.88:6379> info replication

# Replication

role:slave

master_host:192.168.0.99

master_port:6379

master_link_status:up #显示up

2.1在mysql2上安装redis服务
[root@mysql2 ~]# bash redis-install.sh

使用sentinel搭建集群_第8张图片

2.2配置slave的主
[root@mysql2 ~]# sed -i '/# replicaof/c replicaof 192.168.0.88 6379' /usr/local/redis/etc/redis6379.conf #指定master IP,端口
[root@mysql2 ~]# sed -i '/# masterauth/a masterauth 123123' /usr/local/redis/etc/redis6379.conf #指定主redis连接认证密码
[root@mysql2 ~]# systemctl restart redis6379.service #重启服务

2.3在mysql2上查看主信息

192.168.0.99:6379> info replication

使用sentinel搭建集群_第9张图片

2.4在mysql2上查看同步信息

[root@mysql2 ~]# redis-cli -a 123123 -p 6379 -h 192.168.0.99

192.168.0.99:6379> keys *

1) "aa"

2) "bb"

192.168.0.99:6379> get aa

"123"

192.168.0.99:6379> get bb

"456"

使用sentinel搭建集群_第10张图片

2.5启动sentinel服务

[root@mysql2 ~]# systemctl start sentinel.service

[root@mysql2 ~]# systemctl enable sentinel.service

[root@mysql2 ~]# systemctl status sentinel.service

使用sentinel搭建集群_第11张图片

2.6在新的master上查看主从状态

[root@mysql2 ~]# redis-cli -a 123123 -p 6379 -h 192.168.0.99

使用sentinel搭建集群_第12张图片

3.1在mysql3上安装redis服务
[root@mysql3 ~]# bash redis-install.sh

使用sentinel搭建集群_第13张图片

3.2配置slave的主
[root@mysql3 ~]# sed -i '/# replicaof/c replicaof 192.168.0.88 6379' /usr/local/redis/etc/redis6379.conf #指定master IP,端口
[root@mysql2 ~]# sed -i '/# masterauth/a masterauth 123123' /usr/local/redis/etc/redis6379.conf #指定主redis连接认证密码
[root@mysql3 ~]# systemctl restart redis6379.service #重启服务

3.3在mysql3上查看主信息

192.168.0.111:6379> info replication

使用sentinel搭建集群_第14张图片

3.4在mysql3上查看同步信息

[root@mysql3 ~]# redis-cli -a 123123 -p 6379 -h 192.168.0.111

192.168.0.111:6379> keys *

1) "bb"

2) "aa"

192.168.0.111:6379> get aa

"123"

192.168.0.111:6379> get bb

"456"

使用sentinel搭建集群_第15张图片

3.5启动sentinel服务

[root@mysql2 ~]# systemctl start sentinel.service

[root@mysql2 ~]# systemctl enable sentinel.service

[root@mysql3 ~]# systemctl status sentinel.service

使用sentinel搭建集群_第16张图片

你可能感兴趣的:(sentinel,集群搭建,centos7,服务器,运维,架构,linux,centos)