应公司要求在本地环境搭建一套redis的集群,应对大容量数据,故开始研究集群,研究集群代理,提供统一rukou
好了,话不多说,直接上码,其实很简单的,我也不知道我怎么会写这么啰嗦,好了,请接下来别眨眼,往下看吧
version: '2.4'
services:
# redis1配置
redis1:
image: redis:6.0.6-alpine
restart: always
container_name: redis-1
environment: # 环境变量
- PORT=6385 # 会使用config/nodes-${PORT}.conf这个配置文件
- TZ=Asia/Shanghai
ports:
- 6385:6385
- 16385:16385
networks:
redis-cluster:
ipv4_address: 172.19.0.2
stdin_open: true # 标准输入打开
tty: true # 后台运行不退出
privileged: true # 拥有容器内命令执行的权限
healthcheck:
test: [ "CMD", "redis-cli","-h","127.0.0.1","-p","6385" ,"CONFIG" ,"GET" ,"requirepass" ]
volumes:
- /data/redis/redis-cluster/redis-config/6385/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/redis-data/6379/appendonly.aof:/data/appendonly.aof:rw
command: sh -c "redis-server /usr/local/etc/redis/redis.conf"
# redis2配置
redis2:
image: redis:6.0.6-alpine
restart: always
container_name: redis-2
environment: # 环境变量
- PORT=6380 # 会使用config/nodes-${PORT}.conf这个配置文件
- TZ=Asia/Shanghai
ports:
- 6380:6380
- 16380:16380
networks:
redis-cluster:
ipv4_address: 172.19.0.3
stdin_open: true # 标准输入打开
tty: true # 后台运行不退出
privileged: true # 拥有容器内命令执行的权限
healthcheck:
test: [ "CMD", "redis-cli","-h","127.0.0.1","-p","6380" ,"CONFIG" ,"GET" ,"requirepass" ]
depends_on:
redis1:
condition: service_healthy
volumes:
- /data/redis/redis-cluster/redis-config/6380/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/redis-data/6380/appendonly.aof:/data/appendonly.aof:rw
command: sh -c "redis-server /usr/local/etc/redis/redis.conf"
# redis3配置
redis3:
image: redis:6.0.6-alpine
restart: always
container_name: redis-3
environment: # 环境变量
- PORT=6381 # 会使用config/nodes-${PORT}.conf这个配置文件
- TZ=Asia/Shanghai
ports:
- 6381:6381
- 16381:16381
networks:
redis-cluster:
ipv4_address: 172.19.0.4
stdin_open: true # 标准输入打开
tty: true # 后台运行不退出
healthcheck:
test: [ "CMD", "redis-cli","-h","127.0.0.1","-p","6381" ,"CONFIG" ,"GET" ,"requirepass" ]
depends_on:
redis2:
condition: service_healthy
volumes:
- /data/redis/redis-cluster/redis-config/6381/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/redis-data/6381/appendonly.aof:/data/appendonly.aof:rw
command: sh -c "redis-server /usr/local/etc/redis/redis.conf"
# redis4配置
redis4:
image: redis:6.0.6-alpine
restart: always
container_name: redis-4
environment: # 环境变量
- PORT=6382 # 会使用config/nodes-${PORT}.conf这个配置文件
- TZ=Asia/Shanghai
ports:
- 6382:6382
- 16382:16382
networks:
redis-cluster:
ipv4_address: 172.19.0.5
stdin_open: true # 标准输入打开
tty: true # 后台运行不退出
healthcheck:
test: [ "CMD", "redis-cli","-h","127.0.0.1","-p","6382" ,"CONFIG" ,"GET" ,"requirepass" ]
depends_on:
redis3:
condition: service_healthy
volumes:
- /data/redis/redis-cluster/redis-config/6382/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/redis-data/6382/appendonly.aof:/data/appendonly.aof:rw
command: sh -c "redis-server /usr/local/etc/redis/redis.conf"
# redis5配置
redis5:
image: redis:6.0.6-alpine
restart: always
container_name: redis-5
environment: # 环境变量
- PORT=6383 # 会使用config/nodes-${PORT}.conf这个配置文件
- TZ=Asia/Shanghai
ports:
- 6383:6383
- 16383:16383
networks:
redis-cluster:
ipv4_address: 172.19.0.6
stdin_open: true # 标准输入打开
tty: true # 后台运行不退出
healthcheck:
test: [ "CMD", "redis-cli","-h","127.0.0.1","-p","6383" ,"CONFIG" ,"GET" ,"requirepass" ]
depends_on:
redis4:
condition: service_healthy
volumes:
- /data/redis/redis-cluster/redis-config/6383/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/redis-data/6383/appendonly.aof:/data/appendonly.aof:rw
command: sh -c "redis-server /usr/local/etc/redis/redis.conf"
# redis6配置
redis6:
image: redis:6.0.6-alpine
restart: always
container_name: redis-6
environment: # 环境变量
- PORT=6384 # 会使用config/nodes-${PORT}.conf这个配置文件
- TZ=Asia/Shanghai
ports:
- 6384:6384
- 16384:16384
networks:
redis-cluster:
ipv4_address: 172.19.0.7
stdin_open: true # 标准输入打开
tty: true # 后台运行不退出
healthcheck:
test: [ "CMD", "redis-cli","-h","127.0.0.1","-p","6384" ,"CONFIG" ,"GET" ,"requirepass" ]
depends_on:
redis5:
condition: service_healthy
volumes:
- /data/redis/redis-cluster/redis-config/6384/redis.conf:/usr/local/etc/redis/redis.conf
- /data/redis/redis-data/6384/appendonly.aof:/data/appendonly.aof:rw
command: sh -c "redis-server /usr/local/etc/redis/redis.conf"
networks:
redis-cluster:
external: true
#ipam:
# config:
# - subnet: 172.19.0.0/16
# gateway: 172.19.0.1
因为是用docker部署的,所以,要注意文件的挂载,因为重启的原因,所以最好是固定容器的ip这样在机器重启的时候,集群不会出现问题,,,很重要,因为我就踩过坑,早上上班发现集群毙了,一查,我擦,ip变了导致集群毙了。
接下来呢,有了集群,就会发现,这玩意需要一个统一的链接方式啊 ,不然咋闹,连单点redis吗
那这玩个der啊,所以接下来,就是搭建proxy了,
因为习惯docker所以,这次代理也就用docker部署吧,找了好久,没有找见靠谱的docker镜像,人家的也担心有后门,索性,自己打一个镜像吧,用别人的镜像, 也不知道配置文件怎么配,阿西,真是麻烦
接下来附上镜像文件
FROM ubuntu:latest COPY ./redis-cluster-proxy-unstable /opt/redis-cluster-proxy-unstable RUN sed -i "s/archive.ubuntu.com/mirrors.aliyun.com/g" /etc/apt/sources.list RUN apt update && apt install -y make WORKDIR /opt/redis-cluster-proxy-unstable RUN cd /opt/redis-cluster-proxy-unstable/ && make install EXPOSE 7777
接下来配置文件
```yaml ################################## INCLUDES ################################### # Include one or more other config files here. Include files can include # other files. # # If instead you are interested in using includes to override configuration # options, it is better to use include as the last line. # # include /path/to/local.conf # include /path/to/other.conf ######################## CLUSTER ENTRY POINT ADDRESS ########################## # Indicate the entry point address in the same way it can be indicated in the # redis-cluster-proxy command line arguments. # Note that it can be overridden by the command line argument itself. # You can also specify multiple entry-points, by adding more lines, ie: # cluster 127.0.0.1:7000 # cluster 127.0.0.1:7001 # You can also use the "entry-point" alias instead of cluster, ie: # entry-point 127.0.0.1:7000 # cluster 192.168.248.135:6385 cluster 192.168.248.135:6380 cluster 192.168.248.135:6381 cluster 192.168.248.135:6382 cluster 192.168.248.135:6383 cluster 192.168.248.135:6384 ################################### MAIN ###################################### # Set the port used by Redis Cluster Proxy to listen to incoming connections # from clients (default 7777) port 7777 # If you want you can bbleind a single interface, if the bind option is not # specified all the interfaces will listen for incoming connections. # You can also bind on multiple interfaces by declaring bind on multiple lines # bind 0.0.0.0 # Specify the path for the Unix socket that will be used to listen for # incoming connections. There is no default, so Redis Cluster Proxy won't # listen on a Unix socket when not specified. # # unixsocket /path/to/proxy.socket # Set the Unix socket file permissions (default 0) # # unixsocketperm 760 # Set the number of threads. threads 8 # Set the TCP keep-alive value on the Redis Cluster Proxy's socket # # tcpkeepalive 300 # Set the TCP backlog on the Redis Cluster Proxy's socket # # tcp-backlog 511 # Size of the connections pool used to provide ready-to-use sockets to # private connections. The number (size) indicates the number of starting # connections in the pool. # Use 0 to disable connections pool at all. # Every thread will have its pool of ready-to-use connections. # When the proxy starts, every thread will populate a pool containing # connections to all the nodes of the cluster. # Whenever a client needs a private connection, it can take a connection # from the pool, if available. This will speed-up the client transition from # the thread's shared connection to its own private connection, since the # connection from the thread's pool should be already connected and # ready-to-use. Otherwise, clients with priovate connections must re-connect # the the nodes of the cluster (this re-connection will act in a 'lazy' way). # # connections-pool-size 10 # Minimum number of connections in the the pool. Below this value, the # thread will start re-spawning connections at the defined rate until # the pool will be full again. # # connections-pool-min-size 10 # Interval in milliseconds used to re-spawn connections in the pool. # Whenever the number of connections in the pool drops below the minimum # (see 'connections-pool-min-size' above), the thread will start # re-spawing connections in the pool, until the pool will be full again. # New connections will be added at this specified interval. # # connections-pool-spawn-every 50 # Number of connections to re-spawn in the pool at every cycle that will # happen with an interval defined by 'connections-pool-spawn-every' (see above). # # connections-pool-spawn-rate 50 # Run Redis Cluster Proxy as a daemon. daemonize no # If a pid file is specified, the proxy writes it where specified at startup # and removes it at exit. # # When the proxy runs non daemonized, no pid file is created if none is # specified in the configuration. When the proxy is daemonized, the pid file # is used even if not specified, defaulting to # "/var/run/redis-cluster-proxy.pid". # # Creating a pid file is best effort: if the proxy is not able to create it # nothing bad happens, the server will start and run normally. # #pidfile /var/run/redis-cluster-proxy.pid # Specify the log file name. Also the empty string can be used to force # Redis Cluster Porxy to log on the standard output. Note that if you use # standard output for logging but daemonize, logs will be sent to /dev/null # #logfile "" # Enable cross-slot queries that can use multiple keys belonging to different # slots or even different nodes. # WARN: these queries will break the the atomicity deisgn of many Redis # commands. # NOTE: cross-slots queries are not supported by all the commands, even if # this feature is enabled # enable-cross-slot yes # Maximum number of clients allowed # # max-clients 10000 # Authentication password used to authenticate on the cluster in case its nodes # are password-protected. The password will be used both for fetching cluster's # configuration and to automatically authenticate proxy's internal connections # to the cluster itself (both multiplexing shared connections and clients' # private connections. So, clients connected to the proxy won't need to issue # the Redis AUTH command in order to be authenticated. # # auth mypassw # Authentication username (supported by Redis >= 6.0) # # auth-user myuser ################################# LOGGING ##################################### # Log level: can be debug, info, success, warning o error. log-level error # Dump queries received from clients in the log (log-level debug required) # # dump-queries no # Dump buffer in the log (log-level debug required) # # dump-buffer no # Dump requests' queues (requests to send to cluster, request pending, ...) # in the log (log-level debug required) # # dump-queues no
然后是docker-compose
version: '3'
services:
redis-cluster-proxy:
image: redis-cluster-proxy:v2.0
container_name: proxy
restart: always
ports:
- 7777:7777
command: sh -c "/opt/redis-cluster-proxy-unstable/src/redis-cluster-proxy -c proxy.conf"
volumes:
- ./proxy.conf:/opt/redis-cluster-proxy-unstable/proxy.conf
最后是启动成功截图



