1. 程式人生 > 實用技巧 >Kubernetes 安裝Redis叢集

Kubernetes 安裝Redis叢集

一、Operator

https://github.com/operator-framework/awesome-operators

二、安裝Redis叢集的operator

# git clone https://github.com/ucloud/redis-cluster-operator.git

# kubectl create -f deploy/crds/redis.kun_distributedredisclusters_crd.yaml

# kubectl create -f deploy/crds/redis.kun_redisclusterbackups_crd.yaml

  

以叢集模式安裝

# 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.yam

  可以在service_account和 operator後面加-n 引數指定namespace

三、建立redis叢集

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

這個yaml檔案中使用的資源較高,測試環境可能起不來,可以換一個

kubectl create -f deploy/example/custom-resources.yaml

  

檢視Pod

# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
drc-example-distributedrediscluster-0-0   1/1     Running   0          3m33s
drc-example-distributedrediscluster-0-1   1/1     Running   0          2m59s
drc-example-distributedrediscluster-1-0   1/1     Running   0          3m33s
drc-example-distributedrediscluster-1-1   1/1     Running   0          2m54s
drc-example-distributedrediscluster-2-0   1/1     Running   0          3m33s
drc-example-distributedrediscluster-2-1   1/1     Running   0          2m59s

  通過節點 親和性,確保redis分佈在不同節點上

檢視service

# kubectl get svc
NAME                                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)              AGE
example-distributedrediscluster     ClusterIP   10.106.160.212   <none>        6379/TCP,16379/TCP   4m14s
example-distributedrediscluster-0   ClusterIP   None             <none>        6379/TCP,16379/TCP   4m15s
example-distributedrediscluster-1   ClusterIP   None             <none>        6379/TCP,16379/TCP   4m15s
example-distributedrediscluster-2   ClusterIP   None             <none>        6379/TCP,16379/TCP   4m15s

  應用程式可以使用example-distributedrediscluster來連線叢集

四、自定義Redis叢集配置

custom-redis.yaml

apiVersion: v1
kind: Secret
metadata:
  annotations:
    # if your operator run as cluster-scoped, add this annotations
    redis.kun/scope: cluster-scoped
  name: mysecret
type: Opaque
data:
  password: MWYyZDFlMmU2N2Rm
---
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:
  image: redis:5.0.4-alpine
  masterSize: 3
  clusterReplicas: 1
  passwordSecret:
      name: mysecret
  resources:
    limits:
      cpu: 200m
      memory: 200Mi
    requests:
      cpu: 200m
      memory: 100Mi
  storage:
    type: persistent-claim
    size: 1Gi
    class: rook-ceph-block
    deleteClaim: true

  

  • password是以base64加密的,並以環境變數的形式注入Pod中

  • 需要提前建立storageClass,class即為storageclass的名稱

修改時區

# kubectl get sts
NAME                                    READY   AGE
drc-example-distributedrediscluster-0   2/2     80m
drc-example-distributedrediscluster-1   2/2     80m
drc-example-distributedrediscluster-2   2/2     80m

  

修改statefulset,volumeMounts和volumes

省略其他配置資訊

        volumeMounts:
        - mountPath: /usr/share/zoneinfo/Asia/Shanghai
          name: tz-config
        - mountPath: /etc/localtime
          name: tz-config
          
          
      volumes:
      - hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
          type: ""
        name: tz-config

  檢視

kubectl exec -it drc-example-distributedrediscluster-0-1 -- date
Thu Oct 29 16:45:50 CST 2020

redis-cli -a 1f2d1e2e67df