Loading

Ubuntu下docker部署

使用docker进行容器化集成部署

远程服务器更新源

更新ubuntu的apt源

sudo apt-get update

安装包允许apt通过HTTPS使用仓库

sudo dpkg --configure -a
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common

添加Docker官方GPG key【这个是国外服务器地址,所以网路不好的时候,会失败!在网路好的情况下,多执行几次就没问题了,如果下一行结果不是OK,则执行多几遍】

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

设置Docker稳定版仓库

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

添加仓库后,更新apt源索引

sudo apt-get update

前面的准备工作完成以后,接下来安装最新版Docker CE(社区版)

sudo apt-get install -y docker-ce

检查Docker CE是否安装正确

sudo docker pull ubuntu:20.04
# docker pull registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:ubuntu20.04

要获取基础镜像可以直接去官方网站上获取: https://hub.docker.com/

阿里云镜像:https://cr.console.aliyun.com/cn-heyuan/instances

启动与停止

安装完成Docker后,默认已经启动了docker服务,如需手动控制docker服务的启停,可执行如下命令

# 启动docker
sudo service docker start

# 停止docker
sudo service docker stop

# 重启docker
sudo service docker restart

安装docker-compose,这种安装方式是基于Linux操作系统的源来决定安装版本的,所以往往版本会比较低。

apt install -y docker-compose

ubuntu安装yarn

(可以直接在本地打包好前端项目后,放到ubuntu中)

导入软件源的 GPG key 并且添加 Yarn APT 软件源到你的系统,运行下面的命令:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

一旦软件源被启用,升级软件包列表,并且安装 Yarn。

sudo apt update
sudo apt install yarn -y

上面的命令同时会安装 Node.js。如果你已经通过 nvm 安装了 Node,跳过 Node.js 安装过程:

sudo apt install --no-install-recommends yarn

完成之后,通过打印 Yarn 版本来验证安装过程:

yarn --version

目录结构

拉取项目:https://github.com/HkwJsxl/feisheng.git

服务器,目录结构:

/home/feisheng/
├── conf/
│   └── nginx/
│       └── web.conf
├── docker-compose.yml
└── fsweb/
└── fsapi/

远程部署服务器终端操作:

mkdir -p /home/feisheng/conf/nginx/
touch /home/feisheng/conf/nginx/web.conf
vim /home/feisheng/docker-compose.yml

阿里云镜像:https://cr.console.aliyun.com/cn-heyuan/instances
登录:docker login --username=hkwjsxl registry.cn-hangzhou.aliyuncs.com

编排 nginx服务容器

拉取nginx

docker pull registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:nginx1.23.3
docker tag registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:nginx1.23.3 nginx:1.23.3

/home/feisheng/docker-compose.yml,代码:

version: '3'
services:
  nginx:
    image: nginx:1.23.3
    restart: always
    ports:
      - "80:80"
    container_name: "nginx"
    volumes:
      - ./fsweb:/home/fsweb
      - ./conf/nginx/web.conf:/etc/nginx/conf.d/nginx.conf
    networks:
      - default
networks:
  default:

/home/feisheng/conf/nginx/web.conf,这个文件是��备给nginx容器内部的虚拟主机配置文件

vim /home/feisheng/conf/nginx/web.conf

web.conf,代码:

server {
    listen       80;
    server_name  www.fs.hkwpro.com;
    # client_max_body_size    1000M;
    # root         /opt/dist;

    # client_max_body_size 20M;

    # Load configuration files for the default server block.

    location / {
        root  /home/fsweb;
        index index.html;
        # 单入口的url路径转发
        try_files $uri $uri/ /index.html;
    }
}

在远程部署终端启动容器服务

docker-compose -f /home/feisheng/docker-compose.yml up -d

服务端项目部署

根据我们之前分析的项目部署架构,后端需要调用mysql、redis、elasticsearch。因此我们需要先把这些外部软件先预装。

目录结构

/home/luffycity/
├── conf/
│   ├── redis/
│   │   └── master.conf
│   ├── mysql/
│   └── nginx/
│       └── web.conf
├── data/
│   ├── redis/
│   ├── elastic/
│   └── mysql/
├── docker-compose.yml
├── fsweb/
├── fsapi/
├── initdb/
│   └── feisheng.sql
├── logs/
│   └── mysql
└── plugins/

编排mysql服务容器

拉取mysql镜像

# 拉取
docker pull registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:mysql8.0.32
# 改名
docker tag registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:mysql8.0.32 mysql:8.0.32
# 删除改名之前的镜像
docker rmi registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:mysql8.0.32

在远程部署服务器上进行终端操作,创建目录结构

cd /home/feisheng
mkdir -p data/mysql logs/mysql initdb
vim /home/feisheng/docker-compose.yml

/home/feisheng/docker-compose.yml,代码:

version: '3'
services:
  nginx:
    image: nginx:1.23.3
    restart: always
    ports:
      - "80:80"
    container_name: "nginx"
    volumes:
      - ./fsweb:/home/fsweb
      - ./conf/nginx/web.conf:/etc/nginx/conf.d/nginx.conf
    networks:
      - default

  mysql_master:
    image: mysql:8.0.30
    restart: always
    container_name: "mysql_master"
    networks:
      - default
    environment:
      - "MYSQL_USER=fs"
      - "MYSQL_PASSWORD=root123456"
      - "MYSQL_DATABASE=fs"
      - "TZ=Asia/Shanghai"
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
    ports:
      - 3306:3306
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./logs/mysql:/logs
      - ./initdb:/docker-entrypoint-initdb.d/

networks:
  default:

把SQL文件移动到指定目录

# 将项目下的sql文件复制到服务器的initdb文件夹下(feisheng/docs/fs.sql)

重新启动docker-compose的容器服务

cd /home/feisheng
docker-compose -f docker-compose.yml down
docker-compose -f docker-compose.yml up -d

# 注意:如果mysql容器要初始化数据库,则必须要保证在运行mysql容器之前,data目录是没有任何内容的!!!
# rm -rf /home/feisheng/data/

# 可以通过以下命令进入mysql容器
docker exec -it mysql_master mysql -uroot -proot123456
use fs;
show tables;

编排redis服务容器

拉取redis镜像

docker pull registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:redis7
docker tag registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:redis7 redis:7
docker rmi registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:redis7

创建目录和必须的文件

cd /home/feisheng
mkdir -p conf/redis data/redis/master
touch conf/redis/master.conf
vim conf/redis/master.conf

conf/redis/master.conf,主库配置文件,代码:

requirepass root123456
masterauth root123456
bind 0.0.0.0

# RDB快照
save 900 1
save 300 10
save 60 10000
dir "/data"
rdbcompression yes
dbfilename dump.rdb

# AOF日志
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec

保存redis配置文件,并编辑/home/luffycity/docker-compose.yml

cd /home/feisheng
vim /home/feisheng/docker-compose.yml

代码:

version: '3'
services:
  nginx:
    image: nginx:1.23.3
    restart: always
    ports:
      - "80:80"
    container_name: "nginx"
    volumes:
      - ./fsweb:/home/fsweb
      - ./conf/nginx/web.conf:/etc/nginx/conf.d/nginx.conf
    networks:
      - default

  mysql_master:
    image: mysql:8.0.32
    restart: always
    container_name: "mysql_master"
    networks:
      - default
    environment:
      - "MYSQL_ROOT_PASSWORD=root123456"
      - "MYSQL_USER=fs"
      - "MYSQL_PASSWORD=root123456"
      - "MYSQL_DATABASE=fs"
      - "TZ=Asia/Shanghai"
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
    ports:
      - 3306:3306
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./logs/mysql:/logs
      - ./initdb:/docker-entrypoint-initdb.d/

  redis_master:
    image: redis:7
    restart: always
    networks:
      - default
    ports:
      - "6379:6379"
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    container_name: "redis_master"
    volumes:
      - ./conf/redis/master.conf:/usr/local/etc/redis/redis.conf
      - ./data/redis/master:/data

networks:
  default:

启动服务

docker-compose -f docker-compose.yml down

docker-compose -f docker-compose.yml up -d

# 测试
docker exec -it redis_master redis-cli -a root123456

编排elasticsearch搜索引擎服务容器

拉取es镜像

docker pull registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:elasticsearch7.17.6

docker tag registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:elasticsearch7.17.6 elasticsearch:7.17.6

docker rmi registry.cn-hangzhou.aliyuncs.com/hankewei/hkwimage:elasticsearch7.17.6

在服务器网络安全组中,开放9200,9300这2个端口

image-20230416221427512

编辑/home/feisheng/docker-compose.yml

vim /home/feisheng/docker-compose.yml

代码:(至少2核4G的才能把es启动起来,2核2G的就不用尝试了)

version: '3'
services:
  nginx:
    image: nginx:1.23.3
    restart: always
    ports:
      - "80:80"
    container_name: "nginx"
    volumes:
      - ./fsweb:/home/fsweb
      - ./conf/nginx/web.conf:/etc/nginx/conf.d/nginx.conf
    networks:
      - default

  mysql_master:
    image: mysql:8.0.32
    restart: always
    container_name: "mysql_master"
    networks:
      - default
    environment:
      - "MYSQL_ROOT_PASSWORD=root123456"
      - "MYSQL_USER=fs"
      - "MYSQL_PASSWORD=root123456"
      - "MYSQL_DATABASE=fs"
      - "TZ=Asia/Shanghai"
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
    ports:
      - 3306:3306
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./logs/mysql:/logs
      - ./initdb:/docker-entrypoint-initdb.d/

  redis_master:
    image: redis:7
    restart: always
    networks:
      - default
    ports:
      - "6379:6379"
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    container_name: "redis_master"
    volumes:
      - ./conf/redis/master.conf:/usr/local/etc/redis/redis.conf
      - ./data/redis/master:/data

  elasticsearch_1:
    image: elasticsearch:7.17.6
    restart: always
    container_name: "elasticsearch_1"
    environment:
      - "discovery.type=single-node"
      - "bootstrap.memory_lock=true"
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "xpack.security.enabled=false"
      - "http.cors.enabled=true"
      - "http.cors.allow-origin=*"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - ./data/elastic:/usr/share/elasticsearch/data
      - ./logs/elastic:/var/log/elasticsearch
      - ./plugins:/usr/share/elasticsearch/plugins
    ports:
      - 9200:9200
    networks:
      - default

networks:
  default:

ik分词器下载解压到/home/feisheng/plugins/目录下。

mkdir -p /home/feisheng/data/elastic /home/feisheng/plugins
chmod 777 /home/feisheng/data/elastic

# 下载ik中文分词插件
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.6/elasticsearch-analysis-ik-7.17.6.zip

# 解压保存到es的插件目录
apt install unzip
unzip elasticsearch-analysis-ik-7.17.6.zip -d /home/feisheng/plugins/ik-7.17.6

# 启动服务
docker-compose -f docker-compose.yml down
docker-compose -f docker-compose.yml up -d

# postman中测试访问

post http://47.120.38.34:9200/_analyze?pretty

{
  "analyzer": "ik_max_word",
  "text": "我是中国人,我来自北京。"
}
# 监控docker-compose的所有容器运行的日志信息
docker-compose logs -f

es索引的生成是要结合django项目的ORM来生成,所以暂时先不用执行其他操作,先完成django项目的��署先。

基于Dockerfile定制服务端项目镜像

使用 Dockerfile之前,先切换成国内docker镜像源

mkdir -p /etc/docker
sudo vim /etc/docker/daemon.json


{
    "registry-mirrors" : [
        "https://registry.docker-cn.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://hub-mirror.c.163.com",
        "https://cr.console.aliyun.com/"
    ]
}

下载Miniconda3

官网:https://docs.conda.io/en/latest/miniconda.html

https://repo.anaconda.com/miniconda/Miniconda3-py38_23.1.0-1-Linux-x86_64.sh

创建Dockerfile文件:

vim /home/feisheng/Dockerfile

/home/feisheng/Dockerfile,代码:

FROM ubuntu:20.04
LABEL maintainer="feisheng.Edu"

ENV PYTHONUNBUFFERED 1

COPY ./Miniconda3-py38_23.1.0-1-Linux-x86_64.sh /opt/
COPY ./sources.list /etc/apt/sources.list
COPY ./fsapi /fsapi
COPY ./fsapi/requirements.txt /requirements.txt

RUN apt-get update \
    && apt-get -y install wget \
    && mkdir /root/.conda \
    && bash /opt/Miniconda3-py38_23.1.0-1-Linux-x86_64.sh -b \
    && rm -f /opt/Miniconda3-py38_23.1.0-1-Linux-x86_64.sh

ENV PATH /root/miniconda3/bin:$PATH

RUN conda install pymysql -c conda-forge \
    && conda install uwsgi -c conda-forge \
    && pip install -r /requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple \
    && chmod -R 755 /fsapi

WORKDIR /fsapi

EXPOSE 8000

VOLUME /fsapi

注释版:

# 设置当前镜像的基础镜像,FROM指令只能使用一次,而且在Dockerfile顶部第一个使用
FROM ubuntu:20.04
# 设置描述信息
LABEL maintainer="feisheng.Edu"
# 设置环境变量,PYTHONUNBUFFERED设置python解析器在终端下stdout错误信息,一次性输出,要不在暂存区积攒
ENV PYTHONUNBUFFERED 1

# 复制文件    容器外部:容器内部
COPY ./Miniconda3-py38_23.1.0-1-Linux-x86_64.sh /opt/
COPY ./sources.list /etc/apt/sources.list
COPY ./fsapi /fsapi
COPY ./fsapi/requirements.txt /requirements.txt
# 运行系统命令
RUN apt-get update \  # 更新源
    && apt-get -y install wget \  # 安装wget下载工具
    && mkdir /root/.conda \
    && bash /opt/Miniconda3-py38_4.10.3-Linux-x86_64.sh -b \
    && rm -f /opt/Miniconda3-py38_4.10.3-Linux-x86_64.sh  # 设置环境变量

ENV PATH /root/miniconda3/bin:$PATH  # 设置环境变量

# 给conda全局base虚拟环境安装pymysql和uwsgi
RUN conda install pymysql -c conda-forge \
    && conda install uwsgi -c conda-forge \
    # 递归安装requirements.txt中的所有依赖模块
    && pip install -r /requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple \
    # 设置目录权限
    && chmod -R 755 /fsapi

# 设置进入容器的默认工作目录
WORKDIR /fsapi
# 暴露容器的端口
EXPOSE 8000
# 设置/luffycityapi目录为逻辑卷,方便创建容器时,可以把当前目录的内容映射到容器外面进行持久化存储
VOLUME /fsapi
vim /home/feisheng/sources.list

/home/feisheng/sources.list,代码:

deb http://repo.huaweicloud.com/ubuntu/ focal main restricted
deb http://repo.huaweicloud.com/ubuntu/ focal-updates main restricted
deb http://repo.huaweicloud.com/ubuntu/ focal universe
deb http://repo.huaweicloud.com/ubuntu/ focal-updates universe
deb http://repo.huaweicloud.com/ubuntu/ focal multiverse
deb http://repo.huaweicloud.com/ubuntu/ focal-updates multiverse
deb http://repo.huaweicloud.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://repo.huaweicloud.com/ubuntu focal-security main restricted
deb http://repo.huaweicloud.com/ubuntu focal-security universe
deb http://repo.huaweicloud.com/ubuntu focal-security multiverse

通过docker-compose启动fsapi镜像,docker-compose.yml,代码:

version: '3'
services:
  nginx:
    image: nginx:1.23.3
    restart: always
    ports:
      - "80:80"
    container_name: "nginx"
    volumes:
      - ./fsweb:/home/fsweb
      - ./conf/nginx/web.conf:/etc/nginx/conf.d/nginx.conf
    networks:
      - default

  mysql_master:
    image: mysql:8.0.32
    restart: always
    container_name: "mysql_master"
    networks:
      - default
    environment:
      - "MYSQL_ROOT_PASSWORD=root123456"
      - "MYSQL_USER=fs"
      - "MYSQL_PASSWORD=root123456"
      - "MYSQL_DATABASE=fs"
      - "TZ=Asia/Shanghai"
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
    ports:
      - 3306:3306
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./logs/mysql:/logs
      - ./initdb:/docker-entrypoint-initdb.d/

  redis_master:
    image: redis:7
    restart: always
    networks:
      - default
    ports:
      - "6379:6379"
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    container_name: "redis_master"
    volumes:
      - ./conf/redis/master.conf:/usr/local/etc/redis/redis.conf
      - ./data/redis/master:/data

  api1:
    build: .
    image: fsapi:1.0.0
    container_name: api1
    restart: always
    ports:
      - 8000:8000
    networks:
      - default
    volumes:
      - ./fsapi:/fsapi
    command: >
      sh -c "uwsgi --http :8000 --workers 4 --master --enable-threads --module fsapi.wsgi"
    environment:
      - "DJANGO_SETTINGS_MODULE=fsapi.settings.prod"
      - "MYSQL_PASSWORD=root123456"
      - "REDIS_PASSWORD=root123456"
      - "RONGLIANYUNACCTOKEN=b6c417ddecb64f899a0a48e30c97b55d"
    depends_on:
      - mysql_master
      - redis_master

networks:
  default:

启动服务

cd /home/feisheng

docker-compose -f docker-compose.yml down

docker-compose -f docker-compose.yml up -d

# 如果有es的,等待1分钟后给elasticsearch构建索引
# docker exec -it api1 python manage.py rebuild_index

# 进入api1容器内部
docker exec -it api1 /bin/bash

# 在mysql容器中导入数据
docker cp /home/feisheng/initdb/fs.sql mysql_master:/home/
docker exec -it mysql_master /bin/bash
mysql -uroot -p
root123456
use fs;
set names utf8mb4;
source /home/fs.sql

访问:http://api.fs.hkwpro.com:8000/home/banner/

使用nginx对uwsgi进行反向代理

接下来,我们就需要把api1服务端容器里面的uwsgi的运行模式从http改成socket模式。

docker-compose.yml,修改如下。

version: '3'
services:
  nginx:
    image: nginx:1.23.3
    restart: always
    ports:
      - "80:80"
    container_name: "nginx"
    volumes:
      - ./fsweb:/home/fsweb
      - ./conf/nginx/web.conf:/etc/nginx/conf.d/nginx.conf
    networks:
      - default

  mysql_master:
    image: mysql:8.0.32
    restart: always
    container_name: "mysql_master"
    networks:
      - default
    environment:
      - "MYSQL_ROOT_PASSWORD=root123456"
      - "MYSQL_USER=fs"
      - "MYSQL_PASSWORD=root123456"
      - "MYSQL_DATABASE=fs"
      - "TZ=Asia/Shanghai"
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
    ports:
      - 3306:3306
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./logs/mysql:/logs
      - ./initdb:/docker-entrypoint-initdb.d/

  redis_master:
    image: redis:7
    restart: always
    networks:
      - default
    ports:
      - "6379:6379"
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    container_name: "redis_master"
    volumes:
      - ./conf/redis/master.conf:/usr/local/etc/redis/redis.conf
      - ./data/redis/master:/data


  api1:
    build: .
    image: fsapi:1.0.0
    container_name: api1
    restart: always
    ports:
      - 8000:8000
    networks:
      - default
    volumes:
      - ./fsapi:/fsapi
    command: >
      sh -c "uwsgi --socket :8000 --workers 4 --master --enable-threads --module fsapi.wsgi"
    environment:
      - "DJANGO_SETTINGS_MODULE=fsapi.settings.prod"
      - "MYSQL_PASSWORD=root123456"
      - "REDIS_PASSWORD=root123456"
      - "RONGLIANYUNACCTOKEN=b6c417ddecb64f899a0a48e30c97b55d"
    depends_on:
      - mysql_master
      - redis_master

networks:
  default:

修改nginx配置文件

vim /home/feisheng/conf/nginx/web.conf

conf/nginx/web.conf,如下:

# 集群分发(upstream+uwsgi_pass)
upstream feisheng {
    server 47.120.38.34:8000;
}


server {
    listen  80;
    server_name api.fs.hkwpro.com;

    location / {
        include uwsgi_params;
        uwsgi_pass feisheng;
    }
}


server {
    listen       80;
    server_name  www.fs.hkwpro.com;
    # client_max_body_size    1000M;
    # root         /opt/dist;

    # client_max_body_size 20M;

    # Load configuration files for the default server block.

    location / {
        root  /home/feisheng/fsweb;
        index index.html;
        # 单入口的url路径转发
        try_files $uri $uri/ /index.html;
    }
}


重启docker-compose

docker-compose -f docker-compose.yml down
docker-compose -f docker-compose.yml up -d

# 在mysql容器中导入数据
docker cp /home/feisheng/initdb/fs.sql mysql_master:/home/
docker exec -it mysql_master /bin/bash
mysql -uroot -p
root123456
use fs;
set names utf8mb4;
source /home/fs.sql

# 接下来,我们直接在浏览器中,通过如下地址访问:
http://api.fs.hkwpro.com/home/nav/header/
http://www.fs.hkwpro.com/

针对后端服务器的运营站点提供静态文件的访问支持

直接让前端的nginx容器同时提供服务端静态文件的访问支持。

修改docker-compose.yml,把服务端的静态文件目录映射到nginx容器中。代码:

version: '3'
services:
  nginx:
    image: nginx:1.23.3
    restart: always
    ports:
      - "80:80"
    container_name: "nginx"
    volumes:
      - ./fsapi/static:/home/fsapi/static
      - ./fsweb:/home/fsweb
      - ./conf/nginx/web.conf:/etc/nginx/conf.d/nginx.conf
    networks:
      - default

  mysql_master:
    image: mysql:8.0.32
    restart: always
    container_name: "mysql_master"
    networks:
      - default
    environment:
      - "MYSQL_ROOT_PASSWORD=root123456"
      - "MYSQL_USER=fs"
      - "MYSQL_PASSWORD=root123456"
      - "MYSQL_DATABASE=fs"
      - "TZ=Asia/Shanghai"
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
    ports:
      - 3306:3306
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./logs/mysql:/logs
      - ./initdb:/docker-entrypoint-initdb.d/

  redis_master:
    image: redis:7
    restart: always
    networks:
      - default
    ports:
      - "6379:6379"
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    container_name: "redis_master"
    volumes:
      - ./conf/redis/master.conf:/usr/local/etc/redis/redis.conf
      - ./data/redis/master:/data


  api1:
    build: .
    image: fsapi:1.0.0
    container_name: api1
    restart: always
    ports:
      - 8000:8000
    networks:
      - default
    volumes:
      - ./fsapi:/fsapi
    command: >
      sh -c "uwsgi --socket :8000 --workers 4 --master --enable-threads --module fsapi.wsgi"
    environment:
      - "DJANGO_SETTINGS_MODULE=fsapi.settings.prod"
      - "MYSQL_PASSWORD=root123456"
      - "REDIS_PASSWORD=root123456"
      - "RONGLIANYUNACCTOKEN=b6c417ddecb64f899a0a48e30c97b55d"
    depends_on:
      - mysql_master
      - redis_master

networks:
  default:

修改nginx配置文件web.conf,服务端对应的server配置项中增加一个location配置信息。conf/nginx/web.conf,如下:

upstream feisheng {
    server 47.120.38.34:8000;
}


server {
    listen  80;
    server_name api.fs.hkwpro.com;

    location / {
        include uwsgi_params;
        uwsgi_pass feisheng;
    }

    location /static {
        root /home/fsapi;
    }
}


server {
    listen       80;
    server_name  www.fs.hkwpro.com;
    # client_max_body_size    1000M;
    # root         /opt/dist;

    # client_max_body_size 20M;

    # Load configuration files for the default server block.

    location / {
        root  /home/fsweb;
        index index.html;
        # 单入口的url路径转发
        try_files $uri $uri/ /index.html;
    }
}


完成上面操作以后,我们可以看到当前api服务端的静态文件就提供了http正常浏览了。

重启docker-compose

docker-compose -f docker-compose.yml down
docker-compose -f docker-compose.yml up -d

# 在mysql容器中导入数据
docker cp /home/feisheng/initdb/fs.sql mysql_master:/home/
docker exec -it mysql_master /bin/bash
mysql -uroot -p
root123456
use fs;
set names utf8mb4;
source /home/fs.sql

# 访问(80端口)
http://api.fs.hkwpro.com/home/banner/

启动celery

关于celery的启动,因为需要依赖于django项目的。所以celery的启动自然也就和当前api容器一并运行。

修改celery的启动配置文件

vim /home/feisheng/fsapi/script/fs_celery_worker.ini

代码:

[program:fs_celery_worker]
# 启动命令 conda env list
command=/root/miniconda3/bin/celery -A fsapi worker -l info -n worker1
# 以管理员身份执行运行celery
user=root
numprocs=1
# 项目根目录的绝对路径,通过pwd查看
directory=/fsapi
# 项目虚拟环境
enviroment=PATH="/root/miniconda3/bin"
# 输出日志绝对路径
stdout_logfile=/fsapi/logs/celery.worker.info.log
# 错误日志绝对路径
stderr_logfile=/fsapi/logs/celery.worker.error.log
# 自动启动,开机自启
autostart=true
# 重启
autorestart=true

# 进程启动后跑了几秒钟,才被认定为成功启动,默认1
startsecs=10
# 进程结束后60秒才被认定结束
stopwatisecs=60

# 优先级
priority=999
vim /home/feisheng/fsapi/script/fs_celery_beat.ini

代码:

[program:fs_celery_beat]
# 启动命令 conda env list
command = /root/miniconda3/bin/celery -A fsapi  beat -l info
# 项目根目录的绝对路径,通过pwd查看
directory = /fsapi
# 项目虚拟环境
enviroment = PATH="/root/miniconda3/bin"
# 输出日志绝对路径
stdout_logfile = /fsapi/logs/celery.beat.info.log
# 错误日志绝对路径
stderr_logfile = /fsapi/logs/celery.beat.error.log
# 自动启动,开机自启
autostart = true
# 重启
autorestart = true

# 进程启动后跑了几秒钟,才被认定为成功启动,默认1
startsecs = 10

# 进程结束后60秒才被认定结束
stopwatisecs = 60

# 优先级
priority = 998
vim /home/feisheng/fsapi/script/fs_celery_flower.ini

代码:

[program:fs_celery_flower]
# 启动命令 conda env list
command = /root/miniconda3/bin/celery -A fsapi flower --port=5555
# 项目根目录的绝对路径,通过pwd查看
directory = /fsapi
# 项目虚拟环境
enviroment = PATH="/root/miniconda3/bin"
# 输出日志绝对路径
stdout_logfile = /fsapi/logs/celery.flower.info.log
# 错误日志绝对路径
stderr_logfile = /fsapi/logs/celery.flower.error.log
# 自动启动,开机自启
autostart = true
# 重启
autorestart = true

# 进程启动后跑了几秒钟,才被认定为成功启动,默认1
startsecs = 10

# 进程结束后60秒才被认定结束
stopwatisecs = 60

# 优先级
priority = 990
vim /home/feisheng/fsapi/script/supervisor.service

代码:

[Unit]
Description=supervisor
After=network.target

[Service]
Type=forking
ExecStart=/root/miniconda3/bin/supervisord -n -c /fsapi/script/supervisord.conf
ExecStop=/root/miniconda3/bin/supervisorctl $OPTIONS shutdown
ExecReload=/root/miniconda3/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

服务端的安全组,开放5555端口。

docker-compose,代码:

version: '3'
services:
  nginx:
    image: nginx:1.23.3
    restart: always
    ports:
      - "80:80"
    container_name: "nginx"
    volumes:
      - ./fsapi/static:/home/fsapi/static
      - ./fsweb:/home/fsweb
      - ./conf/nginx/web.conf:/etc/nginx/conf.d/nginx.conf
    networks:
      - default

  mysql_master:
    image: mysql:8.0.32
    restart: always
    container_name: "mysql_master"
    networks:
      - default
    environment:
      - "MYSQL_ROOT_PASSWORD=root123456"
      - "MYSQL_USER=fs"
      - "MYSQL_PASSWORD=root123456"
      - "MYSQL_DATABASE=fs"
      - "TZ=Asia/Shanghai"
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
    ports:
      - 3306:3306
    volumes:
      - ./data/mysql:/var/lib/mysql
      - ./logs/mysql:/logs
      - ./initdb:/docker-entrypoint-initdb.d/

  redis_master:
    image: redis:7
    restart: always
    networks:
      - default
    ports:
      - "6379:6379"
    command:
      ["redis-server", "/usr/local/etc/redis/redis.conf"]
    container_name: "redis_master"
    volumes:
      - ./conf/redis/master.conf:/usr/local/etc/redis/redis.conf
      - ./data/redis/master:/data

  elasticsearch:
    image: elasticsearch:7.17.2
    restart: always
    environment:
      - "discovery.type=single-node"
      - "bootstrap.memory_lock=true"
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "xpack.security.enabled=false"
      - "http.cors.enabled=true"
      - "http.cors.allow-origin=*"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - ./data/elastic:/usr/share/elasticsearch/data
      - ./logs/elastic:/var/log/elasticsearch
      - ./plugins:/usr/share/elasticsearch/plugins
    ports:
      - 9200:9200
    networks:
      - default


  api1:
    build: .
    image: fsapi:1.0.0
    container_name: api1
    restart: always
    ports:
      - 8000:8000
    networks:
      - default
    volumes:
      - ./fsapi:/fsapi
    command: >
      sh -c "supervisord -c script/supervisord.conf && uwsgi --socket :8000 --workers 4 --master --enable-threads --module fsapi.wsgi"
    environment:
      - "DJANGO_SETTINGS_MODULE=fsapi.settings.prod"
      - "C_FORCE_ROOT=1"
      - "MYSQL_PASSWORD=root123456"
      - "REDIS_PASSWORD=root123456"
      - "RONGLIANYUNACCTOKEN=b6c417ddecb64f899a0a48e30c97b55d"
    depends_on:
      - mysql_master
      - redis_master
      - elasticsearch
      
networks:
  default:

启动服务

cd /home/feisheng
docker-compose -f docker-compose.yml down
docker-compose -f docker-compose.yml up -d

# 在mysql容器中导入数据
docker cp /home/feisheng/initdb/fs.sql mysql_master:/home/
docker exec -it mysql_master /bin/bash
mysql -uroot -p
root123456
use fs;
set names utf8mb4;
source /home/fs.sql

# 进入容器
docker exec -it api1 /bin/bash

结束

posted @ 2023-04-27 15:44  hkwJsxl  阅读(94)  评论(0编辑  收藏  举报