刚开始接触运维这块儿的活儿,记录一下其过程。
找了张比较清晰的流程图如下:
我的流程:
开发上传代码至svn——jenkins从svn拉取代码编译打包——远程docker用docker-compose进行镜像构建上传至镜像私服——从私服拉取最新镜像启动容器完成部署
jenkins是从docker hub上拉下来的镜像,docker的安装以及docker-compose的安装jenkins的启动:
docker安装(官网)
https://www.runoob.com/docker/ubuntu-docker-install.html
jenkins镜像:
https://hub.docker.com/r/jenkins/jenkins/tags?page=1&ordering=last_updated
jenkins启动注意:
docker run
-u root
-p 50001:8080 //映射端口 50001后续为访问端口
-v /data/jenkins_home:/var/jenkins_home // :前段为映射主机地址,方便后续配置jenkins,后端为容器地址
-v /var/run/docker.sock:/var/run/docker.sock // 配置docker远程访问
-v $(which docker):/usr/bin/docker
jenkins/jenkins:2.263.1-lts-centos7
docker-compose:
$ curl -L https://github.com/docker/compose/releases/download/1.8.0/run.sh > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
$ docker-compose --version
因为我使用的是ubantu会有几个需要注意的点:
更换国内源:
cd etc/apt 更改sources.list切换为阿里的
deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib
镜像私服的搭建在此不做赘述了,配置docker连接仓库daemon.json配置文件:cd etc/docker/daemon.json
{
//配置仓库镜像地址
"registry-mirrors": ["https://kzflb.mirror.aliyuncs.com"],
//默认http私有仓库不能访问,设置后才可以
"insecure-registries": ["http://192.168.2.196"],
//开启docker-API远程访问
"hosts": ["tcp://0.0.0.0:2375","unix:///var/run/docker.sock"]
}
插件
Localization: Chinese (Simplified)
SSH plugin
Subversion Plug-in
全局jdk maven都是用的自动安装的
jenkins系统配置:
另外svn账户账户凭证,以及主机ssh凭证,包括自动安装jdk需要登陆oracle账号的凭证避免后续返回重复操作可以先在系统管理的凭证管理中添加好凭证
开始配置任务
输入任务名称,一般自由风格软件项目就行了源码管理配置
新增构建步骤命令
1.maven打包命令一般跳过单元测试打包
2.执行将打包好的项目构建镜像,上传至私服
Dockerfile:
FROM docker2.usscity.com/common/java8:v1 #从仓库导入的java8环境
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV TZ Asia/Shanghai #调整为上海时区
RUN mkdir -p /usr/local/mining
COPY main-server.jar /usr/local/mining/main-server.jar
COPY start-server.sh /usr/local/mining/start-server.sh #jar包以及启动命令
WORKDIR /usr/local/mining #工作路径
EXPOSE 8050
CMD ["sh","/usr/local/mining/start-server.sh"]
start-server.sh:
#!/bin/bash
java -jar -Xms1024M -Xmx2048M -Dfile.encoding=utf-8 -XX:+HeapDumpOnOutOfMemoryError main-server.jar com.usscity.mining.server.Application --spring.profiles.active=sprod
build-image.sh:
#!/bin/bash -v
rm -rf *.jar
cp ../target/main-server-3.0.0-mining-local-SNAPSHOT.jar ./main-server.jar
echo "\n\n====================MAVEN打包完成,开始构建docker镜像===========================\n"
docker build -t docker2.usscity.com/mining/mining-local-server:v2 . 镜像仓库中位置以及版本号
echo "\n\n====================镜像已经生成,开始归档docker镜像===========================\n"
docker login --username ***** --password ***** docker2.usscity.com 登陆镜像私服
docker push docker2.usscity.com/mining/mining-local-server:v2
echo "\n\n====================归档完成===========================\n"
3.在远程主机上使用ssh执行shell脚本docker-compose配置启动容器参数
docker-compose-local-mining.yml:
version: '2' 版本号
services:
mining-server-k:
container_name: mining-server-k 容器名称
restart: always
image: docker2.usscity.com/mining/mining-server-k:v2 仓库镜像名称
ports:
- '18605:8080' 映射端口号
devices:
- "/dev/mem:/dev/mem"
privileged: true
volumes:
- /data/mining-server-k/logs:/usr/local/mining/logs 映射日志文件
- /data/mining-server-k/mining-files:/data/mining-files
- /data/mining-server-k/userPrefs:/root/.java/.userPrefs
- ./config/mining-server-k/start-server.sh:/usr/local/mining/start-server.sh 执行启动
networks:
default:
external:
name: usscity 网段
update-mining-server.sh:
#!/bin/bash -v
echo "\n\n====================获取最新镜像===========================\n"
docker login --username **** --password **** docker2.usscity.com
docker-compose -f docker-compose-local-mining.yml pull mining-server-k
echo "\n\n====================停止===========================\n"
docker-compose -f docker-compose-local-mining.yml stop mining-server-k
echo "\n\n====================删除===========================\n"
docker-compose -f docker-compose-local-mining.yml rm -f mining-server-k
echo "\n\n====================重新创建===========================\n"
docker-compose -f docker-compose-local-mining.yml create mining-server-k
echo "\n\n====================启动===========================\n"
docker-compose -f docker-compose-local-mining.yml start mining-server-k
到此配置结束,构建中出现问题再根据问题一一解决
我在这碰见了一个jenkins自动安装的maven因为没有配置setting.xml导致有些包没有下载到也希望大家注意一下。
再见!