如何连接k8s,redis sentinel,flask_caching



我使用Helm图表部署了一个k8s redis哨兵:https://github.com/bitnami/charts/tree/master/bitnami/redis

我只更改了这些值(https://github.com/bitnami/charts/blob/master/bitnami/redis/values.yaml(:

auth:
enabled: false
sentinel: false
sentinel:
enabled: true
masterSet: mymaster

部署后,我得到了这样的消息:

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 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'  --image docker.io/bitnami/redis:6.2.6-debian-10-r103 --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:
redis-cli -h redis -p 6379 # Read only operations
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 &
redis-cli -h 127.0.0.1 -p 6379

这运行得很好:

kubectl get pods
NAME           READY   STATUS    RESTARTS   AGE
redis-node-0   2/2     Running   0          2m23s
redis-node-1   2/2     Running   0          71s
redis-node-2   2/2     Running   0          43s

但关于访问——概括地说——我有两个访问redis的选项:

  1. redis.default.svc.cluster.local:6379上的只读访问
  2. redis.default.svc.cluster.local:26379的读写访问(文档中的某种sentinel访问:
Master-Replicas with Sentinel
When installing the chart with architecture=replication and sentinel.enabled=true, it will deploy a Redis™ master StatefulSet (only one master allowed) and a Redis™ replicas StatefulSet. In this case, the pods will contain an extra container with Redis™ Sentinel. This container will form a cluster of Redis™ Sentinel nodes, which will promote a new master in case the actual one fails. In addition to this, only one service is exposed:
Redis™ service: Exposes port 6379 for Redis™ read-only operations and port 26379 for accessing Redis™ Sentinel.
For read-only operations, access the service using port 6379. For write operations, it's necessary to access the Redis™ Sentinel cluster and query the current master using the command below (using redis-cli or similar):
SENTINEL get-master-addr-by-name <name of your MasterSet. e.g: mymaster>
This command will return the address of the current master, which can be accessed from inside the cluster.
In case the current master crashes, the Sentinel containers will elect a new master node.

现在我想将我的Flask缓存模块连接到它:https://flask-caching.readthedocs.io/en/latest/

正如您所看到的,有一个连接到redissentinel的选项,但是我不知道如何连接。这是我的代码:

from flask_caching import Cache
cache = Cache(app, config={
'CACHE_TYPE': 'RedisSentinelCache', 
'CACHE_REDIS_SENTINELS': ['redis.default.svc.cluster.local'], 
'CACHE_REDIS_SENTINEL_MASTER': 'mymaster'}
)

我的问题是:

  1. 参数CACHE_REDIS_SENTINELS中应该包含什么?我应该以某种方式获取每个节点的IP地址并将其获取到那里吗?

  2. 参数CACHE_REDIS_SENTINEL_MASTER中应该包含什么?是";mymaster";(sentinel->masterSet?(

  3. 我是否应该始终连接到读写服务器(在这种情况下,是否会使用其他副本(?或者我需要这样调整我的应用程序:如果我写,我总是使用端口26379的sentinel访问,而在我读的情况下,我总是连接到只读6379端口?我需要保持2个连接吗?

谢谢

编辑:我正在深入研究flask_caching的代码,这似乎还可以(但我不确定是否使用了副本(:

import time
from flask import Flask
from flask_caching import Cache
config = {
"DEBUG": True,          # some Flask specific configs
'CACHE_TYPE': 'RedisSentinelCache',
'CACHE_REDIS_SENTINELS': [
['redis.default.svc.cluster.local', 26379]
],
'CACHE_REDIS_SENTINEL_MASTER': 'mymaster'
}
app = Flask(__name__)
# tell Flask to use the above defined config
app.config.from_mapping(config)
cache = Cache(app)
@app.route("/")
@cache.cached(timeout=5)
def index():
return "%dn" % time.time()
app.run()

第2版:

事实上,稍微深入研究一下flask_caching,它也使用了副本:

在文件flask_caching/backends/rediscache.py

代码正在获取写入和读取访问的主机:

self._write_client = sentinel.master_for(master)
self._read_clients = sentinel.slave_for(master)

干杯!

第3版:

redis驱动程序示例:

from redis.sentinel import Sentinel
sentinel = Sentinel([('redis.default.svc.cluster.local', 26379)])
redis_conn = sentinel.master_for('mymaster')
redis_conn_read = sentinel.slave_for('mymaster')
redis_conn.set('test', 'Hola!')
print(redis_conn_read.get('test'))

TLDR:

import time
from flask import Flask
from flask_caching import Cache
config = {
"DEBUG": True,          # some Flask specific configs
'CACHE_TYPE': 'RedisSentinelCache',
'CACHE_REDIS_SENTINELS': [
['redis.default.svc.cluster.local', 26379]
],
'CACHE_REDIS_SENTINEL_MASTER': 'mymaster'
}
app = Flask(__name__)
# tell Flask to use the above defined config
app.config.from_mapping(config)
cache = Cache(app)
@app.route("/")
@cache.cached(timeout=5)
def index():
return "%dn" % time.time()
app.run()

有关更多详细信息,请参阅我的原始问题(EDIT和EDIT2(。

最新更新