Helm部署Redis(哨兵)

添加Helm仓库

sh 复制代码
[root@node1 ~]# helm repo add bitnami https://charts.bitnami.com/bitnami

查找Redis相关Chart

sh 复制代码
[root@node1 ~]# helm search repo redis
NAME                    CHART VERSION   APP VERSION     DESCRIPTION                                       
bitnami/redis           18.1.2          7.2.1           Redis(R) is an open source, advanced key-value ...
bitnami/redis-cluster   9.0.7           7.2.1           Redis(R) is an open source, scalable, distribut...

下载部署Redis使用的Chart

sh 复制代码
[root@node1 redis]# helm fetch bitnami/redis
[root@node1 redis]# ls
redis-18.1.2.tgz
[root@node1 redis]# 

解压缩

sh 复制代码
[root@node1 redis]# tar -xf redis-18.1.2.tgz 
[root@node1 redis]# ls
redis  redis-18.1.2.tgz
[root@node1 redis]# cd redis
[root@node1 redis]# ls
Chart.lock  charts  Chart.yaml  img  README.md  templates  values.schema.json  values.yaml
[root@node1 redis]# 

修改values.yaml文件的配置

yaml 复制代码
global:
  imageRegistry: ""
  ## E.g.
  ## imagePullSecrets:
  ##   - myRegistryKeySecretName
  ##
  imagePullSecrets: []
  storageClass: "rook-ceph-block"  # 此处修改为K8s集群上的storageClass名称
  redis:
    password: "pass_123456"        # 连接Redis所使用的密码
    
 
sentinel:
  ## @param sentinel.enabled Use Redis® Sentinel on Redis® pods.
  ## IMPORTANT: this will disable the master and replicas services and
  ## create a single Redis® service exposing both the Redis and Sentinel ports
  ##
  enabled: true  # 开启sentinel哨兵模式 

安装Redis复制集群

sh 复制代码
[root@node1 redis]# helm install redis . -f values.yaml 
NAME: redis
LAST DEPLOYED: Wed Oct  4 21:06:27 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: redis
CHART VERSION: 18.1.2
APP VERSION: 7.2.1

** 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.default.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 default 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 default redis-client --restart='Never'  --env REDIS_PASSWORD=$REDIS_PASSWORD  --image docker.io/bitnami/redis:7.2.1-debian-11-r0 --command -- sleep infinity

   Use the following command to attach to the pod:

   kubectl exec --tty -i redis-client \
   --namespace default -- 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 default svc/redis 6379:6379 &
    REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h 127.0.0.1 -p 6379

查看Pod部署情况

sh 复制代码
[root@node1 redis]# kubectl get pods 
NAME           READY   STATUS    RESTARTS   AGE
redis-node-0   2/2     Running   0          6m15s
redis-node-1   2/2     Running   0          4m58s
redis-node-2   2/2     Running   0          3m9s

查看Service

sh 复制代码
[root@node1 redis]# kubectl get svc
NAME             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)              AGE
kubernetes       ClusterIP   10.96.0.1      <none>        443/TCP              5d14h
redis            ClusterIP   10.96.225.57   <none>        6379/TCP,26379/TCP   8m7s
redis-headless   ClusterIP   None           <none>        6379/TCP,26379/TCP   8m7s

连接测试

sh 复制代码
kubectl run --namespace default redis-client --restart='Never'  --env REDIS_PASSWORD=$REDIS_PASSWORD  --image docker.io/bitnami/redis:7.2.1-debian-11-r0 --command -- sleep infinity
   
   
[root@node1 redis]#    kubectl exec --tty -i redis-client \
   --namespace default -- bash   
I have no name!@redis-client:/$  redis-cli -h redis-node-0.redis-headless.default.svc.cluster.local -p 6379 -a pass_123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
redis-node-0.redis-headless.default.svc.cluster.local:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=redis-node-1.redis-headless.default.svc.cluster.local,port=6379,state=online,offset=68433,lag=0
slave1:ip=redis-node-2.redis-headless.default.svc.cluster.local,port=6379,state=online,offset=68433,lag=1
master_failover_state:no-failover
master_replid:6010cb66d9586e7518d2ec1572b59e8577373bb2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:68433
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:68433
redis-node-0.redis-headless.default.svc.cluster.local:6379> 

访问redis-node-0.redis-headless.default.svc.cluster.local即可连接到master

更新集群信息

当修改values.yaml文件后,可以使用如下命令修改部署的集群信息

sh 复制代码
[root@node1 redis]# helm upgrade -f values.yaml redis .

参考文档

相关推荐
码拉松1 小时前
千万不要错过,优惠券设计与思考初探
后端·面试·架构
白总Server1 小时前
MongoDB解说
开发语言·数据库·后端·mongodb·golang·rust·php
计算机学姐2 小时前
基于python+django+vue的家居全屋定制系统
开发语言·vue.js·后端·python·django·numpy·web3.py
程序员-珍2 小时前
SpringBoot v2.6.13 整合 swagger
java·spring boot·后端
海里真的有鱼2 小时前
好文推荐-架构
后端
骆晨学长3 小时前
基于springboot的智慧社区微信小程序
java·数据库·spring boot·后端·微信小程序·小程序
AskHarries3 小时前
利用反射实现动态代理
java·后端·reflect
Flying_Fish_roe3 小时前
Spring Boot-Session管理问题
java·spring boot·后端
hai405874 小时前
Spring Boot中的响应与分层解耦架构
spring boot·后端·架构
Adolf_19936 小时前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask