[英]How to connect 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:
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/
如您所見,有一個連接到 redis 哨兵的選項,但我不知道如何。 這是我的代碼:
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'}
)
我的問題是:
參數 CACHE_REDIS_SENTINELS 中應該包含什么? 我是否應該以某種方式獲取每個節點的 IP 地址並在那里獲取這些地址?
參數 CACHE_REDIS_SENTINEL_MASTER 應該是什么? 是“mymaster”(哨兵-> masterSet?)
我是否應該始終連接到讀寫服務器(在這種情況下,是否會使用其他副本)? 或者我是否需要以這種方式調整我的應用程序:如果我寫我總是使用端口 26379 的哨兵訪問,如果我讀我總是連接到只讀的 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 "%d\n" % 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 "%d\n" % time.time()
app.run()
有關更多詳細信息,請參閱我的原始問題(EDIT 和 EDIT2)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.