helm 安装 Redis 1 主 2 从 3哨兵

资源清单

本文安装 Redis 依赖 K8S集群helm,本文不提供 K8S集群helm 安装方式

使用此文档部署,需要自行解决 storageClass 问题 ( NFS, ceph, openebs等 )

软件 版本
chart 16.11.2
redis 6.2.7
kubernetes v1.19.3
helm v3.8.1

helm 安装 redis 集群

1. 添加 redis 的仓库

$ helm repo add bitnami https://charts.bitnami.com/bitnami

2. 查询 redis 资源

$ helm repo update
$ helm search repo redis
NAME                           	CHART VERSION	APP VERSION  	DESCRIPTION                                       
bitnami/redis                  	16.11.2      	6.2.7        	Redis(R) is an open source, advanced key-value ...
bitnami/redis-cluster          	7.6.1        	6.2.7        	Redis(TM) is an open source, scalable, distribu...

3. 拉取 redis chart 到本地

$ mkdir -p /root/redis/ && cd /root/redis/

# 拉取 chart 到本地 /root/redis/ 目录
$ helm pull bitnami/redis --version 16.11.2

$ tar -zxvf redis-16.11.2.tgz
$ cp redis/values.yaml ./values-test.yaml

# 查看当前目录层级
$ tree -L 2
.
├── redis
│   ├── Chart.lock
│   ├── charts
│   ├── Chart.yaml
│   ├── img
│   ├── README.md
│   ├── templates
│   ├── values.schema.json
│   └── values.yaml
├── redis-16.11.2.tgz
└── values-test.yaml

4. 对本地 values-test.yaml 修改

  • 查看集群 storageclasses
$ kubectl get storageclasses.storage.k8s.io 
NAME                   PROVISIONER           RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
openebs-jiva-default   jiva.csi.openebs.io   Delete          Immediate              true                   33d
  • 修改配置
$ cat values-test.yaml 

global:
  # 全局定义 storageClass 使用 openebs
  storageClass: "openebs-jiva-default"
  # 定义 redis 集群认证密码
  redis:
    password: "redis123"


fullnameOverride: "redis"


# 定义集群的模式  Allowed values: `standalone` or `replication`
architecture: replication


# redis 服务配置定义
commonConfiguration: |-
  # Enable AOF https://redis.io/topics/persistence#append-only-file
  appendonly yes
  # Disable RDB persistence, AOF persistence already enabled.
  save ""


# master 节点配置信息
master:
  containerPorts:
    redis: 6379

  kind: StatefulSet

  persistence:
    enabled: true
    accessModes:
      - ReadWriteOnce
    size: 8Gi

  service:
    type: ClusterIP
    ports:
      redis: 6379


# replica 节点配置信息
replica:
  replicaCount: 3
  containerPorts:
    redis: 6379

  persistence:
    enabled: true
    storageClass: ""
    accessModes:
      - ReadWriteOnce
    size: 8Gi

  service:
    type: ClusterIP
    ports:
      redis: 6379


# sentinel 节点配置信息
sentinel:
  enabled: true
  containerPorts:
    sentinel: 26379

  persistence:
    enabled: true
    storageClass: ""
    accessModes:
      - ReadWriteOnce
    size: 100Mi

  service:
    type: ClusterIP
    ports:
      redis: 6379
      sentinel: 26379

5. 安装 redis 集群

# 创建 test-middleware 名称空间
$ kubectl create ns test-middleware

# 安装 redis 集群
$ helm -n test-middleware install redis  redis -f values-test.yaml | tee test.log

## helm -n NAMESAPCE install SERVER_NAME FILE_NAME -f CONFIG_FILE
-n 指定 kubernetes 集群名称空间
-f 指定使用的配置文件,文件中定义的配置可以覆盖 redis/values.yaml 文件中配置

NAME: redis
LAST DEPLOYED: Sat Jun  4 06:05:40 2022
NAMESPACE: test-middleware
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: redis
CHART VERSION: 16.11.2
APP VERSION: 6.2.7

** Please be patient while the chart is being deployed **

Redis® can be accessed via port 6379 on the following DNS name from within your cluster:

    redis.test-middleware.svc.cluster.local for read only operations

For read/write operations, first access the Redis® Sentinel cluster, which is available in port 26379 using the same domain name above.



To get your password run:

    export REDIS_PASSWORD=$(kubectl get secret --namespace test-middleware redis -o jsonpath="{.data.redis-password}" | base64 -d)

To connect to your Redis® server:

1. Run a Redis® pod that you can use as a client:

   kubectl run --namespace test-middleware redis-client --restart='Never'  --env REDIS_PASSWORD=$REDIS_PASSWORD  --image docker.io/bitnami/redis:6.2.7-debian-10-r23 --command -- sleep infinity

   Use the following command to attach to the pod:

   kubectl exec --tty -i redis-client \
   --namespace test-middleware -- bash

2. Connect using the Redis® CLI:
   REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis -p 6379 # Read only operations
   REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis -p 26379 # Sentinel access

To connect to your database from outside the cluster execute the following commands:

    kubectl port-forward --namespace test-middleware svc/redis 6379:6379 &
    REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h 127.0.0.1 -p 6379

6. 查看部署的 redis 集群

$ helm -n test-middleware list
NAME         	NAMESPACE      	REVISION	UPDATED                                	STATUS  	CHART        	APP VERSION
redis        	test-middleware	1       	2022-06-04 06:05:40.507941913 -0400 EDT	deployed	redis-16.11.2	6.2.7 


$ kubectl get pods --namespace=test-middleware
NAME                           READY   STATUS    RESTARTS   AGE
redis-node-0                   2/2     Running   0          3h45m
redis-node-1                   2/2     Running   0          3h44m
redis-node-2                   2/2     Running   0          3h42m
  • 查看 pvc
$ kubectl -n test-middleware get pvc
NAME                                STATUS        VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS           AGE
redis-data-redis-node-0             Bound         pvc-8bdc6235-be2a-488c-bc3b-a21ba6309a10   8Gi        RWO            openebs-jiva-default   3h46m
redis-data-redis-node-1             Bound         pvc-1ce07a36-a3ab-44d1-85b5-265059886701   8Gi        RWO            openebs-jiva-default   3h44m
redis-data-redis-node-2             Bound         pvc-3a897609-981e-4d99-a12d-afb5e2123126   8Gi        RWO            openebs-jiva-default   3h42m
sentinel-data-redis-node-0          Bound         pvc-43f829ef-c0e0-4ccc-a638-0f377c2d984f   100Mi      RWO            openebs-jiva-default   3h46m
sentinel-data-redis-node-1          Bound         pvc-2f640247-6fcd-4edd-a8d4-e26bc2460ef0   100Mi      RWO            openebs-jiva-default   3h44m
sentinel-data-redis-node-2          Bound         pvc-1e87b557-d484-40c6-8853-6e24d5542490   100Mi      RWO            openebs-jiva-default   3h42m

7. 验证 redis 集群

# 登陆 redis 集群
$ export REDIS_PASSWORD=$(kubectl get secret --namespace test-middleware redis -o jsonpath="{.data.redis-password}" | base64 -d)

$ kubectl run --namespace test-middleware redis-client --restart='Never'  --env REDIS_PASSWORD=$REDIS_PASSWORD  --image docker.io/bitnami/redis:6.2.7-debian-10-r23 --command -- sleep infinity

$ kubectl exec --tty -i redis-client --namespace test-middleware -- bash


# 查看 redis 主从集群
I have no name!@redis-client:/$ REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis -p 6379

redis:6379> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=redis-node-1.redis-headless.test-middleware.svc.cluster.local,port=6379,state=online,offset=4482538,lag=0
slave1:ip=redis-node-2.redis-headless.test-middleware.svc.cluster.local,port=6379,state=online,offset=4482538,lag=1
master_failover_state:no-failover
master_replid:8e48433d13ae59dbe2703a704504a98ecc61076a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4482538
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:3433963
repl_backlog_histlen:1048576


# 查看 Sentinel 节点
I have no name!@redis-client:/$ REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h redis-headless -p 26379

redis-headless:26379> info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=redis-node-0.redis-headless.test-middleware.svc.cluster.local:6379,slaves=2,sentinels=3

参考文档

https://artifacthub.io/packages/helm/bitnami/redis
posted @ 2022-06-04 22:03  evescn  阅读(2291)  评论(0编辑  收藏  举报