Kubernetes Redis 高可用方案

Redis 高可用方案常用的有两种:Redis Sentinel 和 Redis Cluster ,本篇笔记介绍这两种方案如何在 Kubernetes 中部署。在 Kubernetes 里部署服务通常有三种方式:自己手写 Kubernetes 资源描述文件(Manifests YAML)、Helm Chart 和 Operator 。手写资源描述文件需要对配置非常熟悉才能写的好,对于初学者一般采用 Helm 或者 Operator 方式安装,这里采用 Operator ,之所以选择 Operator ,是因为本着最简安装的原则,使用 Operator 方式安装可以少安装一个组件 Helm 。

Redis Sentinel

https://github.com/spotahome/redis-operator

安装 Operator 以及相关权限:

kubectl create -f https://raw.githubusercontent.com/spotahome/redis-operator/master/example/operator/all-redis-operator-resources.yaml
kubectl create -f https://raw.githubusercontent.com/spotahome/redis-operator/master/example/redisfailover/basic.yaml
apiVersion: databases.spotahome.com/v1
kind: RedisFailover
metadata:
  name: redisfailover
spec:
  sentinel:
    replicas: 3
    resources:
      requests:
        cpu: 100m
      limits:
        memory: 100Mi
  redis:
    replicas: 3
    resources:
      requests:
        cpu: 100m
        memory: 100Mi
      limits:
        cpu: 400m
        memory: 500Mi

Redis Cluster

https://github.com/ucloud/redis-cluster-operator

测试

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: redis-master
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
      role: master
      tier: backend
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
        role: master
        tier: backend
    spec:
      containers:
      - name: master
        image: redis:5.0-alpine  # or just image: redis
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 6379
NAME                                      READY   STATUS    RESTARTS   AGE
drc-example-distributedrediscluster-0-0   1/1     Running   0          40m
drc-example-distributedrediscluster-0-1   1/1     Running   0          39m
drc-example-distributedrediscluster-1-0   1/1     Running   0          40m
drc-example-distributedrediscluster-1-1   1/1     Running   0          39m
drc-example-distributedrediscluster-2-0   1/1     Running   0          40m
drc-example-distributedrediscluster-2-1   1/1     Running   0          39m
redis-cluster-operator-fd6fb58c4-m977x    1/1     Running   0          75m
redis-master-5d7f574d67-qjpxr             1/1     Running   0          27m
kubectl exec -it redis-master-5d7f574d67-qjpxr sh
redis-cli -h example-distributedrediscluster

注意:-c 这个参数是需要加上的,否则会报错,因为按照分配算法需要 redis-cli 能够对 Cluster 自动寻址。

你可能感兴趣的:(Kubernetes Redis 高可用方案)