kubernetes的基于Operator实现Redis主从复制

 

 

operator 实现过程

1)安装operator,生成对应CRD,以及对应CRD Controller Pod
2) 编写 CRD 清单文件,里面定义用户的需求
3)apply CRD 清单文件

 

Operator 案例

案例: 基于 Operator 实现 Redis 主从复制

#Redis Operator 国内项目
https://github.com/tongdun/td-redis-operator
#著名Redist Operator项目
https://github.com/OT-CONTAINER-KIT/redis-operator/

 

安装 Operator

wget https://raw.githubusercontent.com/tongdun/td-redisoperator/main/deploy/deploy.yaml

kubectl apply -f deploy.yaml

 #查看CRD

root@master1 stateful]# kubectl get crd |grep redis
redisclusters.cache.tongdun.net    2026-05-28T15:29:01Z
redisstandbies.cache.tongdun.net   2026-05-28T15:29:01Z

 

[root@master1 stateful]# kubectl api-resources |grep redis
redisclusters                                    cache.tongdun.net/v1alpha1        true         RedisCluster
redisstandbies                                   cache.tongdun.net/v1alpha1        true         RedisStandby

#基于Operator安装redis主从和哨兵

wget https://raw.githubusercontent.com/tongdun/td-redisoperator/main/cr/redis_standby.yaml

 

#创建redis主从资源清单文件

 

[root@master1 ~]#vim redis_standby.yaml 

apiVersion: cache.tongdun.net/v1alpha1
kind: RedisStandby
metadata:
 name: redis-standby-tom
 namespace: redis
spec:
 app: standby-tom
 capacity: 2048
 dc: hz
 env: demo
 image: tongduncloud/redis-standby:1.0
 monitorimage: tongduncloud/redis-exporter:1.0
  #netmode: ClusterIP
 netmode: NodePort   #修改SVC类型
 realname: sa
 secret: "123456"    #指定连接密码
 sentinelimage: tongduncloud/sentinel-standby:latest
 storageclass: "sc-nfs" #指定storageclass
 vip: 172.17.128.8

 

#参数说明
https://github.com/tongdun/td-redis-operator/wiki/RedisStandby%E4%BA%A4%E4%BB%98%E4%BB%8B%E7%BB%8D
app           关联应用
capacity     容量,单位Mb
dc           机房
env           可选(demo,production)
image         Redis镜像
monitorimage exporter镜像
netmode       网络模式,可选(ClusterIP、NodePort)
realname     负责人
secret       密码
sentinelimage 哨兵镜像
storageclass PVC
vip           外部网络访问地址,无实际功能

 

root@master1 stateful]# cat redis_standby.yaml    
apiVersion: cache.tongdun.net/v1alpha1
kind: RedisStandby
metadata:
  name: redis-standby-tom
  namespace: redis
spec:
  app: standby-tom
  capacity: 2048
  dc: guangzhou
  env: demo
  image: tongduncloud/redis-standby:1.0
  monitorimage: tongduncloud/redis-exporter:1.0
  netmode: NodePort
  realname: sa
  secret: "123456"
  sentinelimage: tongduncloud/sentinel-standby:latest
  storageclass: "sc-nfs"
  vip: 172.17.128.8

 

[root@master1 stateful]# kubectl get pod -n redis
NAME                        READY   STATUS    RESTARTS   AGE
operator-66c655b5c8-rhrfc   1/1     Running   0          7m51s

 

kubectl apply -f redis_standby.yaml 

 

创建出来是 redis主从架构带哨兵

一主一从3个哨兵

#查看生成的Pod

kubectl get pod -n redis -o wide

[root@master1 stateful]# kubectl get pod -o wide -n redis
NAME                        READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
operator-66c655b5c8-rhrfc   1/1     Running   0          48m   10.244.1.49   node1.org   <none>           <none>
redis-standby-tom-0         2/2     Running   0          57s   10.244.2.43   node2.org   <none>           <none>
redis-standby-tom-1         1/2     Running   0          36s   10.244.1.55   node1.org   <none>           <none>
sentinel-standby-tom-0      1/1     Running   0          57s   10.244.1.53   node1.org   <none>           <none>
sentinel-standby-tom-1      1/1     Running   0          54s   10.244.2.44   node2.org   <none>           <none>
sentinel-standby-tom-2      1/1     Running   0          52s   10.244.1.54   node1.org   <none>           <none>

 

 

#查看PVC和PV

pvc pv 自动创建 自动绑定

[root@master1 stateful]# kubectl get pvc,pv -n redis     
NAME                                                   STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
persistentvolumeclaim/redis-data-redis-standby-tom-0   Bound    pvc-34274930-2b20-4328-8189-b5c2ba2d3a0f   64Gi       RWO            sc-nfs         <unset>                 93s
persistentvolumeclaim/redis-data-redis-standby-tom-1   Bound    pvc-3cf4c53c-0432-4265-bcff-d4c5ea6e7d84   64Gi       RWO            sc-nfs         <unset>                 72s

NAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                  STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
persistentvolume/pvc-34274930-2b20-4328-8189-b5c2ba2d3a0f   64Gi       RWO            Delete           Bound    redis/redis-data-redis-standby-tom-0   sc-nfs         <unset>                          93s
persistentvolume/pvc-3cf4c53c-0432-4265-bcff-d4c5ea6e7d84   64Gi       RWO            Delete           Bound    redis/redis-data-redis-standby-tom-1   sc-nfs         <unset>                          72s

 

[root@master1 stateful]# ls /data/sc-nfs/
redis-redis-data-redis-standby-tom-0-pvc-34274930-2b20-4328-8189-b5c2ba2d3a0f
redis-redis-data-redis-standby-tom-1-pvc-3cf4c53c-0432-4265-bcff-d4c5ea6e7d84
[root@master1 stateful]# 

 

测试功能

#测试主从复制功能
#连接到第一个redis节点

kubectl exec -it -n redis redis-standby-tom-0 -c redis-standby-tom -- sh

[root@master1 stateful]# kubectl exec -it -n redis redis-standby-tom-0 -c redis-standby-tom -- sh
sh-4.2# 
sh-4.2# redis-cli -a 123456 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> 

 

查看主从复制信息

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.244.1.55,port=6379,state=online,offset=44417,lag=0
master_replid:1c60033a39a97d76e4d148b4053f97834cc02082
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:44417
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:134217728
repl_backlog_first_byte_offset:1
repl_backlog_histlen:44417

 

[root@master1 ~]# kubectl get pod -o wide -n redis
NAME                        READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
operator-66c655b5c8-rhrfc   1/1     Running   0          53m     10.244.1.49   node1.org   <none>           <none>
redis-standby-tom-0         2/2     Running   0          5m10s   10.244.2.43   node2.org   <none>           <none>
redis-standby-tom-1         1/2     Running   0          4m49s   10.244.1.55   node1.org   <none>           <none>
sentinel-standby-tom-0      1/1     Running   0          5m10s   10.244.1.53   node1.org   <none>           <none>
sentinel-standby-tom-1      1/1     Running   0          5m7s    10.244.2.44   node2.org   <none>           <none>
sentinel-standby-tom-2      1/1     Running   0          5m5s    10.244.1.54   node1.org   <none>           <none>

 

127.0.0.1:6379> role
1) "master"
2) (integer) 72333
3) 1) 1) "10.244.1.55"
      2) "6379"
      3) "72333"

写数据

127.0.0.1:6379> 
127.0.0.1:6379> set name ming
OK
127.0.0.1:6379> get name
"ming"

 

#连接到第二个redis节点

kubectl exec -it -n redis redis-standby-tom-1 -c redis-standby-tom -- sh

root@master1 stateful]# kubectl exec -it -n redis redis-standby-tom-1 -c redis-standby-tom -- sh
sh-4.2# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> 
127.0.0.1:6379> role
1) "slave"
2) "10.244.2.43"
3) (integer) 6379
4) "connected"
5) (integer) 92372
127.0.0.1:6379> 

 

127.0.0.1:6379>  info replication
# Replication
role:slave
master_host:10.244.2.43
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:101108
slave_priority:100
slave_read_only:0
connected_slaves:0
master_replid:1c60033a39a97d76e4d148b4053f97834cc02082
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:101108
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:134217728
repl_backlog_first_byte_offset:1
repl_backlog_histlen:101108

 

127.0.0.1:6379> get name
"ming"

 

 

[root@master1 stateful]# kubectl get pod -o wide -n redis
NAME                        READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
operator-66c655b5c8-rhrfc   1/1     Running   0          57m     10.244.1.49   node1.org   <none>           <none>
redis-standby-tom-0         2/2     Running   0          9m55s   10.244.2.43   node2.org   <none>           <none>
redis-standby-tom-1         1/2     Running   0          9m34s   10.244.1.55   node1.org   <none>           <none>
sentinel-standby-tom-0      1/1     Running   0          9m55s   10.244.1.53   node1.org   <none>           <none>
sentinel-standby-tom-1      1/1     Running   0          9m52s   10.244.2.44   node2.org   <none>           <none>
sentinel-standby-tom-2      1/1     Running   0          9m50s   10.244.1.54   node1.org   <none>           <none>
[root@master1 stateful]# 
[root@master1 stateful]# 
[root@master1 stateful]# ls /data/sc-nfs/
redis-redis-data-redis-standby-tom-0-pvc-34274930-2b20-4328-8189-b5c2ba2d3a0f
redis-redis-data-redis-standby-tom-1-pvc-3cf4c53c-0432-4265-bcff-d4c5ea6e7d84
[root@master1 stateful]# ls /data/sc-nfs/redis-redis-data-redis-standby-tom-0-pvc-34274930-2b20-4328-8189-b5c2ba2d3a0f
dump.rdb  redis.pid
[root@master1 stateful]# 
[root@master1 stateful]# 
[root@master1 stateful]# ls /data/sc-nfs/redis-redis-data-redis-standby-tom-1-pvc-3cf4c53c-0432-4265-bcff-d4c5ea6e7d84
dump.rdb  redis.pid
[root@master1 stateful]# 

 

root@master1 stateful]# ll /data/sc-nfs/redis-redis-data-redis-standby-tom-0-pvc-34274930-2b20-4328-8189-b5c2ba2d3a0f
total 8
-rw-r--r-- 1 root root 175 May 29 00:17 dump.rdb
-rw-r--r-- 1 root root   2 May 29 00:17 redis.pid
[root@master1 stateful]# 
[root@master1 stateful]# ll /data/sc-nfs/redis-redis-data-redis-standby-tom-1-pvc-3cf4c53c-0432-4265-bcff-d4c5ea6e7d84
total 8
-rw-r--r-- 1 root root 175 May 29 00:17 dump.rdb
-rw-r--r-- 1 root root   2 May 29 00:17 redis.pid

 

清理环境

kubectl delete -f redis_standby.yaml

pvc pv 不会自动删除 会保留

需要手动删除pvc,删除pvc,pv也会自动删除

[root@master1 stateful]# kubectl get pvc -n redis
NAME                             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
redis-data-redis-standby-tom-0   Bound    pvc-34274930-2b20-4328-8189-b5c2ba2d3a0f   64Gi       RWO            sc-nfs         <unset>                 16m
redis-data-redis-standby-tom-1   Bound    pvc-3cf4c53c-0432-4265-bcff-d4c5ea6e7d84   64Gi       RWO            sc-nfs         <unset>                 15m

kubectl delete pvc --all  -n redis

 

 

posted @ 2026-05-29 00:27  minger_lcm  阅读(9)  评论(0)    收藏  举报