redis集群数据迁移
系统版本CentOS 7.9
k8s版本v1.19.16
redis版本6.2.6
源:单点redis数据
目的:redis集群,3主3从
1.查看集群信息
[root@k8s-master01 redis]# kubectl -n ops get po -owide |grep redis
redis-0 1/1 Running 0 13m 100.84.122.168 k8s-master02 <none> <none>
redis-cluster-0 1/1 Running 0 16h 100.84.122.161 k8s-master02 <none> <none>
redis-cluster-1 1/1 Running 0 16h 100.66.195.13 k8s-master03 <none> <none>
redis-cluster-2 1/1 Running 0 16h 100.84.122.160 k8s-master02 <none> <none>
redis-cluster-3 1/1 Running 0 16h 100.66.195.11 k8s-master03 <none> <none>
redis-cluster-4 1/1 Running 0 16h 100.84.122.166 k8s-master02 <none> <none>
redis-cluster-5 1/1 Running 0 16h 100.66.195.12 k8s-master03 <none> <none>
[root@k8s-master01 redis]#
[root@k8s-master01 redis]# kubectl get pvc -n ops |grep redis-pvc
redis-pvc Bound pvc-399ae488-2ca8-4100-92d6-21b47c836234 1Gi RWO nfs-boge 33m
[root@k8s-master01 redis]#
2.迁移思路
1.创建一个单点redis的pvc,名称为redis-pvc
cat > 01-redis-pv.yaml <<EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
namespace: ops
name: redis-pvc
spec:
storageClassName: nfs-boge #修改为自己的动态存储名称
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
EOF
kubectl apply -f 01-redis-pv.yaml
2.将redis的备份文件导入pvc(redis-pvc)中 3.部署一个单点redis(redis-0)
cat > 02-redis-dan-yaml.yaml << 'eof'
apiVersion: v1
kind: ConfigMap
metadata:
namespace: ops
name: redis-single-config
data:
redis.conf: |
daemonize no
bind 0.0.0.0
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
pidfile /data/redis-server.pid
logfile /data/redis.log
loglevel notice
databases 1
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated 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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
namespace: ops
name: redis
spec:
serviceName: redis
replicas: 1 # 副本数,根据需求进行更改
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:6.2.6
ports:
- containerPort: 6379
volumeMounts:
- name: redis-data
mountPath: /data
- name: redis-single-config
mountPath: "/etc/redis/redis.conf"
volumes:
- name: redis-single-config
configMap:
name: redis-single-config
- name: redis-data
persistentVolumeClaim:
claimName: redis-pvc
---
apiVersion: v1
kind: Service
metadata:
namespace: ops
name: redis-service
spec:
selector:
app: redis
type: NodePort # 添加type字段,并设置为NodePort类型
ports:
- name: redis
port: 6379
protocol: TCP
targetPort: 6379
nodePort: 30011 # 添加nodePort字段,设置为您想要的节点端口号
eof
kubectl apply -f 02-redis-dan-yaml.yaml
4.集群执行命令同步redis-0中的数据
可以将数据从外部实例导入到 Redis 集群 将外部Redis实例 (100.84.122.168:6379) 导入到集群中的任意一节点, 倒入之后,原来集群的key变为空,导入到新集群的key会自动分片到各个mater节点的slot -cluster-replace 如果集群 (100.84.122.161:6379) 中存在外部redis实例的key,则会覆盖掉(100.84.122.161:6379)的value
kubectl -n ops exec -it redis-cluster-0 -- redis-cli --cluster import 100.84.122.161:6379 --cluster-from 100.84.122.168:6379 --cluster-replace -a OpsRedis
3.验证redis信息
#登入redis
kubectl -n ops exec -it redis-cluster-0 -- redis-cli -c -p 6379 -a OpsRedis
127.0.0.1:6379> keys *
kubectl -n ops exec -it redis-cluster-1 -- redis-cli -c -p 6379 -a OpsRedis
127.0.0.1:6379> keys *
kubectl -n ops exec -it redis-cluster-2 -- redis-cli -c -p 6379 -a OpsRedis
127.0.0.1:6379> keys *
标签:cluster,redis,6379,集群,100.84,k8s
From: https://blog.51cto.com/u_16251158/7408077