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 .

参考文档

相关推荐
一只叫煤球的猫18 分钟前
手撕@Transactional!别再问事务为什么失效了!Spring-tx源码全面解析!
后端·spring·面试
旷世奇才李先生31 分钟前
Ruby 安装使用教程
开发语言·后端·ruby
沃夫上校3 小时前
Feign调Post接口异常:Incomplete output stream
java·后端·微服务
LeeGe3 小时前
SpringAOP中@within和@annotation以及 @within和@target的区别
后端
一个平平无奇的Java小学生3 小时前
Spring Cloud Alibaba 微服务从入门到生产部署完整指南
后端
一个平平无奇的Java小学生4 小时前
Spring Cloud Alibaba 微服务实战指南
后端
张小洛4 小时前
Spring IOC容器核心阶段解密:★Bean实例化全流程深度剖析★
java·后端·spring·ioc容器·bean实例化
小王子10244 小时前
Django+DRF 实战:从异常捕获到自定义错误信息
后端·django·web开发
hdsoft_huge4 小时前
Spring Boot 高并发框架实现方案:数字城市的奇妙之旅
java·spring boot·后端
00后程序员5 小时前
WebView 无法调用原生分享功能?调试复现与异常排查全过程
后端