Redis的一主二从三哨兵搭建【五种安装方式,助你无痛拿下Redis】

Redis的一主二从三哨兵搭建

五种安装方式

  1. 网络源安装
  2. 源码包安装
  3. 容器安装
  4. shell安装
  5. ansible安装

1、网络源安装

主机名 IP OS
master 192.168.96.210 Centos8.4
slave1 192.168.96.211 Centos8.4
slave2 192.168.96.212 Centos8.4

1、安装软件包(所有节点操作)

yum install redis*  -y

2、关闭防火墙和selinux (所有节点操作)

systemctl disable --now  firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

3、修改master配置文件

打开配置文件

vim /etc/redis.conf

修改参数

bind 192.168.96.210  192.168.96.211 192.168.96.212  # redis客户端监听的地址,把bind修改为0.0.0.0表示允许所有远程访问
port 6379     # redis进程的端口号
protected-mode no   # 是否开启保护模式,如果开启,则只监听本地地址
daemonize yes  # redis进程是否以守护进程的方式运行,yes为是,no为否。默认为no(不以守护进程的方式运行会占用一个终端)。
logfile /var/log/redis/redis.log # redis日志文件保存位置
requirepass redhat 设置redis连接密码,如果配置了连接密码,客户端在连接redis是需要通过AUTH<password>命令提供密码,默认关闭
masterauth redhat  # 当master设置了密码保护时,slave服务连接master的密码

4、修改slave配置文件(两台从节点都要操作)

打开配置文件

vim /etc/redis.conf

修改参数

bind 0.0.0.0
protected-mode no
port 6380
daemonize yes
logfile /var/log/redis/redis.log
masterauth redhat
requirepass redhat
replicaof 192.168.96.210 6379

5、开启服务并验证

systemctl enable --now  redis

master

redis-cli -h 192.168.96.210 -a redhat -p 6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.96.210:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.96.212,port=6380,state=online,offset=84,lag=1
slave1:ip=192.168.96.211,port=6380,state=online,offset=84,lag=1
master_replid:2fa638f601379594846acabe4bcecbca79612a2b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:84
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:84

slave1

redis-cli -h 127.0.0.1 -a redhat -p 6380
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:192.168.96.210
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:98
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:2fa638f601379594846acabe4bcecbca79612a2b
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

slave2

redis-cli -h 127.0.0.1 -a redhat -p 6380
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:192.168.96.210
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:98
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:2fa638f601379594846acabe4bcecbca79612a2b
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

6、哨兵模式(sentinel)

Redis Sentinel是Redis 的高可用性解决方案,由一个或多个Sentinel(哨兵)实例组成。它可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主服务器进入下线状态时,自动将下线主服务器属下的某个从服务器升级为新的主服务器,它的主要功能如下:

  1. 监控(Monitoring): Sentinel会不断地检查你的主服务器和从服务器是否运作正常。
  2. 通知(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel可以通过API向管理员或者其他应用程序发送通知。
  3. 故障迁移: 当主服务器不能正常工作时,Sentinel会自动进行故障迁移,也就是主从切换。
  4. 统一的配置管理: 连接者询问sentinel取得主从的地址。
1、master节点sentinel的配置

修改配置文件

vim /etc/redis-sentinel.conf 

修改参数

port 26379 # 哨兵模式使用的端口
daemonize yes  # 是否要在后端运行
dir /tmp # 哨兵模式的工作目录
protected-mode no # 是否开启保护模式
logfile "" # 当进程在后台运行的时候,直接将日志传输到/dev/null,但是这里不写"",最好写一个固定的文件
logfile /var/log/redis/redis-sentinel.log
sentinel monitor master 192.168.96.210 6379 2 # 修改master的名字,ip和端口,并且设置当有两个slave节点认为master挂掉的时候,就重新选举
sentinel auth-pass master redhat  #在Redis实例中开启了requirepass,这里就需要提供密码。
sentinel down-after-milliseconds master 3000  #这里设置了主机有多久没响应就认为主机怪掉了
sentinel parallel-syncs master 1 #主备切换时候,有多少slave想master同步
sentinel failover-timeout master 180000 # 故障转移超时时间

【注意:以上所有的master都必须要是主节点的主机名】
2、slave节点sentinel的配置

修改配置文件

vim /etc/redis-sentinel.conf 

修改参数

port 26379 # 哨兵模式使用的端口
daemonize yes  # 是否要在后端运行
dir /tmp # 哨兵模式的工作目录
protected-mode no # 是否开启保护模式
logfile "" # 当进程在后台运行的时候,直接将日志传输到/dev/null,但是这里不写"",最好写一个固定的文件
logfile /var/log/redis/redis-sentinel.log   # 如果报错,那么就还是""
sentinel monitor master 192.168.96.210 6379 2 # 修改master的名字,ip和端口,并且设置当有两个slave节点认为master挂掉的时候,就重新选举
sentinel auth-pass master redhat  #在Redis实例中开启了requirepass,这里就需要提供密码。
sentinel down-after-milliseconds master 3000  #这里设置了主机有多久没响应就认为主机怪掉了
sentinel parallel-syncs master 1 #主备切换时候,有多少slave想master同步
sentinel failover-timeout master 180000 # 故障转移超时时间

【注意:以上所有的master都必须要是主节点的主机名】

可以这样配置,也可以直接从master传输文件过去

3、重启服务并验证【三个节点都执行】
systemctl restart redis  # 重启服务
systemctl enable --now redis-sentinel.service
[root@master ~]# redis-cli  -h 192.168.96.210  -a redhat -p 26379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.96.210: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=master,status=ok,address=192.168.96.210:6379,slaves=2,sentinels=3

2、源码包安装【7.0.14】

1、下载并解压源码包【三节点执行】

wget https://github.com/redis/redis/archive/refs/tags/7.0.14.tar.gz
tar -xvf 7.0.14.tar.gz

2、创建redis存放目录【三节点执行】

mkdir /usr/local/redis
mv redis-7.0.14 /usr/local/redis/
cd /usr/local/redis/redis-7.0.14/

3、关闭防火墙和selinux【三节点执行】

systemctl disable --now firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

3、安装解析【三节点执行】

yum install make gcc gcc-c++ -y

4、编译安装【三节点安装】

make
cd /src
make install 

5、验证

在编译安装完成后,他默认安装到/usr/local/bin下

redis-server -v
redis-sentinel -v

6、将redis做成服务

将redis-server做成服务

vim /usr/lib/systemd/system/redis.service
[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server  /usr/local/redis/redis-7.0.14/redis.conf
# 写自己的文件路径
PrivateTmp=true
[Install]
WantedBy=multi-user.target

将redis-sentinel做成服务

vim /usr/lib/systemd/system/redis-sentinel.service
[Unit]
Description=redis-sentinel
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-sentinel  /usr/local/redis/redis-7.0.14/sentinel.conf
修改成自己的地址
PrivateTmp=true
[Install]
WantedBy=multi-user.target

7、修改配置文件【redis.conf】(三节点执行)

master参数

bind 0.0.0.0
protected-mode no
daemonize yes
logfile ./redis.log
masterauth redhat
requirepass redhat

slave参数

bind 0.0.0.0
protected-mode no
daemonize yes
logfile ./redis.log
masterauth redhat
requirepass redhat
replicaof  192.168.96.210  6379

8、启动redis并验证【三节点】

systemctl daemon-reload
systemctl enable --now redis

master

[root@master redis-7.0.14]# redis-cli -h 127.0.0.1 -a redhat -p 6379
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:2
slave0:ip=192.168.96.211,port=6379,state=online,offset=126,lag=1
slave1:ip=192.168.96.212,port=6379,state=online,offset=126,lag=1
master_failover_state:no-failover
master_replid:8196760f2a8931c2b0bd8748fc6741ae57bf9b92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126

slave

[root@slave1 redis-7.0.14]# redis-cli -h 127.0.0.1 -a redhat -p 6379
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:slave
master_host:192.168.96.210
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_read_repl_offset:126
slave_repl_offset:126
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:8196760f2a8931c2b0bd8748fc6741ae57bf9b92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126

9、哨兵模式

1、修改配置文件【sentinel.conf】(三节点)

master参数

protected-mode no
port 26379
daemonize yes
logfile ./sentinel.log
sentinel monitor master 192.168.96.210 6379 2
sentinel auth-pass master redhat
sentinel down-after-milliseconds master 3000
sentinel parallel-syncs master 1
sentinel failover-timeout master 180000

slave参数

protected-mode no
port 26379
daemonize yes
logfile ./sentinel.log
sentinel monitor master 192.168.96.210 6379 2
sentinel auth-pass master redhat
sentinel down-after-milliseconds master 3000
sentinel parallel-syncs master 1
sentinel failover-timeout master 180000

2、启动服务并验证

systemctl enable --now redis-sentinel.service

master

[root@master redis-7.0.14]# redis-cli -h 127.0.0.1 -a redhat -p 26379 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
AUTH failed: ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=master,status=ok,address=192.168.96.210:6379,slaves=2,sentinels=3

slave

[root@slave1 redis-7.0.14]# redis-cli -h 127.0.0.1 -a redhat -p 26379 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
AUTH failed: ERR AUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=master,status=ok,address=192.168.96.210:6379,slaves=2,sentinels=3

3、容器安装

1、配置docker并关闭防火墙、selinux【三节点执行】

安装docker

yum install -y yum-utils
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce
systemctl enable --now docker 

关闭防火墙和selinux

systemctl disable --now firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

2、拉取redis镜像【三节点执行】

docker pull uhub.service.ucloud.cn/variety/redis:7.0.14

3、运行镜像【三节点执行】

由于我们拉取的镜像,他默认位置redis.conf文件不存在,所以,我们给定一个外部的配置文件,让容器运行

master

创建文件和目录

mkdir /opt/redis
touch /opt/redis/redis.conf
mkdir /opt/redis/data

redis.conf参数

bind 0.0.0.0
protected-mode no
daemonize no
loglevel notice
port 6379
tcp-backlog 2048
databases 16
dir /data
masterauth redhat
requirepass redhat
maxclients 10000
timeout 600
tcp-keepalive 600
slowlog-log-slower-than 1000
slowlog-max-len 128
maxmemory 1gb
maxmemory-policy allkeys-lru
dbfilename dump.rdb
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
replicaof 192.168.96.210 6379

运行容器

docker run -tid --name redis-master --privileged  -v /opt/redis/redis.conf:/etc/redis/redis.conf:Z  -v /opt/redis/data/:/data:Z  --restart always  --network host  --entrypoint redis-server   uhub.service.ucloud.cn/variety/redis:7.0.14   /etc/redis/redis.conf

--entrypoint redis-server 在这里指定了entrypoint,覆盖掉原本的entrypoint,以便于我们链接的文件生效

修改文件权限

docker exec -it redis-node /bin/bash -c "chown redis:redis /etc/redis/redis.conf"

slave

创建文件和目录

mkdir /opt/redis
touch /opt/redis/redis.conf
mkdir /opt/redis/data

redis.conf参数

bind 0.0.0.0
protected-mode no
daemonize no
loglevel notice
port 6379
tcp-backlog 2048
databases 16
dir /data
masterauth redhat
requirepass redhat
maxclients 10000
timeout 600
tcp-keepalive 600
slowlog-log-slower-than 1000
slowlog-max-len 128
maxmemory 1gb
maxmemory-policy allkeys-lru
dbfilename dump.rdb
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes

运行容器

docker run -tid --name redis-slave --privileged  -v /opt/redis/redis.conf:/etc/redis/redis.conf:Z  -v /opt/redis/data/:/data:Z  --restart always  --network host  --entrypoint redis-server   uhub.service.ucloud.cn/variety/redis:7.0.14   /etc/redis/redis.conf

修改文件权限

docker exec -it redis-slave /bin/bash -c "chown redis:redis /etc/redis/redis.conf"

4、验证

master

docker exec  -ti redis-master /bin/bash
root@master:/data# redis-cli -p 6379 -a redhat 
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:2
slave0:ip=192.168.96.212,port=6379,state=online,offset=294,lag=1
slave1:ip=192.168.96.211,port=6379,state=online,offset=294,lag=1
master_failover_state:no-failover
master_replid:6e51430433c8b371ffd05828942979a75385e4ef
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:294
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:294

slave

docker exec -ti redis-slave /bin/bash
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.96.210
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:420
slave_repl_offset:420
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:6e51430433c8b371ffd05828942979a75385e4ef
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:420
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:420

5、哨兵模式

1、创建配置文件

touch /opt/redis/redis-sentinel.conf

2、运行容器

master

bind 0.0.0.0
port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/sentinel_26379.log"
dir "/tmp"
sentinel monitor master 192.168.96.210 6379 2       
sentinel auth-pass master redhat                 
sentinel down-after-milliseconds master 3000    
sentinel parallel-syncs master 1
sentinel failover-timeout master 180000
sentinel deny-scripts-reconfig yes

运行容器

docker run -tid --name redis-sentinel --restart always --privileged  -v /opt/redis/redis-sentinel.conf:/etc/redis/redis-sentinel.conf:Z  --network host --entrypoint redis-sentinel uhub.service.ucloud.cn/variety/redis:7.0.14  /etc/redis/redis-sentinel.conf

slave

bind 0.0.0.0
port 26379
daemonize no
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/sentinel_26379.log"
dir "/tmp"
sentinel monitor master 192.168.96.210 6379 2       
sentinel auth-pass master redhat                 
sentinel down-after-milliseconds master 3000    
sentinel parallel-syncs master 1
sentinel failover-timeout master 180000
sentinel deny-scripts-reconfig yes

运行容器

docker run -tid --name redis-sentinel --restart always --privileged  -v /opt/redis/redis-sentinel.conf:/etc/redis/redis-sentinel.conf:Z  --network host --entrypoint redis-sentinel uhub.service.ucloud.cn/variety/redis:7.0.14  /etc/redis/redis-sentinel.conf

3、验证

docker exec -ti redis-sentinel /bin/bash
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=master,status=ok,address=192.168.96.210:6379,slaves=2,sentinels=3

4、shell安装redis

这里shell安装使用tar包安装

#!/bin/bash
echo "--------------------"
echo "Start Install Redis"
rm -rf * /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
yum clean all && yum makecache 
yum install wget vim net-tools  bash-completion
echo "----------------------------------------"
echo "Basic Packages Installed And YUM already"

echo "Downloading Redis"
wget https://github.com/redis/redis/archive/refs/tags/7.0.14.tar.gz
if [ $? -eq 0 ];then 
    echo "download complete, tar"
    tar -xvf ./7.0.14.tar.gz
else
    echo "github network is unreachable , use kk agent"
    wget https://kkgithub.com/redis/redis/archive/refs/tags/7.0.14.tar.gz
    echo "download complete, tar"
    tar -xvf 7.0.14.tar.gz
fi
echo "-------------------------------"
echo "Stop Firewalld And Stop Selinux"
systemctl disable --now firewalld > /dev/null
setenforce 0 
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
echo "---------"
echo "Install C"
yum install -y make gcc gcc-c++  python3
echo "--------------"
echo "move directory"
mkdir /usr/local/redis
mv redis-7.0.14 /usr/local/redis/
cd /usr/local/redis/redis-7.0.14
echo "---------------------"
echo "MAKE AND MAKE INSTALL"
make  && cd src/  && make install
echo "-----"
echo "Check"
redis-server -v > /dev/null 2>&1
if [ $? -eq 0 ];then
    echo "redis is normal"
else
    echo "redis is abnormal, exit"
    exit 
fi     
echo "-----------------------------------------------"
echo "Create redis.service and redis-sentinel.service"
cat <<EOF &> /dev/null > /usr/lib/systemd/system/redis.service
[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server  /usr/local/redis/redis-7.0.14/redis.conf

PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
cat << EOF &> /dev/null >  /usr/lib/systemd/system/redis-sentinel.service
[Unit] 
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-sentinel  /usr/local/redis/redis-7.0.14/sentinel.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

echo "-----------------"
echo "change redis.conf"
cd /usr/local/redis/redis-7.0.14/
name=$(hostname)
if [ "$name" != "master" ];then 
    echo "This is slave"
    sed -i '/^bind/s/.*/bind 0.0.0.0/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^protected-mode/s/.*/protected-mode no/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^daemonize/s/.*/daemonize yes/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^logfile/s/.*/logfile  .\/redis.log/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^# masterauth/s/.*/masterauth redhat/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^# requirepass/s/.*/requirepass redhat/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^# replicaof/s/.*/replicaof 192.168.96.210 6379/' /usr/local/redis/redis-7.0.14/redis.conf
else 
    echo "This is master"
    sed -i '/^bind/s/.*/bind 0.0.0.0/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^protected-mode/s/.*/protected-mode no/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^daemonize/s/.*/daemonize yes/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^logfile/s/.*/logfile  .\/redis.log/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^# masterauth/s/.*/masterauth redhat/' /usr/local/redis/redis-7.0.14/redis.conf
    sed -i '/^# requirepass/s/.*/requirepass redhat/' /usr/local/redis/redis-7.0.14/redis.conf
fi
echo "Completed"
echo "---------"
echo "Change sentinel.conf"
sed -i '/^protected-mode/s/.*/protected-mode no/' /usr/local/redis/redis-7.0.14/sentinel.conf
sed -i '/^daemonize/s/.*/daemonize yes/' /usr/local/redis/redis-7.0.14/sentinel.conf
sed -i '/^logfile/s/.*/logfile  .\/sentinel.log/' sentinel.conf
sed -i '/^sentinel monitor/s/.*/sentinel monitor master 192.168.96.210 6379 2/' /usr/local/redis/redis-7.0.14/sentinel.conf
sed -i '/^# sentinel auth-pass/s/.*/sentinel auth-pass master redhat/' /usr/local/redis/redis-7.0.14/sentinel.conf
sed -i '/^sentinel down-after-milliseconds/s/.*/sentinel down-after-milliseconds master 3000/' /usr/local/redis/redis-7.0.14/sentinel.conf
sed -i '/^sentinel parallel-syncs/s/.*/sentinel parallel-syncs master 1/' /usr/local/redis/redis-7.0.14/sentinel.conf
sed -i '/^sentinel failover-timeout/s/.*/sentinel auth-pass master redhat/' /usr/local/redis/redis-7.0.14/sentinel.conf
sed -i '/^SENTINEL master-reboot-down-after-period/s/.*/SENTINEL master-reboot-down-after-period master 0/' /usr/local/redis/redis-7.0.14/sentinel.conf

echo "Completed"
echo "--------------"
echo "Start Services"
systemctl daemon-reload
systemctl enable --now redis   && systemctl restart redis  > /dev/null 2>&1
systemctl enable --now redis-sentinel   && systemctl restart redis > /dev/null 2>&1
if [ $? -eq 0 ];then 
    echo  "Script ends"
else
    echo  "Service error"
fi

5、ansible安装redis

1、安装ansible【wget安装】

wget https://github.com/ansible/ansible/tree/v2.9.0 

2、 安装python 【wget】

wget https://www.python.org/ftp/python/3.8.16/Python-3.8.16.tgz

3、解压

tar -xvf  Python-3.8.16.tgz
yum install unzip
unzip v2.9.0 

4、编译

# python
make
make install 
# ansible
python3 setup.py  build
python3 setup.py  install 

5、redis.yaml

- name: Redis Install
  hosts: all
  tasks: 
    - name: Stop Firewalld
      service:
        name: firewalld
        state: stopped
        enabled: no
    - name: Stop Selinux
      selinux: 
        state: disabled
    - name: Create repo
      shell: rm -rf /etc/yum.repos.d/* && curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
    - name: Install packages
      yum: 
        state: present 
        name: 
          - vim 
          - wget 
          - bash-completion
          - net-tools 
          - make 
          - gcc 
          - gcc-c++
    - name: Wget redis
      block:
        - file: 
            path: /usr/local/redis
            state: directory  
        - get_url: 
            url: https://github.com/redis/redis/archive/refs/tags/7.0.14.tar.gz
            dest: /usr/local/redis/
          register: wget_github
        - unarchive: 
            src: /usr/local/redis/redis-7.0.14.tar.gz
            dest: /usr/local/redis/
            remote_src: yes  
      ignore_errors: yes
    - name: debug wget error
      debug: 
        msg: "Network is unreachable"
      when: wget_github != 0
    - name: Wget kkgithub 
      block:
        - file:
            path: /usr/local/redis
            state: directory
        - get_url:
            url: https://kkgithub.com/redis/redis/archive/refs/tags/7.0.14.tar.gz
            dest: /usr/local/redis/
        - unarchive:
            src: /usr/local/redis/redis-7.0.14.tar.gz
            dest: /usr/local/redis/
            remote_src: yes
      when: wget_github !=0
    - name: debug  wget success
      debug: 
        msg: "Network is normal"
      when: wget_github == 0 
    - name: Make And Install 
      shell: cd /usr/local/redis/redis-7.0.14/ && make && cd src/ && make install 
    - name: Create redis.service redis-sentinel.service
      block: 
        - file: 
            path: /usr/lib/systemd/system/redis.service
            state: touch
        - file:
            path: /usr/lib/systemd/system/redis-sentinel.service
            state: touch

        - name: create redis.serice
          copy: 
            content: |
              [Unit]
              Description=redis-server
              After=network.target

              [Service]
              Type=forking
              ExecStart=/usr/local/bin/redis-server  /usr/local/redis/redis-7.0.14/redis.conf

              PrivateTmp=true
              [Install]
              WantedBy=multi-user.target
            dest:  /usr/lib/systemd/system/redis.service
        - name: create redis-sentinel.service
          copy:  
            content: |
              [Unit]
              Description=redis-sentinel
              After=network.target

              [Service]
              Type=forking
              ExecStart=/usr/local/bin/redis-sentinel  /usr/local/redis/redis-7.0.14/sentinel.conf

              PrivateTmp=true
              [Install]
              WantedBy=multi-user.target
            dest:  /usr/lib/systemd/system/redis-sentinel.service
    - name: Replace redis.conf
      block:
          - replace: 
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^bind .*'
              replace:  'bind 0.0.0.0'
          - replace:    
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^protected-mode .*'
              replace:  'protected-mode no'
          - replace:    
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^daemonize .*'
              replace:  'daemonize yes'
          - replace:    
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^logfile .*'
              replace:  'logfile  ./redis.log'
          - replace:    
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^# masterauth .*'
              replace:  'masterauth redhat'
          - replace:    
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^# requirepass .*'
              replace:  'requirepass redhat'
      when:  ansible_hostname  == "master"
      ignore_errors: yes 
    - name: Replace redis.conf
      block:
          - replace:
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^bind .*'
              replace:  'bind 0.0.0.0'
          - replace:
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^protected-mode .*'
              replace:  'protected-mode no'
          - replace:
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^daemonize .*'
              replace:  'daemonize yes'
          - replace:
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^logfile .*'
              replace:  'logfile  ./redis.log'
          - replace:
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^# masterauth .*'
              replace:  'masterauth redhat'
          - replace:
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^# requirepass .*'
              replace:  'requirepass redhat'
          - replace:
              path: /usr/local/redis/redis-7.0.14/redis.conf
              regexp:  '^# replicaof .*'
              replace:  'replicaof 192.168.96.210 6379'
      when:  ansible_hostname  != "master"
      ignore_errors: yes

    - name: Replace redis-sentinel.conf
      block: 
          - replace:
              path: /usr/local/redis/redis-7.0.14/sentinel.conf
              regexp:  '^protected-mode .*'
              replace:  'protected-mode no'
          - replace:
              path: /usr/local/redis/redis-7.0.14/sentinel.conf
              regexp:  '^daemonize .*'
              replace:  'daemonize yes'
          - replace:
              path: /usr/local/redis/redis-7.0.14/sentinel.conf
              regexp:  '^logfile .*'
              replace:  'logfile ./sentinel.log'
          - replace:
              path: /usr/local/redis/redis-7.0.14/sentinel.conf
              regexp:  '^sentinel monitor .*'
              replace:  'sentinel monitor master 192.168.96.210 6379 2'
          - replace:
              path: /usr/local/redis/redis-7.0.14/sentinel.conf
              regexp:  '^# sentinel auth-pass .*'
              replace:  'sentinel auth-pass master redhat'
          - replace:
              path: /usr/local/redis/redis-7.0.14/sentinel.conf
              regexp:  '^sentinel down-after-milliseconds .*'
              replace:  'sentinel down-after-milliseconds master 3000'
          - replace:
              path: /usr/local/redis/redis-7.0.14/sentinel.conf
              regexp:  '^sentinel parallel-syncs .*'
              replace:  'sentinel parallel-syncs master 1'
          - replace:
              path: /usr/local/redis/redis-7.0.14/sentinel.conf
              regexp:  '^sentinel failover-timeout .*'
              replace:  'sentinel auth-pass master redhat'
          - replace:
              path: /usr/local/redis/redis-7.0.14/sentinel.conf
              regexp:  '^SENTINEL master-reboot-down-after-period .*'
              replace:  'SENTINEL master-reboot-down-after-period master 0'
    - name: Service
      systemd:  
        daemon_reload: yes
    - name: Redis server
      systemd: 
        state: started
        name: redis.service
        enabled: yes
    - name: Redis-sentinel
      systemd:
        state: started
        name: redis-sentinel.service
        enabled: yes

         
posted @ 2025-04-24 15:30  super派大星  阅读(34)  评论(0)    收藏  举报