开源地址:https://github.com/ucloud/redis-cluster-operator

一、概述

Redis Cluster Operator用于管理基于k8s的Redis Cluster
该operator基于Operator framework之上(https://github.com/operator-framework/operator-sdk)
在这里插入图片描述
每个master node和slave node由一个statefulset管理,每个statefulset创建一个headless svc, 所有的node创建一个clusterIP service。

每个statefulset使用PodAntiAffinity来确保master和slave部署在不同的node上。同时,当operator在一个statefulset上选择master时,他优先选择不同k8s nodes上的pod作为master。

二、Features

  • 自定义master nodes的数量和replica nodes的数量
  • Password
  • 安全地扩容Redis Cluster
  • 备份和恢复
  • 持久化卷
  • 自定义配置
  • Prometheus发现

三、使用redis-cluster-operator

3.1、部署一个redis cluster operator

注册CRD(Custom Resource Definition):

$ kubectl create -f deploy/crds/redis.kun_distributedredisclusters_crd.yaml
$ kubectl create -f deploy/crds/redis.kun_redisclusterbackups_crd.yaml

关键配置:

masterSize:
format: int32
type: integer
minimum: 3
maximum: 10
clusterReplicas:
format: int32
type: integer
minimum: 1
maximum: 3
serviceName:
type: string
pattern: '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*'

CRD的生命周期包括namespace和cluster,cluster > namespace:

// 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

这样查看redis-cluster-operator是否运行:

$ kubectl get deployment
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
redis-cluster-operator   1/1     1            1           1d

部署一个redis cluster:

$ kubectl apply -f deploy/example/redis.kun_v1alpha1_distributedrediscluster_cr.yaml

扩容Redis Cluster:

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:
  # Increase the masterSize to trigger the scaling.
  masterSize: 4
  ClusterReplicas: 1
  image: redis:5.0.4-alpine

缩容Redis Cluster:

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:
  # Increase the masterSize to trigger the scaling.
  masterSize: 3
  ClusterReplicas: 1
  image: redis:5.0.4-alpine
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐