Docker+Redis3集群环境搭建(2)

9cb25bcd52d1        redis/nodemodule:v1  "/bin/bash"              About an hour ago  Up About an hour    0.0.0.0:7005-7006->7005-7006/tcp  redisNodeA7005-70006

91dac3ea23c9        redis/nodemodule:v1  "/bin/bash"              About an hour ago  Up About an hour    0.0.0.0:7003-7004->7003-7004/tcp  redisNodeA7003-70004

容器ID为9cb25bcd52d1的ip地址:172.17.0.5 port: 7005 7006

容器ID为91dac3ea23c9的ip地址:172.17.0.4 port: 7003 7004

容器ID为e2189fc1d4d9的ip地址:172.17.0.2 port: 7001 7002

安装redis3.0.7

下面的操作实例只记录在9cb25bcd52d1:172.17.0.5 上面的操作, 一定要在每个节点都进行操作,对应的端口目录,修改对面的端口

[root@localhost redissoft]# docker-enter 9cb25bcd52d1

Last login: Tue Mar 15 08:13:51 UTC 2016

[root@9cb25bcd52d1 ~]# cd /root/redissoft/

[root@9cb25bcd52d1 redissoft]# ls

redis-3.0.7.tar.gz  redis-3.2.1.gem

[root@9cb25bcd52d1 redissoft]#

[root@9cb25bcd52d1 local]# cp redis-3.0.7.tar.gz /usr/local/

[root@9cb25bcd52d1 local]# tar -xvf redis-3.0.7.tar.gz

[root@9cb25bcd52d1 local]# mv redis-3.0.7 redis3.0

[root@9cb25bcd52d1 local]# cd redis3.0

[root@9cb25bcd52d1 local]# make && make install

创建节点配置文件

保证3个节点都有对应的端口目录

[root@9cb25bcd52d1 redis3.0]# mkdir -p /usr/local/cluster

[root@9cb25bcd52d1 redis3.0]# mkdir -p /usr/local/cluster/7005

[root@9cb25bcd52d1 redis3.0]# mkdir -p /usr/local/cluster/7006

[root@9cb25bcd52d1 redis3.0]# cp -rf /usr/local/redis3.0/* /usr/local/cluster/7005/

[root@9cb25bcd52d1 redis3.0]# cp -rf /usr/local/redis3.0/* /usr/local/cluster/7006/

[root@9cb25bcd52d1 redis3.0]#

修个节点配置文件

保证3个节点都要在相应的配置文件内配置相应的端口号,只修改列出的项目;

[root@9cb25bcd52d1 redis3.0]#  vi /usr/local/cluster/7005/redis.conf 

port 7005

daemonize yes

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

appendonly yes

[root@9cb25bcd52d1 redis3.0]# vi /usr/local/cluster/7006/redis.conf

port 7006

daemonize yes

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

appendonly yes

启动服务

保证3个节点都要起来

[root@9cb25bcd52d1 cluster]# cd /usr/local/cluster/7005/src/

[root@9cb25bcd52d1 src]# ./redis-server ../redis.conf

[root@9cb25bcd52d1 src]# cd /usr/local/cluster/7006/src/

[root@9cb25bcd52d1 src]# ./redis-server ../redis.conf

[root@9cb25bcd52d1 src]# ps -ef |grep redis

root      3000    1  0 09:12 ?        00:00:00 ./redis-server *:7005 [cluster]

root      3004    1  0 09:13 ?        00:00:00 ./redis-server *:7006 [cluster]

root      3008    58  0 09:13 ?        00:00:00 grep --color=auto redis

[root@9cb25bcd52d1 src]#

创建集群

cd /usr/local/redis3.0/src

./redis-trib.rb create --replicas 1 172.17.0.2:7001 172.17.0.2:7002 172.17.0.4:7003 172.17.0.4:7004 172.17.0.5:7005 172.17.0.5:7006

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

172.17.0.2:7001

172.17.0.4:7003

172.17.0.5:7005

Adding replica 172.17.0.4:7004 to 172.17.0.2:7001

Adding replica 172.17.0.2:7002 to 172.17.0.4:7003

Adding replica 172.17.0.5:7006 to 172.17.0.5:7005

M: b9597621be3c89721e980bf21211349d816719f8 172.17.0.2:7001

slots:0-5460 (5461 slots) master

S: 198746c9c20225b2f35312e3f787590ae8fbc450 172.17.0.2:7002

replicates ae4adef75beba6f20129f7a7263ec301b85862c5

M: ae4adef75beba6f20129f7a7263ec301b85862c5 172.17.0.4:7003

slots:5461-10922 (5462 slots) master

S: 3f89d374e0e6461b566527d5a9dffe2d91bf43dd 172.17.0.4:7004

replicates b9597621be3c89721e980bf21211349d816719f8

M: 1b26d10b8a6ee3aa731123716e68bd3e911d805e 172.17.0.5:7005

slots:10923-16383 (5461 slots) master

S: 75e63758ce188ab1ccaa4e9bc195d560b9a7c417 172.17.0.5:7006

replicates 1b26d10b8a6ee3aa731123716e68bd3e911d805e

Can I set the above configuration? (type 'yes' to accept): yes

>>> Nodes configuration updated

>>> Assign a different config epoch to each node

>>> Sending CLUSTER MEET messages to join the cluster

Waiting for the cluster to join...

>>> Performing Cluster Check (using node 172.17.0.2:7001)

M: b9597621be3c89721e980bf21211349d816719f8 172.17.0.2:7001

slots:0-5460 (5461 slots) master

M: 198746c9c20225b2f35312e3f787590ae8fbc450 172.17.0.2:7002

slots: (0 slots) master

replicates ae4adef75beba6f20129f7a7263ec301b85862c5

M: ae4adef75beba6f20129f7a7263ec301b85862c5 172.17.0.4:7003

slots:5461-10922 (5462 slots) master

M: 3f89d374e0e6461b566527d5a9dffe2d91bf43dd 172.17.0.4:7004

slots: (0 slots) master

replicates b9597621be3c89721e980bf21211349d816719f8

M: 1b26d10b8a6ee3aa731123716e68bd3e911d805e 172.17.0.5:7005

slots:10923-16383 (5461 slots) master

M: 75e63758ce188ab1ccaa4e9bc195d560b9a7c417 172.17.0.5:7006

slots: (0 slots) master

replicates 1b26d10b8a6ee3aa731123716e68bd3e911d805e

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

创建结束后我们会发现有:

3个主节点:172.17.0.2:7001 172.17.0.2:7003 172.17.0.2:7005  这正是我们想要的结果

集群测试

发现3个set语句都把数据存在不同节点的slot里面了,读取的时候也从相应节点进行读取

[root@e2189fc1d4d9 ~]# redis-cli -c -p 7001

127.0.0.1:7001> set foo bar

-> Redirected to slot [12182] located at 172.17.0.5:7005

OK

172.17.0.5:7005> set hello newjoyful

-> Redirected to slot [866] located at 172.17.0.2:7001

OK

172.17.0.2:7001> set test newjoy

-> Redirected to slot [6918] located at 172.17.0.4:7003

OK

172.17.0.4:7003> get foo

-> Redirected to slot [12182] located at 172.17.0.5:7005

"bar"

172.17.0.5:7005> get hello

-> Redirected to slot [866] located at 172.17.0.2:7001

"newjoyful"

172.17.0.2:7001> get test

-> Redirected to slot [6918] located at 172.17.0.4:7003

"newjoy"

172.17.0.4:7003>

在宿主机192.168.100.67上安装redis3.0.7之后同样可以连接redis集群环境,因为容器与主机有端口映射关系

[root@localhost ~]# cp redis-3.0.7.tar.gz /usr/local/

[root@localhost ~]#  cd /usr/local/

[root@localhost ~]# tar -xvf redis-3.0.7.tar.gz

[root@localhost ~]# mv redis-3.0.7 redis3.0

[root@localhost ~]# cd redis3.0/

[root@localhost ~]# make

[root@localhost ~]# make install

[root@localhost ~]# netstat -an |grep :::700

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e7460bfaf36125f7008f8cf73abd6d5b.html