Docker Redis集群搭建

最早看Redis集群搭建的文章,还是多开几个虚拟机或者单机多开几个端口的方法,后来发现其实用docker更加简单方便。

1.拉取镜像

docker search redis
docker pull redis

2.生成配置文件

mkdir redis-cluster
cd redis-cluster
vim redis-cluster.tmpl
port ${PORT}

masterauth 123456

requirepass 123456

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

cluster-announce-ip 192.168.10.83

cluster-announce-port ${PORT}

cluster-announce-bus-port 1${PORT}

appendonly yes
参数描述

(1)port(端口号)

(2)masterauth(设置集群节点间访问密码,跟下面一致)

(3)requirepass(设置redis访问密码)

(4)cluster-enabled yes(启动集群模式)

(5)cluster-config-file nodes.conf(集群节点信息文件)

(6)cluster-node-timeout 5000(redis节点宕机被发现的时间)

(7)cluster-announce-ip(集群节点的汇报ip,防止nat,预先填写为网关ip后续需要手动修改配置文件)

(8)cluster-announce-port(集群节点的汇报port,防止nat)

(9)cluster-announce-bus-port(集群节点的汇报bus-port,防止nat)

(10) appendonly yes(开启aof)

3.生成配置文件和数据目录

执行以下命令,按模板生成文件以及文件夹

for port in `seq 7001 7006`; do \

  mkdir -p ./${port}/conf \

  && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \

  && mkdir -p ./${port}/data; \

done

image

4.创建6个redis容器

docker run -d --net=host -v /redis-cluster/7001/conf/redis.conf:/etc/redis/redis.conf -v /redis-cluster/7001/data:/data --restart always --name=redis-7001 redis redis-server /etc/redis/redis.conf
docker run -d --net=host -v /redis-cluster/7002/conf/redis.conf:/etc/redis/redis.conf -v /redis-cluster/7002/data:/data --restart always --name=redis-7002 redis redis-server /etc/redis/redis.conf
docker run -d --net=host -v /redis-cluster/7003/conf/redis.conf:/etc/redis/redis.conf -v /redis-cluster/7003/data:/data --restart always --name=redis-7003 redis redis-server /etc/redis/redis.conf
docker run -d --net=host -v /redis-cluster/7004/conf/redis.conf:/etc/redis/redis.conf -v /redis-cluster/7004/data:/data --restart always --name=redis-7004 redis redis-server /etc/redis/redis.conf
docker run -d --net=host -v /redis-cluster/7005/conf/redis.conf:/etc/redis/redis.conf -v /redis-cluster/7005/data:/data --restart always --name=redis-7005 redis redis-server /etc/redis/redis.conf
docker run -d --net=host -v /redis-cluster/7006/conf/redis.conf:/etc/redis/redis.conf -v /redis-cluster/7006/data:/data --restart always --name=redis-7006 redis redis-server /etc/redis/redis.conf

或者

for port in `seq 7001 7006`; do \
    docker run -d --net=host -v /redis-cluster/${port}/conf/redis.conf:/etc/redis/redis.conf -v /redis-cluster/${port}/data:/data --restart always --name=redis-${port}  redis redis-server /etc/redis/redis.conf \
done
docker ps

image

5.进入任一容器

docker exec -it redis-7001 bash
redis-cli -a 123456 --cluster create 192.168.10.83:7001 192.168.10.83:7002 192.168.10.83:7003 192.168.10.83:7004 192.168.10.83:7005 192.168.10.83:7006 --cluster-replicas 1

image

image

6.在Lumen中使用

/config/database.php redis 配置

Lumen版本
Lumen (7.1.2) (Laravel Components ^7.0)

    'redis' => [
        'client' => env('REDIS_CLIENT', 'predis'),//必须是predis,phpredis不支持集群
        'cluster' => false,这个实测好像并没有用,true/false都一样
        'clusters' => [
            'default' => [//填一个就好,自动分槽
                [
                    'host' => '127.0.0.1',
                    'port' => 7001
                ],
             ],
        ],
        'options' => [
            'cluster' => 'redis',
            'parameters'=>[//密码要写到这里,之前写到上面的配置里,一直报错
                'password'=>'123456'
        ],
    ],

如果跟我一样,也使用了laravel的队列,则会报

Predis\NotSupportedException:
Cannot use 'EVAL' with redis-cluster.

解决办法是将config/queue.php里redis的配置

-'queue' => 'default',
+'queue' => '{default}',

你可能感兴趣的:(redis-cluster,redis集群,docker,lumen)