From 0daad7a2298c98604641d30f88b40ff665cdb9e5 Mon Sep 17 00:00:00 2001 From: BryantHe Date: Sat, 9 Dec 2023 17:35:13 +0800 Subject: [PATCH] refactor: move to docker swarm --- .dockerignore | 8 +++++ .drone.yml | 34 +++++++-------------- Dockerfile | 14 +++++---- app/__init__.py | 7 ++--- app/config/config.py | 2 +- docker-compose-build.yml | 8 +++++ docker-compose-run.yml | 32 ++++++++++++++++++++ docker-compose.yml | 65 ---------------------------------------- requirements.txt | 2 +- 9 files changed, 72 insertions(+), 100 deletions(-) create mode 100644 docker-compose-build.yml create mode 100644 docker-compose-run.yml delete mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore index 1d17dae..d174e49 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,9 @@ .venv +venv/ +local* +*.env +*.venv +env/ +ENV/ +env.bak/ +venv.bak/ \ No newline at end of file diff --git a/.drone.yml b/.drone.yml index 9bc72eb..1b56c30 100644 --- a/.drone.yml +++ b/.drone.yml @@ -22,37 +22,23 @@ clone: disable: true steps: -- name: deploy-k8s-master +- name: build-push-image-and-deploy image: appleboy/drone-ssh settings: host: - from_secret: k8s_master + from_secret: diyue_host username: ubuntu key: - from_secret: k8s_key + from_secret: diyue_key port: 22 script_stop: true script: - - cd /var/www/pzx-web-api + - cd /var/ddk/pzx-web-api - sudo git fetch - sudo git reset --hard ${DRONE_COMMIT} - - sudo docker-compose down - - sudo docker-compose up -d --build --force-recreate - - sudo docker image prune -f -- name: deploy-k8s-worker - image: appleboy/drone-ssh - settings: - host: - from_secret: k8s_worker - username: ubuntu - key: - from_secret: k8s_key - port: 22 - script_stop: true - script: - - cd /var/www/pzx-web-api - - sudo git fetch - - sudo git reset --hard ${DRONE_COMMIT} - - sudo docker-compose down - - sudo docker-compose up -d --build --force-recreate - - sudo docker image prune -f \ No newline at end of file + - docker-compose -f docker-compose-build.yml build + - source .apiflask.env + - echo "$DOCKER_PASSWORD" | docker login $DOCKER_REGISTRY -u $DOCKER_USERNAME --password-stdin + - docker push $DOCKER_REGISTRY/bryant/ddk/pzx-web-api:latest + - docker stack deploy -c docker-compose-run.yml ddk + - docker image prune -f \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2769b81..6e8774c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,10 @@ -FROM python:3.11 -RUN python3 -m pip uninstall -y chassis -# 拷贝依赖 -COPY requirements.txt . +FROM python:3.11-slim + +# 拷贝项目文件 +COPY . /app +# 设置工作路径 +WORKDIR /app # 安装依赖 -RUN python3 -m pip install -i https://mirrors.cloud.tencent.com/pypi/simple --trusted-host=mirrors.cloud.tencent.com -r requirements.txt \ No newline at end of file +RUN python3 -m pip install -i https://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com -r requirements.txt +# 启动时运行命令 +CMD ["sh", "docker-deploy.sh"] \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py index 57a05c6..f5618c8 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,14 +1,12 @@ -import os - # from dotenv import load_dotenv from apiflask import APIFlask -from chassis.flask_nameko import FlaskPooledClusterRpcProxy +from pychassislib.namekoproxy_pool import FlaskPooledServiceRpcProxy from app.config.config import Config from app.util.common import basedir -rpc = FlaskPooledClusterRpcProxy() +rpc = FlaskPooledServiceRpcProxy() def register_blueprints(apiflask_app): @@ -34,6 +32,7 @@ def load_rpc_client(apiflask_app): rpc.init_app(apiflask_app, extra_config={ 'INITIAL_CONNECTIONS': 2, 'MAX_CONNECTIONS': 10, + 'POOL_RECYCLE': 360 # 6 分钟后过期所有已有链接 }) diff --git a/app/config/config.py b/app/config/config.py index 62f8f4e..e010ea2 100644 --- a/app/config/config.py +++ b/app/config/config.py @@ -1,6 +1,6 @@ import os -from chassis.config import Config as _Config +from pychassislib import Config as _Config class Config(_Config): diff --git a/docker-compose-build.yml b/docker-compose-build.yml new file mode 100644 index 0000000..ce6c5fe --- /dev/null +++ b/docker-compose-build.yml @@ -0,0 +1,8 @@ +version: "3.8" + +services: + pzx-web-api: + image: gitea.bearcatlog.com/bryant/ddk/pzx-web-api:latest + build: + context: . + dockerfile: ./Dockerfile \ No newline at end of file diff --git a/docker-compose-run.yml b/docker-compose-run.yml new file mode 100644 index 0000000..c94f6bf --- /dev/null +++ b/docker-compose-run.yml @@ -0,0 +1,32 @@ +version: "3.8" + +services: + pzx-web-api: + image: gitea.bearcatlog.com/bryant/ddk/pzx-web-api:latest + env_file: + - .apiflaskenv + - .apiflask.env + ports: + - target: 5000 + published: 7000 + mode: ingress + deploy: + mode: replicated + replicas: 2 + update_config: + parallelism: 1 + failure_action: rollback + restart_policy: + condition: on-failure + delay: 5s + max_attempts: 3 + window: 120s + placement: + constraints: + - 'node.role == worker' + networks: + - pzx-web-api-network + +networks: + pzx-web-api-network: + driver: overlay \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 1e68c51..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,65 +0,0 @@ -version: "3" - -services: - pzx-web-api-1: - build: - context: . - dockerfile: ./Dockerfile - container_name: pzx-web-api-1 - hostname: pzx-web-api-1 - ports: - - "5010:5000" - volumes: - - .:/app - environment: - TZ: "Asia/Shanghai" - LOG_LEVEL: "DEBUG" - env_file: - - .apiflaskenv - - .apiflask.env - working_dir: /app - tty: true - restart: always - command: ["sh", "docker-deploy.sh"] - pzx-web-api-2: - build: - context: . - dockerfile: ./Dockerfile - container_name: pzx-web-api-2 - hostname: pzx-web-api-2 - ports: - - "5020:5000" - volumes: - - .:/app - environment: - TZ: "Asia/Shanghai" - LOG_LEVEL: "DEBUG" - env_file: - - .apiflaskenv - - .apiflask.env - working_dir: /app - tty: true - restart: always - command: [ "sh", "docker-deploy.sh" ] - pzx-web-api-3: - build: - context: . - dockerfile: ./Dockerfile - container_name: pzx-web-api-3 - hostname: pzx-web-api-3 - ports: - - "5030:5000" - volumes: - - .:/app - environment: - TZ: "Asia/Shanghai" - LOG_LEVEL: "DEBUG" - env_file: - - .apiflaskenv - - .apiflask.env - working_dir: /app - tty: true - restart: always - command: [ "sh", "docker-deploy.sh" ] - - diff --git a/requirements.txt b/requirements.txt index 89df5c1..e6de5da 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -git+https://gitea.bearcatlog.com/BryantStudio/chassis.git@main#egg=chassis[apiflask,nameko] +pychassislib[nameko,apiflask,schema] psutil