# Helm 安装 Redis 哨兵主从高可用

> 参考:  <https://www.kubernetes.org.cn/7659.html>\
> 参考:  [https://ot-redis-operator.netlify.app](https://ot-redis-operator.netlify.app/)

### 哨兵选型

* 多库高可用 ---> 哨兵主从
* 单库高可用 ---> 哨兵集群

### 1、整体方案

> 本文中的Redis高可用方案采用Sentinel(哨兵)模式（一个master:M1、两个slave:R2、R3，每个redis节点都有一个Sentinel:S1、S2、S3），Sentinel自身也是一个集群。在reids集群出现故障的时候，会自动进行故障转移，从而保证集群的可用性。

**Sentiel的在redis集群中功能如下所示：**

* 监控：不间断的检查redis master/slave实例否是按照预期正常工作；
* 通知：当 Redis 实例出现错误的时候，负责通知管理员；
* 自动故障转移：在master发生故障时，哨兵会开启故障转移处理，将一台slave提升为master；
* 配置信息：客户端通过Sentinel获取当前Redis master的地址，如果发生故障转移，Sentinel将会提供新的服务器地址。

**Sentinel本身是一套分布式系统，它被设计成能够进行多个进程间协同工作的模式，这样的好处如下：**

* 降低错报：多个Sentinel一致明确给定的主机不再可用时，才会执行故障检测，这能够有效降低错报的概率。
* 高健壮性：即使只有一个Sentinel在正常运行，Redis也是可用的，从而保证系统具有较高的健壮性。

### 2、场景描述

> Sentinel 将对redis集群中的所有redis节点进行监控，并在被监视的redis master（这里为server1）处于下线状态时，自动将某个redis slave升级为新的master。同上，会将其它redis slave设置为新的master节点的slave。

在Server1 掉线后：

升级Server2 为新的主服务器：

### 3、redis部署

通过执行下面的命令，添加helm stable仓库：

```bash
helm repo add bitnami https://charts.bitnami.com/bitnami
```

通过执行下面的命令，在Kubernetes中部署redis高可用方案（<mark style="color:red;">**持久化需动态存储, 临时测试可选择关闭存储**</mark>）：

```bash
helm install bitnami-redis –set master.service.type=NodePort \
–set cluster.enabled=true \
–set sentinel.enabled=true \
–set metrics.enabled=true \
–set password=redis bitnami/redis \
–-namespace kube-public -create-namesapce
```

其中：

* 使用的charts为bitnami/redis；
* 通过设置cluster.enabled为true，启用redis集群模式；
* 通过设置sentinel.enabled为true，启用哨兵模式；
* 通过设置metrics.enabled为true，允许指标被外部进行监控；
* 通过设置master.service.type类型为NodePort，外部应用通过宿主机IP+端口访问redis服务。

<figure><img src="/files/oKrl9DpQv533S7CuRF03" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/Mbq8z1NcRqq6hUkETmOw" alt=""><figcaption><p>UI界面， 没有UI界面忽略</p></figcaption></figure>

<figure><img src="/files/oFQD8Y7XC9CGvOHAQd2M" alt=""><figcaption><p>Pod中有三个容器，一个Redis，一个哨兵，一个监控</p></figcaption></figure>

### 4、验证

查看密码

```bash
export REDIS_PASSWORD=$(kubectl get secret --namespace kube-public bitnami-redis -o jsonpath="{.data.redis-password}" | base64 -d)
echo $REDIS_PASSWORD
```

启动 redis-client 客户端

```bash
# 快速启动 Redis 第一个客户端
kubectl run --namespace kube-public redis-client --restart='Never'  --env REDIS_PASSWORD=$REDIS_PASSWORD  --image docker.io/bitnami/redis:7.4.0-debian-12-r2 --command -- sleep infinity

# 进入 pod 单元中的容器
kubectl exec --tty -i redis-client    --namespace kube-public -- bash


# 集群内部通过无头 svc 连接
I have no name!@redis-client:/$  redis-cli -h bitnami-redis-headless -p 6379
```

<figure><img src="/files/93Dgsku3RDAzCN8DjaQk" alt=""><figcaption><p>经测试，通过 svc 连接是随机的， 但可以切换多库</p></figcaption></figure>

<mark style="color:red;">**😲**</mark><mark style="color:red;">**`那么问题来了，如果程序链接这个地址，要执行写操作，却连接到从库，那该怎么办？？？我们留到本文最后说明`**</mark>

### 5 基于Prometheus和Grafana进行监控

在部署是设置了 <mark style="color:green;">metrics.enabled</mark> 为 <mark style="color:green;">true</mark>，在k8s中对应部署了metrics容器，通过此容器用于获取redis的指标信息，并提供给Prometheus。

#### 5.1 Prometheus配置

在Prometheus的配置文件（/etc/prometheus/prometheus.yml）的最后面增加下面的内容。

```yaml
– job_name: ‘redis-ha-exporter’
   static_configs:
   – targets: [‘bitnami-redis-metrics.redis-ha:9121’]
```

并在k8s中重新部署Prometheus。

#### 5.2 导入redis的Grafana DashBoard

在Grafana中导入Redis Dashboard for Prometheus Redis Exporter，通过此DashBoard就能监控redis运行的相关指标。

<figure><img src="/files/Ca5x6f8J2gC9zlrNgtTx" alt=""><figcaption></figcaption></figure>

### 多主多从

\
采用代理模式，客户端的请求由代理节点转发至数据分片，可享受代理节点带来的负载均衡（数据分片）、读写分离、故障转移、代理查询缓存、**长连接**（通常情况下，Proxy通过与数据分片建立长连接来处理请求）等特性能力，相比代理模式，直连模式节约了通过代理处理请求的时间，可以在一定程度上提高Redis服务的响应速度。\
作者：Ice\
链接：<https://juejin.cn/post/7001895591064125471\\>
来源：稀土掘金\
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。

<figure><img src="/files/ltL2QMFiZhvlA3hE0owp" alt=""><figcaption></figcaption></figure>

代理选型

<figure><img src="/files/4QTTBccgsDZkLP1qAnTK" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/kubernetes/helm/helm-an-zhuang-redis-shao-bing-zhu-cong-gao-ke-yong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
