docker_jenkins_ansible 部署 java 服务_02

lance 2020-09-10 PM 39℃ 0条

前情提要

在上文,我们已经把 jenkins 部署好了,其中 ansible 和 gitlab 因为在环境中已经有,就省略吧。
要部署的话,可以百度找资料,或者看我这几篇文章以作参考:
https://www.itwordsweb.com/linux_doc/auto_01.html
https://www.itwordsweb.com/linux_doc/auto_02.html
https://www.itwordsweb.com/linux_doc/auto_03.html


制作 jdk8 的 docker 镜像

# 把 jdk8 的压缩包丢到服务器,和 jdk8.Dockerfile 一起
# 编写 Dockerfile,任意一台装了 docker 的机器都可以
cd /etc/ansible/playbooks/docker_deploy/jdk8 && vim jdk8.Dockerfile
# 指定初始镜像
FROM centos:7
# 一些头信息(作者/版本/邮箱等)
LABEL maintainer="[email protected]" version="1.0"
# 把 jdk8 复制进去
COPY jdk-8u241-linux-x64.tar.gz /tmp
# 运行一系列命令部署 jdk8
RUN cd /tmp && tar xvf jdk-8u241-linux-x64.tar.gz && mv jdk1.8.0_241 /usr/local/jdk8 && rm -rf jdk-8u241-linux-x64.tar.gz && echo Asia/Shanghai > /etc/timezone
# 设置环境变量
ENV JAVA_HOME /usr/local/jdk8
ENV JAVA_BIN /usr/local/jdk8/bin
ENV JRE_HOME /usr/local/jdk8/jre
ENV PATH $PATH:/usr/local/jdk8/bin:/usr/local/jdk8/jre/bin
ENV CLASSPATH /usr/local/jdk8/jre/lib:/usr/local/jdk8/lib:/usr/local/jdk8/jre/lib/charsets.jar
ENV TZ Asia/Shanghai
# 容器启动时执行的命令,tail -f /etc/passwd 是预防容器每次启动就退出
CMD source /etc/profile && tail -f /etc/passwd

# 构建 jdk8 的镜像
docker build -t cf_jdk8:1.0 -f /etc/ansible/playbooks/docker_deploy/jdk8/jdk8.Dockerfile .

# 登录镜像仓库
docker login -u "用户名" -p "密码" ccr.ccs.tencentyun.com/chainedfinance/cf

# 给镜像打上 tag
docker tag cf_jdk8:1.0 ccr.ccs.tencentyun.com/chainedfinance/cf:cf_jdk8_1.0

# 把镜像上传到镜像仓库
docker push ccr.ccs.tencentyun.com/chainedfinance/cf:cf_jdk8_1.0

在 jenkins 上配置项目,测试打包功能

57227-rmvioobyojh.png

02478-8w3mg4opor2.png

41923-jwut85szpyn.png

65549-ukdfwca8lw.png

60694-7075iy9aip9.png

91746-bpzq9obwaxp.png

73719-jtjztsy9doi.png

11013-6uyrc39ut1.png

我这里设置了私有 maven 仓库,所以选择的是事先配置好的 maven
我们来构建一下,看能不能正常打包

01813-jvmgldmpxjd.png

打包成功

87897-tay3t731xa.png


结合 ansible 和 docker 部署服务

在 ansible 上写 ansible yml 文件

# 整体结构如下
/etc/ansible/playbooks/docker_deploy
├── alpine
│   ├── alpine.glibc.Dockerfile
│   ├── glibc-2.32-r0.apk
│   └── sgerrand.rsa.pub
├── docker_deploy.yml
└── jdk8
    ├── jdk8.Dockerfile
    └── jdk-8u241-linux-x64.tar.gz

/etc/ansible/roles/docker_deploy_java/
├── files
│   └── dockerfile.template
└── tasks
    ├── create_dockerfile.yml
    ├── get_from_jenkins.yml
    ├── login_private_docker.yml
    ├── main.yml
    ├── push_docker.yml
    ├── put_to_server.yml
    └── run_server.yml
# 编写 docker_deploy.yml
vim /etc/ansible/playbooks/docker_deploy/docker_deploy.yml
---
- hosts: "{{Host}}"
  gather_facts: False
  remote_user: root
  roles:
    - docker_deploy_java

# 编写 dockerfile.template
vim /etc/ansible/roles/docker_deploy_java/files/dockerfile.template
From {{Image}}
LABEL maintainer="[email protected]" version={{BUILD_NUMBER}}
RUN mkdir /data
COPY {{Filename}} /data
CMD java -jar -Djava.awt.headless=true -Dzk.url={{zk_url}} -Dzk.config.enabled=true /data/{{Filename}}

# 编写 create_dockerfile.yml
vim /etc/ansible/roles/docker_deploy_java/tasks/create_dockerfile.yml
---
- name: 在 ansible 上生成项目 dockerfile
  template:
      src: /etc/ansible/roles/docker_deploy_java/files/dockerfile.template
      dest: /data/docker/Dockerfile
      mode: 0644
  delegate_to: dev-of-ansible-32

# 编写 get_from_jenkins.yml
vim /etc/ansible/roles/docker_deploy_java/tasks/get_from_jenkins.yml
---
- name: 从 jenkins 拉取 jar 包
  fetch:
      src: /data/jenkins/workspace/{{Project}}/target/{{Filename}}
      dest: /data/source_tmp/{{Filename}}
      flat: yes
  delegate_to: dev-of-jenkins-12

# 编写 login_private_docker.yml
vim /etc/ansible/roles/docker_deploy_java/tasks/login_private_docker.yml
---
- name: 登录私有仓库,并验证
  shell: docker login -u "用户名" -p "密码" ccr.ccs.tencentyun.com/chainedfinance/cf
  delegate_to: "{{Host}}"

# 编写 main.yml
vim /etc/ansible/roles/docker_deploy_java/tasks/main.yml
---
- import_tasks: login_private_docker.yml
- import_tasks: get_from_jenkins.yml
- import_tasks: create_dockerfile.yml
- import_tasks: put_to_server.yml
- import_tasks: run_server.yml
- import_tasks: push_docker.yml

# 编写 put_to_server.yml
vim /etc/ansible/roles/docker_deploy_java/tasks/put_to_server.yml
---
- name: 把 jar 包传到服务器
  copy:
      src: /data/source_tmp/{{Filename}}
      dest: /data/{{Serverdir}}/
      mode: 0644
- name: 把 dockerfile 传到服务器
  copy:
      src: /data/docker/Dockerfile
      dest: /data/{{Serverdir}}/
      mode: 0644

# 编写 run_server.yml
vim /etc/ansible/roles/docker_deploy_java/tasks/run_server.yml
---
- name: 构建镜像,运行容器
  shell: sh /data/shell/kill_container.sh cf_{{Serverdir}}_* && cd /data/{{Serverdir}} && docker build -t cf_{{Serverdir}}_{{BUILD_NUMBER}} . && docker run -itd --name cf_{{Serverdir}}_{{BUILD_NUMBER}} --net host cf_{{Serverdir}}_{{BUILD_NUMBER}}

# 编写 push_docker.yml
vim /etc/ansible/roles/docker_deploy_java/tasks/push_docker.yml
---
- name: 上传打好 tag 的镜像到私有仓库
  shell: docker tag cf_{{Serverdir}}_{{BUILD_NUMBER}} ccr.ccs.tencentyun.com/chainedfinance/cf:cf_{{Serverdir}}_{{BUILD_NUMBER}} && docker push ccr.ccs.tencentyun.com/chainedfinance/cf:cf_{{Serverdir}}_{{BUILD_NUMBER}}

在 jenkins 上新增配置

33452-tio6fyy2xx.png

39783-279v6nou0me.png

提供一下命令,仅供参考

BUILD_NUMBER=${BUILD_NUMBER}
Project=${JOB_NAME}
ansible-playbook /etc/ansible/playbooks/docker_deploy/docker_deploy.yml --extra-vars "{'Host':'$Host','zk_url':'$zk_url','Filename':'$Filename','Serverdir':'$Serverdir','Image':'$Image','BUILD_NUMBER':'$BUILD_NUMBER','Project':'$Project'}"

测试

测试没问题~

34610-0iqnl6btedpf.png

49044-9pi9d3gdtuv.png

24648-nyw60rekjmi.png


后续

批量安装 docker:

ansible 组名和主机名 -m shell -a 'yum install -y yum-utils && yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && yum install -y docker-ce && systemctl start docker && systemctl enable docker'

批量传输文件:

ansible 组名和主机名 -m copy -a 'src=/etc/ansible/files/shell/kill_container.sh dest=/data/shell/ mode=0755'

kill_container.sh 参考:

#!/bin/bash
for i in `docker ps | grep $1* | awk '{print $1}'`;
do
    docker stop $i
    docker rm $i
done

for i in `docker images | grep $1* | awk '{print $1}' | grep -v ccr.ccs.tencentyun.com/chainedfinance/cf`;
do
    docker rmi $i
    docker rmi ccr.ccs.tencentyun.com/chainedfinance/cf:$i
done

非特殊说明,本博所有文章均为博主原创。

觉得文章不错,打赏一点吧,1分也是爱😀

WeChat Pay

微信打赏

Alipay

支付宝打赏

上一篇 docker_jenkins_ansible 部署 java 服务_01
下一篇 没有了

评论啦~