使用docker-compose在一台机器创建redis-culuster集群
yum -y install yum-utils device-mapper-persistent-data lvm2 #下载docker环境
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #下载docker源
yum -y install docker-ce #下载最新版docker,当前24.0.6
service docker start #启动docker
如果启动docker后关闭防火墙,需要在重启docker =service docker restart
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-`uname -s`-`uname -m`" -o /usr/local/bin/docker-compose #下载
或者直接去官网下载二进制文件,放到/usr/local/bin/改名为docker-compose
https://github.com/docker/compose/releases
docker-compose-linux-x86_64 #这里提供linux x86_64版本
移动过去后给执行权限
然后输入命令查看版本,是否可以使用
[root@localhost ~]# docker-compose version
<p>Docker Compose version v2.22.0 #我下载的最新版本
创建一个工作目录
mkdir -p /opt/docker_redis_cluster/conf && cd /opt/docker_redis_cluster
编写一份配置文件,配置文件写入了6个redis:6.2.7版本实例,指定了每个实例的端口和配置文件,建立cluster集群
vim docker-compose.yml 写入
version: "3.1"
services:
redis1:
image: seiyar81/rediscurl
restart: always
container_name: redis1
environment:
- TZ=Asia/Shanghai
ports:
- 7001:7001
- 17001:17001
volumes:
- ./conf/redis1.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]
redis2:
image: seiyar81/rediscurl
restart: always
container_name: redis2
environment:
- TZ=Asia/Shanghai
ports:
- 7002:7002
- 17002:17002
volumes:
- ./conf/redis2.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]<br />
redis3:
image: seiyar81/rediscurl
restart: always
container_name: redis3
environment:
- TZ=Asia/Shanghai
ports:
- 7003:7003
- 17003:17003
volumes:
- ./conf/redis3.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]<br />
redis4:
image: seiyar81/rediscurl
restart: always
container_name: redis4
environment:
- TZ=Asia/Shanghai
ports:
- 7004:7004
- 17004:17004
volumes:
- ./conf/redis4.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]<br />
redis5:
image: seiyar81/rediscurl
restart: always
container_name: redis5
environment:
- TZ=Asia/Shanghai
ports:
- 7005:7005
- 17005:17005
volumes:
- ./conf/redis5.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]<br />
redis6:
image: seiyar81/rediscurl
restart: always
container_name: redis6
environment:
- TZ=Asia/Shanghai
ports:
- 7006:7006
- 17006:17006
volumes:
- ./conf/redis6.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]
然后写入6个配置文件,对应6台redis
vim conf/redis1.conf 第一个
port 7001 #指定redis的端口号
cluster-enabled yes #开启Redis集群
cluster-config-file nodes-7001.conf #集群配置文件
cluster-announce-ip 192.168.48.139 #集群的对外ip地址linux ip 写宿主机的ip
cluster-announce-port 7001 #集群的对外port
cluster-announce-bus-port 17001 #集群的总线端口
复制5份并且修改里面的redis实例端口号即可
for i in {2..6} ; do cp conf/redis1.conf redis${i}.conf ; done #拷贝5份同样配置
sed -i 's/7001/7002/g ; s/17001/17002/g' conf/redis2.conf #修改端口
sed -i 's/7001/7003/g ; s/17001/17003/g' conf/redis3.conf
sed -i 's/7001/7004/g ; s/17001/17004/g' conf/redis4.conf
sed -i 's/7001/7005/g ; s/17001/17005/g' conf/redis5.conf
sed -i 's/7001/7006/g ; s/17001/17006/g' conf/redis6.conf
在docker_redis_cluster目录内使用命令开始构建
docker-compose up -d #-d后台运行
进入任意一个节点,创建集群
docker exec -it redis1 /bin/sh
redis-cli --cluster create 192.168.48.139:7001 192.168.48.139:7002 192.168.48.139:7003 192.168.48.139:7004 192.168.48.139:7005 192.168.48.139:7006 --cluster-replicas 1
--cluster-replicas 1 表示创建的集群每个master有 1 个从机,如果2就是每个master有2个从机
输入yes
redis-cli --cluster info 192.168.48.139:7001 #查看集群状态
redis-cli -c -h 192.168.48.139 -p 7001 #进去集群用set和get测试节点
192.168.48.139:7001> CLUSTER INFO #查看集群实例
192.168.48.139:7001> CLUSTER NODES #查看集群信息
评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果
音乐天地