https://charts.bitnami.com/bitnami
仓库中的 redis-cluster 包直接提供了 3个master 和 3个slave的标准集群模式
使用RDM客户端此时是可以正常访问集群模式的,因为 RDM客户端支持
Change host on cluster redirects
不过在这种状态下,绑定 redis 实例的 nodeport 无法使用 python的 redis 包直接访问 需要使用 redis-py-cluster
redis集群在kubernetes 中最主要的麻烦来自于它的 16384 个哈希槽,redis cluster 的客户端需要在各个节点之间进行forward
from rediscluster import RedisCluster
startup_nodes = [{"host": "192.168.194.194", "port": "30002"}]
rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True, password="f1w6shwtLa")
rc.set("foo", "bar")
print(rc.get("foo"))
print(rc.get("foo"))
print(rc.get("foo"))
还有一个选择是 redis-cluster-proxy
git clone https://github.com/RedisLabs/redis-cluster-proxy.git
make PREFIX=/usr/local/redis_cluster_proxy install
如果出现错误
../deps/hiredis/libhiredis.a: No such file or directory
root@ubuntu:~/proxy/redis-cluster-proxy# cd deps
root@ubuntu:~/proxy/redis-cluster-proxy/deps# make lua hiredis linenoise
root@ubuntu:~/proxy/docker# cp /usr/local/redis_cluster_proxy/bin/redis-cluster-proxy ./
root@ubuntu:~/proxy/docker# ls
Dockerfile redis-cluster-proxy
root@ubuntu:~/proxy/docker# cat Dockerfile
FROM ubuntu
WORKDIR /data
ADD redis-cluster-proxy /usr/local/bin/
EXPOSE 7777
root@ubuntu:~/proxy/docker#
docker build . -t libaibai/redis-cluster-proxy:v1.0.0
root@ubuntu:~/proxy/k8s# cat configmap.yaml
---
# Redis-Proxy Config
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-proxy
namespace: redis-cluster
data:
proxy.conf: |
cluster redis-cluster.redis-cluster.svc:6379 # 配置为Redis Cluster Service
bind 0.0.0.0
port 7777 # redis-cluster-proxy 对外暴露端口
threads 8 # 线程数量
daemonize no
enable-cross-slot yes
auth f1w6shwtLa # 配置Redis Cluster 认证密码
log-level error
root@ubuntu:~/proxy/k8s#
root@ubuntu:~/proxy/k8s# cat deploy.yaml
---
# Redis-Proxy NodePort
apiVersion: v1
kind: Service
metadata:
name: redis-proxy
namespace: redis-cluster
spec:
type: NodePort # 对K8S外部提供服务
ports:
- name: redis-proxy
nodePort: 30001 # 对外提供的端口
port: 7777
protocol: TCP
targetPort: 7777
selector:
app: redis-proxy
---
# Redis-Proxy Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-proxy
namespace: redis-cluster
spec:
replicas: 1
selector:
matchLabels:
app: redis-proxy
template:
metadata:
labels:
app: redis-proxy
spec:
imagePullSecrets:
- name: harbor
containers:
- name: redis-proxy
image: libaibai/redis-cluster-proxy:v1.0.0
imagePullPolicy: Always
command: ["redis-cluster-proxy"]
args:
- -c
- /data/proxy.conf # 指定启动配置文件
ports:
- name: redis-7777
containerPort: 7777
protocol: TCP
volumeMounts:
- name: redis-proxy-conf
mountPath: /data/
volumes: # 挂载proxy配置文件
- name: redis-proxy-conf
configMap:
name: redis-proxy
root@ubuntu:~/proxy/k8s#