refactor: move to docker swarm
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
BryantHe 2023-12-09 17:35:13 +08:00
parent f3c980f8f7
commit 0daad7a229
9 changed files with 72 additions and 100 deletions

View File

@ -1 +1,9 @@
.venv
venv/
local*
*.env
*.venv
env/
ENV/
env.bak/
venv.bak/

View File

@ -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
- 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

View File

@ -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
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"]

View File

@ -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 分钟后过期所有已有链接
})

View File

@ -1,6 +1,6 @@
import os
from chassis.config import Config as _Config
from pychassislib import Config as _Config
class Config(_Config):

8
docker-compose-build.yml Normal file
View File

@ -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

32
docker-compose-run.yml Normal file
View File

@ -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

View File

@ -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" ]

View File

@ -1,3 +1,3 @@
git+https://gitea.bearcatlog.com/BryantStudio/chassis.git@main#egg=chassis[apiflask,nameko]
pychassislib[nameko,apiflask,schema]
psutil