雲服務器中執行:yum update
安裝並驗證 docker、docker-compose
,參考:docker學習總結2 - docker\docker-compose安裝配置
根目錄下依次執行:mkdir /opt/vhosts/portainer
cd /opt/vhosts/portainer
touch docker-compose.yml
復制內容到 docker-compose.yml 中:
version: '3'
services:
portainer:
image: portainer/portainer-ce
container_name: portainer
restart: always
ports:
- 9001:9000
command: -H unix:///var/run/docker.sock
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- portainer_data:/data
volumes:
portainer_data:
踩坑:
(1)第一次排查過程:
portainer官網:https://docs.portainer.io/v/ce-2.11/start/install/server/docker/linux
docker官網:https://hub.docker.com/r/portainer/portainer
訪問portainer官網後,查看如何在linux部署後把commond那行去掉了,問題解決
(2) 第二次發現沒有識別到我在服務器部署的容器,繼續排查:
發現我把docker-compose.yml
配錯了,在- /var/run/docker.sock:/var/run/docker.sock
最後少了個k,問題排查了2小時
portainer:http://162.14.119.65:9001
後,創建管理員賬號:admin / [email protected]
FROM python:3.8-buster
ENV PYTHONUNBUFFERED=1
ADD ./requirements.txt /app/requirements.txt
RUN sh -c "echo 'Asia/Shanghai' > /etc/timezone" \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple \
&& cd /app \
&& mkdir static \
&& pip install -r ./requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
#是否將源代碼打包到鏡像內?取決於什麼因素?頻繁的發版本就不要放裡面了,只需要改下代碼提價後,git自動拉取就好了
# ADD . /app
EXPOSE 8000
ENTRYPOINT cd /app; python manage.py collectstatic -c --no-input; gunicorn -b 0.0.0.0:8000 auto_test_platform.wsgi;
# 不管加不加 migrate ,需要注意到對後續流程的影響
# python manage.py migrate
- 關掉
add . /app
,沒有將代碼打包進鏡像,意味著:我們開發了新功能,只需要 git pull 一下代碼,重啟下容器,就可以在linux中部署開發的功能了。- 如果打開
add . /app
,我們就將當前版本的代碼放入鏡像中,別人拉取代碼鏡像+其他部署需要的鏡像部署容器後,進入鏡像配置下 .env文件,啟動容器就可以使用這個版本的功能了。
mkdir /opt/vhosts/auto-test-plt-project
cd /opt/vhosts/auto-test-plt-project
git clone https://gitee.com/jiao-tengfei/auto_test_platform.git
git pull origin master
注意:
我這塊拉取了100多M的代碼,因為開發過程中我上傳過其他資料文檔,所以比較大。
注意你的代碼倉庫名必須是:auto_test_plt
DEBUG=False
DATABASE_URL=mysql://auto_test_plt:atp2021$Atstudy@mysql-8:3306/auto_test_plt
TEST_PLT_API_TIMEOUT_CONNECT=3.1
TEST_PLT_API_TIMEOUT_RESP=30
LIST_PER_PAGE=10
LOG_DIR=/var/log/django
ERROR_LOG_FILE=error.log
INFO_LOG_FILE=info.log
LOG_TEST_PLT_LEVEL=WARNING
CELERY_BROKER_URL=amqp://rabbit:[email protected]:5672/test_plt
CELERY_RESULT_EXPIRES=259200
CELERY_TASK_TIME_LIMIT=600
DINGTALK_WEB_HOOK_TOKEN=a97afd37850226e4f52bcf03f38ddaaeb5bdd6be626515e623e5005adb26c8a6
DINGTALK_WEB_HOOK_SIGN=SEC4b24231218a79134c9ee9602d54ccaadb8f7d3b41e698d217d5df2fced9ee972
注意:
(1)將CELERY_BROKER_URL
的 ip 值修改為你的雲服務器 ip**,這個用於連接 rabbitmq 的虛擬機test_plt,如果配置錯誤,會導致異步任務執行失敗,界面報錯 500,這個坑太深,我踩了4個小時。
(2)配置你自己的DINGTALK_WEB_HOOK_TOKEN
、DINGTALK_WEB_HOOK_SIGN
構建自動化測試平台 docker 鏡像,從前面可以看出這個鏡像是用來運行我們開發的代碼,並創建部署所需的 docker volume 數據卷,創建數據卷的意義在於我們刪除了容器,不至於將我們寫的N多條測試用例都給刪除了,容器的數據卷會將數據都映射到容器外面。
# 構建鏡像
cd /opt/vhosts/auto-test-plt-project/auto_test_platform
docker build -t auto_test_platform:v1.0.0 .
# 創建數據卷
cd /opt/vhosts/auto-test-plt-project
mkdir -p ./mysql-data
mkdir -p ./rabbitmq-data
mkdir -p ./logs/rabbitmq
mkdir -p ./logs/django
mkdir -p ./logs/worker
mkdir -p ./logs/beat
mkdir -p ./logs/flower
mkdir -p ./logs/nginx
mkdir -p ./nginx/
cd /opt/vhosts/auto-test-plt-project
touch nginx/nginx.conf
nginx.conf
#user nobody;
worker_processes 1;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name auto_test_platform;
location /static/ {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /static/;
}
location / {
proxy_pass http://auto_test_platform-web:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
}
cd /opt/vhosts/auto-test-plt-project
touch docker-compose.yml
docker-compose.yml
中,並執行:docker-compose up -d
version: '3'
services:
db:
# *注意:該鏡像默認已經包含CONVERT_TZ所需的時區數據,不需要再從官網下載導入
image: mysql
container_name: mysql-8
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --innodb_buffer_pool_size=512M
restart: always
ports:
- 3306:3306
volumes:
- ./mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: [email protected]
MYSQL_DATABASE: auto_test_plt
MYSQL_USER: auto_test_plt
MYSQL_PASSWORD: atp2021$$Atstudy
# *注意:小心在docker-compose yml中$字面量必須用$$代替,盡量避免在密碼中使用$。
networks:
- default
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
depends_on:
- db
restart: always
ports:
- 8080:80
environment:
- PMA_HOST=mysql-8
- PMA_PORT=3306
- UPLOAD_LIMIT=20M
networks:
- default
rabbitmq:
image: rabbitmq:management
container_name: rabbitmq-common
# *注意:先建立目錄
volumes:
- ./rabbitmq-data/:/var/lib/rabbitmq/
restart: always
ports:
- 5672:5672
- 15672:15672
environment:
- RABBITMQ_DEFAULT_USER=rabbit
- RABBITMQ_DEFAULT_PASS=atstudy
networks:
- default
auto_test_plt:
# *注意:先git clone源碼然後構建鏡像 docker build -t auto_test_platform:v1.0.0 .
image: auto_test_platform:v1.0.0
container_name: auto_test_platform-web
depends_on:
- db
working_dir: /app
# *注意:先建立目錄
volumes:
- ./auto_test_platform:/app
- ./logs/django:/var/log/django
expose:
- 8000
networks:
- default
# 注意worker和beat的日志級別,穩定投產之後就不要用info了
celery_worker:
image: auto_test_platform:v1.0.0
container_name: auto_test_platform-celery_worker
depends_on:
- auto_test_plt
- rabbitmq
working_dir: /app
# *注意:先建立目錄volumes
volumes:
- ./auto_test_platform:/app
- ./logs/worker:/var/log/worker
entrypoint: celery -A auto_test_platform worker -l info -f /var/log/worker/celery-worker.log
networks:
- default
celery_beat:
image: auto_test_platform:v1.0.0
container_name: auto_test_platform-celery_beat
depends_on:
- auto_test_plt
- rabbitmq
working_dir: /app
# *注意:先建立目錄
volumes:
- ./auto_test_platform:/app
- ./logs/beat:/var/log/beat
entrypoint: celery -A auto_test_platform beat -l info -f /var/log/beat/celery-beat.log
networks:
- default
celery_flower:
image: auto_test_platform:v1.0.0
container_name: auto_test_platform-celery_flower
depends_on:
- auto_test_plt
- rabbitmq
working_dir: /app
ports:
- 5555:5555
volumes:
- ./auto_test_platform:/app
- ./logs/flower:/var/log/flower
entrypoint: celery -A auto_test_platform flower --port=5555 --broker_api=http://rabbit:[email protected]:15672/api/
networks:
- default
nginx:
image: nginx:latest
container_name: auto_test_platform-nginx
depends_on:
- auto_test_plt
# *注意:先建立目錄,復制nginx.conf
volumes:
- ./auto_test_platform/static:/static
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./logs/nginx:/var/log/nginx
ports:
- 80:80
networks:
- default
networks:
default:
driver: bridge
5555(auto_test_platform-celery_flower)
80(auto_test_platform-nginx)
8080(phpmyadmin)
5672、15672(rabbitmq-common)
3306(mysql-8)
9001( portainer)
自動化測試平台:http://162.14.119.65/admin 【root / [email protected]】
portainer:http://162.14.119.65:9001【admin / [email protected]】
phpmyadmin:http://162.14.119.65:8080/【auto_test_plt / atp2021$Atstudy】
rabbitmq:http://162.14.119.65:15672/【rabbit / atstudy】
flower:http://162.14.119.65:5555/
rabbitmq
中創建 host
portainer
,重啟相關報錯容器後,全綠,進入各個容器查看日志是否報錯注意:
(1)啟動報錯:發現我少寫了 ngix 的yml,補充後,繼續查看容器日志是否有報錯的
(2)啟動報錯:2022-07-27 13:23:34.405268+00:00 [noti] <0.44.0> Application rabbit exited with reason: >>{ {cannot_log_to_file,"/var/log/rabbitmq/[email protected]解決思路:
groupadd rabbitmq
useradd -m -g rabbitmq rabbitmq
chown -R rabbitmq:rabbitmq /var/log/rabbitmq/
還是沒解決,最後,我直接把yml裡面的日志數據卷給刪掉了,好氣啊(3)beat報錯:因為我們沒有在rabbitmq中創建test_plt,所以 beat連接不上自動就會報錯
(1)打開 mysql workbench
,菜單 server 》 data export
,去勾選5張表,因為之前migrate的數據庫已經有數據了,會產生沖突,導出數據後並壓縮成 zip 文件
(2)將zip文件導入 phpmyadmin 中