小团队如何从零搭建一个自动化运维体系?
此刻我们必要汇报 Jenkins 怎样对我们的营业代码举办编译打包,有两种要领: 界面上配置 行使 Jenkinsfile:相同于 Dockerfile 的一种文本文件,详细先容:Using a Jenkinsfile[7] 我绝不踌躇地选择了第二种,由于一是利于版本化;二是机动。 Jenkinsfile 相同这样: pipeline { agent any stages { stage('Build') { steps { sh './gradlew clean build' archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true } } } } 那么 Jenkinsfile 放那边呢?谜底是和营业代码放在一路,相同这样每个工程各自打点本身的 Jenkinsfile: 这时,我们就可以在 Jenkins 上建设一个 pipleline Job了。关于分支打点,我们人少,以是,提议全部项目同一在 master 分支举办开拓并宣布。 让 Jenkins 辅佐我们执行 Ansible 之前我们都是在措施员的电脑执行 Ansible 的,此刻我们要把这项事变交给 Jenkins。 详细操纵: 在 Jenkins 安装 Ansible 插件[8] 在 Jenkinsfile 中执行 withCredentials([sshUserPrivateKey(keyFileVariable:"deploy_private",credentialsId:"deploy"),file(credentialsId: 'vault_password', variable: 'vault_password')]) { ansiblePlaybook vaultCredentialsId: 'vault_password', inventory: "environments/prod", playbook: "playbook.yaml", extraVars:[ ansible_ssh_private_key_file: [value: "${deploy_private}", hidden: true], build_number: [value: "${params.build_number}", hidden: false] ] } 这里必要表明下: ansiblePlaybook 是 Jenkins ansible 插件提供的 pipeline 语法,相同手工执行:ansible-playbook 。 withCredentials 是 Credentials Binding[9] 插件的语法,用于引用一些敏感信息,好比执行 Ansible 时必要的 ssh key 及 Ansible Vault 暗码。 一些敏感设置变量,我们行使 Ansible Vault[10] 技能加密。 Ansible 剧本应该放哪? 我们已经知道各个项目各自认真本身的自动化构建,以是 Jenkinfile 就放到各自项目中。 那项目标陈设呢?同样的原理,我们认为也应该由各个项目自行认真,以是我们的每个要举办陈设的项目下城市有一个 Ansible 目次,用于存放 Ansible 剧本。 相同这样: 可是,怎么用呢?我们会在打包阶段将 Ansible 目次举办 zip 打包,到真正陈设时,再解压执行内里的 playbook。 快速为全部的项目天生 Ansible 剧本及Jenkinsfile 上面,我们将一个项目举办 Jenkins 化和 Ansible 化,可是我们尚有许多项目必要举办同样的举措。 思量到这是体力活,并且往后我们还会常常做这样事,以是我抉择行使 cookiecutter[11] 技能自动天生 Jenkinsfile 及 Ansible 剧本,建设一个项目,像这样: 小结 总结下来,我们小团队的自动化运维实验的次序或许为: 上基本监控 上 Gitlab 上 Jenkins,并集成 Gitlab 行使 Jenkins 实现自动编译打包 行使 Jenkins 执行 Ansible 以上只是一个架子,基于这个“架子”,就可以向那些大厂高峻上的架构举办演进了,好比: CMDB 的建树:我们行使 ansible-cmdb[12] 按照 inventory 自动天生当前全部呆板的环境。 宣布打点:Jenkins 上可以对宣布的每个阶段举办定制。蓝绿宣布等宣布方法可以通过修改 Ansible 剧本和 Inventory 实现。 自动扩缩容:通过设置 Prometheus 告警法则,挪用响应 webhook 就可以实现。 ChatOps:ChatOps 拭魅战[13]。 以上就是我关于自动化运维的一些实践,可是还在演进的路上,但愿能与各人交换。 相干链接: [1]https://github.com/prometheus/node_exporter [2]https://github.com/ernestas-poskus/ansible-prometheus [3]https://github.com/timonwong/prometheus-webhook-dingtalk [4]https://www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible [5]http://docs.ansible.com/ansible/latest/modules/consul_module.html [6]https://github.com/geerlingguy/ansible-role-jenkins [7]https://jenkins.io/doc/book/pipeline/jenkinsfile/ [8]https://wiki.jenkins.io/display/JENKINS/Ansible+Plugin [9]https://jenkins.io/doc/pipeline/steps/credentials-binding/ [10]http://docs.ansible.com/ansible/2.5/user_guide/vault.html [11]https://github.com/audreyr/cookiecutter [12]https://github.com/fboender/ansible-cmdb [13]https://showme.codes/2017-10-08/chatops-in-action/ (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |