安装Docker
参考:https://blog.csdn.net/yan_dk/article/details/89427641
安装Docker-Compose
Docker-Compose简介
Compose是Docker的服务编排工具,主要用来构建基于Docker的复杂应用,Compose 通过一个配置文件来管理多个Docker容器,非常适合组合使用多个容器进行开发的场景。
Docker-Compose是编排容器的。例如,你有一个php镜像,一个mysql镜像,一个nginx镜像。如果没有Docker-Compose,那么每次启动的时候,你需要敲各个容器的启动参数,环境变量,容器命名,指定不同容器的链接参数等等一系列的操作,相当繁琐。而用了docker-compose之后,你就可以把这些命令一次性写在docker-composer.yml文件中,以后每次启动这一整个环境(含3个容器)的时候,你只要敲一个docker-composer up命令就ok了。
开始安装Docker-Compose
通过pip安装(pip是python的包管理工具)
pip install --upgrade pip
指定版本安装
pip install docker-compose==1.22
出现报错
原因:python库中urllib3 (1.22) or chardet (2.2.1) 的版本不兼容
解决如下:
pip uninstall urllib3
pip uninstall chardet
pip install requests
Curl方式下载新的版本
curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
修改权限
chmod +x /usr/local/bin/docker-compose
安装完成后可以查看版本:
docker-compose --version
集群节点规划
容器名称 |
容器IP地址 |
映射端口号 |
服务运行模式 |
Redis-master |
172.50.0.2 |
6391-> 6391 |
master |
Redis-master2 |
172.50.0.3 |
6392-> 6392 |
master |
Redis-master3 |
172.50.0.4 |
6393 -> 6393 |
master |
redis-slave |
172.30.0.2 |
6394 -> 6394 |
Slave |
redis-slave2 |
172.30.0.3 |
6395 -> 6395 |
Slave |
redis-slave3 |
172.30.0.4 |
6396 -> 6396 |
Slave |
集群环境预装
安装目录结构
新建脚本文件Dockfile
FROM centos:latest
MAINTAINER yuyan "[email protected]"
RUN groupadd -r redis && useradd -r -g redis redis
RUN yum -y update && yum -y install epel-release \
&& yum -y install redis && yum -y install wget \
&& yum -y install net-tools \
&& yum -y install ruby && yum -y install rubygems
RUN wget https://rubygems.org/downloads/redis-3.3.5.gem && gem install -l ./redis-3.3.5.gem \
&& rm -f redis-3.3.5.gem
COPY ./config/redis-trib.rb /usr/bin
COPY ./config/redis.sh /usr/bin
RUN mkdir -p /config && chmod 775 /usr/bin/redis.sh
新建脚本文件docker-compose.yaml
version: "3.6"
services:
redis-master1:
image: redis-cluster
container_name: redis-master1
working_dir: /config
environment:
- PORT=6391
ports:
- "6391:6391"
- "16391:16391"
stdin_open: true
networks:
redis-master:
ipv4_address: 172.50.0.2
tty: true
privileged: true
volumes: ["/usr/docker/redis-cluster/config:/config"]
entrypoint:
- /bin/bash
# - redis.sh
redis-master2:
image: redis-cluster
working_dir: /config
container_name: redis-master2
environment:
- PORT=6392
networks:
redis-master:
ipv4_address: 172.50.0.3
ports:
- "6392:6392"
- "16392:16392"
stdin_open: true
tty: true
privileged: true
volumes: ["/usr/docker/redis-cluster/config:/config"]
entrypoint:
- /bin/bash
# - redis.sh
redis-master3:
image: redis-cluster
container_name: redis-master3
working_dir: /config
environment:
- PORT=6393
networks:
redis-master:
ipv4_address: 172.50.0.4
ports:
- "6393:6393"
- "16393:16393"
stdin_open: true
tty: true
privileged: true
volumes: ["/usr/docker/redis-cluster/config:/config"]
entrypoint:
- /bin/bash
# - redis.sh
redis-slave1:
image: redis-cluster
container_name: redis-slave1
working_dir: /config
environment:
- PORT=6394
networks:
redis-slave:
ipv4_address: 172.30.0.2
ports:
- "6394:6394"
- "16394:16394"
stdin_open: true
tty: true
privileged: true
volumes: ["/usr/docker/redis-cluster/config:/config"]
entrypoint:
- /bin/bash
#- redis.sh
redis-slave2:
image: redis-cluster
working_dir: /config
container_name: redis-slave2
environment:
- PORT=6395
ports:
- "6395:6395"
- "16395:16395"
stdin_open: true
networks:
redis-slave:
ipv4_address: 172.30.0.3
tty: true
privileged: true
volumes: ["/usr/docker/redis-cluster/config:/config"]
entrypoint:
- /bin/bash
#- redis.sh
redis-salve3:
image: redis-cluster
container_name: redis-slave3
working_dir: /config
environment:
- PORT=6396
ports:
- "6396:6396"
- "16396:16396"
stdin_open: true
networks:
redis-slave:
ipv4_address: 172.30.0.4
tty: true
privileged: true
volumes: ["/usr/docker/redis-cluster/config:/config"]
entrypoint:
- /bin/bash
#- redis.sh
networks:
redis-master:
driver: bridge
ipam:
driver: default
config:
-
subnet: 172.50.0.0/16
redis-slave:
driver: bridge
ipam:
driver: default
config:
-
subnet: 172.30.0.0/16
redis-test:
external:
name: mynetwork
集群节点配置
配置文件目录结构
注:其中redis-3.3.5.gem为redis的ruby支持软件、redis-trib.rb为redis-ruby脚本工具、redis.sh为启动redis服务器脚本。
配置文件nodes-xx.conf修改点如下:
#节点端口
port 6379
#开启集群模式
cluster-enabled yes
#节点超时时间,单位毫秒
cluster-node-timeout 15000
#集群内部配置文件
cluster-config-file "nodes-6379.conf"
生成redis集群
# cd redis-cluster
# docker-compose -p redis-cluster up -d
# docker ps
Redis集群配置成功。
持续完善,待续...