0%

Flask部署

1
2
3
4
5
6
7
使用 supervisor 管理进程
http://liyangliang.me/posts/2015/06/using-supervisor/
sudo pip3 install supervisor

首先来看 supervisord 的配置文件。安装完 supervisor 之后,可以运行echo_supervisord_conf 命令输出默认的配置项,也可以重定向到一个配置文件里:

echo_supervisord_conf > /etc/supervisord.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
去除里面大部分注释和“不相关”的部分,我们可以先看这些配置:

[unix_http_server]
file=/tmp/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用
;chmod=0700 ; socket 文件的 mode,默认是 0700
;chown=nobody:nogroup ; socket 文件的 owner,格式: uid:gid

;[inet_http_server] ; HTTP 服务器,提供 web 管理界面
;port=127.0.0.1:9001 ; Web 管理后台运行的 IP 和端口,如果开放到公网,需要注意安全性
;username=user ; 登录管理后台的用户名
;password=123 ; 登录管理后台的密码

[supervisord]
logfile=/tmp/supervisord.log ; 日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB ; 日志文件大小,超出会 rotate,默认 50MB
logfile_backups=10 ; 日志文件保留备份数量默认 10
loglevel=info ; 日志级别,默认 info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ; pid 文件
nodaemon=false ; 是否在前台启动,默认是 false,即以 daemon 的方式启动
minfds=1024 ; 可以打开的文件描述符的最小值,默认 1024
minprocs=200 ; 可以打开的进程数的最小值,默认 200

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致
;serverurl=http://127.0.0.1:9001 ; 通过 HTTP 的方式连接 supervisord

; 包含其他的配置文件
[include]
files = relative/directory/*.ini ; 可以是 *.conf 或 *.ini
1
2
3
4
5
6
我们把上面这部分配置保存到 /etc/supervisord.conf(或其他任意有权限访问的文件),然后启动 supervisord(通过 -c 选项指定配置文件路径,如果不指定会按照这个顺序查找配置文件:$CWD/supervisord.conf, $CWD/etc/supervisord.conf, /etc/supervisord.conf):

supervisord -c /etc/supervisord.conf

查看 supervisord 是否在运行:
ps aux | grep supervisord
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
WSGI服务器
pip3 install gunicorn

https://www.cnblogs.com/xmxj0707/p/8452881.html
$ gunicorn -w 4 app:app --error-logfile - --worker-class eventlet

配置文件编写
gunicorn -c gunicorn.conf run:app -k eventlet
python3 run.py --host=0.0.0.0 --port=8888 --no-reload
sudo apt-get install gunicorn
gunicorn -b 0.0.0.0:8889 run:app -k eventlet

#配置文件内容
import os
from gevent import monkey
monkey.patch_all()
import multiprocessing
debug = False
bind = "0.0.0.0:5001"
pidfile = "gunicorn.pid"
logfile = "error.log"
workers = multiprocessing.cpu_count()*2 + 1
worker_class = "gevent" # 异步非阻塞
reload = True # 自动重新加载
#daemon = True
1
2
3
4
5
6
7
8
9
10
11
主要问题:http的socket如何在https的网站使用??
https://segmentfault.com/q/1010000013762709
# socket代理配置
location /socket.io/ {
proxy_pass http://192.54.2.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
注意:
配置前端socket连接时别带端口,但要配置到https://ineedtm.com/而不是htt...://ineedtm.com/socket.io/
1
2
3
4
5
6
https://www.jianshu.com/p/260f18aa5462
配置证书

/usr/local/bin/gunicorn -c /www/wwwroot/swzl.iboy.tech/config.py run:app

gunicorn -c config.py run:app
1
2
3
4
5
6
开启异步队列
celery worker -A run.celery --loglevel=info --pool=eventlet -E


celery worker -A run.celery --loglevel=info --pool=eventlet -E
export C_FORCE_ROOT="True"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
启动
这两个命令是不一样的,supervisord 是启动 supervisor 守护进程(类似于 systemd 或 init.d 这种用于托管进程的服务),supervisorctl 是 supervisord 的命令行控制台。
supervisorctl -c /www/wwwroot/swzl.iboy.tech/supervisord.conf

supervisord -c /www/wwwroot/swzl.iboy.tech/supervisord.conf

重启服务 supervisord -c -n /www/wwwroot/swzl.iboy.tech/supervisord.conf


启动supervisor

1
$ supervisor –c /etc/supervisor/supervisor.conf


8、 启动服务并添加开机启动
#启动服务
$ /usr/local/bin/supervisord -c /etc/supervisor/supervisor.conf

#添加开机启动
$ sudo vim /etc/rc.local
/usr/local/bin/supervisord -c /www/wwwroot/swzl.iboy.tech/supervisord.conf

后台运行
nohup /usr/local/bin/gunicorn -c /www/wwwroot/swzl.iboy.tech/config.py run:app



上面这个命令会进入 supervisorctl 的 shell 界面,然后可以执行不同的命令了:
> status # 查看程序状态
> stop swzl # 关闭 swzl 程序
> start swzl # 启动 swzl 程序
> restart swzl # 重启 swzl 程序
> reread # 读取有更新(增加)的配置文件,不会启动新添加的程序
> update # 重启配置文件修改过的程序


上面这些命令都有相应的输出,除了进入 supervisorctl 的 shell 界面,也可以直接在 bash 终端运行:

$ supervisorctl status
$ supervisorctl stop swzl
$ supervisorctl start swzl
$ supervisorctl restart swzl
$ supervisorctl reread
$ supervisorctl update

查看进程 ps -ef | grep supervisord
杀掉
创建supervisor.sock

sudo touch /var/run/supervisor.sock
sudo chmod 777 /var/run/supervisor.sock
1
2
3
4
5
6
7
8
9
10
11
12
13
Nginx操作
Centos nginx重启

重启Nginx

service nginx restart
/etc/init.d/nginx stop
/etc/init.d/nginx start

Ubuntu Nginx

$sudo service nginx start
$sudo service nginx stop
1
2
3
4
5
6
7
8
9
10
11
12
cd /var/lib/dpkg/info/
util-linux
python-gunicorn
gunicorn
supervisor
这里只列出了部分错误信息,实际上输出的内容极多,多得让人眼都瞎。
原因我不知道,但经过一番Google,我找到了一个解决办法:
cd /var/lib/dpkg/info/
sudo mv python-gunicorn python-gunicorn.bak
sudo mv gunicorn gunicorn.bak
sudo mv util-linux util-linux.bak
sudo mv supervisor supervisor.bak

Docker+Gunicorn部署Flask

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
https://wizardforcel.gitbooks.io/the-flask-mega-tutorial-2017-zh/docs/19.html
用docker部署flask+gunicorn+nginx
https://www.cnblogs.com/xuanmanstein/p/7692256.html

知乎Flask部署 Flask + Docker 无脑部署新手教程 - 知乎
https://zhuanlan.zhihu.com/p/78432719

docker教程
https://yeasy.gitbooks.io/docker_practice/image/dockerfile/workdir.html

删除镜像
docker rmi imgid
删除容器
docker rm conid
进入容器
docker exec -it conid bash

重启容器服务
service docker restart

构建容器镜像
单步测试
docker build -t flask-copy:latest -f Dockerfile1 --no-cache .

docker build -t flask --no-cache .

sudo docker build -t 'testflask' .

查看容器配置
docker logs swzl

查看进程
docker ps |grep swzl

后台守护进程
Docker Dockerfile 定制镜像
https://www.cnblogs.com/lienhua34/p/5170335.html

sudo docker run -it --rm=true 698277f85e44 ls

进入终端
docker run -it --rm=true 698277f85e44 /bin/sh

进入python
docker run -it --rm=true 698277f85e44

docker之Dockerfile实践
https://www.cnblogs.com/lienhua34/p/5170335.html

运行 生产环境运行(以daemon方式运行)
sudo docker run -d -p 5000:5000 --name swzl swzl


激活虚拟环境
source venv/bin/activate

从容器中构建新的镜像


docker commit -m="Created from img" -a="iBoy" e537d42ba392 flask-v1:1.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

FROM python:3.6-alpine
WORKDIR /www/wwwroot/test
COPY requirements.txt requirements.txt
RUN python -m venv venv
RUN venv/bin/pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
RUN venv/bin/pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN mkdir -p /www/wwwroot/swzl.iboy.tech
COPY app.py /www/wwwroot/swzl.iboy.tech
COPY wsgi.conf /www/wwwroot/swzl.iboy.tech
RUN pip --no-cache-dir install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
#CMD gunicorn -c gun.conf app:app

/usr/local/bin/gunicorn -c /www/wwwroot/swzl.iboy.tech/config.py run:app

docker run -p 8889:8889 --name swzl bc678a304 gunicorn config.py run:app

测试运行:docker run -p 8889:8889 --name swzl bc678a304 gunicorn config.py run:app

docker run -p 5000:5000 --name swzl flask-v1:1.0

docker run --rm -p 8889:8889 --name swzl

docker run --name swzl -d -p 8889:8889 --rm swzl:latest

docker history node-vanilla
1
2
3
4
PWD使用
base=https://github.com/docker/machine/releases/download/v0.14.0 &&
curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
sudo install /tmp/docker-machine /usr/local/bin/docker-machine

VsCode远程连接Docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vim /lib/systemd/system/docker.service
原docker.service配置中的ExecStart配置项

ExecStart=/usr/bin/dockerd -H unix://

修改为

ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375

重启Docker配置生效

systemctl daemon-reload
systemctl restart docker
————————————————

1. 登录阿里云Docker Registry

1
$ sudo docker login --username=yang.hao@aliyun.com registry.cn-hangzhou.aliyuncs.com

用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码。

您可以在访问凭证页面修改凭证密码。

2. 从Registry中拉取镜像

1
$ sudo docker pull registry.cn-hangzhou.aliyuncs.com/iboy/qmx:[镜像版本号]

3. 将镜像推送到Registry

1
$ sudo docker login --username=yang.hao@aliyun.com registry.cn-hangzhou.aliyuncs.com$ sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/iboy/qmx:[镜像版本号]$ sudo docker push registry.cn-hangzhou.aliyuncs.com/iboy/qmx:[镜像版本号]

请根据实际镜像信息替换示例中的[ImageId]和[镜像版本号]参数。

4. 选择合适的镜像仓库地址

从ECS推送镜像时,可以选择使用镜像仓库内网地址。推送速度将得到提升并且将不会损耗您的公网流量。

如果您使用的机器位于VPC网络,请使用 registry-vpc.cn-hangzhou.aliyuncs.com 作为Registry的域名登录,并作为镜像命名空间前缀。

5. 示例

使用”docker tag”命令重命名镜像,并将它通过专有网络地址推送至Registry。

1
$ sudo docker imagesREPOSITORY                                                         TAG                 IMAGE ID            CREATED             VIRTUAL SIZEregistry.aliyuncs.com/acs/agent                                    0.7-dfb6816         37bb9c63c8b2        7 days ago          37.89 MB$ sudo docker tag 37bb9c63c8b2 registry-vpc.cn-hangzhou.aliyuncs.com/acs/agent:0.7-dfb6816

使用”docker images”命令找到镜像,将该镜像名称中的域名部分变更为Registry专有网络地址。

1
$ sudo docker push registry-vpc.cn-hangzhou.aliyuncs.com/acs/agent:0.7-dfb6816
iBoy wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!