之前一直没有时间研究学习docker,这回有个新项目需要快速独立部署,因此正好学习一下。
1. 说明
该文档采用docker compose部署,因此服务器需安装docker
和docker-compose
,具体的安装步骤本文档不进行赘述,可参考https://www.runoob.com/docker/docker-compose.html。
部署的容器有六个:
- Cassandra 3.11.10
- Elasticsearch 7.6.2
- RabbitMQ 3.8.16
- MinIO latest
- MySQL 8
- Nginx
- OpenJDK 8 (Jar启动服务)
目录结构
docker
├── cassandra
| ├── schema.cql
| └── cassandra.yaml
├── db
| └── db.sql
├── es
| ├── elasticsearch-analysis-ik-7.6.2
| └── Dockerfile
├── jar
| ├── Dockerfile
| └── {java application}.jar (由于频繁更新,部署时再放入目录)
├── rabbit
| └── Dockerfile
├── nginx
| ├── html (静态页面,前后端分离)
| └── conf
└── docker-compose.yml
2. 数据挂载准备
为保证数据安全,容器的数据需挂载到宿主机的文件目录,以防数据丢失。本文档的挂载路径统一放在/home/volumes
。若需要修改挂载目录路径,勿必记得修改docker-compose.yml
里面的全部挂载目录路径。
// 新建目录
cd /home
mkdir volumes
3. Elasticsearch
需对Elasticsearch访问宿主机的权限进行配置。Es挂载目录在/home/volumes
,新建es
目录并授权。
cd /home/volumes
mkdir es
chomd 777 es/
Es使用宿主机虚拟内存区域,因此需要修改max_map_count
参数。修改有两种方式:
- 直接执行命令
sudo sysctl -w vm.max_map_count=262144
- 修改文件
sudo vi /etc/sysctl.conf
// 添加到最后一行
vm.max_map_count=262144
- yaml文件
version: '3.5'
services:
es:
build: es
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
container_name: es
environment:
- node.name=es
- cluster.name=anson-es
- cluster.initial_master_nodes=es
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /home/volumes/es:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- anson-net
- Dockerfile
复制ik
插件到容器
FROM elasticsearch:7.6.2
COPY /elasticsearch-analysis-ik-7.6.2/ /usr/share/elasticsearch/plugins/elasticsearch-analysis-ik-7.6.2/
4. Cassandra
配置容器标签和环境变量、端口、挂载的路径。cqlsh
服务是实现容器启动后自动建表,因为容器启动后才能只能cqlsh,所以命令中加入等待80秒再执行schema.cql
。
version: '3.5'
services:
cassandra:
image: cassandra:3.11.10
container_name: cassandra
environment:
- CASSANDRA_DC=datacenter1
- CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch
- CASSANDRA_LISTEN_ADDRESS=cassandra
- CASSANDRA_BROADCAST_ADDRESS=cassandra
- CASSANDRA_SEEDS=cassandra
ports:
- "9042:9042"
restart: always
volumes:
- ./cassandra/cassandra.yaml:/opt/cassandra/conf/cassandra.yaml
- /home/volumes/cassandra:/var/lib/cassandra
networks:
- anson-net
cqlsh:
image: cassandra:3.11.10
container_name: cqlsh
depends_on:
- cassandra
volumes:
- ./cassandra/schema.cql:/schema.cql
command: /bin/bash -c "sleep 80 && echo loading cassandra keyspace && cqlsh cassandra -f /schema.cql"
networks:
- anson-net
5. MySQL
version: '3.5'
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: test_db
ports:
- 3306:3306
volumes:
- ./db:/docker-entrypoint-initdb.d
- /home/volumes/db:/var/lib/mysql
networks:
- anson-net
表与数据也是在创建容器时自动初始化,因此需要注意表不要重复创建及数据不要重复导入,使用IF NOT EXISTS
和INSERT IGNORE INTO
。
如:
CREATE TABLE IF NOT EXISTS `example` (
`id` int(64) NOT NULL AUTO_INCREMENT,
`value` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
)
INSERT IGNORE INTO `example` VALUES (1, 'abc');
6. MinIO
version: '3.5'
services:
minio:
image: minio/minio:latest
container_name: minio
environment:
MINIO_ROOT_USER: anson
MINIO_ROOT_PASSWORD: 123456
command: server /home/volumes/minio
ports:
- 9000:9000
volumes:
- /home/volumes/minio:/data
networks:
- anson-net
ps: 官网给的集群配置文件地址https://raw.githubusercontent.com/minio/minio/master/docs/orchestration/docker-compose/docker-compose.yaml
7. RabbitMQ
- YAML
设置容器版本,连接用户的账号密码及挂载路径。
version: '3.5'
services:
rabbit:
build: rabbit
image: rabbitmq:3.8.16
container_name: rabbit
environment:
- RABBITMQ_DEFAULT_USER=anson
- RABBITMQ_DEFAULT_PASS=123456
ports:
- 5672:5672
- 15672:15672
volumes:
- /home/volumes/rabbit:/var/lib/rabbitmq
networks:
- anson-net
- Dockerfile
启动后台插件,方便管理
FROM rabbitmq:3.8.16-management
RUN rabbitmq-plugins enable --offline rabbitmq_mqtt rabbitmq_federation_management rabbitmq_stomp
8. Nginx
- YAML
设置容器版本,挂载路径。
version: '3.5'
services:
nginx:
image: nginx:latest
container_name: nginx
network_mode: 'host'
ports:
- '80:80'
restart: always
volumes:
- ./html:/usr/share/nginx/html
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
networks:
- anson-net
- nginx.conf
Nginx配置文件,路径转发
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 6000;
proxy_connect_timeout 6000;
proxy_send_timeout 6000;
proxy_read_timeout 6000;
send_timeout 6000;
client_max_body_size 200m;
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
#root /home/newtranx/new_document/html;
#index index.html;
}
location /api/v1 {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
9. OpenJDK 8
- YAML
version: '3.5'
services:
jar:
build: jar
ports:
- 8080:8080
networks:
- anson-net
- Dockerfile
复制jar到容器并运行
FROM openjdk:8
ADD anson-0.0.1-SNAPSHOT.jar /anson.jar
ENTRYPOINT ["java", "-jar", "/anson.jar"]
ENV LANG C.UTF-8
10. docker compose
直接运行compose命令。
docker-compose up --build -d
11. MinIO配置
compose结束后,需配置minIO的bucket。浏览器打开http://{IP}:9000
,输入用户名、密码进行登陆。点击页面右下角,创建bucket
12. 总结
Docker比Ansible简单太多了,而且不需要怎么关注宿主机的环境,方便快捷,不过还是需要点时间学习一下。附上完整的YAML。
version: '3.5'
services:
cassandra:
image: cassandra:3.11.10
container_name: cassandra
environment:
- CASSANDRA_DC=datacenter1
- CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch
- CASSANDRA_LISTEN_ADDRESS=cassandra
- CASSANDRA_BROADCAST_ADDRESS=cassandra
- CASSANDRA_SEEDS=cassandra
ports:
- "9042:9042"
restart: always
volumes:
- ./cassandra/cassandra.yaml:/opt/cassandra/conf/cassandra.yaml
- /home/volumes/cassandra:/var/lib/cassandra
networks:
- anson-net
cqlsh:
image: cassandra:3.11.10
container_name: cqlsh
depends_on:
- cassandra
volumes:
- ./cassandra/schema.cql:/schema.cql
command: /bin/bash -c "sleep 80 && echo loading cassandra keyspace && cqlsh cassandra -f /schema.cql"
networks:
- anson-net
es:
build: es
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
container_name: es
environment:
- node.name=es
- cluster.name=anson-es
- cluster.initial_master_nodes=es
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /home/volumes/es:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- anson-net
rabbit:
build: rabbit
image: rabbitmq:3.8.16
container_name: rabbit
environment:
- RABBITMQ_DEFAULT_USER=anson
- RABBITMQ_DEFAULT_PASS=123456
ports:
- 5672:5672
- 15672:15672
volumes:
- /home/volumes/rabbit:/var/lib/rabbitmq
networks:
- anson-net
minio:
image: minio/minio:latest
container_name: minio
environment:
MINIO_ROOT_USER: anson
MINIO_ROOT_PASSWORD: 123456
command: server /home/volumes/minio
ports:
- 9000:9000
volumes:
- /home/volumes/minio:/data
networks:
- anson-net
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: test_db
ports:
- 3306:3306
volumes:
- ./db:/docker-entrypoint-initdb.d
- /home/volumes/db:/var/lib/mysql
networks:
- anson-net
nginx:
image: nginx:latest
container_name: nginx
network_mode: 'host'
ports:
- '80:80'
restart: always
volumes:
- ./html:/usr/share/nginx/html
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
networks:
- anson-net
jar:
build: jar
ports:
- 8081:8081
- 8084:8084
networks:
- anson-net
networks:
anson-net:
driver: bridge