第1章 概述
1.1 背景
之前的发布流程都是单节点,不具备扩展性,现在设计了可扩展的发布流程;发布流程分两个项目
Job_manager和pool
工作流程:
开发人员提交代码到Git版本仓库;
Jenkins人工/定时触发项目构建;
Jenkins拉取代码、代码编码、打包镜像、推送到镜像仓库;
Jenkins在Docker主机创建容器并发布。
现在的方案:
开发人员提交代码到VisualStudioTeamFoundationServer(tfs)版本仓库;
Jenkins人工/定时触发项目构建;
Jenkins拉取代码、代码编码,重启容器(容器挂载代码,提前启动好容器)
问题:
1打包镜像的过程不稳定,是在有外网的环境下,打包好之后上传镜像到环境中;
2在原有的环境下更新代码git pull ,过程更快点,但是开发人员有直接登陆服务器更改代码的习惯,git pull自动更新会产生问题;
3 想把容器作为一个主机管理提供给开发,pool容器应用官方容器不能安装openssh-server,其他均可以;
4 更新软件包需要手动加载;
5 业务量不大,现有的发布流程,是一个基于非docker环境的一个想法;以下的环境研究也是基于现有环境,做的多主机扩展流程;
1.2 Job_manager发布流程
利用ansible 实现多主机操作,因为考虑灰度发布,所以主机分组;
[root@honey1 cae_job_develop]# cat /etc/ansible/hosts
[develop]
*.*.*.*
*.*.*.*
*.*.*.*
[develop1]
*.*.*.*
[develop2]
*.*.*.*
*.*.*.*
发布总脚本
[root@honey1 cae_job_develop]# cat deploy_job.sh
#判断和创建锁文件是防止多人同时发布;
#!/bin/bash
if [ -f /tmp/deploy.lock ]
then
echo "Somebody is Posting"
exit
fi
touch /tmp/deploy.lock
#日志记录功能
echo "`date` start deploy cae_job_manager" > /var/log/deploy_job.log
cd /www
#拉取代码
git clone http://*.cae_job_manager >/dev/null && echo "git clone cae_job_manager success" >>/var/log/deploy_job.log
#打包代码,代码中有许多遗留文件;保险起见只能完全打包,包括隐藏文件;
cd /www
zip -r cae_job_manager.zip cae_job_manager/ >/dev/null && echo "zip cae_job_manager.zip success" >>/var/log/deploy_job.log
#发布代码到主机节点的过度位置
ansible develop -m copy -a "src=/www/cae_job_manager.zip dest=/tmp" >/dev/null && echo "copy cae_job_manager.zip success" >>/var/log/deploy_job.log
#在一部分节点上删除原有代码
ansible develop1 -m file -a "path=/www/cae_job_manager state=absent" >/dev/null && echo "develop1 delete cae_job_manager success" >>/var/log/deploy_job.log
#发布代码到正式目录下
ansible develop1 -m command -a "unzip /tmp/cae_job_manager.zip -d /www" >/dev/null && echo "develop1 unzip cae_job_manager.zip success" >> /var/log/deploy_job.log
ansible develop1 -m script -a "/www/cae_job_develop/check.sh" && echo "develep1 checkout origin/develop success" >> /var/log/deploy_job.log
#发布环境配置文件
ansible develop1 -m copy -a "src=/www/cae_job_develop/settings_local.py dest=/www/cae_job_manager/cae_job_manager/" && echo "develep1 cp settings_local.py success" >> /var/log/deploy_job.log
#发布程序启动文件
ansible develop1 -m copy -a "src=/www/cae_job_develop/conf/ dest=/www/cae_job_manager/conf/" && echo "develop1 cp supervisor config success" >> /var/log/deploy_job.log
#重启容器,发布代码
ansible develop1 -m script -a "/www/cae_job_develop/boot_job.sh" && echo "develop1 reboot docker success" >> /var/log/deploy_job.log
ansible develop2 -m file -a "path=/www/cae_job_manager state=absent" >/dev/null && echo "develop2 delete cae_job_manager success" >>/var/log/deploy_job.log
ansible develop2 -m command -a "unzip /tmp/cae_job_manager.zip -d /www" >/dev/null && echo "develop2 unzip cae_job_manager success" >>/var/log/deploy_job.log
ansible develop2 -m script -a "/www/cae_job_develop/check.sh" && echo "develep2 checkout origin/develop success" >> /var/log/deploy_job.log
ansible develop2 -m copy -a "src=/www/cae_job_develop/settings_local.py dest=/www/cae_job_manager/cae_job_manager/" && echo "develep2 cp settings_local.py success" >> /var/log/deploy_job.log
ansible develop2 -m copy -a "src=/www/cae_job_develop/conf/ dest=/www/cae_job_manager/conf/" && echo "develop2 cp supervisor config success" >> /var/log/deploy_job.log
ansible develop2 -m script -a "/www/cae_job_develop/boot_job.sh" && echo "develop2 reboot docker success" >> /var/log/deploy_job.log
#删除过度目录的文件
ansible develop -m file -a "path=/tmp/cae_job_manager.zip state=absent"
#删除发布节点的代码目录
/usr/bin/rm /www/cae_job_manager* -rf
#删除发布锁文件
/usr/bin/rm /tmp/deploy.lock -f
echo "`date` finish deploy cae_job_manager" >> /var/log/deploy_job.log
cat /var/log/deploy_job.log
[root@honey1 cae_job_develop]# cat check.sh
#!/bin/bash
cd /www/cae_job_manager/ && git pull
cd /www/cae_job_manager/ && git checkout origin/develop
[root@honey1 cae_job_develop]# ls
boot_job.sh check.sh conf deploy_job.sh settings_local.py
[root@honey1 cae_job_develop]# cat boot_job.sh
#!/bin/bash
chmod +x /www/cae_job_manager/docker_run.sh
for i in `docker ps |grep cae_job|awk '{print $NF}'`
do
docker restart $i
done
conf里是supervisor的配置文件
[root@honey1 cae_job_develop]# ls conf/
cae_job_manager.conf celery_beat.conf celery_worker.conf message_center.conf scrapy_monitor.conf visual_monitor.conf
思考:主机节点上的docker 容器相当于环境;代码挂载到容器上;有一些代码不常变视为一个服务;代码更新的主要是job_manager和pool;另外这个发布没有回滚步骤;开发有快速回滚分支,通过回滚分支回滚;
1.3 Pool部署
[root@honey1 pool_develop]# ls
boot_pool.sh deploy_pool_1.sh deploy_pool.sh install update_pool.sh
[root@honey1 pool_develop]# cat deploy_pool.sh
#!/bin/bash
sv1=develop1
sv2=develop2
if [ -f /tmp/pool.lock ]
then
echo "Somebody is Posting"
exit
fi
touch /tmp/pool.lock
#这个环境代码需要npm构建和bower构建保险起见,而且多主机构建过程时间较长;所以提前把代码放到部署环境中;通过git pull更新;
ansible $sv1 -m script -a "/www/pool_develop/update_pool.sh $sv1"
#这个环境代码应用后的数据,会存在部署的代码项目里;所以需要备份这部分数据
ansible $sv1 -m command -a "cp -a /app/pool/data /tmp"
#删除源代码
ansible $sv1 -m file -a "path=/app/pool state=absent"
#复制代码到正式目录下,然后构建代码;考虑到代码环境问题,没有先构建,在部署代码到节点环境;
ansible $sv1 -m script -a "/www/pool_develop/deploy_pool_1.sh $sv1"
#把备份的数据,放到指定的位置;
ansible $sv1 -m command -a "mv /tmp/data /app/pool/"
#重启应用程序;
ansible $sv1 -m script -a "/www/pool_develop/boot_pool.sh $sv1"
ansible $sv2 -m script -a "/www/pool_develop/update_pool.sh $sv2"
ansible $sv2 -m command -a "cp -a /app/pool/data /tmp"
ansible $sv2 -m file -a "path=/app/pool state=absent"
ansible $sv2 -m script -a "/www/pool_develop/deploy_pool_1.sh $sv2"
ansible $sv2 -m command -a "mv /tmp/data /app/pool/"
ansible $sv2 -m script -a "/www/pool_develop/boot_pool.sh $sv2"
#删除锁文件
rm /tmp/pool.lock -f
[root@honey1 pool_develop]# cat update_pool.sh
#!/bin/bash
echo "`date` $1 start deploy pool" > /var/log/deploy_pool.log
chattr -i /deploy/
cd /deploy/pool && git pull
cd /deploy/pool && git checkout origin/develop
cd /deploy/pool/poolui && npm install >/dev/null && echo "$1 deploy npm install success" >> /var/log/deploy_pool.log
cd /deploy/pool/poolui && bower install --allow-root >/dev/null && echo "$1 deploy bower install success" >> /var/log/deploy_pool.log
[root@honey1 pool_develop]# cat deploy_pool_1.sh
#!/bin/bash
cp -a /deploy/pool/ /app/
chattr +i /deploy/
chmod +x /app/pool/docker/*
cd /app/pool/poolui/ && npm install >/dev/null && echo "$1 app npm install success" >>/var/log/deploy_pool.log
cd /app/pool/poolui/ && bower install --allow-root >/dev/null && echo "$1 app bower install success" >>/var/log/deploy_pool.log
cd /app/pool/poolui/ && npm run build >/dev/null && echo "$1 app build success" >>/var/log/deploy_pool.log
[root@honey1 pool_develop]# cat boot_pool.sh
#!/bin/bash
for i in `docker ps |grep pool|awk '{print $NF}'`
do
docker restart $i
done
if [ $? -eq 0 ]
then
echo "`date` $1 pool docker reboot success" >> /var/log/deploy_pool.log
cat /var/log/deploy_pool.log
fi
1.4 job_manager docker 化发布
Dockfile一键化的发布,网络原因,docker build 不稳定,有些步骤需外网;
root@honey1:/www# ls
cae_job_manager cae_job_manager1 cae_job_manager.conf Dockerfile settings_local.py
root@honey1:/www# cat Dockerfile
FROM ubuntu:16.04
# Install packages
RUN apt-get update && apt-get install -y \
libmysqlclient-dev \
nginx \
python-dev \
python-mysqldb \
python-setuptools \
python-pip \
python-lxml \
openssh-server \
net-tools \
vim \
git \
lrzsz \
supervisor
RUN apt-get install -y libxml2 libxml2-dev libxslt-dev
RUN apt-get -y install tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN mkdir -p /www/cae_job_manager
RUN mkdir /www/log
RUN mkdir /www/static
RUN mkdir /var/log/celery
RUN useradd celery
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN pip install nltk
RUN pip install networkx
RUN pip install https://s3-us-west-2.amazonaws.com/jdimatteo-personal-public-readaccess/nltk-2.0.5-https-distribute.tar.gz
WORKDIR /www/
RUN git clone http://123:5000/tfs/DefaultCollection/CAE/\_git/cae\_job\_manager
WORKDIR /www/cae_job_manager
RUN git checkout origin/develop
ADD settings_local.py /www/cae_job_manager/cae_job_manager/
ADD cae_job_manager.conf /etc/supervisor/conf.d/
RUN pip install -r requirements.txt
RUN pip install uwsgi
RUN pip install pymysql
# Configure Nginx
RUN ln -s /www/cae_job_manager/conf/nginx_cae.conf /etc/nginx/sites-enabled/
RUN rm /etc/nginx/sites-enabled/default
# Run Supervisor (i.e., start MySQL, Nginx, and Gunicorn)
#CMD ["/usr/bin/supervisord","-n","-c","/etc/supervisor/supervisord.conf"]
#RUN chmod 777 docker_run.sh
#CMD ["sh", "docker_run.sh"]
ENTRYPOINT ["/www/cae_job_manager/docker_run.sh"]
登陆harbor,需要设置docker才能登陆
root@honey1:/etc/docker# cat daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"\],
"insecure-registries":["192.168.138.111:8885"]
}
root@honey1:/etc/docker# docker login 192.168.138.111:8885
Username: admin
Password:
Login Succeeded
构想基础镜像[主要是构建环境,预先安装必要的安装包和创建必要的目录],外网环境下;
root@honey1:/www# ls
cae_job_manager.conf Dockerfile DockerfileBase DockerfileTest requirements.txt settings_local.py
root@honey1:/www# docker build -t cae_job_manager:test2 .
root@honey1:/www# docker tag cae_job_manager:test2 192.168.138.111:8885/ispider/cae_job_manager:base
root@honey1:/www# docker push 192.168.138.111:8885/ispider/cae_job_manager:base
root@honey1:/www# cat DockerfileBase
FROM ubuntu:16.04
# Install packages
RUN apt-get update && apt-get install -y \
libmysqlclient-dev \
nginx \
python-dev \
python-mysqldb \
python-setuptools \
python-pip \
python-lxml \
openssh-server \
net-tools \
vim \
git \
lrzsz \
supervisor
RUN apt-get install -y libxml2 libxml2-dev libxslt-dev
RUN apt-get -y install tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#RUN mkdir -p /www/
RUN mkdir -p /www/log
RUN mkdir /www/static
RUN mkdir /var/log/celery
RUN useradd celery
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN pip install nltk
RUN pip install networkx
RUN pip install https://s3-us-west-2.amazonaws.com/jdimatteo-personal-public-readaccess/nltk-2.0.5-https-distribute.tar.gz
#WORKDIR /www/
#RUN git clone http://123/tfs/DefaultCollection/CAE/\_git/cae\_job\_manager
#RUN git checkout origin/develop
#ADD settings_local.py /www/cae_job_manager/cae_job_manager/
ADD requirements.txt /www/
WORKDIR /www/
RUN pip install -r requirements.txt
RUN pip install uwsgi
RUN pip install pymysql
# Configure Nginx
#RUN ln -s /www/cae_job_manager/conf/nginx_cae.conf /etc/nginx/sites-enabled/
#RUN rm /etc/nginx/sites-enabled/default
# Run Supervisor (i.e., start MySQL, Nginx, and Gunicorn)
#CMD ["/usr/bin/supervisord","-n","-c","/etc/supervisor/supervisord.conf"]
#RUN chmod 777 docker_run.sh
#CMD ["sh", "docker_run.sh"]
#ENTRYPOINT ["/www/cae_job_manager/docker_run.sh"]
- 从harbor加载基础镜像时间长;故提前加载好;
2. 构建发布docker
a.拉取代码
b.放置配置文件
c.更新python安装包
3. 删除旧的容器,运行新的容器实例;
root@inspur_spider07:/www# ls
cae_job_manager.conf Dockerfile settings_local.py
root@inspur_spider07:/www# docker build -t cae_job_manager:prod2 .
root@inspur_spider07:/www# cat Dockerfile
FROM 192.168.138.111:8885/ispider/cae_job_manager:base
# Install packages
WORKDIR /www/
RUN git clone http://123:5000/tfs/DefaultCollection/CAE/\_git/cae\_job\_manager
WORKDIR /www/cae_job_manager
RUN git checkout origin/develop
ADD settings_local.py /www/cae_job_manager/cae_job_manager/
ADD cae_job_manager.conf /etc/supervisor/conf.d/
RUN pip install -r requirements.txt
RUN pip install uwsgi
RUN pip install pymysql
# Configure Nginx
RUN ln -s /www/cae_job_manager/conf/nginx_cae.conf /etc/nginx/sites-enabled/
RUN rm /etc/nginx/sites-enabled/default
# Run Supervisor (i.e., start MySQL, Nginx, and Gunicorn)
#CMD ["/usr/bin/supervisord","-n","-c","/etc/supervisor/supervisord.conf"]
#RUN chmod 777 docker_run.sh
#CMD ["sh", "docker_run.sh"]
ENTRYPOINT ["/www/cae_job_manager/docker_run.sh"]
1.5 pool docker化发布
pool采用开源项目,它的官方镜像有许多限制,例如不能安装git,openssh,也就不能把其作为一个服务器节点管理;
Docker化的发布流程
拉取代码
cd /inspur/pool
git clone http://tfs.123:5000/tfs/DefaultCollection/CAE/_git/pool
git pull
git checkout origin/develop
放置本地配置文件cat /inspur/pool/pool_server/pool_server/settings_local.py
编译代码cd /inspur/pool/poolui
sudo npm install
sudo bower install --allow-root
sudo npm run build
打包代码 cd /inspur/pool/
Docker build -t pool:run .
运行容器 1.删除之前的运行容器;
2.创建运行实例;
生产应用方案
拉取代码,放置配置文件,npm安装包,bower安装包 都提前做好,这些较耗费时间;
更新代码
cd /inspur/pool
git pull
git checkout origin/develop
cd /inspur/pool/poolui
sudo npm run build
docker 采用挂载的方式,应用程序;
设想方案
通过打包代替挂载,数据文件更新后丢失,但是能够更新python安装包;加载数据文件和代码项目在一块不合理;
利用挂载容器的方式,更新python安装包docker exec -it pool1 pip install -r /app/pool_server/requirements.txt;
root@honey1:/pool/pool# cat Dockerfile
FROM pool:h
ADD . /app
RUN ln -fs /app/nginx/* /etc/nginx/
RUN pip install -r /app/pool_server/requirements.txt
RUN ln -s /app/supervisord/supervisord.conf /etc/supervisor/
EXPOSE 9001
ENTRYPOINT ["/app/docker/entry"]
pool基础镜像
root@honey1:/pool# ls
Dockerfile Dockerfile.s pool pool.tar.gz requirements.txt
Requirements.txt 是pool 的项目安装包
root@honey1:/pool# cat Dockerfile
FROM scrapinghub/pool
WORKDIR /app
RUN apt-get update && apt-get install -y build-essential python-dev
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN pip install uwsgi
RUN pip install gevent
RUN pip install supervisor
RUN mkdir /etc/supervisor/
RUN mkdir /var/log/supervisor
RUN ln -s /usr/local/bin/supervisord /usr/bin/supervisord
RUN ln -s /usr/local/bin/supervisorctl /usr/bin/supervisorctl
ADD requirements.txt /root
RUN pip install -r /root/requirements.txt
#RUN mkdir /etc/supervisor/conf.d/
#RUN echo_supervisord_conf > /etc/supervisor/supervisord.conf
#RUN ln -s /app/supervisord/supervisord.conf /etc/supervisor/
EXPOSE 9001
#ENTRYPOINT ["/app/docker/entry"]
pool更新,要切换到项目里,宿主机要安装docker, nodejs, bower
root@honey1:/pool/pool# ls
bin docker Dockerfile_backup LICENSE nginx pool_server provision.sh slybot splash_utils Vagrantfile
CHANGES Dockerfile docs mytest pool.conf poolui README.md slyd supervisord VERSION
root@honey1:/pool/pool# cat Dockerfile
FROM pool:h
ADD . /app
RUN ln -fs /app/nginx/* /etc/nginx/
RUN pip install -r /app/pool_server/requirements.txt
RUN ln -s /app/supervisord/supervisord.conf /etc/supervisor/
EXPOSE 9001
ENTRYPOINT ["/app/docker/entry"]