redis-cluster容器搭建(redis-trib工具搭建)

1.构建redis镜像

1.1.tar包下载
[root@localhost ~]# mkdir redis
[root@localhost ~]# cd redis
[root@localhost redis]# yum install -y gcc make wget vim
[root@localhost redis]# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
[root@localhost redis]# tar -zxvf redis-3.0.7.tar.gz
[root@localhost redis] cd redis-3.0.7
[root@localhost redis-3.0.7]# make && make install
1.2.编辑redis.conf文件
[root@localhost redis-3.0.7]# vim redis.conf
daemonize no
port 6379
logfile ""
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
daemonize : 是否后台运行,将其设为no,表示前台运行。
port :redis服务监听的端口。
logfile : 指定日志文件路径。#不设置的话,打印标准输出
appendonly : 是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。
cluster-node-timeout : 集群结点超时限制。
1.3.安装docker
[root@localhost redis]# yum install -y docker
[root@localhost redis]# systemctl start docker && systemctl enable docker
1.4编辑Dockerfile
[root@localhost redis]# cat Dockerfile
FROM docker.io/centos
ADD redis-3.0.7.tar.gz /
RUN mkdir -p /redis
ADD redis.conf /redis/
RUN rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
RUN yum install -y gcc make
WORKDIR /redis-3.0.7
RUN make
RUN mv /redis-3.0.7/src/redis-server  /redis/
WORKDIR /
VOLUME ["/var/log/redis/"]
EXPOSE 6379
ENTRYPOINT ["/redis/redis-server", "/redis/redis.conf"]
1.5.创建镜像
[root@localhost redis]# docker build -t redis:3.0.7 .

2.创建容器

2.1.二进制安装docker-compose
[root@localhostredis]#curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
[root@localhost redis]# chmod +x /usr/local/bin/docker-compose
2.2.编写docker-compose
[root@localhost redis]# cat docker-compose.yml
version: "2"
services:
 master1:
  image: redis:3.0.7
  ports:
   - 6001:6379
  container_name: redis6001
 master2:
  image: redis:3.0.7
  ports:
   - 6002:6379
  container_name: redis6002
 master3:
  image: redis:3.0.7
  ports:
   - 6003:6379
  container_name: redis6003
 master4:
  image: redis:3.0.7
  ports:
   - 6004:6379
  container_name: redis6004
 master5:
  image: redis:3.0.7
  ports:
   - 6005:6379
  container_name: redis6005
 master6:
  image: redis:3.0.7
  ports:
   - 6006:6379
  container_name: redis6006
[root@localhost redis]# docker-compose up -d
  1. 安装ruby
3.1.安装依赖包
yum install openssl* openssl-devel zlib-devel gcc gcc-c++ make autoconf readline-devel curl-devel expat-devel gettext-devel -y
3.2.下载二进制包
wget http://ftp.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
tar zxvf ruby-2.3.1.tar.gz
cd ruby-2.3.1
./configure --enable-shared --enable-pthread --prefix=/usr/local/ruby
make && make install
3.3.添加环境变量
echo "PATH=$PATH:/usr/local/ruby/bin;export PATH" >> /etc/profile
source /etc/profile
3.4.验证版本信息
ruby -v
gem --version
irb 环境测试
irb(main):001:0> 3+5
=> 8
irb(main):002:0> puts "hello world!"
hello world!
irb(main):001:0>exit
3.5.将gem换成国内源
gem sources -l
gem sources -a https://gems.ruby-china.com/ --remove https://rubygems.org/
3.6.安装redis
gem install redis 
  1. 创建redis cluster集群
[root@kuang src]# pwd
/root/redisr/redis-3.0.7/src
[root@localhost src]# cp redis-trib.rb /usr/local/bin/
[root@localhost src]# chmod +x /usr/local/bin/redis-trib.rb
[root@localhost src]# docker inspect redis6001 redis6002 redis6003 redis6004 redis6005 redis6006 |grep \"IPAddress\"
            "IPAddress": "",
                    "IPAddress": "172.18.0.6",
            "IPAddress": "",
                    "IPAddress": "172.18.0.4",
            "IPAddress": "",
                    "IPAddress": "172.18.0.2",
            "IPAddress": "",
                    "IPAddress": "172.18.0.3",
            "IPAddress": "",
                    "IPAddress": "172.18.0.7",
            "IPAddress": "",
                    "IPAddress": "172.18.0.5",
[root@localhost redis]# redis-trib.rb create --replicas 1 172.18.0.2:6379 172.18.0.3:6379 172.18.0.4:6379 172.18.0.5:6379 172.18.0.6:6379 172.18.0.7:6379
验证集群
[root@localhost redis]# redis-cli -h 172.18.0.2 -p 6379
172.18.0.2:6379> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:117
cluster_stats_messages_received:117
172.18.0.2:6379> set hello world
-> Redirected to slot [866] located at 172.18.0.2:6379
OK
[root@localhost redis]# redis-trib.rb check 172.18.0.7:6379
>>> Performing Cluster Check (using node 172.18.0.7:6379)
S: e7a2682709b7bf26fac194f621fd0804bde4159f 172.18.0.7:6379
   slots: (0 slots) slave
   replicates 91e08cb96de731c38d893a83f6b5c78bec157aba
M: 91e08cb96de731c38d893a83f6b5c78bec157aba 172.18.0.4:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 188b65356cd20143950e55c3f74253ee19b4b846 172.18.0.5:6379
   slots: (0 slots) slave
   replicates 61a843e4a9a7db41fef181a3ba4957cc1dae9936
M: 61a843e4a9a7db41fef181a3ba4957cc1dae9936 172.18.0.2:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: be1605061e5c09d816118bcb010ddee0320ce4d7 172.18.0.6:6379
   slots: (0 slots) slave
   replicates a4049a3c2812eb7ccbef6ca440070b2b7cf09fa0
M: a4049a3c2812eb7ccbef6ca440070b2b7cf09fa0 172.18.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.

你可能感兴趣的:(redis-cluster容器搭建(redis-trib工具搭建))