docker-compos搭建redis-cluster

1.安装docker环境并配置加速

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
sudo service docker start
docker --version
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{

  "registry-mirrors": ["https://jk2v9c0j.mirror.aliyuncs.com"]

}
EOF

systemctl daemon-reload
systemctl restart docker

2.拉取镜像

[root@c7-41 ~]# docker pull publicisworldwide/redis-cluster
[root@c7-41 ~]# docker pull inem0o/redis-trib
[root@c7-41 redis]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
publicisworldwide/redis-cluster   latest              29e4f38e4475        18 months ago       94.9MB
inem0o/redis-trib                 latest              0f7b910114d5        2 years ago         32MB

3.上传docker-compose到/usr/local/bin并赋予执行权限


[root@c7-41 ~]# cd /usr/local/bin/
[root@c7-41 bin]# pwd
/usr/local/bin
[root@c7-41 bin]# ls
docker-compose
[root@c7-41 bin]# chmod +x docker compose

#查看是否可用
[root@c7-41 bin]# docker-compose --version
docker-compose version 1.24.0, build 0aa59064

4.编写docker-compose.yml

[root@c7-41 ~]# mkdir redis-cluster
[root@c7-41 ~]# cd redis-cluster
[root@c7-41 redis-cluster]# cat >> docker-compose.yml <<\EOF 
version: "3"
services: 
  redis1:
    image: publicisworldwide/redis-cluster
    environment:
      REDIS_PORT: 7001
    ports:
      - "7001:7001"
      - "17001:17001"
  redis2:
    image: publicisworldwide/redis-cluster
    environment:
      REDIS_PORT: 7002
    ports:
      - "7002:7002"
      - "17002:17002"
  redis3:
    image: publicisworldwide/redis-cluster
    environment:
      REDIS_PORT: 7003
    ports:
      - "7003:7003"
      - "17003:17003"
  redis4:
    image: publicisworldwide/redis-cluster
    environment:
      REDIS_PORT: 7004
    ports:
      - "7004:7004"
      - "17004:17004"
  redis5:
    image: publicisworldwide/redis-cluster
    environment:
      REDIS_PORT: 7005
    ports:
      - "7005:7005"
      - "17005:17005"
  redis6:
    image: publicisworldwide/redis-cluster
    environment:
      REDIS_PORT: 7006
    ports:
      - "7006:7006"
      - "17006:17006"
EOF

5.启动所有容器

[root@c7-41 redis-cluster]# docker-compose up -d
Creating network "redis_default" with the default driver
Creating redis_redis4_1 ... done
Creating redis_redis2_1 ... done
Creating redis_redis1_1 ... done
Creating redis_redis5_1 ... done
Creating redis_redis3_1 ... done
Creating redis_redis6_1 ... done

6.创建集群

[root@c7-41 redis-cluster]# docker run --rm -it inem0o/redis-trib create --replicas 1 10.0.0.41:7001 10.0.0.41:7002 10.0.0.41:7003 10.0.0.41:7004 10.0.0.41:7005 10.0.0.41:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.0.0.41:7001
10.0.0.41:7002
10.0.0.41:7003
Adding replica 10.0.0.41:7004 to 10.0.0.41:7001
Adding replica 10.0.0.41:7005 to 10.0.0.41:7002
Adding replica 10.0.0.41:7006 to 10.0.0.41:7003
M: 594081c27f135a9e426318c6c6f80a2a04ff0893 10.0.0.41:7001
   slots:0-5460 (5461 slots) master
M: bf7a2bc7a74dfaa64060a78fd3466731b3c9a8a4 10.0.0.41:7002
   slots:5461-10922 (5462 slots) master
M: b13310aec6adc0802a64e5d84814a8bc05e660a8 10.0.0.41:7003
   slots:10923-16383 (5461 slots) master
S: 192b603929f7cc91498664890982a89914abf49d 10.0.0.41:7004
   replicates 594081c27f135a9e426318c6c6f80a2a04ff0893
S: cf604dccff29d238de67fa2054727b5c0a9db741 10.0.0.41:7005
   replicates bf7a2bc7a74dfaa64060a78fd3466731b3c9a8a4
S: 2a3d4c6a7a919a0ae8a17cc046c38afff2808c80 10.0.0.41:7006
   replicates b13310aec6adc0802a64e5d84814a8bc05e660a8
Can I set the above configuration? (type 'yes' to accept): yes       #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 10.0.0.41:7001)
M: 594081c27f135a9e426318c6c6f80a2a04ff0893 10.0.0.41:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 2a3d4c6a7a919a0ae8a17cc046c38afff2808c80 172.18.0.1:7006@17006
   slots: (0 slots) slave
   replicates b13310aec6adc0802a64e5d84814a8bc05e660a8
M: b13310aec6adc0802a64e5d84814a8bc05e660a8 172.18.0.1:7003@17003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: bf7a2bc7a74dfaa64060a78fd3466731b3c9a8a4 172.18.0.1:7002@17002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 192b603929f7cc91498664890982a89914abf49d 172.18.0.1:7004@17004
   slots: (0 slots) slave
   replicates 594081c27f135a9e426318c6c6f80a2a04ff0893
S: cf604dccff29d238de67fa2054727b5c0a9db741 172.18.0.1:7005@17005
   slots: (0 slots) slave
   replicates bf7a2bc7a74dfaa64060a78fd3466731b3c9a8a4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

7.查看集群


[root@c7-41 redis-cluster]# docker ps
CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                                                                  NAMES
31a6a96fe1b4        publicisworldwide/redis-cluster   "/usr/local/bin/entr…"   57 seconds ago      Up 54 seconds       6379/tcp, 0.0.0.0:7003->7003/tcp, 7000/tcp, 0.0.0.0:17003->17003/tcp   redis-cluster_redis3_1
7eb419c98601        publicisworldwide/redis-cluster   "/usr/local/bin/entr…"   57 seconds ago      Up 54 seconds       6379/tcp, 0.0.0.0:7004->7004/tcp, 7000/tcp, 0.0.0.0:17004->17004/tcp   redis-cluster_redis4_1
7c6ca48507f2        publicisworldwide/redis-cluster   "/usr/local/bin/entr…"   57 seconds ago      Up 54 seconds       6379/tcp, 0.0.0.0:7001->7001/tcp, 7000/tcp, 0.0.0.0:17001->17001/tcp   redis-cluster_redis1_1
d5823dd426da        publicisworldwide/redis-cluster   "/usr/local/bin/entr…"   57 seconds ago      Up 54 seconds       6379/tcp, 0.0.0.0:7002->7002/tcp, 7000/tcp, 0.0.0.0:17002->17002/tcp   redis-cluster_redis2_1
5e304c7629fc        publicisworldwide/redis-cluster   "/usr/local/bin/entr…"   57 seconds ago      Up 54 seconds       6379/tcp, 0.0.0.0:7006->7006/tcp, 7000/tcp, 0.0.0.0:17006->17006/tcp   redis-cluster_redis6_1
a0908c891ccf        publicisworldwide/redis-cluster   "/usr/local/bin/entr…"   57 seconds ago      Up 54 seconds       6379/tcp, 0.0.0.0:7005->7005/tcp, 7000/tcp, 0.0.0.0:17005->17005/tcp   redis-cluster_redis5_1

8.登陆并查看集群节点信息

#从集群的任意节点都可以登录
#docker-compose exec 容器名 命令
#-c是使用集群的方式连接redis -h是redis主机ip地址 -p是redis主机的端口

[root@c7-41 redis-cluster]# docker-compose exec redis1 /bin/bash
root@7c6ca48507f2:/data# redis-cli -c -h 10.0.0.41 -p 7001
10.0.0.41:7001> cluster nodes
4044374764cc4303058de2289593b596cc76855b 172.18.0.1:7002@17002 master - 0 1589360230000 2 connected 5461-10922
827e0e1698953e920de1b9fb000ed890f2d1a525 172.18.0.1:7006@17006 slave 896a6d82a6d0c5a2e30a673ee0d9761d3d24ec36 0 1589360230804 6 connected
9acec4810b9e6ff5500605101801992e8a992265 172.18.0.1:7005@17005 slave 4044374764cc4303058de2289593b596cc76855b 0 1589360229596 5 connected
43636106a986e32490a2d88649200758210e78a0 172.18.0.1:7004@17004 slave 7e9a29a90e80a5cfa686babffef7106a8d587604 0 1589360229000 4 connected
896a6d82a6d0c5a2e30a673ee0d9761d3d24ec36 172.18.0.1:7003@17003 master - 0 1589360229000 3 connected 10923-16383
7e9a29a90e80a5cfa686babffef7106a8d587604 172.18.0.4:7001@17001 myself,master - 0 1589360230000 1 connected 0-5460
10.0.0.41:7001>

9.创建一个键对值并验证

10.0.0.41:7001> set name tom
-> Redirected to slot [5798] located at 172.18.0.1:7002
OK
172.18.0.1:7002> set class 1708a
OK
172.18.0.1:7002> set hh jj
-> Redirected to slot [12077] located at 172.18.0.1:7003
OK
172.18.0.1:7003> get class
-> Redirected to slot [7755] located at 172.18.0.1:7002
"1708a"
172.18.0.1:7002> get name
"tom"
172.18.0.1:7002>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云原生解决方案

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值