文章目录
单个容器可以参考
进步ing
- docker-compose 编排python容器
- docker-compose 编排mysql容器
- docker-compose 编排超小nginx容器
- Docker Nginx服务器部署简单静态网页
- docker-compose 编排Apache服务器(httpd)
- 远程连接docker中mysql容器
- docker-compose容器编排综合(nginx+apache+flask)
一、项目概述
![](https://gitee.com/diyunlong/images-lib/raw/master/img/20210608204930.png)
项目结构
本文结构主要如下
二、相关挂载文件编写
/root/nginx_web1_web1_flask_flask/
flask1
| requirements.txt
| run.sh
| server.py
flask2
| requirements.txt
| run.sh
| server.py
nginx
| conf
| | nginx.conf
| conf.d
| | default.conf
| html
| | hohai
| | | index.html
| logs
| | 空
web1
| web
| | index.html
| index.html
web2
| web
| | index.html
| index.html
docker-compose.yml
(1)nginx相关
可参考之前单个nginx容器搭建过程
1)apache、 创建需要挂载的相应的挂载目录
mkdir -p /root/nginxdocker/conf
mkdir -p /root/nginxdocker/html
mkdir -p /root/nginxdocker/conf.d
mkdir -p /root/nginxdocker/logs
将build的vue项目dist下的文件拷贝到 /root/nginxdocker/html 中即可,这是等下需要访问的页面,也可以随手写一个html文件
2)创建conf/nginx.conf文件
(这里我用的是官方默认的配置文件,可以替换成自己的配置文件)
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
3) 创建conf.d/default.conf文件
实现负载均衡
upstream mydocker {
server 192.169.0.2;
server 192.169.0.3;
}
upstream myflask {
server 192.169.0.4:8082;
server 192.169.0.5:8083;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location /hohai {
root /usr/share/nginx/html;
index index.html;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_pass http://mydocker;
}
location /flask {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_pass http://myflask;
}
}
(2)apache相关
web1/index.html
web1
web2/index.html
web2
(3)flask相关
可参考之前单个flask容器搭建过程(flask1 flask2两个文件夹相同,可以改变返回值)
1)run.sh
pip install -r /code/requirements.txt -i https://pypi.douban.com/simple
python /code/server.py
2)requirements.txt
flask
3)server.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/flask')
def hello_world1():
return 'Hello World zhangsan flask 1'
if __name__ == '__main__':
app.run(host='0.0.0.0', port='8082')
4)mysql相关
参考之前的博客
![](https://gitee.com/diyunlong/images-lib/raw/master/img/20210608105215.png)
目录结构
conf 下新建my.cnf
[mysqld]
user=mysql
default-storage-engine=INNODB
#character-set-server=utf8
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[client]
#utf8mb4字符集可以存储emoji表情字符
#default-character-set=utf8
default-character-set=utf8mb4
[mysql]
#default-character-set=utf8
default-character-set=utf8mb4
datadir存放myslq数据,暂时不放东西
三、docker-compose.yml编写
version: "3"
services:
nginx-lsb:
container_name: nginx-lsb
image: nginx:alpine
ports:
- "80:80"
privileged: true
volumes:
- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/html:/usr/share/nginx/html
- ./nginx/logs:/var/log/nginx
- ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
networks:
nginx-lsb:
ipv4_address: 192.169.0.100
web1:
container_name: web1
image: httpd
ports:
- "8080:80"
privileged: true
volumes:
- "./web1/:/usr/local/apache2/htdocs/"
networks:
nginx-lsb:
ipv4_address: 192.169.0.2
web2:
container_name: web2
image: httpd
ports:
- "8081:80"
privileged: true
volumes:
- "./web2/:/usr/local/apache2/htdocs/"
networks:
nginx-lsb:
ipv4_address: 192.169.0.3
flask1:
image: python:3.7
container_name: flask1
ports:
- "8082:80"
volumes:
- ./flask1/:/code
privileged: true
command: bash /code/run.sh
networks:
nginx-lsb:
ipv4_address: 192.169.0.4
flask2:
image: python:3.7
container_name: flask2
ports:
- "8083:80"
volumes:
- ./flask2/:/code
privileged: true
command: bash /code/run.sh
networks:
nginx-lsb:
ipv4_address: 192.169.0.5
mysql:
restart: always
image: mysql:5.7.16
container_name: my_mysql
volumes:
- /root/mysqldocker/mydir:/mydir
- /root/mysqldocker/datadir:/var/lib/mysql
- /root/mysqldocker/conf/my.cnf:/etc/my.cnf
- /root/mysqldocker/source:/docker-entrypoint-initdb.d
environment:
- "MYSQL_ROOT_PASSWORD=123456"
- "MYSQL_USER=root2"
- "MYSQL_PASSWORD=123456"
- "MYSQL_DATABASE=test"
- "TZ=Asia/Shanghai"
ports:
- "3306:3306"
networks:
nginx-lsb:
ipv4_address: 192.169.0.6
networks:
nginx-lsb:
driver: bridge
ipam:
config:
- subnet: 192.169.0.0/16
四、启动运行
(1)启动
docker-compose up -d
[root@iZ2ze5ot8cnsficuqqnoj5Z nginx_web1_web1_flask_flask]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5554d884444d python:3.7 "bash /code/run.sh" 5 seconds ago Up 4 seconds 0.0.0.0:8082->80/tcp flask1
0ab3a00aefca nginx:alpine "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp nginx-lsb
ff1f691f053c httpd "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:8081->80/tcp web2
6d94d4b139ae httpd "httpd-foreground" 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp web1
0da2efb0619d python:3.7 "bash /code/run.sh" 5 seconds ago Up 4 seconds 0.0.0.0:8083->80/tcp flask2
(2)修改nginx配置文件
docker-compose restart nginx-lsb
(3) 查看新建的网桥
docker network ls
NETWORK ID NAME DRIVER SCOPE
db9692bfcd3d nginxweb1web1flaskflask_nginx-lsb bridge local
(4)查看新建网桥下挂的所有容器信息
[
{
"Name": "nginxweb1web1flaskflask_nginx-lsb",
"Id": "db9692bfcd3d94b8908b4da86c05c6ca434e814377490693dd29d81af2824cdd",
"Created": "2021-06-08T19:13:24.884575589+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "192.169.0.0/16"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"2181d2c698e41071be3687538447c8a8bbf18e416a06ac2279fbf2cb4d5c3dc1": {
"Name": "flask1",
"EndpointID": "0a0241ae7bc47f24bbf3c60e624900a9ff4570967f0f5c27a3d58820f513be9b",
"MacAddress": "02:42:c0:a9:00:04",
"IPv4Address": "192.169.0.4/16",
"IPv6Address": ""
},
"317618a2aac2dc61e268a5a1527a5bcc6c86b0361b36f98b11f605cc60788031": {
"Name": "nginx-lsb",
"EndpointID": "17d252abae428399ef3a12c38d4163238e695c582e5f175e13a670f9df58c062",
"MacAddress": "02:42:c0:a9:00:64",
"IPv4Address": "192.169.0.100/16",
"IPv6Address": ""
},
"bcf007011599891cfd2f0a5889994eaf2b75a62bfbabd421dff83d9350883be7": {
"Name": "web1",
"EndpointID": "e3f2ff553bb21695864e25ed0d240c2deeefc5ec8bca53633d61b6ef5f20d137",
"MacAddress": "02:42:c0:a9:00:02",
"IPv4Address": "192.169.0.2/16",
"IPv6Address": ""
},
"da81649173289eb1d4c39e3ac0960d4237aa0e141f1df4c3b05bbaf8eca348ff": {
"Name": "web2",
"EndpointID": "8b35f3011b838f6597f4e93b2b629fb8cce29f6a8b559c0df7ad43b6a3fcc752",
"MacAddress": "02:42:c0:a9:00:03",
"IPv4Address": "192.169.0.3/16",
"IPv6Address": ""
},
"e62325db16d07e50b3792bf67918c0b2fd45a1d52038ca557f2684f988550b97": {
"Name": "flask2",
"EndpointID": "ad551edf338bf9f281ea1a6bcdca0551ed6bf9239bf54bf62f10a8a64442448d",
"MacAddress": "02:42:c0:a9:00:05",
"IPv4Address": "192.169.0.5/16",
"IPv6Address": ""
},
"fbc1fbd0ff7d08c9e310e66dba901cdcf812ffb7c270a0f979340ef1e7206d3a": {
"Name": "my_mysql",
"EndpointID": "2128ec47466f16a405c58ae3a4096b48b6b8dc7a36bcbc3cbc19924b19c00b1d",
"MacAddress": "02:42:c0:a9:00:06",
"IPv4Address": "192.169.0.6/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "nginx-lsb",
"com.docker.compose.project": "nginxweb1web1flaskflask"
}
}
]
五、测试
(1)测试nginx下面的静态页面
因为我这里加了一个网页
location /hohai {
root /usr/share/nginx/html;
index index.html;
}
所以
![](https://gitee.com/diyunlong/images-lib/raw/master/img/20210608212548.png)
(2)测试apache下面的静态页面
访问:http://47.-------.82:80,快速刷新,可以看到两个不同的页面,这是nginx负载均衡的结果
(3)测试flask接口
访问:http://47.-------.82:80/flask,快速刷新,也可以看到两个不同的页面,这也是nginx负载均衡的结果