简介:由于redis集群的搭建是较为复杂的工作,手动搭建难以实现扩容缩容。推荐采用operator的方式搭建,更加方便,也更不容易出错。
原文档:https://github.com/ucloud/redis-cluster-operator
概述
Redis 集群 Operator在Kubernetes之上管理Redis 集群。
Operator本身是用Operator framework构建的。
每个主节点及其从属节点由一个statefulSet管理,为每个statefulSet创建一个无头svc,并为所有节点创建一个clusterIP服务。
每个statefulset使用PodAntiAffinity来确保主节点和从节点分散在不同的节点上。
同时,当operator在每个statefulset中选择master时,会优先选择不同k8s节点的pod作为master 。
环境
- go version v1.13+
- Kubernetes v1.13.10 集群
特点
- 自定义主节点的数量和每个主节点的复制节点的数量
- 密码
- 安全地扩展 Redis 集群
- 备份和恢复
- 持久卷
- 自定义配置
- Prometheus监控
操作方法
部署redis集群的 operator
安装
注册DistributedRedisCluster和RedisClusterBackup的自定义资源定义(custom resource definition CRD)。
1
2
|
kubectl create -f deploy/crds/redis.kun_distributedredisclusters_crd.yaml
kubectl create -f deploy/crds/redis.kun_redisclusterbackups_crd.yaml
|
namespace-scoped的operator监控和管理单个namespace的资源,而cluster-scoped的operator监控和管理整个集群的资源。
你可以选择执行你的 operator 以 namespace-scoped 的方式或者以 cluster-scoped 的方式:
(推荐使用cluster-scoped的方式,使得 operator 在整个k8s集群生效)
1
2
3
4
5
6
7
8
9
10
11
|
# cluster-scoped
kubectl create -f deploy/service_account.yaml
kubectl create -f deploy/cluster/cluster_role.yaml
kubectl create -f deploy/cluster/cluster_role_binding.yaml
kubectl create -f deploy/cluster/operator.yaml
# namespace-scoped
kubectl create -f deploy/service_account.yaml
kubectl create -f deploy/namespace/role.yaml
kubectl create -f deploy/namespace/role_binding.yaml
kubectl create -f deploy/namespace/operator.yaml
|
使用 helm chart 安装
添加 Helm 仓库
1
2
|
helm repo add ucloud-operator https://ucloud.github.io/redis-cluster-operator/
helm repo update
|
安装 chart
1
|
helm install --generate-name ucloud-operator/redis-cluster-operator
|
验证 redis-cluster-operator 已经启动和运行
1
2
3
|
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
redis-cluster-operator 1/1 1 1 1d
|
使用方式
部署一个简单的Redis集群
只有使用持久性存储(pvc)的redis集群才能在意外删除或滚动更新后恢复。即使你不使用持久性(如rdb或aof),你也需要为redis设置pvc。
1
|
$ kubectl apply -f deploy/example/redis.kun_v1alpha1_distributedrediscluster_cr.yaml
|
验证Cluster实例和它的组件都已经开始运行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
$ kubectl get distributedrediscluster
NAME MASTERSIZE STATUS AGE
example-distributedrediscluster 3 Scaling 11s
$ kubectl get all -l redis.kun/name=example-distributedrediscluster
NAME READY STATUS RESTARTS AGE
pod/drc-example-distributedrediscluster-0-0 1/1 Running 0 2m48s
pod/drc-example-distributedrediscluster-0-1 1/1 Running 0 2m8s
pod/drc-example-distributedrediscluster-1-0 1/1 Running 0 2m48s
pod/drc-example-distributedrediscluster-1-1 1/1 Running 0 2m13s
pod/drc-example-distributedrediscluster-2-0 1/1 Running 0 2m48s
pod/drc-example-distributedrediscluster-2-1 1/1 Running 0 2m15s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/example-distributedrediscluster ClusterIP 172.17.132.71 <none> 6379/TCP,16379/TCP 2m48s
service/example-distributedrediscluster-0 ClusterIP None <none> 6379/TCP,16379/TCP 2m48s
service/example-distributedrediscluster-1 ClusterIP None <none> 6379/TCP,16379/TCP 2m48s
service/example-distributedrediscluster-2 ClusterIP None <none> 6379/TCP,16379/TCP 2m48s
NAME READY AGE
statefulset.apps/drc-example-distributedrediscluster-0 2/2 2m48s
statefulset.apps/drc-example-distributedrediscluster-1 2/2 2m48s
statefulset.apps/drc-example-distributedrediscluster-2 2/2 2m48s
$ kubectl get distributedrediscluster
NAME MASTERSIZE STATUS AGE
example-distributedrediscluster 3 Healthy 4m
|
扩大Redis集群的规模
增加masterSize以触发缩放:
1
2
3
4
5
6
7
8
9
10
11
12
|
apiVersion: redis.kun/v1alpha1
kind: DistributedRedisCluster
metadata:
annotations:
# if your operator run as cluster-scoped, add this annotations
redis.kun/scope: cluster-scoped
name: example-distributedrediscluster
spec:
# 增加masterSize以触发缩放
masterSize: 4
ClusterReplicas: 1
image: redis:5.0.4-alpine
|
缩小Redis集群的规模
减小masterSize以触发缩放:
1
2
3
4
5
6
7
8
9
10
11
12
|
apiVersion: redis.kun/v1alpha1
kind: DistributedRedisCluster
metadata:
annotations:
# if your operator run as cluster-scoped, add this annotations
redis.kun/scope: cluster-scoped
name: example-distributedrediscluster
spec:
# 减小masterSize以触发缩放
masterSize: 3
ClusterReplicas: 1
image: redis:5.0.4-alpine
|
其他的自定义功能
备份和恢复
注:现在只支持Ceph S3对象存储和PVC
备份
1
|
kubectl create -f deploy/example/backup-restore/redisclusterbackup_cr.yaml
|
从备份中恢复
1
|
kubectl create -f deploy/example/backup-restore/restore.yaml
|
Prometheus 监控
1
|
kubectl create -f deploy/example/prometheus-exporter.yaml
|
创建带有密码的 redis 集群
1
|
kubectl create -f deploy/example/custom-password.yaml
|
持久化 Volume
1
|
kubectl create -f deploy/example/persistent.yaml
|
自定义 Configuration
1
|
kubectl create -f deploy/example/custom-config.yaml
|
自定义 Service
1
|
kubectl create -f deploy/example/custom-service.yaml
|
自定义 Resource (资源)
1
|
kubectl create -f deploy/example/custom-resources.yaml
|
验证Webhook
验证Webhook
端到端测试
端到端测试