Skip to main content

Docker快速搭建(CentOS)

通过Docker方式,可在30分钟内完成整个环境的搭建,方便快捷。

更新系统

sudo yum update -y

安装 Java SDK 8

# yum安装
yum install -y java-1.8.0-openjdk.x86_64

安装 ImageMagick 7.0.11

本应用通过 ImageMagick 生成图形验证码,使用图形验证码前需要先安装 ImageMagick。

# 安装相关依赖
yum install -y gcc gcc-c++ glib2-devel cairo-devel libtool-ltdl-devel tcl-devel libpng-devel libjpeg-devel ghostscript-devel bzip2-devel freetype-devel libtiff-devel

# 下载
wget https://imagemagick.org/download/ImageMagick.tar.gz --no-check-certificate

# 解压
tar -xvzf ImageMagick.tar.gz

# 进入解压之后的文件夹
cd /ImageMagick-7.1.0-0

# 执行
./configure --with-modules --enable-shared --with-perl

# 编译
make && make install

# 设置快捷方式
ldconfig /usr/local/lib

# 测试
magick -version

docker部署环境

该docker环境包含mysql、reids、elasticsearch、mongodb、kafka-manager、kakak、zookeeper,对应端口/账号见文末端口汇总

安装/启动docker

# 设置Yum源:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

# 执行以下命令,添加docker的yum源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 安装
sudo yum install -y docker-ce

# 启动
sudo systemctl start docker

# 设置开机启动
sudo systemctl enable docker

# 验证是否安装成功
docker run hello-world

# 等待一段时间后出现`Hello from Docker!`就是成功了:

安装docker-compose

# 下载
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 设置权限
sudo chmod +x /usr/local/bin/docker-compose

# 创建快捷方式
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

# 验证
docker-compose --version

创建docker-compose.yml文件

KAFKA_ADVERTISED_LISTENERS属性值替换成宿主机器的真实ip(共三处)

version: '3'
services:
mysql:
image: mysql:5.7
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: 1qazxsw2
ports:
- 3306:3306
volumes:
- /docker-volume/mysql/conf:/etc/mysql
- /docker-volume/mysql/logs:/var/log/mysql
- /docker-volume/mysql/data:/var/lib/mysql
restart: always
command: --character-set-server=utf8 --collation-server=utf8_general_ci

redis:
image: redis:6
container_name: redis
ports:
- 6379:6379
volumes:
- /docker-volume/redis/data:/data
restart: always
command: redis-server --requirepass 1qazxsw2 --appendonly yes

elasticsearch:
image: elasticsearch:7.13.2
container_name: elasticsearch
ports:
- 9200:9200
- 9300:9300
environment:
discovery.type: single-node
ES_JAVA_OPTS: -Xms512m -Xmx512m

zookeeper-node1:
image: zookeeper:3.4
restart: always
hostname: zookeeper-node1
container_name: zookeeper-node1
privileged: true
ports:
- 2181:2181
volumes:
- /docker-volume/zookeeper-node1/data:/data
- /docker-volume/zookeeper-node1/datalog:/datalog
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 1
ZOO_PORT: 2181
ZOO_SERVERS: server.1=zookeeper-node1:2888:3888 server.2=zookeeper-node2:2888:3888 server.3=zookeeper-node3:2888:3888

zookeeper-node2:
image: zookeeper:3.4
restart: always
hostname: zookeeper-node2
container_name: zookeeper-node2
privileged: true
ports:
- 2182:2181
volumes:
- /docker-volume/zookeeper-node2/data:/data
- /docker-volume/zookeeper-node2/datalog:/datalog
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 2
ZOO_PORT: 2181
ZOO_SERVERS: server.1=zookeeper-node1:2888:3888 server.2=zookeeper-node2:2888:3888 server.3=zookeeper-node3:2888:3888

zookeeper-node3:
image: zookeeper:3.4
restart: always
hostname: zookeeper-node3
container_name: zookeeper-node3
privileged: true
ports:
- 2183:2181
volumes:
- /docker-volume/zookeeper-node3/data:/data
- /docker-volume/zookeeper-node3/datalog:/datalog
environment:
TZ: Asia/Shanghai
ZOO_MY_ID: 3
ZOO_PORT: 2181
ZOO_SERVERS: server.1=zookeeper-node1:2888:3888 server.2=zookeeper-node2:2888:3888 server.3=zookeeper-node3:2888:3888

kafka-node1:
image: wurstmeister/kafka
restart: always
hostname: kafka-node1
container_name: kafka-node1
privileged: true
ports:
- 9091:9092
environment:
KAFKA_BROKER_ID: 1
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://xx.xx.xx.xx:9091
KAFKA_ADVERTISED_HOST_NAME: kafka-node1
KAFKA_ADVERTISED_PORT: 9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper-node1:2181/kafka1,zookeeper-node2:2181/kafka1,zookeeper-node3:2181/kafka1
JMX_PORT: 9988
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /docker-volume/kafka-node1:/kafka/kafka\-logs\-broker1
external_links:
- zoo1
- zoo2
- zoo3

kafka-node2:
image: wurstmeister/kafka
restart: always
hostname: kafka-node2
container_name: kafka-node2
privileged: true
ports:
- 9092:9092
environment:
KAFKA_BROKER_ID: 2
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://xx.xx.xx.xx:9092
KAFKA_ADVERTISED_HOST_NAME: kafka-node2
KAFKA_ADVERTISED_PORT: 9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper-node1:2181/kafka1,zookeeper-node2:2181/kafka1,zookeeper-node3:2181/kafka1
JMX_PORT: 9988
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /docker-volume/kafka-node2:/kafka/kafka\-logs\-broker2
external_links: # 连接本compose文件以外的container
- zoo1
- zoo2
- zoo3

kafka-node3:
image: wurstmeister/kafka
restart: always
hostname: kafka-node3
container_name: kafka-node3
privileged: true
ports:
- 9093:9092
environment:
KAFKA_BROKER_ID: 3
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://xx.xx.xx.xx:9093
KAFKA_ADVERTISED_HOST_NAME: kafka-node3
KAFKA_ADVERTISED_PORT: 9092
KAFKA_ZOOKEEPER_CONNECT: zookeeper-node1:2181/kafka1,zookeeper-node2:2181/kafka1,zookeeper-node3:2181/kafka1
JMX_PORT: 9988
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /docker-volume/kafka-node3:/kafka/kafka\-logs\-broker3
external_links: # 连接本compose文件以外的container
- zookeeper-node1
- zookeeper-node2
- zookeeper-node3

kafka-manager:
image: sheepkiller/kafka-manager:latest
restart: always
container_name: kafka-manager
hostname: kafka-manager
ports:
- 9000:9000
links: # 连接本compose文件创建的container
- kafka-node1
- kafka-node2
- kafka-node3
external_links: # 连接本compose文件以外的container
- zookeeper-node1
- zookeeper-node2
- zookeeper-node3
environment:
ZK_HOSTS: zookeeper-node1:2181/kafka1,zookeeper-node2:2181/kafka1,zookeeper-node3:2181/kafka1
KAFKA_BROKERS: kafka-node1:9092,kafka-node2:9092,kafka-node3:9092
APPLICATION_SECRET: letmein
KM_ARGS: -Djava.net.preferIPv4Stack=true

mongodb:
image: mongo:4.4.6
restart: always
container_name: mongodb
hostname: mongodb
privileged: true
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 1qazxsw2
volumes:
- /docker-volume/mongodb:/data/db
- /docker-volume/mongodb:/var/log/mongodb

构建环境

# 如果没有代理可配置国内镜像加快下载速度, 否则直接执行构建环境
vi /etc/docker/daemon.json

{
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
]
}

# 使配置生效
systemctl daemon-reload

# 重启Docker
systemctl restart docker

# 构建环境(在docker-compose.yml所在路径执行)
docker-compose up -d

端口汇总

服务端口账户密码
mysql3306root1qazxsw2
redis6379root1qazxsw2
elasticsearch9200
mongodb27017root1qazxsw2
kafka-manager9000
zookeeper2181,2182,2183
kafka9091,9092,9093