redis-sentinel 部署脚本

基于centos7 三台机器一键部署redis-sentinel哨兵模式,达到高可用自动切换

#!/bin/bash

#请注意修改服务器的ip和验证密码,分别再主从服务器上进行跑
#
#######################master#############################
# redis-master/slave 新建配置目录
mkdir -p /data/todesk/redis/conf
 
# redis-master redis配置
cd /data/todesk/redis/conf
touch redis.conf sentinel.conf
echo 'requirepass "123456"
masterauth "123456"' >  /data/todesk/redis/conf/redis.conf
# redis-master  sentinel配置

cat > /data/todesk/redis/conf/sentinel.conf < EOF
#bind 0.0.0.0
port 26379
#daemonize no
#pidfile /var/run/redis-sentinel.pid
#logfile /var/log/redis/sentinel.log
dir /tmp
sentinel monitor mymaster 10.12.3.151 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel auth-pass mymaster 123456
EOF
# redis-master compose文件
touch docker-redis-master.yml
cat > docker-redis-master.yml < EOF
version: '3.7'
services:
  redis:
    image: redis:6.2.11
    container_name: redis-master
    restart: always
#    command: redis-server --requirepass 123456--appendonly yes
    command: redis-server /usr/local/etc/redis/redis.conf
    network_mode: host
    privileged: true
#    cap_add:
#      - SYS_ADMIN
#      - SYS_RESOURCE
#    sysctls:
#      - vm.overcommit_memory=1
    ports:
      - 6379:6379
    volumes:
      - /data/todesk/redis/data:/data
      - /data/todesk/redis/conf:/usr/local/etc/redis
    entrypoint:
      - bash
      - -c
      - |
        cat /proc/sys/net/core/somaxconn | xargs -i test {} -lt 512  && echo 65535 | tee /proc/sys/net/core/somaxconn
        echo 1 | tee /proc/sys/vm/overcommit_memory
        exec docker-entrypoint.sh $$@
  sentinel:
    image: redis:6.2.11
    container_name: redis-sentinel
    restart: always
    network_mode: host
    ports:
      - 26379:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes_from:
      - redis:rw
#    volumes:
#      - /data/todesk/redis/conf:/usr/local/etc/redis
    depends_on:
      - redis
EOF



#################slave1############################
# redis-slave redis配置
mkdir -p /data/todesk/redis/conf
cd /data/todesk/redis/conf
touch redis.conf sentinel.conf 
# redis-slave redis配置
echo 'requirepass "123456"
masterauth "123456"
slaveof 10.12.3.151 6379' > /data/todesk/redis/conf/redis.conf
 
# redis-master/slave  sentinel配置
echo '#bind 0.0.0.0
port 26379
#daemonize no
#pidfile /var/run/redis-sentinel.pid
#logfile /var/log/redis/sentinel.log
dir /tmp
sentinel monitor mymaster 10.12.3.151 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel auth-pass mymaster 123456'> /data/todesk/redis/conf/sentinel.conf

touch docker-redsi-slave.yml
cat > docker-redsi-slave.yml < EOF
version: '3.7'
services:
  redis:
    image: redis:6.2.11
    container_name: redis-slave
    restart: always
#    command: redis-server --requirepass 123456 --appendonly yes
    command: redis-server /usr/local/etc/redis/redis.conf
    network_mode: host
    privileged: true
#    ports:
#      - 6379:6379
    volumes:
      - /data/todesk/redis/data:/data
      - /data/todesk/redis/conf:/usr/local/etc/redis
    entrypoint:
      - bash
      - -c
      - |
        cat /proc/sys/net/core/somaxconn | xargs -i test {} -lt 512  && echo 65535 | tee /proc/sys/net/core/somaxconn
        echo 1 | tee /proc/sys/vm/overcommit_memory
        exec docker-entrypoint.sh $$@
  sentinel:
    image: redis:6.2.11
    container_name: redis-sentinel
    restart: always
    network_mode: host
#    ports:
#      - 26379:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes_from:
      - redis:rw
#    volumes:
#      - /data/todesk/redis/conf:/usr/local/etc/redis
    depends_on:
      - redis
EOF

#################slave2############################
# redis-slave redis配置
mkdir -p /data/todesk/redis/conf
cd /data/todesk/redis/conf
touch redis.conf sentinel.conf 
# redis-slave redis配置
echo 'requirepass "123456"
masterauth "123456"
slaveof 10.12.3.151 6379' > /data/todesk/redis/conf/redis.conf
 
# redis-master/slave  sentinel配置
echo '#bind 0.0.0.0
port 26379
#daemonize no
#pidfile /var/run/redis-sentinel.pid
#logfile /var/log/redis/sentinel.log
dir /tmp
sentinel monitor mymaster 10.12.3.151 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel auth-pass mymaster 123456'> /data/todesk/redis/conf/sentinel.conf

touch docker-redsi-slave.yml
cat > docker-redsi-slave.yml < EOF
version: '3.7'
services:
  redis:
    image: redis:6.2.11
    container_name: redis-slave
    restart: always
#    command: redis-server --requirepass 123456 --appendonly yes
    command: redis-server /usr/local/etc/redis/redis.conf
    network_mode: host
    privileged: true
#    ports:
#      - 6379:6379
    volumes:
      - /data/todesk/redis/data:/data
      - /data/todesk/redis/conf:/usr/local/etc/redis
    entrypoint:
      - bash
      - -c
      - |
        cat /proc/sys/net/core/somaxconn | xargs -i test {} -lt 512  && echo 65535 | tee /proc/sys/net/core/somaxconn
        echo 1 | tee /proc/sys/vm/overcommit_memory
        exec docker-entrypoint.sh $$@
  sentinel:
    image: redis:6.2.11
    container_name: redis-sentinel
    restart: always
    network_mode: host
#    ports:
#      - 26379:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes_from:
      - redis:rw
#    volumes:
#      - /data/todesk/redis/conf:/usr/local/etc/redis
    depends_on:
      - redis
EOF

状态查看,查看slave数量和sentinel数量,看到master机器

6379>info replication

26379>info sentinel

你可能感兴趣的:(redis,sentinel,数据库)