使用Docker构建redis集群

Ruby环境准备

建议版本:2.2.3+
安装curl : yum install curl
升级nss版本 : yum -y update nss
安装rvm : curl -L get.rvm.io | bash -s stable

若提示
or if it fails:

    command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
    command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
执行
	command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
     command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -

保存执行变更 : source /etc/profile.d/rvm.sh
查看Ruby版本 :rvm list known
升级Ruby版本 :rvm install 2.7.0
使用Ruby版本 :rvm use ruby-2.7.0
查看Ruby版本 :ruby -v
安装RubyGem :yum install rubygems
安装Redis Gem : gem install redis --version 4.0.1

redis准备

1.安装Docker(详见docker安装文档)
2.下载redis源码包

mkdir /usr/local/redis_docker
cd /usr/local/redis_docker
wget http://download.redis.io/releases/redis-4.0.1.tar.gz 

3.解压

tar -xzf redis-4.0.1.tar.gz

4.安装gcc

yum install -y gcc make

5.编译

cd /usr/local/redis_docker/redis-4.0.1/
make
#如果报错,可以尝试:make MALLOC=libc

6.配置redis.conf

vim /usr/local/redis_docker/redis-4.0.1/redis.conf
ip绑定
bind 0.0.0.0
0.0.0.0为不绑定ip

#后台运行(检查配置文件是否设置了daemonize yes,如果是,就要改为daemonize no,因为该选项让redis成为在后台运行的守护进程,而docker容器必须要有一个前台进程才能留存,否则容器会自动退出)
daemonize no

设置密码(redis访问密码,也可以不设置密码)
#requirepass 123456789

主从复制的验证密码(集群安全验证)
#masterauth 123456789

日志文件
logfile “/var/log/redis/redis-server.log”

开启集群配置
cluster-enabled yes
集群配置文件
cluster-config-file nodes-6379.conf
集群超时时间
cluster-node-timeout 15000

创建Dockerfile文件生产redis镜像

1.创建Dockerfile文件

cd /usr/docker_root/redis_cluster
vim Dockerfile
#没有这个镜像会自动pull
FROM centos:7
ADD redis-4.0.1.tar.gz /
RUN mkdir -p /redis
ADD ./redis-4.0.1/redis.conf /redis/

RUN yum -y update &&  yum install -y gcc make   
  
WORKDIR /redis-4.0.1
RUN make && mv /redis-4.0.1/src/redis-server /redis/
  
WORKDIR /
RUN rm -rf /redis-4.0.1 &&  yum remove -y gcc make

VOLUME ["/var/log/redis/"]
  
EXPOSE 6379
ENTRYPOINT ["/redis/redis-server", "/redis/redis.conf"]

2.构建redis基础镜像

#"."表示Dockerfile文件的相对路径
[root@mysqltest redis_docker]# docker build -t redis:4.0.1 .
[root@mysqltest redis_docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               4.0.1               f3ef5d9ec418        5 seconds ago       461MB
centos              7                   b5b4d78bc90c        3 weeks ago         203MB

3.启动镜像

[root@mysqltest redis_docker]# docker run -d --name redis01 -p 6379:6379 redis:4.0.1 
eb3baccf097b27ad7bd4ba45796500f98b9f2b6dcc12dcde1e458bf3bd73e4d4
[root@mysqltest redis_docker]# docker run -d --name redis02 -p 6380:6379 redis:4.0.1 
79fabb71de2733ce01b1f76cfbf8040330ec2316b96975a97087e8a26eda9e4b
[root@mysqltest redis_docker]# docker run -d --name redis03 -p 6381:6379 redis:4.0.1 
5c6b8d263c2b3b6251eac08be61af3d976ba471c433877af27b8af078421a015
[root@mysqltest redis_docker]# docker run -d --name redis04 -p 6382:6379 redis:4.0.1 
d82c2f5378bf9d961ccd5e3d0bb479b2023b2ddd1a1ac3f11eff802b9d7317d7
[root@mysqltest redis_docker]# docker run -d --name redis05 -p 6383:6379 redis:4.0.1 
44e7bcb96c9a7016303b27dc8417cce346bcba7c8753c55134c6c7c4ce061cb1
[root@mysqltest redis_docker]# docker run -d --name redis06 -p 6384:6379 redis:4.0.1 
18da0af35ab682c92725ce61537e662b943d654251513dad4b94d3c36355b92f

4.查找配置文件 :

find / -name client.rb 【中间有redis目录的那个】

5.修改配置文件 :

vim /usr/local/rvm/gems/ruby-2.3.8/gems/redis-4.1.2/lib/redis/client.rb
DEFAULTS = {
      :url => lambda { ENV["REDIS_URL"] },
      :scheme => "redis",
      :host => "127.0.0.1",
      :port => 6379,
      :path => nil,
      :timeout => 5.0,
      :password => "xxxxxx",  #主要是修改这个,要和redis.conf配置的密码一致,如果没有密码就删掉
      :db => 0,
      :driver => nil,
      :id => nil,
      :tcp_keepalive => 0,
      :reconnect_attempts => 1,
      :inherit_socket => false
    }

6.查看Redis节点IP

[root@mysqltest redis_docker]# docker inspect redis01 redis02 redis03 redis04 redis05 redis06 | grep IPAddress | grep 172
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
            "IPAddress": "172.17.0.4",
                    "IPAddress": "172.17.0.4",
            "IPAddress": "172.17.0.5",
                    "IPAddress": "172.17.0.5",
            "IPAddress": "172.17.0.6",
                    "IPAddress": "172.17.0.6",
            "IPAddress": "172.17.0.7",
                    "IPAddress": "172.17.0.7",

7.搭建集群

cd /usr/local/redis_docker/redis-4.0.1/src
./redis-trib.rb create --replicas 1 172.17.0.2:6379 172.17.0.3:6379 172.17.0.4:6379 172.17.0.5:6379 172.17.0.6:6379 172.17.0.7:6379

8.验证集群

[root@mysqltest src]# ./redis-trib.rb check 172.17.0.2:6379
>>> Performing Cluster Check (using node 172.17.0.2:6379)
M: 2c3bbbba188381b08b4ac763b7ce00b191b3698f 172.17.0.2:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 6b05d66d03c71ca6a6c67efa84a17f28d3d6541c 172.17.0.5:6379
   slots: (0 slots) slave
   replicates 2c3bbbba188381b08b4ac763b7ce00b191b3698f
S: f500fda4543e6557e36389d93222cb793ec7945f 172.17.0.6:6379
   slots: (0 slots) slave
   replicates 69cecddf1df6009ee9afb9df189583e0f1bed35a
M: 5f9893fcdda2863cde67d70fc32c26d56759990b 172.17.0.4:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 2f0ff504ac4a392150342f1e925393980f3f8a91 172.17.0.7:6379
   slots: (0 slots) slave
   replicates 5f9893fcdda2863cde67d70fc32c26d56759990b
M: 69cecddf1df6009ee9afb9df189583e0f1bed35a 172.17.0.3:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[root@mysqltest src]# ./redis-cli -c -h 172.17.0.2 -p 6382
Could not connect to Redis at 172.17.0.2:6382: Connection refused
Could not connect to Redis at 172.17.0.2:6382: Connection refused
not connected> 
[root@mysqltest src]# ./redis-cli -c -h 172.17.0.2 -p 6379
172.17.0.2:6379> keys *
(empty list or set)
172.17.0.2:6379> set a 123
-> Redirected to slot [15495] located at 172.17.0.4:6379
OK
172.17.0.4:6379> get a
"123"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值