redis集群搭建

  • Post author:
  • Post category:其他


[root@centos ~]# mkdir /root/redis-cluster
[root@centos redis-cluster]# vim redis-cluster.tmpl


192.168.19.4机器redis模板文件

#端口

#端口
port ${PORT}
#密码
requirepass 1234
#集群的从节点跟主节点通信的密码
masterauth 1234
#安全模式
protected-mode no
#后台运行,容器设置为前台运行
daemonize no
#aof的文件是否开启
appendonly yes
#cluster环境是否开启
cluster-enabled yes
#cluster的配置文件
cluster-config-file nodes.conf
#cluster通信的超时时间 毫秒
cluster-node-timeout 15000
#cluster的ip 
cluster-announce-ip 192.168.19.4
#cluster的端口
cluster-announce-port ${PORT}
#cluster消息总线的内部端口
cluster-announce-bus-port 1${PORT}


192.168.19.5机器redis模板文件

#端口
prot ${PORT}
#密码
requirepass 1234
#集群的从节点跟主节点通信的密码
requireauth 1234
#安全模式
protected-mode on
#后台运行,容器设置为前台运行
daemonize on
#aof的文件是否开启
appendonly yes
#cluster环境是否开启
cluster-enabled yes
#cluster的配置文件
cluster-config-file nodes.conf
#cluster通信的超时时间 毫秒
cluster-node-time 15000
#cluster的ip 
cluster-announce-ip 192.168.19.5
#cluster的端口
cluster-announce-port ${PORT}
#cluster消息总线的内部端口
cluster-announce-bus-port 1${PORT}


创建对应的配置文件

for port in $(seq 6371 6373); do
  mkdir -p ${port}/conf &&
    PORT=${port} envsubst <redis-cluster.tmpl >${port}/conf/redis.conf &&
    mkdir -p ${port}/data
done


创建yml服务文件

#描述compose文件版本信息
version: "3.3"
services:
  redis-6371: #服务名称
    image: redis #创建容器时所需要的镜像
    container_name: redis-6371 #容器名称
    restart: always #容器总是重新启动
    network_mode: "host" #host网络模式
    volumes: #数据卷
      - /root/redis-cluster/6371/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /root/redis-cluster/6371/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf #覆盖容器启动后执行的默认命令
  redis-6372:
    image: redis
    container_name: redis-6372
    restart: always
    network_mode: "host"
    volumes:
      - /root/redis-cluster/6372/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /root/redis-cluster/6372/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf
  redis-6373:
    image: redis
    container_name: redis-6373
    restart: always
    network_mode: "host"
    volumes:
      - /root/redis-cluster/6373/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /root/redis-cluster/6373/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf


配置docker加速镜像

[root@centos redis-cluster]# cat /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com"]
}


创建集群


先确保两台机器可以通信,

也可以选择直接关闭防火墙

#添加暴露端口
firewall-cmd --add-port=6371/tcp
#删除暴露端口
firewall-cmd  --remove-port 6374/tc
#查询端口是否暴露
firewall-cmd  --query-port 6375/tcp
#重启防火墙
systemctl  restart firewalld

然后随便进入一台节点,进入

/usr/local/bin

目录

cluster-replicas 1 表示一主一从

redis-cli -a 1234 --cluster create 192.168.19.4:6371 192.168.19.4:6372 192.168.19.4:6373 192.168.19.5:6371 192.168.19.5:6372 192.168.19.5:6373  --cluster-replicas 1


问题:

[ERR] Node 192.168.19.4:6371 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0

删除端口配置目录重新构建



版权声明:本文为qq_36738861原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。