[Docker][ansible-playbook]3 持续集成环境之分布式部署

预计阅读时间: 30分钟


本期解决痛点如下:
1. 代码版本的多样性,编译环境的多样性如何解决?
答案是使用docker,将不同的编译环境images统统打包到私有仓库上,根据需求进行下载,从宿主机上挂载volume到docker container上进行编译等操作
2. 打包编译好的各个模块组件如何部署到不同的服务器上?
答案是使用ansible-playbook,根据yml脚本进行分布式部署,其各个服务器的部署ip由统一的inventory配置文件控制(默认路径在 /etc/ansible/hosts)

Talk is cheap,let's go:

环境列表:
角色1: 安装了docker以及git 的宿主机 (源代码服务器)
角色2: 需要被部署的服务器组
角色3: docker container(每次根据docker image生成),运行在宿主机上

环境配置(yml和inventory hosts配置参数以及具体配置步骤在后面):

  • 角色1:ssh-keygen生成秘钥和公钥,将公钥copy置角色2的authorized_keys文化中
  • 配置docker私有仓库,为docker run做准备,  请参考 http://www.cnblogs.com/lienhua34/p/4922130.html
  • 使用git下载自己的repository脚本到角色1(其中包含 playbook 运行所需要的yml以及inventory hosts配置文件)

运行脚本: 宿主机角色1上运行并观察结果

#docker run --rm=true --publish-all=true --name my_deploy --volume=/root/.ssh/id_rsa:/root/.ssh/id_rsa:ro --volume=/export/jenkins/workspace/my-playbooks:/playbooks --workdir=/playbooks 192.168.111.99:5000/centos7-ansible ansible-playbook web.yml -i inventory/test_env1/hosts


# docker run --rm=true --publish-all=true --name my_deploy --volume=/root/.ssh/id_rsa:/root/.ssh/id_rsa:ro--volume=/export/jenkins/workspace/my-playbooks:/playbooks --workdir=/playbooks 192.168.111.99:5000/centos7-ansible ansible-playbook web.yml -i inventory/test_env1/hosts


PLAY [127.0.0.1] ***************************************************************


TASK [setup] *******************************************************************
ok: [127.0.0.1]


TASK [download web page or pkg] ************************************************
changed: [127.0.0.1]


PLAY [web] *********************************************************************


TASK [setup] *******************************************************************
ok: [222.177.111.222]


TASK [creates depoy directory] *************************************************
ok: [222.177.111.222]


TASK [copy package to web node] ************************************************
changed: [222.177.111.222]


TASK [check web process] *******************************************************
changed: [222.177.111.222]


PLAY [database] ****************************************************************


TASK [setup] *******************************************************************
ok: [222.177.111.222]


TASK [creates db depoy directory] **********************************************
ok: [222.177.111.222]


TASK [copy package to db node] *************************************************
changed: [222.177.111.222]


TASK [shell operation process] *************************************************
changed: [222.177.111.222]


PLAY RECAP *********************************************************************
127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0
222.177.111.222 : ok=8 changed=4 unreachable=0 failed=0


在角色2 被部署服务器组web和database上观察部署结果:

[root@角色2 web服务器]# cd /root/dist/
[root@角色2 web服务器]# ls
samplepage
[root@角色2 web服务器]# cat /tmp/check.Log
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 May17 ?        00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root         2     0  0 May17 ?        00:00:00 [kthreadd]

[root@角色2 db服务器~]# cat database/db.
db.conf db.Log
[root@角色2 db服务器~]# cat database/db.Log
Fri May 19 14:33:01 CST 2017
[root@角色2 db服务器~]# pwd
/root

Docker Run的脚本具体解读

#docker run --rm=true --publish-all=true --name my_deploy --volume=/root/.ssh/id_rsa:/root/.ssh/id_rsa:ro --volume=/export/jenkins/workspace/my-playbooks:/playbooks --workdir=/playbooks 192.168.111.99:5000/centos7-ansible ansible-playbook web.yml -i inventory/test_env1/hosts

--rm=true 表示不保留container

--publish-all=true 表示将随机选择映射端口对外进行通信 (https://docs.docker.com/engine/userguide/networking/default_network/binding/)

--volume=/root/.ssh/id_rsa:/root/.ssh/id_rsa:ro 告诉角色3(docker container)使用角色1(宿主机)的ssh id_rsa key(采用只读的方式)来访问角色3(需要被部署的服务器)

--volume=/export/jenkins/workspace/my-playbooks:/playbooks 讲角色1的git repository 工作路径映射至角色2的/playbooks 路径,这样就巧妙的免去了原先手工将代码copy到编译环境的步骤

192.168.111.99:5000/centos7-ansible  指定需要运行的docker image 编译环境

ansible-playbook web.yml -i inventory/test_env1/hosts    整句话为一段段,指在角色3(docker container)上使用ansible-playbook根据角色1(宿主机)的inventory/test_env1/hosts配置文件,按照web.yml脚本来配置角色2(被部署web服务器)

=====环境配置的具体步骤:==========

  • 角色1:ssh-keygen生成秘钥和公钥,将公钥copy置角色2的authorized_keys文化中

角色1:

 1 [root@角色1]# ssh-keygen
 2 Generating public/private rsa key pair.
 3 Enter file in which to save the key (/root/.ssh/id_rsa):
 4 Created directory '/root/.ssh'.
 5 Enter passphrase (empty for no passphrase):
 6 Enter same passphrase again:
 7 Your identification has been saved in /root/.ssh/id_rsa.
 8 Your public key has been saved in /root/.ssh/id_rsa.pub.
 9 The key fingerprint is:
10 f3:5f:f4:37:d44f:18 root@cbfb30
11 The key's randomart image is:
12 +--[ RSA 2048]----+
13 | E+o...|
14 | o |
15 +-----------------+

角色2:  (作为角色2,每一台机器都要配置authorized_keys )

1 [root@角色2 .ssh]# cat authorized_keys
2 ssh-rsa Abvx2bklJcJLn+439iaQ== test@jtest.com
3 [root@角色2 .ssh]# pwd
4 /root/.ssh

坑1:如果从角色1无法从私有仓库下载,请修改Docker的配置文件如下

root@角色1:/home/test# cat /etc/default/docker |grep 5000
DOCKER_OPTS="--insecure-registry <Yourprivate.docker.imagesRepository.IP>:5000"
# systemctl restart docker
  • 使用git下载自己的repository脚本到角色1(其中包含 playbook 运行所需要的yml以及inventory hosts配置文件)

其中web.yml的配置如下

root@角色1:/export/jenkins/workspace/my-playbooks# cat web.yml

---
- hosts: 127.0.0.1
connection: local
tasks:
- name: download web page or pkg
get_url: url=https://www.google.com.hk dest=/tmp/samplepage


- hosts: web
tasks:
- name: creates depoy directory
file: path=/root/dist/ state=directory
- name: copy package to web node
copy: src=/tmp/samplepage dest=/root/dist/
- name: check web process
shell: "ps -ef>/tmp/check.Log"


- hosts: database
tasks:
- name: creates db depoy directory
file: path=/root/database/ state=directory
- name: copy package to db node
copy: src=/tmp/samplepage dest=/root/database/db.conf
- name: shell operation process
shell: "echo `date` >/root/database/db.Log"

 

由上图可以看到样例中需要配置2台机器 ,其hosts分别为webdatabase,如其中 web 里面的IP为角色2,即需要被部署的服务器

然后再修改对应的inventory  hosts配置文件,指定web和host服务器角色的ip和路径等参数

root@角色1:/export/jenkins/workspace/my-playbooks# cat inventory/test_env1/hosts

 [web]
 222.177.111.222 web_path=/export/App/web

 [database]
 222.177.111.222 db_path=/export/App/db

后记

1. 依托于强大的playbook,不但可以做文件/目录的增删改,还可以做解压、循环等更高级的操作,请参考

playbook 入门: http://msiyuetian.blog.51cto.com/8637744/1752326

playbook 官方文档 http://ansible-tran.readthedocs.io/en/latest/docs/playbooks.html

预告:下一篇将介绍“如何使用Jenkins结合makefile来进行docker run的操作”,这样就打通了从脚本自动编译到部署的任脉。

原文地址:https://www.cnblogs.com/carol2000/p/6877685.html