docker 实现redis集群搭建(一)
2023-09-27 14:25:58 时间
首先 我们需要理论知识 Redis Cluster是Redis的分布式解决方案 它解决了redis单机中心化的问题 分布式数据库——首要解决把整个数据集按照分区规则映射到多个节点的问题。
这边就需要知道分区规则——哈希分区规则。Redis Cluster 采用哈希分区规则中的虚拟槽分区。所有的键根据哈希函数映射到0 16383 计算公式 slot CRC16(key) 16383。每一个节点负责维护一部分槽以及槽所映射的键值数据。
[root etcd1 tmp]# mkdir docker_redis_cluster [root etcd1 tmp]# cd docker_redis_cluster/ [root etcd2 docker_redis_cluster]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz
[root etcd1 docker_redis_cluster]# tar zxvf redis-4.0.1.tar.gz [root etcd1 docker_redis_cluster]# cd redis-4.0.1/ [root etcd1 redis-4.0.1]# make
[root etcd3 redis-4.0.1]# vi /tmp/docker_redis_cluster/redis-4.0.1/redis.conf
修改bind ip地址
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # internet, binding to all the interfaces is dangerous and will expose the # instance to everybody on the internet. So by default we uncomment the # following bind directive, that will force Redis to listen only into # the IPv4 lookback interface address (this means Redis will be able to # accept connections only from clients running into the same computer it # is running). # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #bind 127.0.0.1 bind 0.0.0.0
将守护进程yes改成no
# By default Redis does not run as a daemon. Use yes if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. daemonize no
将密码项注释去掉 添加新密码
# Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # requirepass foobared
修改为
# Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. requirepass 123456
因为配置了密码 所以 配置中另外一处主从连接也需要配置密码
# If the master is password protected (using the requirepass configuration # directive below) it is possible to tell the slave to authenticate before # starting the replication synchronization process, otherwise the master will # refuse the slave request. # masterauth master-password
修改为
# If the master is password protected (using the requirepass configuration # directive below) it is possible to tell the slave to authenticate before # starting the replication synchronization process, otherwise the master will # refuse the slave request. # masterauth master-password masterauth 123456
设置日志路径
# Specify the log file name. Also the empty string can be used to force # Redis 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 /var/log/redis/redis-server.log
配置集群相关信息 去掉配置项前面的注释
# Normal Redis instances can t be part of a Redis Cluster; only nodes that are # started as cluster nodes can. In order to start a Redis instance as a # cluster node enable the cluster support uncommenting the following: cluster-enabled yes # Every cluster node has a cluster configuration file. This file is not # intended to be edited by hand. It is created and updated by Redis nodes. # Every Redis Cluster node requires a different cluster configuration file. # Make sure that instances running in the same system do not have # overlapping cluster configuration file names. cluster-config-file nodes-6379.conf # Cluster node timeout is the amount of milliseconds a node must be unreachable # for it to be considered in failure state. # Most other internal time limits are multiple of the node timeout. cluster-node-timeout 15000
[root etcd3 docker_redis_cluster]# cd /tmp/docker_redis_cluster [root etcd3 docker_redis_cluster]# vi Dockerfile # Redis # Version 4.0.1 FROM Centos:7 br ENV REDIS_HOME /usr/local br ADD redis-4.0.1.tar.gz / # 本地的redis源码包复制到镜像的根路径下 ADD命令会在复制过后自动解包。被复制的对象必须处于Dockerfile同一路径 且ADD后面必须使用相对路径 RUN mkdir -p $REDIS_HOME/redis # 创建安装目录 ADD redis-4.0.1/redis.conf $REDIS_HOME/redis/ # 将一开始编译产生并修改后的配置复制到安装目录 RUN yum -y update # 更新yum源 RUN yum install -y gcc make # 安装编译需要的工具 WORKDIR /redis-4.0.1 RUN make RUN mv /redis-4.0.1/src/redis-server $REDIS_HOME/redis/ # 编译后 容器中只需要可执行文件redis-server WORKDIR / RUN rm -rf /redis-4.0.1 # 删除解压文件 RUN yum remove -y gcc make # 安装编译完成之后 可以删除多余的gcc跟make VOLUME [ /var/log/redis ] # 添加数据卷 EXPOSE 6379 # 暴露6379端口 也可以暴露多个端口 这里不需要如此
PS.当前镜像非可执行镜像 所以没有包含ENTRYPOINT和CMD指令
# 切换中国源 [root etcd3 docker_redis_cluster]# vi /etc/docker/daemon.json registry-mirrors : [ https://registry.docker-cn.com ] [root etcd3 docker_redis_cluster]# docker build -t hakimdstx/cluster-redis . Complete! --- 546cb1d34f35 Removing intermediate container 6b6556c5f28d Step 14/15 : VOLUME /var/log/redis --- Running in 05a6642e4046 --- e7e2fb8676b2 Removing intermediate container 05a6642e4046 Step 15/15 : EXPOSE 6379 --- Running in 5d7abe1709e2 --- 2d1322475f79 Removing intermediate container 5d7abe1709e2 Successfully built 2d1322475f79
镜像制作完成 制作中间可能会报 Public key for glibc-headers-2.17-222.el7.x86_64.rpm is not installed 错误 这时候需要在镜像配置中添加一句命令
... RUN rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 RUN yum -y update # 更新yum源 RUN yum install -y gcc make # 安装编译需要的工具
[root etcd3 docker_redis_cluster]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hakimdstx/cluster-redis 4.0.1 1fca5a08a4c7 14 seconds ago 435 MB centos 7 49f7960eb7e4 2 days ago 200 MB
以上 redis 基础镜像就制作完成了
[root etcd3 tmp]# mkdir docker_redis_nodes [root etcd3 tmp]# cd docker_redis_nodes [root etcd3 docker_redis_nodes]# vi Dockerfile # Redis Node # Version 4.0.1 br FROM hakimdstx/cluster-redis:4.0.1 # MAINTAINER_INFO MAINTAINER hakim 1194842583 qq.com ENTRYPOINT [ /usr/local/redis/redis-server , /usr/local/redis/redis.conf ]
[root etcd3 docker_redis_nodes]# docker build -t hakimdstx/nodes-redis:4.0.1 . Sending build context to Docker daemon 2.048 kB Step 1/3 : FROM hakimdstx/cluster-redis:4.0.1 --- 1fca5a08a4c7 Step 2/3 : MAINTAINER hakim 1194842583 qq.com --- Running in cc6e07eb2c36 --- 55769d3bfacb Removing intermediate container cc6e07eb2c36 Step 3/3 : ENTRYPOINT /usr/local/redis/redis-server /usr/local/redis/redis.conf --- Running in f5dedf88f6f6 --- da64da483559 Removing intermediate container f5dedf88f6f6 Successfully built da64da483559
[root etcd3 docker_redis_nodes]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hakimdstx/nodes-redis 4.0.1 da64da483559 51 seconds ago 435 MB hakimdstx/cluster-redis 4.0.1 1fca5a08a4c7 9 minutes ago 435 MB centos 7 49f7960eb7e4 2 days ago 200 MB
docker 实现redis集群搭建(二) 摘要:接触docker以来,似乎养成了一种习惯,安装什么应用软件都想往docker方向做,今天就想来尝试下使用docker搭建redis集群。
之前介绍了用docker来搭建redis主从环境,但这只是对数据添加了从库备份(主从复制),当主库down掉的时候,从库是不会自动升级为主库的,也就是说,该redis主从集群并非是高可用的。
Lansonli CSDN大数据领域博客专家,华为云享专家、阿里云专家博主、腾云先锋(TDP)核心成员、51CTO专家博主,全网六万多粉丝,知名互联网公司大数据高级开发工程师
相关文章
- CentOS安装Redis及基本配置
- 【Docker 基础教程】容器数据持久化(三)------ Redis的基础配置
- Docker下redis的主从配置
- redis使用分布式锁解决超卖问题
- redis不支持多个数据库实例但是支持多个字典
- k8s官方案例练习-使用 ConfigMap 来配置 Redis
- Redis 集群
- Redis-dump Docker搭建的快速指南
- redis缓存分页思路
- Springboot分布式锁实践(redis)
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例(转)
- redis 安装启动及设置密码<windows>
- docker 启动一个 redis
- Redis源码之——SDS
- 虚拟机linux+centos7+docker+springboot+Redis的使用
- 【红队靶场】利用redis未授权写入,docker提权逃逸,ms17-010,横向拿下域控
- Redis分布式锁的会出现的问题
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
- 如何将 Redis 快速部署为 Docker 容器
- Docker安装tomcat, mysql,redis(单机版)
- Docker安装Redis
- redis集群环境的搭建和错误分析
- redis参数改进建议