标签:只读 read google make node package makefile down random
预计阅读时间: 20分钟
本期解决痛点如下:
1. 代码版本的多样性,编译环境的多样性如何解决?
答案是使用docker,将不同的编译环境images统统打包到私有仓库上,根据需求进行下载,从宿主机上挂载volume到docker container上进行编译等操作
2. 打包编译好的各个模块组件如何部署到不同的服务器上?
答案是使用ansible-playbook,根据yml脚本进行部署,其服务器部署ip由统一的inventory配置文件控制(默认路径在 /etc/ansible/hosts)
Talking is cheap,let‘s go:
环境列表:
角色1: 安装了docker以及git 的宿主机 (源代码服务器)
角色2: 需要被部署的服务器
角色3: docker container(每次根据docker image生成),运行在宿主机上
环境配置(具体步骤在后面):
运行脚本: 宿主机角色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
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 2 3 PLAY [127.0.0.1] *************************************************************** 4 5 TASK [setup] ******************************************************************* 6 ok: [127.0.0.1] 7 8 TASK [download web page or pkg] ************************************************ 9 changed: [127.0.0.1] 10 11 PLAY [web] ********************************************************************* 12 13 TASK [setup] ******************************************************************* 14 ok: [222.177.111.222] 15 16 TASK [creates depoy directory] ************************************************* 17 ok: [222.177.111.222] 18 19 TASK [copy package to web node] ************************************************ 20 changed: [222.177.111.222] 21 22 TASK [check web process] ******************************************************* 23 changed: [222.177.111.222] 24 25 PLAY RECAP ********************************************************************* 26 127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0 27 222.177.111.222 : ok=4 changed=2 unreachable=0 failed=0
在角色2被部署服务器上观察部署结果:
1 [root@角色2]# cd /root/dist/ 2 [root@角色2 dist]# ls 3 samplepage 4 [root@角色2 dist]# cat /tmp/check.Log 5 UID PID PPID C STIME TTY TIME CMD 6 root 1 0 0 May17 ? 00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21 7 root 2 0 0 May17 ? 00:00:00 [kthreadd]
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:
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:
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
1 root@角色1:/export/jenkins/workspace/my-playbooks# cat web.yml 2 --- 3 - hosts: 127.0.0.1 4 connection: local 5 tasks: 6 - name: download web page or pkg 7 get_url: url=https://www.google.com.hk dest=/tmp/samplepage 8 9 - hosts: web 10 tasks: 11 - name: creates depoy directory 12 file: path=/root/dist/ state=directory 13 - name: copy package to web node 14 copy: src=/tmp/samplepage dest=/root/dist/ 15 - name: check web process 16 shell: "ps -ef>/tmp/check.Log"
修改inventory 配置文件
其中 web 里面的IP为角色2,即需要被部署的服务器。
1 root@角色1:/export/jenkins/workspace/my-playbooks# cat inventory/test_env1/hosts 2 [web] 3 222.177.111.222 web_path=/export/App/ 4 5 root@角色1:/export/jenkins/workspace/my-playbooks# pwd 6 /export/jenkins/workspace/my-playbooks
后记:
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的操作”,这样就打通了从脚本自动编译到部署的任脉。
[Docker][ansible-playbook]3 持续集成环境初探
标签:只读 read google make node package makefile down random
原文地址:http://www.cnblogs.com/carol2000/p/6877685.html