Redis哨兵模式部署

Posted 草根追逐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis哨兵模式部署相关的知识,希望对你有一定的参考价值。

1. Redis编译安装(三台)

以下操作在三台主机同样操作

1.1 安装依赖包

yum -y install gcc gcc-c++ libstdc++-devel

1.2 上传redis二进制包

tar zxvf redis-5.0.14.tar.gz -C /data

1.3 编译redis,生成redis可执行文件

cd /data/redis ;mv redis-5.0.14/* .
make MALLOC=libc

2. 创建redis配置文件(三台)

创建数据目录
mkdir -p /data/redis/cluster/redis.conf
修改权限
chown -R redis:redis /data/redis
创建配置文件

2.1 第一台

cat >/data/redis/cluster/redis.conf<< EOF
bind 10.1.2.121
port 6379
daemonize yes
pidfile "/var/run/redis.pid"
logfile "/data/redis/cluster/redis.log"
dir "/data/redis/cluster/redis"
masterauth glstbrMrVE
requirepass "glstbrMrVE"
appendonly yes
maxmemory 7gb
EOF

2.2 第二台

cat > /data/redis/cluster/redis.conf <<EOF

bind 10.1.2.122
port 6379
daemonize yes
pidfile "/var/run/redis.pid"
logfile "/data/redis/cluster/redis.log"
dir "/data/redis/cluster/redis"
masterauth "glstbrMrVE"
requirepass "glstbrMrVE"

appendonly yes
maxmemory 7gb
EOF

2.3 第三台

cat > /data/redis/cluster/redis.conf <<EOF

bind 10.1.2.123
port 6379
daemonize yes
pidfile "/var/run/redis.pid"
logfile "/data/redis/cluster/redis.log"
dir "/data/redis/cluster/redis"
masterauth "glstbrMrVE"
requirepass "glstbrMrVE"

appendonly yes
maxmemory 7gb
EOF

2.4 拷贝redis-server、redis-cli、redis-sentinel可执行文件

cp src/redis-server /usr/bin/
cp src/redis-cli /usr/bin/
cp src/redis-sentinel /usr/bin/
chmod +x /usr/bin/redis-server
chmod +x /usr/bin/redis-cli
chmod +x /usr/bin/redis-sentinel
chown -R redis:redis /data/redis

2.5 配置自启动配置文件(三台)

2.5.1 第一台

cat > /usr/lib/systemd/system/redis.service << EOF
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis.pid
ExecStart=/usr/bin/redis-server /data/redis/cluster/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/redis-cli -h 10.1.2.121 -p 6379 shutdown
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

2.5.2 第二台

cat > /usr/lib/systemd/system/redis.service << EOF
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis.pid
ExecStart=/usr/bin/redis-server /data/redis/cluster/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/redis-cli -h 10.1.2.122 -p 6379 shutdown
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

2.5.3 第三台

cat > /usr/lib/systemd/system/redis.service << EOF
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis.pid
ExecStart=/usr/bin/redis-server /data/redis/cluster/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/redis-cli -h 10.1.2.123 -p 6379 shutdown
PrivateTmp=true

[Install]
WantedBy=multi-user.target

EOF

2.5.4 添加启动项及启动(三台)

Systemctl daemon-reload
Systemctl enable redis
Systemctl start redis

2.配置哨兵模式

注意事项
创建集群之前需要保证其他节点都正常安装和配置单机redis

2.1 配置哨兵配置文件(三台)

2.1.1 第一台

cat > /data/redis/sentinel.conf << EOF
daemonize yes
bind 10.1.2.121
port 26379
logfile "/data/redis/logs/sentinel.log"
dir "/data/redis/sentinel"
sentinel myid e2f7c688a4285e6b72d8e2e02d1f6a2f0611ea0e
sentinel deny-scripts-reconfig yes
sentinel monitor master 10.1.2.123 6379 2
sentinel down-after-milliseconds master 5000
sentinel failover-timeout master 15000
sentinel parallel-syncs master 2
sentinel auth-pass master glstbrMrVE
EOF

2.1.2 第二台

cat > /data/redis/sentinel.conf << EOF
daemonize yes
bind 10.1.2.122
port 26379
logfile "/data/redis/logs/sentinel.log"
dir "/data/redis/sentinel"
sentinel myid 3e89710158cbfcd99938946cb07e41216a034584
sentinel deny-scripts-reconfig yes
sentinel monitor master 10.1.2.123 6379 2
sentinel down-after-milliseconds master 5000
sentinel failover-timeout master 15000
sentinel parallel-syncs master 2
EOF

2.1.3 第三台

cat > /data/redis/sentinel.conf << EOF
daemonize yes
bind 10.1.2.123
port 26379
logfile "/data/redis/logs/sentinel.log"
dir "/data/redis/sentinel"
sentinel myid 9caba6da81a5452fc7269de27841042fcdfe92ef
sentinel deny-scripts-reconfig yes
sentinel monitor master 10.1.2.123 6379 2
sentinel down-after-milliseconds master 5000
sentinel failover-timeout master 15000
sentinel parallel-syncs master 2
EOF

2.2配置哨兵启动项(三台)

2.2.1 第一台

cat > /usr/lib/systemd/system/sentinel.service << EOF
[Unit]
Description=Redis Sentinel
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/bin/redis-sentinel /data/redis/sentinel.conf --supervised systemd
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify

[Install]
WantedBy=multi-user.target
EOF

2.2.2第二台和第三台同样配置

2.2.3 配置启动项和启动哨兵(三台)

Systemctl enable sentinel
Systemctl start sentinel

3.验证集群

3.1 登陆主节点

redis-cli -h 10.1.2.123 -p 6379 -a glstbrMrVE
执行以下语句
10.1.2.123:6379> info replication
如图:

3.2 登陆从节点

redis-cli -h 10.1.2.121 -p 6379 -a glstbrMrVE
0.1.2.121:6379> info replication
如图:

以上是关于Redis哨兵模式部署的主要内容,如果未能解决你的问题,请参考以下文章

实战之部署Redis哨兵模式-Docker版本

玩转Redis的高可用(主从、哨兵、集群)

Redis(主从复制哨兵模式集群)概述及部署

Redis(主从复制哨兵模式集群)概述及部署

Redis主从加哨兵模式集群部署

Redis哨兵模式(故障转移测试)