0.说明
1.Jenkins必要插件、配置、Job创建、Gogs配置等参考测试环境步骤
2.生产环境Django项目一般nginx+uwsgi+django部署;
3.项目容器介绍
容器1:Django+uwsgi作为核心应用程序,处理客户端的请求;
容器2:Nginx反向代理服务并处理静态资源请求。
4.流水线处理过程参考框架图如下:
1)用户向Gogs推送(push)代码,webhook触发jenkins中项目job;
2)项目job,从Gogs中拉取代码并通过Publish Over SSH传送至Docker服务器;SSH远程通过Docker-compose build两个容器,并启动两个容器。
1.Publish Over SSH远程执行过程
#删除nginx容器
docker inspect local_cmdb_nginx_1 -f '{{.Name}}' > /dev/null
if [ $? -eq 0 ] ;
then
echo "docker local_cmdb_nginx_1 exist" `date +%Y%m%d%H%M%S`>> /var/log/jenkins.tmp
docker rm -f local_cmdb_nginx_1
else
echo "docker local_cmdb_nginx_1 don't exist" `date +%Y%m%d%H%M%S`>> /var/log/jenkins.tmp
fi
#删除django容器
docker inspect local_cmdb_django_1 -f '{{.Name}}' > /dev/null
if [ $? -eq 0 ] ;
then
echo "docker local_cmdb_django_1 exist" `date +%Y%m%d%H%M%S`>> /var/log/jenkins.tmp
docker rm -f local_cmdb_django_1
else
echo "docker local_cmdb_nginx_1 don't exist" `date +%Y%m%d%H%M%S`>> /var/log/jenkins.tmp
fi
# 由于向Docker容器中拷贝文件会影响递归结构,所以需要将项目目录先压缩
cd /root/
ll local_cmdb
if [ $? -ne 0 ]; then
echo " project push succeed" `date +%Y%m%d%H%M%S`>> /var/log/jenkins.tmp
fi
tar -czvf local_cmdb.tar.gz local_cmdb
echo `pwd`
cd /root/local_cmdb
mv -f /root/local_cmdb.tar.gz ./
#构建容器
docker-compose build
if [ $? -ne 0 ]; then
echo " container build failed" `date +%Y%m%d%H%M%S`>> /var/log/jenkins.tmp
fi
#启动容器
docker-compose up -d
if [ $? -ne 0 ]; then
echo " container up failed" `date +%Y%m%d%H%M%S`>> /var/log/jenkins.tmp
fi
2.Docker-compose部署项目
项目大致的目录结构
├── config
│ ├── nginx.conf nginx配置文件
├── docker-compose.yml compose yml文件
├── Dockerfile_django django+uwsgi容器Dockerfile
├── Dockerfile_nginx nginx容器Dockerfile
├── local_cmdb
│ ├── local_cmdb.ini uwsgi配置文件
1)docker-compose.yml文件
version: "2.3"
networks: #两个容器自定义网络(默认桥接),不适用lins通信
nginx_network:
driver: bridge
django_network:
driver: bridge
services: #build两个容器服务
django:
build:
context: . #指定Dockerfile文件目录
dockerfile: Dockerfile_django #指定Dockerfile文件名称
ports: #指定容器端口映射
- "8000:8000"
expose: #暴露容器端口
- "8000"
# volumes:
networks: #挂在容器网络
- django_network
# depends_on:
restart: always
tty: true
stdin_open: true
nginx:
build:
context: .
dockerfile: Dockerfile_nginx
ports:
- "80:80"
expose:
- "80"
volumes:
- ./config/nginx.conf:/etc/nginx/nginx.conf
- ./static:/root/local_cmdb/static
networks:
- nginx_network
depends_on:
- django
restart: always
2)Django+Uwsgi容器Dockerfile
#基础镜像
FROM almalinux
#作者
MAINTAINER luorf
#安装python39等必要组件
RUN yum install -y python39 gcc python39-devel mysql-devel && yum clean all && rm -rf /var/cache/yum/*
#添加项目包
ADD local_cmdb.tar.gz /root/
#配置Python环境
RUN ln -s /usr/bin/python3.9 /usr/bin/python && ln -s /usr/bin/pip3.9 /usr/bin/pip
#安装uwsgi
RUN pip install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple
#安装项目需要Python组件
RUN pip install -r /root/local_cmdb/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
#暴露端口
EXPOSE 8000
#uwsgi启动项目
ENTRYPOINT /usr/local/bin/uwsgi -i /root/local_cmdb/local_cmdb/local_cmdb.ini
uwsgi配置文件local_cmdb.ini
[uwsgi]
master=true
chdir=/root/local_cmdb
module=local_cmdb.wsgi
py-autoreload=1
lazy-apps=true
socket=0.0.0.0:8000 #socket IP为0.0.0.0,不能是127.0.0.1
processes=4
buffer-size=32768
#daemonize=uwsgi.log
logto=/root/local_cmdb/local_cmdb/uwsgi.log
log-maxsize = 5000000
vacuum = true
disable-logging = true
3)Nginx容器Dockerfile
#基础镜像
FROM nginx
#作者
MAINTAINER luorf
#拷贝项目需要的nginx配置文件
COPY config/nginx.conf /etc/nginx/nginx.conf
#暴露80端口
EXPOSE 80
#启动nginx服务
CMD ["nginx", "-g", "daemon off;"]
nginx配置文件
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
upstream django_web{
ip_hash;
server 172.26.37.126:8000; #IP为django容器宿主机IP
}
server {
listen 80;
charset utf-8;
location /static {
alias /root/local_cmdb/static;
}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass django_web;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
3.测试
提交代码
提交修改
# git add .
# git commit -m "docker.django.uwsgi.nginx"
# git push origin master
gogs自动推送确认:略
jenkins脚本执行确认:略
远程Docker服务器镜像制作成功,容器启动正常,django web访问及利用正常
# docker image ls|grep local_cmdb
local_cmdb_django latest 155ecb1fda8c About a minute ago 622MB
local_cmdb_nginx latest 889dd5e2066a 13 hours ago 141MB
# docker ps |grep local_cmdb
0448073ebae8 local_cmdb_nginx "/docker-entrypoint.…" 2 minutes ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp local_cmdb_nginx_1
31cba33003bd local_cmdb_django "/bin/sh -c '/usr/lo…" 2 minutes ago Up 2 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp local_cmdb_django_1
# curl -I http://172.26.37.126/admin
)
自动发布成功。
参考URL
https://www.jianshu.com/p/787565c8f7e2
https://blog.csdn.net/weixin_42134789/article/details/115878467
https://blog.csdn.net/weixin_43810267/article/details/119883350
https://www.jianshu.com/p/e7f185c0e7e8
https://www.cnblogs.com/Dxd-python/p/10642132.html