Skip to content

Commit a4526db

Browse files
committed
集成 Docker 实现项目容器化脚本部署
在V 2.0的基础上加入Nginx实现简单的集群负载均衡
1 parent 9809154 commit a4526db

File tree

8 files changed

+483
-1
lines changed

8 files changed

+483
-1
lines changed

deploy/README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# 集成 Docker 实现项目容器化脚本部署
2+
- [x] V 1.0: 集成 Docker 实现项目容器化脚本部署
3+
- [x] V 2.0: 在V 1.0的基础上提取环境变量值
4+
- [x] V 3.0: 在V 2.0的基础上加入Nginx实现简单的集群负载均衡
5+
- 访问地址: http://NGINX_IP:NGINX_PORT/
26

37

4-
### 文件目录说明
8+
### 文件目录说明(V 1.0 & V 2.0)
59
```bash
610
# 1. 实例化 MySQL 容器启动时自动执行脚本. *.sh | *.sql | *.sql.gz
711
auto-script
@@ -30,3 +34,36 @@
3034
start.sh
3135
```
3236
`注意: 启动项目前, 请先构建 springboot-mybatis-web 项目镜像`
37+
38+
39+
40+
### 文件目录说明(V 3.0)
41+
```bash
42+
# 1. 初始化脚本文件
43+
auto-script
44+
- mysql
45+
- init_account.sql
46+
- init_tables.sql
47+
- nginx
48+
- html/index.html # nginx 首页页面
49+
- nginx.conf # nginx 配置文件
50+
51+
# 2. 服务编排文件信息
52+
docker-compose.yaml
53+
54+
# 3. 环境变量配置文件
55+
.env
56+
57+
# 4. 服务启动脚本
58+
start.sh
59+
60+
# 5. 持久化本地挂载目录
61+
data
62+
63+
# 6. 持久化数据备份目录
64+
dataBak
65+
66+
# 7. 服务启动日志存储目录
67+
logs
68+
- smw.log
69+
```

deploy/V 3.0/.env

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 实例化 WEB 容器时设置的容器名
2+
WEB_CONTAINER_NAME1="springboot.mybatis.web1"
3+
# 实例化 WEB 容器时设置的容器名
4+
WEB_CONTAINER_NAME2="springboot.mybatis.web2"
5+
# Web 容器对外映射的端口
6+
WEB_PORT1="8082"
7+
# Web 容器对外映射的端口
8+
WEB_PORT2="8083"
9+
# wEB 镜像版本, 默认latest
10+
WEB_IMAGE_VERSION="latest"
11+
12+
# 实例化 NGINX 容器时设置的容器名
13+
NGINX_CONTAINER_NAME="springboot.mybatis.nginx"
14+
# NGINX 镜像版本, 默认latest
15+
NGINX_IMAGE_VERSION="latest"
16+
# NGINX 容器对外映射的端口
17+
NGINX_PORT="85"
18+
19+
# 实例化 MySQL 容器时设置的容器名
20+
MYSQL_CONTAINER_NAME="springboot.mybatis.mysql"
21+
# MySQL 镜像版本, 默认latest
22+
MYSQL_IMAGE_VERSION="5.6"
23+
24+
# MySQL 容器部署的宿主机IP
25+
MYSQL_IP="119.23.106.146"
26+
# MySQL 容器对外映射的端口
27+
MYSQL_PORT="3305"
28+
# MySQL 服务root账户对应的密码
29+
MYSQL_ROOT_PASSWORD="root"
30+
# Web 项目中用到的数据库名
31+
MYSQL_DB="answer"
32+
# Web 项目中连接 MySQL 专用账户
33+
MYSQL_UNAME="docker"
34+
# Web 项目中连接 MySQL 专用账户对应的密码
35+
MYSQL_PASSWD="123456"
36+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** 创建 docker 账户 */
2+
use mysql;
3+
4+
create user 'docker'@'%' identified by '123456';
5+
6+
grant all privileges on answer.* to 'docker'@'%';
7+
8+
flush privileges;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Navicat MySQL Data Transfer
3+
4+
Source Server : 119.23.106.146
5+
Source Server Version : 50723
6+
Source Host : 119.23.106.146:3306
7+
Source Database : answer
8+
9+
Target Server Type : MYSQL
10+
Target Server Version : 50723
11+
File Encoding : 65001
12+
13+
Date: 2018-10-17 17:27:23
14+
*/
15+
16+
SET FOREIGN_KEY_CHECKS=0;
17+
18+
CREATE DATABASE IF NOT EXISTS answer default charset utf8 COLLATE utf8_general_ci;
19+
20+
USE answer;
21+
22+
-- ----------------------------
23+
-- Table structure for `smw_user`
24+
-- ----------------------------
25+
DROP TABLE IF EXISTS `smw_user`;
26+
CREATE TABLE `smw_user` (
27+
`id` bigint(18) NOT NULL AUTO_INCREMENT,
28+
`user_name` varchar(55) DEFAULT NULL,
29+
`sex` tinyint(3) DEFAULT NULL,
30+
`password` varchar(55) DEFAULT NULL,
31+
`email` varchar(55) DEFAULT NULL,
32+
`address` varchar(55) DEFAULT NULL,
33+
`create_time` datetime DEFAULT NULL,
34+
`update_time` datetime DEFAULT NULL,
35+
PRIMARY KEY (`id`)
36+
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
37+
38+
-- ----------------------------
39+
-- Records of smw_user
40+
-- ----------------------------
41+
INSERT INTO `smw_user` VALUES ('1', 'answer', '1', '123456', '[email protected]', 'sz', '2018-10-16 11:10:47', '2018-10-16 11:10:50');
42+
INSERT INTO `smw_user` VALUES ('2', 'admin', '1', '123456', '[email protected]', 'sz', '2018-10-16 11:10:47', '2018-10-16 11:10:50');
43+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Welcome to nginx!</title>
5+
<style>
6+
body {
7+
width: 35em;
8+
margin: 0 auto;
9+
font-family: Tahoma, Verdana, Arial, sans-serif;
10+
}
11+
</style>
12+
</head>
13+
<body>
14+
<h1>Welcome to Answer nginx!</h1>
15+
</body>
16+
</html>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
user nginx;
2+
worker_processes 1;
3+
4+
error_log /var/log/nginx/error.log warn;
5+
pid /var/run/nginx.pid;
6+
7+
8+
events {
9+
worker_connections 1024;
10+
}
11+
12+
13+
http {
14+
include /etc/nginx/mime.types;
15+
default_type application/octet-stream;
16+
17+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
18+
'$status $body_bytes_sent "$http_referer" '
19+
'"$http_user_agent" "$http_x_forwarded_for"';
20+
21+
access_log /var/log/nginx/access.log main;
22+
23+
sendfile on;
24+
#tcp_nopush on;
25+
26+
keepalive_timeout 65;
27+
28+
#gzip on;
29+
30+
upstream www.answer_nginx.com {
31+
server 119.23.106.146:8082;
32+
server 119.23.106.146:8083;
33+
# ip_hash;
34+
}
35+
36+
error_page 404 https://www.baidu.com; #错误页
37+
38+
server {
39+
listen 80;
40+
server_name localhost;
41+
42+
location / {
43+
proxy_pass http://www.answer_nginx.com/smw/user/findUsers;
44+
proxy_redirect default;
45+
proxy_set_header Host $host:$server_port;
46+
}
47+
48+
error_page 500 502 503 504 /50x.html;
49+
location = /50x.html {
50+
root /usr/share/nginx/html;
51+
}
52+
53+
}
54+
}

deploy/V 3.0/docker-compose.yaml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
version: '2'
2+
3+
services:
4+
# MySQL 服务
5+
springboot.mybatis.mysql:
6+
container_name: ${MYSQL_CONTAINER_NAME}
7+
hostname: smm
8+
image: mysql:${MYSQL_IMAGE_VERSION}
9+
environment:
10+
- HOSTNAME=mysql
11+
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
12+
working_dir: /home/smm
13+
# MySQL 在容器启动时会自动执行 /usr/local/bin/docker-entrypoint.sh 该脚本, 并自动执行 /docker-entrypoint-initdb.d 目录下的脚本
14+
# command: /bin/bash -c './script.sh;'
15+
volumes:
16+
- ./data/${MYSQL_CONTAINER_NAME}/datadir:/usr/data
17+
- ./data/${MYSQL_CONTAINER_NAME}/conf:/etc/mysql
18+
- ./data/${MYSQL_CONTAINER_NAME}/smm:/home/smm
19+
- ./auto-script/mysql:/docker-entrypoint-initdb.d
20+
ports:
21+
- ${MYSQL_PORT}:3306
22+
logging:
23+
driver: "json-file"
24+
options:
25+
max-size: "200M"
26+
max-file: "5"
27+
28+
29+
# NGINX 服务
30+
springboot.mybatis.nginx:
31+
container_name: ${NGINX_CONTAINER_NAME}
32+
hostname: smn
33+
image: nginx:${NGINX_IMAGE_VERSION}
34+
environment:
35+
- HOSTNAME=nginx
36+
working_dir: /home/nginx
37+
volumes:
38+
- ./data/${NGINX_CONTAINER_NAME}:/home/nginx
39+
- ./auto-script/nginx/html/index.html:/usr/share/nginx/html/index.html:ro
40+
# - ./auto-script/nginx/default.conf:/etc/nginx/conf.d/default.conf
41+
- ./auto-script/nginx/nginx.conf:/etc/nginx/nginx.conf
42+
expose:
43+
- 80
44+
ports:
45+
- ${NGINX_PORT}:80
46+
logging:
47+
driver: "json-file"
48+
options:
49+
max-size: "200M"
50+
max-file: "5"
51+
52+
53+
# 集群 WEB1
54+
springboot.mybatis.web1:
55+
container_name: ${WEB_CONTAINER_NAME1}
56+
hostname: smw
57+
image: springboot-mybatis-web:${WEB_IMAGE_VERSION}
58+
environment:
59+
- MYSQL_IP=${MYSQL_IP}
60+
- MYSQL_PORT=${MYSQL_PORT}
61+
- MYSQL_DB=${MYSQL_DB}
62+
- MYSQL_UNAME=${MYSQL_UNAME}
63+
- MYSQL_PASSWD=${MYSQL_PASSWD}
64+
working_dir: /home/smw
65+
volumes:
66+
- ./data/${WEB_CONTAINER_NAME1}:/home/smw
67+
ports:
68+
- ${WEB_PORT1}:8888
69+
depends_on:
70+
- springboot.mybatis.mysql
71+
- springboot.mybatis.nginx
72+
logging:
73+
driver: "json-file"
74+
options:
75+
max-size: "200M"
76+
max-file: "5"
77+
78+
79+
# 集群 WEB2
80+
springboot.mybatis.web2:
81+
container_name: ${WEB_CONTAINER_NAME2}
82+
hostname: smw
83+
image: springboot-mybatis-web:${WEB_IMAGE_VERSION}
84+
environment:
85+
- MYSQL_IP=${MYSQL_IP}
86+
- MYSQL_PORT=${MYSQL_PORT}
87+
- MYSQL_DB=${MYSQL_DB}
88+
- MYSQL_UNAME=${MYSQL_UNAME}
89+
- MYSQL_PASSWD=${MYSQL_PASSWD}
90+
working_dir: /home/smw
91+
volumes:
92+
- ./data/${WEB_CONTAINER_NAME2}:/home/smw
93+
ports:
94+
- ${WEB_PORT2}:8888
95+
depends_on:
96+
- springboot.mybatis.mysql
97+
- springboot.mybatis.nginx
98+
- springboot.mybatis.web1
99+
logging:
100+
driver: "json-file"
101+
options:
102+
max-size: "200M"
103+
max-file: "5"

0 commit comments

Comments
 (0)