Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: huabing zhao <zhaohuabing@gmail.com>
- Loading branch information
1 parent
b1d6dd9
commit 0df6697
Showing
5 changed files
with
90 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: 连接集群外 Redis 服务 | ||
description: | ||
weight: 11 | ||
--- | ||
|
||
如果需要连接到一个集群外的 Redis 服务,我们可以采用一个[无选择器服务](https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors)来定义该服务,然后创建一个 EndpointSlice 来指定该服务的外部地址。然后就可以像集群内服务一样使用 RedisService 和 Redis Destination 来对该服务进行流量管理了。 | ||
|
||
```yaml | ||
kubectl apply -f- <<EOF | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: external-redis | ||
namespace: redis | ||
spec: | ||
ports: | ||
- name: tcp-redis | ||
protocol: TCP | ||
port: 6379 | ||
targetPort: 6379 | ||
--- | ||
apiVersion: discovery.k8s.io/v1 | ||
kind: EndpointSlice | ||
metadata: | ||
name: external-redis | ||
namespace: redis | ||
labels: | ||
kubernetes.io/service-name: external-redis | ||
addressType: IPv4 | ||
ports: | ||
- name: tcp-redis | ||
port: 6379 | ||
protocol: TCP | ||
endpoints: | ||
- addresses: | ||
- 10.244.0.26 # 集群外 Redis 实例的地址,比如云厂商提供的 Redis 服务。 | ||
EOF | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: Redis 读写分离 | ||
description: | ||
weight: 05 | ||
--- | ||
|
||
在 Redis Cluster 中有多个分片(Slot),每个 Redis 分片中,通常有一个 Master 节点,一到多个 Slave(Replica)节点。Master 节点负责写操作,并将数据变化同步到 Slave 节点。Slave 节点作为备份节点,当 Master 不可用时,Slave 可以被选举成为新的 Master。由于 Slave 中保存了和 Master 中相同的数据,因此也可以响应客户端的读操作。 | ||
|
||
Aeraki Mesh 支持通过 [RedisService](https://aeraki.net/zh/docs/v1.x/reference/redis/#RedisService) 来为 Redis 设置不同的读策略: | ||
|
||
* MASTER: 缺省的读模式。只从 Master 节点读取数据,当客户端要求数据强一致性时需要采用该模式。该模式对 Master 压力较大,在同一个分片内无法采用多个节点对读操作进行负载分担。 | ||
* PREFER_MASTER: 优先从 Master 节点读取数据,当 Master 节点不可用时,从 Replica 节点读取。 | ||
* REPLICA: 只从 Replica 节点读取数据,由于 Master 到 Replica 的数据复制过程是异步执行的,采用该方式有可能读取到过期的数据,因此适用于客户端对数据一致性要求不高的场景。该模式下可以采用多个 Replica 节点来分担来自客户端的读负载。 | ||
* PREFER_REPLICA: 优先从 Replica 节点读取数据,当 Replica 节点不可用时,从 Master 节点读取。 | ||
* ANY: 从任意节点读取数据。 | ||
|
||
如果客户端对缓存数据不要求强一致性,我们可以把读模式设置为 REPLICA。在该模式下,Master 节点只处理写操作,Slave 节点处理读操作,减少了 Master 节点的工作压力。随着业务的扩展,我们还可以在分片中增加更多的 Replica,以对读操作进行负载分担。 | ||
|
||
```yaml | ||
kubectl apply -f- <<EOF | ||
apiVersion: redis.aeraki.io/v1alpha1 | ||
kind: RedisService | ||
metadata: | ||
name: redis-cluster | ||
namespace: redis | ||
spec: | ||
host: | ||
- redis-cluster.redis.svc.cluster.local | ||
settings: | ||
readPolicy: REPLICA | ||
redis: | ||
- route: | ||
host: redis-cluster.redis.svc.cluster.local | ||
EOF | ||
``` | ||
|