背景介绍:因linux系统版本太低导致需要升级,之前用的ubuntu,现在需要使用redhat9.经检查发现旧机上只有个redis集群,需要迁移到新机。下面是比较省事的方法:
一,在旧机上的操作
- 如果对数据有要求,需要先停止服务,否则可以跳过这步
bash
cd /usr/local/docker-redis
docker compose down
2.打包旧机上的镜像。
2.1 登录上旧机,运行~# docker ps,查看image, 比如你的镜像是lemon-redis-image/redis:6.2.6
2.2 把镜像打包成tar 文件,运行
bash
docker save -o redis-image.tar lemon-redis-image/redis:6.2.6
- 打包旧机上的配置文件
3.1 找到配置文件所在目录(比如docker-compose.yml, redis.conf),一般在/usr/local/docker-redis下,直接通过如下命令打包整个目录。
bash
cd /usr/local/
sudo tar -czvf docker-redis-full.tar.gz docker-redis/
- 将文件传到新机上
4.1 可以使用scp命令,也可以借助工具。假如新机server IP是20.1.1.111,在旧机上执行下面命令
bash
scp -i ~/.ssh/mykey.pem redis-image.tar docker-redis-full.tar.gz root@20.1.1.111:/tmp/
4.2 用scp命令前,需要自己调试两个server是否通,如果像我一样想使用pem文件,通过vi命令创建好后,还需要用下面的命令给它权限才能使用
bash
chmod 600 mykey.pem
二,在新机上的操作
- 配置好代理,不然等下下载的时候可能受限
bash
export http_proxy=http://10.1.1.10:8080
export https_proxy=http://10.1.1.10:8080
- 安装并启动docker
bash
sudo dnf install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo systemctl enable --now docker
//查看docker状态
systemctl status docker
- 加载镜像,解压文件
bash
cd /tmp/
docker load -i redis-image.tar
sudo tar -xzvf docker-redis-full.tar.gz -C /usr/local/
- 清除原先的关联文件
bash
sudo find /usr/local/docker-redis/ -name "nodes.conf" -type f -delete
sudo find /usr/local/docker-redis/ -name "appendonly.aof" -type f -delete
- 将配置文件里的旧机IP替换为新机IP
bash
cd /usr/local/docker-redis/
//旧机IP:10.1.1.000 新机IP:20.1.1.111
grep -rl "10.1.1.000" . | xargs sed -i 's/10.1.1.000/20.1.1.111/g'
- 启动容器
bash
docker compose up -d
- 重组集群
进入任意一个容器,执行创建命令
bash
//进入任意一个容器
: docker exec -it redis-6371 bash
//在容器内执行集群创建命令, 你的密码是xxxxx
/data: redis-cli -a xxxxx --cluster create 20.1.1.111:6371 20.1.1.111:6372 20.1.1.111:6373 --cluster-replicas 0
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 89375e5945f8a2cb59aa0fa4c5ef96894a0c4958 20.1.1.111:6371
slots:[0-5460] (5461 slots) master
M: d66cdcda4024101150e9971abd5dda888b1c6bf3 20.1.1.111:6372
slots:[5461-10922] (5462 slots) master
M: 2b7ad379475c63626582aa8abfbf524ab46c7a79 20.1.1.111:6373
slots:[10923-16383] (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 20.1.1.111:6371)
M: 89375e5945f8a2cb59aa0fa4c5ef96894a0c4958 20.1.1.111:6371
slots:[0-5460] (5461 slots) master
M: 2b7ad379475c63626582aa8abfbf524ab46c7a79 20.1.1.111:6373
slots:[10923-16383] (5461 slots) master
M: d66cdcda4024101150e9971abd5dda888b1c6bf3 20.1.1.111:6372
slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
如果看到OK,基本就好了,也可以通过执行命令查看一下, master是主节点,slave是从节点,myself,master 表示当前连接的节点且是主节点。
bash
/data: redis-cli -a xxxxx -p 6371 CLUSTER NODES
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
8937... 20.1.1.111:6371@16371 myself,master - 0 1764841456000 1 connected 0-5460
2b7a... 20.1.1.111:6373@16373 master - 0 1764841457539 3 connected 10923-16383
d66c... 20.1.1.111:6372@16372 master - 0 1764841456535 2 connected 5461-10922
或者退出容器,执行以下命令也能查看
bash
docker exec redis-6371 redis-cli -p 6371 -a xxxxx role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
master
0
docker exec redis-6372 redis-cli -p 6372 -a xxxxx role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
master
0
docker exec redis-6373 redis-cli -p 6373 -a xxxxx role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
master
0
三,在项目里测试新redis server
接下来需要在项目里测试新server上的redis是否可用,先在你的配置文件将新server ip替换掉,然后找到src->main->java->controller->目录,新建一个java类。内容如下:
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class RedisTest implements CommandLineRunner {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void run(String... args) throws Exception {
System.out.println("----------------------------------------");
System.out.println("正在开始 Redis 新地址连接测试...");
try {
String key = "test_new_server_check";
String value = "hello_redis_" + System.currentTimeMillis();
// 1. 测试写入
stringRedisTemplate.opsForValue().set(key, value);
System.out.println("1. 写入测试成功,Value: " + value);
// 2. 测试读取
String result = stringRedisTemplate.opsForValue().get(key);
System.out.println("2. 读取测试成功,Result: " + result);
// 3. 结果比对
if (value.equals(result)) {
System.out.println(" Redis 新环境连接完全正常!");
} else {
System.out.println(" 读写数据不一致!");
}
// 清理测试数据
stringRedisTemplate.delete(key);
} catch (Exception e) {
System.err.println(" Redis 连接失败!错误信息如下:");
e.printStackTrace();
}
System.out.println("----------------------------------------");
}
}
如果能看到如下结果,说明以及迁移成功:
java
----------------------------------------
正在开始 Redis 新地址连接测试...
1. 写入测试成功,Value: hello_redis_1764834747759
2. 读取测试成功,Result: hello_redis_1764834747759
Redis 新环境连接完全正常!
----------------------------------------
四,进阶配置
因为你创建集群时使用了"--cluster-replicas 0",所以3个节点都是master没有slave, 现在想给每个master配一个slave, 打算重新安排一台服务器. 实现双机部署。
-
按照上面第二大步的第4小步先把生成的配置文件都删掉(根据是否需要保留数据决定)
-
按照上面方法在另一台server上启动三个节点。
-
创建新的双机集群
还是先进入任意一个容器:docker exec -it redis-6371 bash 然后执行以下命令:
bash
# 假设第一台新机器 IP: 20.1.1.111
# 假设第二台新机器 IP: 20.2.2.222
redis-cli -a xxxxx --cluster create \
20.1.1.111:6371 \
20.1.1.111:6372 \
20.1.1.111:6373 \
20.2.2.222:6374 \
20.2.2.222:6375 \
20.2.2.222:6376 \
--cluster-replicas 1
另:如果不想关停旧机,在最开始的时候可以手动对几个节点执行保存命令。
bash
docker exec redis-6371 redis-cli -a xxxxx BGSAVE