Redis - 记录一次千万级 key 的 Redis 主从迁移步骤

任务描述


完成 redis 主从迁移:

  • 主节点 从 10.200.16.75 迁移到 10.200.16.10
  • 从节点 从 10.200.16.74 迁移到 10.200.16.11
    最后保证迁移后数据基本一致.

搭建 Redis 主从


编写 ansible

Note:手动给多个机器搭建 redis 主从,比较麻烦… 人生苦短,我选 ansible

初始化配置

给服务器进行目录相关的初始化.

# 1. /ansible/playbooks/redis5_init.yml

# --------
# 1.主节点配置部署
# ansible-playbook -i 10.200.16.10, playbooks/infra/redis5_deploy.yml -e "name=soc-cache port=6379 REDIS_MAXMEMORY=4GB" -v
#
# 2.从节点配置部署
# ansible-playbook -i 10.200.16.11, playbooks/infra/redis5_deploy.yml -e "name=soc-cache port=6379 master_ip=10.200.16.10 master_port=6379 REDIS_MAXMEMORY=4GB" -v
# --------

- name: redis5 deploy
  gather_facts: false
  hosts: all
  serial: 1
  roles:
    - role: redis5_deploy


# 2. /ansible/roles/redis5_init

# ./tasks/main.yml
- name: prepare dir
  file:
    path: "{{ item }}"
    state: directory
    mode: 0755
    owner: admin
  with_items:
    - "{{ INSTALL_DIR }}"
    - "{{ REDIS_DATA_DIR }}"
    - "{{ REDIS_PID_DIR }}"
    - "{{ REDIS_LOG_DIR }}"
    - "{{ REDIS_CONFIG_DIR }}"

- name: Install aptitude
  become: yes
  apt:
    name: aptitude
    state: latest
    update_cache: true

- name: Install required system packages
  become: yes
  apt:
    pkg:
      - unzip
    state: latest
    update_cache: true


# ./vars/main.yml
REDIS_DATA_PATH: "/piqiu/data/redis"

REDIS_DATA_DIR: "{{REDIS_DATA_PATH}}/redis-{{name}}"
REDIS_PID_DIR: "/piqiu/data/work/redis-{{name}}"
REDIS_LOG_DIR: "/piqiu/logs/usr/redis-{{name}}"
REDIS_CONFIG_DIR: "/piqiu/dist/conf/redis-{{name}}"

version: 5.0.5
REDIS_DISTRIBUTION: "redis_{{version}}_linux_amd64.zip"
INSTALL_DIR: "/piqiu/dist/sys/redis-{{name}}"


节点部署

下载 redis,并进行部署(运行、日志配置…)

# 1. /ansible/playbooks/redis5_deploy.yml

# --------
# 1.主节点配置部署
# ansible-playbook -i 10.200.16.10, playbooks/infra/redis5_deploy.yml -e "name=soc-cache port=6379 REDIS_MAXMEMORY=4GB" -v
#
# 2.从节点配置部署
# ansible-playbook -i 10.200.16.11, playbooks/infra/redis5_deploy.yml -e "name=soc-cache port=6379 master_ip=10.200.16.10 master_port=6379 REDIS_MAXMEMORY=4GB" -v
# --------

- name: redis5 deploy
  gather_facts: false
  hosts: all
  serial: 1
  roles:
    - role: redis5_deploy


# 2. /ansible/roles/redis5_deploy

# ./tasks/main.yml
- include_tasks: main_redis_install.yml
- include_tasks: main_conf_deploy.yml
- include_tasks: main_start_action.yml

# ./tasks/main_redis_install.yml
- name: download the redis
  unarchive:
    src={{ REDIS_INSTALL_SRC }}
    dest="{{ INSTALL_DIR }}"
    copy=no
  tags:
    - download

# ./tasks/main_conf_deploy.yml
- name: template redis.sh
  template:
    src=redis.sh.tpl
    dest={{ REDIS_SH_FILE }}
    mode=0744

- name: template redis conf
  template:
    src=redis.conf.tpl
    dest={{ REDIS_CONFIG_FILE }}
    mode=0644

# ./tasks/main_start_action.yml
- name: start redis
  shell: "{{ REDIS_SH_FILE }} start"

# ./templates/redis.confg.tpl
bind {{REDIS_HOST}}
port {{port}}

protected-mode no

tcp-backlog 10240
timeout 3600
tcp-keepalive 300
daemonize yes

supervised no

pidfile {{REDIS_PID_FILE}}
loglevel warning
logfile {{REDIS_LOG_FILE}}
databases 1

always-show-logo no
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes

dbfilename {{REDIS_RDB_FILE}}

dir {{REDIS_DATA_DIR}}

replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100

maxmemory {{REDIS_MAXMEMORY}}
maxmemory-policy {{REDIS_MAXMEMORY_POLICY}}

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no

appendonly no
appendfilename {{REDIS_AOF_FILE}}
appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 512mb
aof-load-truncated yes
aof-use-rdb-preamble yes

lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes

client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

hz 10
dynamic-hz yes

aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

{% if master_ip is defined and master_port is defined %}
slaveof {{ master_ip }} {{ master_port }}
{% endif %}

# ./templates/redis.sh.tpl
#!/bin/bash
# redis start
REDIS_HOME={{ INSTALL_DIR }}
START_OPTS="{{REDIS_CONFIG_FILE}}"
PID_FILE={{REDIS_PID_FILE}}

function start() {
  if [[ -f ${PID_FILE} ]];
    then
    echo "pid file already exist"
    exit 1
  fi
  ${REDIS_HOME}/bin/redis-server $START_OPTS
  echo "start redis success"
}

function stop() {
  if [[ ! -f ${PID_FILE} ]];
    then
    echo "pid file not existd"
    exit 1
  fi
  PID=`cat ${PID_FILE}`
  echo "pid is $PID"
  kill $PID
  rm ${PID_FILE}
  echo "stop redis exporter success"
}

function restart () {
  if [[ ! -f ${PID_FILE} ]];
    then
      start
    else
      stop
      start
  fi
}

usage () {
  echo "Usage"
  echo "$0 [-h] [start/stop/restart]"
}

for i  in `seq 1 $#`
do
 case $1 in
        start)
            start
            exit 0
            ;;
        stop)
            stop
            exit 0
            ;;
        restart)
            restart
            exit 0
            ;;
         *)
          usage
          ;;
    esac
done

# ./vars/main.yml
REDIS_HOST: "{{ansible_host}}"
DATA_DEVICE: "vdb"
REDIS_PORT: "{{port}}"
REDIS_MAXMEMORY: 4GB
REDIS_MAXMEMORY_POLICY: noeviction

REDIS_DATA_PATH: "/piqiu/data/redis"
REDIS_DATA_DIR: "{{REDIS_DATA_PATH}}/redis-{{name}}"

REDIS_PID_DIR: "/piqiu/data/work/redis-{{name}}"
REDIS_PID_FILE: "{{REDIS_PID_DIR}}/redis-{{port}}.pid"

REDIS_LOG_DIR: "/piqiu/logs/usr/redis-{{name}}"
REDIS_LOG_FILE: "{{REDIS_LOG_DIR}}/redis-{{port}}.log"

REDIS_PERSISTENCE: true
REDIS_RDB_FILE: "{{port}}.rdb"
REDIS_AOF_FILE: "{{port}}.aof"

REDIS_CONFIG_DIR: "/piqiu/dist/conf/redis-{{name}}"
REDIS_CONFIG_FILE: "{{REDIS_CONFIG_DIR}}/redis-{{port}}.conf"

version: 5.0.5
REDIS_DISTRIBUTION: "redis_{{version}}_linux_amd64.zip"
INSTALL_DIR: "/piqiu/dist/sys/redis-{{name}}"
REDIS_INSTALL_SRC: "http://10.200.48.3/piqiu/{{REDIS_DISTRIBUTION}}"

REDIS_SH_FILE: "{{INSTALL_DIR}}/redis-{{port}}.sh"

执行 ansible

a)部署主节点
初始化:

ansible-playbook -i 10.200.16.10, playbooks/infra/redis5_init.yml -e name=soc-cache -v

部署:

ansible-playbook -i 10.200.16.10, playbooks/infra/redis5_deploy.yml -e "name=soc-cache port=6379 REDIS_MAXMEMORY=4GB" -v

b)部署从节点
初始化:

ansible-playbook -i 10.200.16.11, playbooks/infra/redis5_init.yml -e name=soc-cache -v

部署:

ansible-playbook -i 10.200.16.11, playbooks/infra/redis5_deploy.yml -e "name=soc-cache port=6379 master_ip=10.200.16.10 master_port=6379 REDIS_MAXMEMORY=4GB" -v

c)验证
进入 redis cli,通过 info replication 查看主节点连接情况:

10.200.16.11:6379> info replication
# Replication
role:slave
master_host:10.200.16.10
master_port:6379

数据迁移

数据迁移

Note:
redis-shake 下载地址:https://github.com/tair-opensource/RedisShake
redis-shake 中文文档:https://tair-opensource.github.io/RedisShake/zh/guide/getting-started.html

a)这里我们使用 redis-shake 完成数据迁移.

# 下载
wget https://github.com/tair-opensource/RedisShake/releases/download/v4.1.1/redis-shake-linux-amd64.tar.gz

#解压
tar -xvf redis-shake-linux-amd64.tar.gz

b)修改 shake.toml 配置文件
主要配置内容如下:

  • [sync_reader] address:从哪个 redis 实例中读.
  • `[sync_reader] sync_rdb:同步 rdb 文件中的内容,相当于全量复制(并不是把 rdb 文件发过去,而是解析并执行里面的命令).
  • `[sync_reader] sync_aof:同步 aof 文件中的内容,相当于实时复制(开启后会一直实时复制,不会停止).
  • [redis_writer] address:写到哪个 redis 实例中去.
[sync_reader]
cluster = false            # set to true if source is a redis cluster
address = "10.200.16.75:6379" # when cluster is true, set address to one of the cluster node
username = ""              # keep empty if not using ACL
password = ""              # keep empty if no authentication is required
tls = false                #
sync_rdb = true            # set to false if you don't want to sync rdb
sync_aof = true            # set to false if you don't want to sync aof
prefer_replica = false     # set to true if you want to sync from replica node
try_diskless = false       # set to true if you want to sync by socket and source repl-diskless-sync=yes

[redis_writer]
cluster = false            # set to true if target is a redis cluster
sentinel = false           # set to true if target is a redis sentinel
master = ""                # set to master name if target is a redis sentinel
address = "10.200.16.10:6379" # when cluster is true, set address to one of the cluster node
username = ""              # keep empty if not using ACL
password = ""              # keep empty if no authentication is required
tls = false
off_reply = false          # ture off the server reply

[advanced]
dir = "data"
ncpu = 0        # runtime.GOMAXPROCS, 0 means use runtime.NumCPU() cpu cores
pprof_port = 0  # pprof port, 0 means disable
status_port = 0 # status port, 0 means disable

# log
log_file = "shake.log"
log_level = "info"     # debug, info or warn
log_interval = 5       # in seconds

# redis-shake gets key and value from rdb file, and uses RESTORE command to
# create the key in target redis. Redis RESTORE will return a "Target key name
# is busy" error when key already exists. You can use this configuration item
# to change the default behavior of restore:
# panic:   redis-shake will stop when meet "Target key name is busy" error.
# rewrite: redis-shake will replace the key with new value.
# skip:  redis-shake will skip restore the key when meet "Target key name is busy" error.
rdb_restore_command_behavior = "panic" # panic, rewrite or skip

# redis-shake uses pipeline to improve sending performance.
# This item limits the maximum number of commands in a pipeline.
pipeline_count_limit = 1024

# Client query buffers accumulate new commands. They are limited to a fixed
# amount by default. This amount is normally 1gb.
target_redis_client_max_querybuf_len = 1024_000_000

# In the Redis protocol, bulk requests, that are, elements representing single
# strings, are normally limited to 512 mb.
target_redis_proto_max_bulk_len = 512_000_000

# If the source is Elasticache or MemoryDB, you can set this item.
aws_psync = "" # example: aws_psync = "10.0.0.1:6379@nmfu2sl5osync,10.0.0.1:6379@xhma21xfkssync"

# destination will delete itself entire database before fetching files
# from source during full synchronization.
# This option is similar redis replicas RDB diskless load option:
#   repl-diskless-load on-empty-db
empty_db_before_sync = false

[module]
# The data format for BF.LOADCHUNK is not compatible in different versions. v2.6.3 <=> 20603
target_mbbloom_version = 20603

c)执行数据同步脚本,如下命令

./redis-shake shake.toml

此时,执行完全量复制后,开始进行实时复制(等到修改完连接redis项目的配置,手动断开即可).

Nacos 修改配置

此处所有涉及到 Redis 到项目配置都是在 nacos 上的,因此先找找项目中的 NacosConfig(namespace、dataId…),然后改 nacos 上对应的 redis.host 和 redis.port 即可.

nacos 会自动完成热更新(发布系统部署,需要重新发布).

在这里插入图片描述

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: another-redis-desktop-manager 是一个 Redis 数据库管理工具,版本号为 1.6.0。Redis 是一个内存数据库,常用于数据缓存、任务队列和排行榜等场景。该软件可以让用户更方便地管理 Redis 数据库。 another-redis-desktop-manager 具有直观的图形用户界面,使用户可以轻松地连接到 Redis 数据库并执行各种操作。它提供了各种功能,包括查看和编辑 Redis 数据的能力、执行命令、查看服务器和客户端信息、监控 Redis 的性能指标等。 该软件支持密钥和值的搜索、过滤和排序,使用户可以更快地找到并操作所需的数据。另外,它还提供了多标签功能,允许用户在不同的 Redis 数据库之间轻松切换和管理。 除了基本的数据操作功能,another-redis-desktop-manager 还支持将 Redis 数据库导出为 JSON、CSV 和 SQL 文件,以便用户可以方便地备份和迁移数据。它还允许用户进行数据的导入,以满足不同的需求。 总之,another-redis-desktop-manager 是一个强大且易于使用的 Redis 数据库管理工具,它提供了丰富的功能和直观的界面,可帮助用户更高效地管理和操作 Redis 数据库。用户可以通过下载并安装该软件,轻松地管理他们的 Redis 数据。 ### 回答2: another-redis-desktop-manager是一个开源的跨平台Redis桌面管理工具,版本号为1.6.0。Redis是一个开源的高性能key-value数据库,广泛用于缓存、消息队列和持久化等场景。another-redis-desktop-manager提供了一个用户友好的图形界面,方便用户查看、管理和操作Redis数据库。 another-redis-desktop-manager的1.6.0版本带来了一些改进和升级。其中包括用户界面的改进,让用户更轻松地浏览和管理Redis数据库。此外,改进了性能和稳定性,提供了更快、更可靠的数据库操作体验。 该工具提供了一些功能,如连接到远程或本地Redis服务器,浏览和搜索键值对,执行常见的Redis操作(如添加、编辑和删除键),查看和分析数据库统计信息等。还可以支持多个Redis实例,并方便地在它们之间切换。 使用another-redis-desktop-manager可以大大简化Redis数据库管理的工作,无需使用命令行或编写复杂的脚本。它具有友好的用户界面和丰富的功能,适用于开发人员、运维人员和其他需要与Redis进行交互的用户。 总之,another-redis-desktop-manager是一个方便实用的Redis桌面管理工具,1.6.0版本带来了更好的性能和用户体验,适合用于管理和操作Redis数据库。 ### 回答3: another-redis-desktop-manager.1.6.0.exe是另一种Redis桌面管理器的安装文件。Redis是一种开源的高性能键值对存储数据库,用于快速处理数据,支持主从复制、持久化、集群等功能。Redis桌面管理器是一个可视化工具,用于管理和监控Redis数据库。 another-redis-desktop-manager.1.6.0.exe是该管理器的1.6.0版本的安装程序。通过安装该程序,用户可以在桌面环境中方便地管理Redis数据库,而无需手动使用Redis命令行工具。 该桌面管理器提供了直观的用户界面,使用户能够轻松执行常见的Redis操作,如查看和编辑键值对、执行命令、监视服务器状态、管理数据库等。它还提供了一个实时监控面板,用于显示Redis服务器的性能指标,如内存使用情况、并发连接数等,帮助用户更好地了解数据库的运行状况。 此外,该管理器支持多个Redis实例的管理,用户可以轻松切换和管理多个数据库。它还提供了一些高级功能,如备份和恢复数据、导入和导出数据、设置键的过期时间等。 总之,another-redis-desktop-manager.1.6.0.exe是一个方便易用的Redis桌面管理器,它提供了可视化的界面和丰富的功能,帮助用户更轻松地管理和监控Redis数据库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈亦康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值