Flask+python3+supervisor+redis+docker+nginx技术架构web项目docker化(二)

栏目: Python · 发布时间: 7年前

内容简介:手里有一个web项目,代码按照前端代码库、后端代码库分别在GitHub上,分散带来的结果是,不容易持续集成,比如你可能需要很多的job去保证一个项目的正常运作,但是这个项目也不是特别大,所以尝试将代码融合,于此同时将代码docker化,用于持续部署。原来的代码使用gunicorn+gevent+supervisor+flask+DB的架构;具体的细节如下:本地服务器搭建了一个nginx域名服务器,里面区分PC端还是手机端;

背景

手里有一个web项目,代码按照前端代码库、后端代码库分别在GitHub上,分散带来的结果是,不容易持续集成,比如你可能需要很多的job去保证一个项目的正常运作,但是这个项目也不是特别大,所以尝试将代码融合,于此同时将代码 docker 化,用于持续部署。

技术架构

原来的代码使用gunicorn+gevent+supervisor+flask+DB的架构;具体的细节如下:

本地服务器搭建了一个nginx域名服务器,里面区分PC端还是手机端;

访问域名通过nginx,访问前端静态页面的内容

静态页面中加载指定地址的数据,提供数据的服务由flask后端提供接口;

后端提供的接口,通过访问 redis 缓存和 mongodb 数据库,返回相应的数据;

docker-compose

上篇文章说了flask项目是怎么拆分和组合的,但是上次仅仅是使用docker,多个容器之间使用的--link连接起来的,本篇文章将介绍如何使用docker-compose代替原来的多个docker命令;

  • docker compose是什么可以自行搜索,我直接上我的docker-compose.yml

    version: '3'
        
        services:
             flask:
                image: "flask:latest"
                restart: always
                depends_on:
                  - mongoDB 
               - redisDB
                tty: true
                stdin_open: false
                environment:
                  SLEEP_SECOND: 10
                container_name: flask
                logging:
                  driver: "json-file"
                  options:
                    max-size: "200M"
                    max-file: "10"
                command: "gunicorn manage:app -k gevent -w 4 -b 0.0.0.0"
                volumes:
                  - $HOME/logs:/app/logs
networks:
              - inline-network
            ports:
              - "8000:8000"
    
          matrix:
            image: "flask:latest"
            restart: always
            depends_on:
              - mongoDB
              - redisDB
            tty: true
            stdin_open: false
            environment:
              SLEEP_SECOND: 10
            container_name: matrix
            command: "flask matrix"
            volumes:
              - $HOME/logs:/app/logs
              - /etc/localtime:/etc/localtime
            networks:
             - inline-network
    
          broadcast:
            image: "flask:latest"
            restart: always
            depends_on:
              - mongoDB
              - redisDB
            tty: true
            stdin_open: false
            environment:
              SLEEP_SECOND: 10
            container_name: broadcast
            command: "flask broadcast"
            volumes:
              - $HOME/logs:/app/logs
              - /etc/localtime:/etc/localtime
            networks:
              - inline-network
    
        redisDB:
          image: "redis:latest"
          container_name: redis
          restart: always
          networks:
            inline-network:
              aliases:
                - redis
          ports:
            - "6379:6379"
    
        mongoDB:
          image: "mongo:latest"
          restart: always
          container_name: mongo
          ports:
            - "27017:27017"
          volumes:
            - /var/lib/mongo:/data/db
          networks:
            inline-network:
              aliases:
                - mongo
    
    networks:
      inline-network:
        driver: "bridge"
    
```
  • 解释:所有的启动的dontainer都在inline-network网络环境中,所以可以直接使用aliases指定的名字作为数据库连接时候的host,本来是不打算将数据库的端口的,只给flask用,但是后面由于切换的时候是现切换数据库,在切换后段flask的镜像,所以就将数据库端口和宿主机绑定了。
  • 其中flask、matrix、broadcast,都是之前代码中的功能,使用supervisor启动的,现在单独启动三个docker进程去完成。

前端docker

前端的PC端和移动端,都使用npm构建成dist文件,然后通过nginx定向到指定的dist文件内容就可以,所以我们对前端的代码也进行了docker化,使用的是nginx;

  • dockerfile:

    FROM nginx:1.15-alpine
    
    COPY dist/ /usr/share/nginx/html
    
    COPY default.conf /etc/nginx/conf.d/default.conf
    
    ENTRYPOINT nginx -g "daemon off;"
  • default.conf

    server {
         listen       80;
         server_name  localhost;
     
         #charset koi8-r;
         #access_log  /var/log/nginx/host.access.log  main;
     
         location / {
             root   /usr/share/nginx/html;
             index  index.html index.htm;
             try_files $uri $uri/ /index.html;
         }
     
     
         #error_page  404              /404.html;
     
         # redirect server error pages to the static page /50x.html
         #
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   /usr/share/nginx/html;
         }
     }
  • PC端和移动端使用的dockerfile和default.conf都是上面的;
  • 在服务其部署的时候,只需要部署域名服务,将PC端和移动端的docker镜像映射到宿主机的8088和8087端口,服务器nginx配置中根据PC请求和移动端的请求,定位到指定的PC/移动端nginx镜像即可。

总结

本次镜像化大概花了一周的时间,包括测试和解决一些坑,很多知识点包括原来的项目都不是太熟悉,所以记录一下,可能回头会发现这些很LOW,但是相信对一些人还是有用的。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Introduction to Linear Optimization

Introduction to Linear Optimization

Dimitris Bertsimas、John N. Tsitsiklis / Athena Scientific / 1997-02-01 / USD 89.00

"The true merit of this book, however, lies in its pedagogical qualities which are so impressive..." "Throughout the book, the authors make serious efforts to give geometric and intuitive explanations......一起来看看 《Introduction to Linear Optimization》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具