├── .gitignore ├── LICENSE ├── README.md ├── TROUBLESHOOTING.md ├── Vagrantfile ├── ansible-playbook_wrapper ├── ansible-vault_wrapper ├── ansible.cfg ├── ansible_wrapper ├── config_ssh.template ├── configure_host.yml ├── create_master.yml ├── create_project.yml ├── requirements.yml ├── sample ├── group_vars │ ├── all │ │ ├── rancher │ │ ├── unix_accounts │ │ └── vars │ └── rancher-master │ │ └── mysql-rancher └── inventory ├── scp_wrapper ├── ssh_wrapper ├── templates └── janitor │ ├── docker-compose.yml │ └── rancher-compose.yml └── utils_clean_host.yml /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.swo 3 | *~ 4 | .idea 5 | *.iml 6 | 7 | # ansible plugins binary 8 | *.pyc 9 | 10 | #binaries 11 | *.gz 12 | *.tgz 13 | *.bzip 14 | *.bz2 15 | *.zip 16 | *.iso 17 | *.tar 18 | 19 | #local ssh config 20 | config_ssh 21 | 22 | *.retry 23 | 24 | .vagrant 25 | *.log 26 | 27 | roles/AdopteUnOps.* 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Project ansible to manage Rancher 2 | 3 | ## Foreword 4 | 5 | If you don't know yet rancher please read the official documentation http://docs.rancher.com/rancher/latest/en/. 6 | 7 | Rancher will be used to create segregated environments for projects. 8 | 9 | Each project/environments should exists as "environments" in rancher (example: my-uat-project my-production-project ). 10 | Each rancher environment is segregated at network level via Docker Overlay and IPSec tunnels (managed by rancher itself). 11 | 12 | Only trusted ops can connect to the machines, unix accounts are managed by ansible. 13 | 14 | Developpers will not be allowed to access rancher by itself, rancher master will be accessible only for ops for the moment. 15 | End users will have tools in their project to see their application logs & metrics (elk, prometheus). 16 | 17 | ### Machine requirements 18 | 19 | At the moment this set of playbooks is designed to be run on ubuntu. We recommend using latest LTS (16.04 at times of writing). 20 | 21 | Each host (including the master) must have at least the following requirements : 22 | * 2 vcpu 23 | * 4 Go ram 24 | * 2 disks with one for the system with 12Go (8Go for / + 4Go for swap) and one for /var/lib/docker with 8Go. 25 | 26 | Note that by default we will only install docker on the machine, so the root filesystem doesn't needs to be big (just system logs and the base packages). 27 | Docker will have it's own partition and everything will run inside containers, so if at some point you need more space you need to extends docker partition (see above) 28 | 29 | ### Exemple for test environments 30 | 31 | 2 hosts per rancher environment : 32 | * one for tools (elk, prometheus, janitor) and the load balancer 33 | * one for project 34 | 35 | Ideally we should provide a secured jenkins for the project with a sample of pipeline to deploy across all environments. 36 | The machine for tools will be identified with specific label on it (role=tools). 37 | 38 | ## Playbooks documentation 39 | 40 | ### Requirements 41 | 42 | * Ansible >= 2.3 43 | 44 | ### Roles dependencies 45 | 46 | First you need to fetch roles with ansible galaxy : 47 | ``` 48 | ansible-galaxy install -r requirements.yml 49 | ``` 50 | 51 | ### Rancher platforms 52 | 53 | Each rancher platform will have it's own configuration (inventory + group_vars + hosts_vars). 54 | By default wrappers will use production one. 55 | 56 | But you can use other ones changing inventory from the command line (everything is relative to the path of the inventory file). 57 | ``` 58 | ./ansible-playbook_wrapper configure_host.yml -K -i path/to/inventory 59 | 60 | ``` 61 | 62 | ### SSH configuration 63 | 64 | Copy the file `config_ssh.template` to `config_ssh` in this local folder 65 | then edit `config_ssh` to configure it. The user must match a user declared 66 | in ssh_users list in `/group_vars/all/vars` 67 | 68 | To prevent typing sudo password when using ansible, create for each cluster 69 | the file `/group_vars/all/private` with the following content: 70 | 71 | ``` 72 | ansible_sudo_pass: your_sudo_password 73 | ``` 74 | 75 | Where your_sudo_password is the password declared in ssh_users list in `/group_vars/all/vars` 76 | 77 | Please note that if you are using this mechanism, it will always be used, even if you are using `-k` or `-K` 78 | 79 | ### Vault 80 | 81 | Please add the vault password in `rancher/ansible/.ansible_vault_pass` 82 | 83 | ### Workflow to setup rancher from scratch 84 | 85 | * run configure_host playbook 86 | * run create_master playbook 87 | * run create_project playbook (everytime you need to setup a new env) 88 | 89 | ### Configure the Machine 90 | Use the configure_host.yml 91 | 92 | You can bootstrap the machine for the first time with the following command 93 | 94 | ``` /ansible-playbook_wrapper configure_host.yml -u genericaccount -Kk ``` 95 | 96 | This ansible install 97 | 98 | * Python 99 | * Docker 100 | * Create local user with ssh_key, put into the group ADM,SUDO and set the password 101 | * Set machine hostname 102 | * Configure ssh (disable root login, enforce auth by key) 103 | * Remove the former ops account 104 | 105 | All the next time you must launch 106 | 107 | ``` /ansible-playbook_wrapper configure_host.yml -K ``` 108 | 109 | Docker will be installed in /var/lib/docker folder with a lvm partition by default it will create it on /dev/sdb but you can configure it in you inventory file by overriding docker_disks variable. 110 | ``` 111 | mymachine docker_disks="/dev/sdb,/dev/sdc" 112 | ``` 113 | 114 | Note that the playbook will auto extends to 100% of the volume group the /var/lib/docker. So if in future you're out of space on docker partition just override that property and rerun the playbook. 115 | 116 | ### Create a Master 117 | 118 | Create a tag for the new Master, for example 119 | ``` 120 | [rancher-masters] 121 | host-1 122 | ``` 123 | 124 | Configure the create_master.yml for [rancher-masters] 125 | 126 | Launch the playbook 127 | ``` 128 | ./ansible-playbook_wrapper create_master.yml -K 129 | ``` 130 | 131 | This playbook will create a file apiKey in {{ inventory_dir }}/group_vars/all/apikey. This file contains the RANCHER_API_KEY_ACCOUNT_TOKEN and RANCHER_API_KEY_ACCOUNT_SECRET 132 | 133 | ### Create a project 134 | 135 | Create a tag for your project into the inventory for example 136 | ``` 137 | [my-first-rancher-project] 138 | host-2 139 | host-3 140 | ``` 141 | 142 | The first host of the tag will become the "tools" host (running elk, etc...). 143 | 144 | Launch the playbook 145 | ``` 146 | ./ansible-playbook_wrapper create_project.yml -K -e "NAME_PROJECT=my-first-rancher-project" 147 | ``` 148 | 149 | This ansible create : 150 | 151 | * A project into RANCHER 152 | * Create "API KEY ENVIRONMENT" into rancher and write into group_vars/{{NAME_PROJECT}} 153 | * Add Host into the project 154 | * Install some stacks : Janitor 155 | 156 | 157 | ### Utils clean project 158 | 159 | To delete all hosts in a project run the playbook utils_add_registry.yml 160 | ``` 161 | ./ansible-playbook_wrapper utils_clean_host.yml -K -e "NAME_PROJECT=myproject" 162 | ``` 163 | 164 | To delete one host in a project run the following command 165 | ``` 166 | ./ansible-playbook_wrapper utils_clean_host.yml -K -e "NAME_PROJECT=myproject" -e "NAME_HOST=myHost 167 | ``` 168 | 169 | ## Deployments 170 | 171 | Deployments will be performed to developers via jenkins. 172 | We will provide to dev teams a jenkins instance for their projects with a sample of deployment pipeline. 173 | Our jenkins instance will have rancher-compose installed and a few utils scripts to simplify deployment. 174 | 175 | rancher-compose will require an apikey per project. 176 | 177 | Pipeline sample 178 | ``` 179 | withEnv(['RANCHER_COMPOSE_HOME=/usr/lib/rancher-compose', 'RANCHER_URL=http://rancher-master:8080/','RANCHER_SECRET_KEY=ABCD','RANCHER_ACCESS_KEY=ABCD']) { 180 | node { 181 | stage 'Prepare deploy' 182 | git credentialsId: 'projectname', url: 'git@your-git-repo' 183 | stage 'Deploy to UAT' 184 | sh '${RANCHER_COMPOSE_HOME}/deploy-stack.sh your-repo-git/deploy' 185 | } 186 | stage 'Validation' 187 | try { 188 | input message: 'UAT valid ?', ok: 'Yes please deploy to prod !' 189 | } catch (InterruptedException e) { 190 | node { 191 | sh '${RANCHER_COMPOSE_HOME}/rollback-stack.sh your-repo-git/deploy' 192 | } 193 | throw e 194 | } 195 | node { 196 | stage 'Deploy to Prod' 197 | sh '${RANCHER_COMPOSE_HOME}/confirm-deploy-stack.sh your-repo-git/deploy' 198 | } 199 | stage 'Validation in production' 200 | input message: 'Production OK ?', ok: 'Yes' 201 | 202 | } 203 | ``` 204 | 205 | ## Rancher catalog 206 | 207 | Rancher offer a capability to write our own catalog (stacks ready to use). This can be useful if we want to provide some stuff for projects (exemple : mongodb + mongo ui + backups) 208 | To create a catalog you just need to have a git repository with a templates folder containing a simple structure with docker-compose and rancher-compose files. 209 | 210 | To register a catalog go to rancher-ui and go to admin/settings and add your catalog in the related section. The url should be something like https://user:token@your-git-repo.git 211 | 212 | For more documentation on this topic check http://docs.rancher.com/rancher/latest/en/catalog/ 213 | 214 | To see our current catalog check https://github.com/AdopteUnOps/rancher-catalog 215 | 216 | ## Disaster recovery 217 | 218 | If for some reason the host running rancher-master dies, application will remain up and running, so there is no impact. 219 | If after running troubleshooting steps you don't have any clue we recommend you to wipe the master machine spawn a new master with "create_master.yml" playbook and run mysql restore script (see above). 220 | 221 | 222 | ### Quick cleanup procedure (optional) 223 | This step can be skipped if you spawn a new master. 224 | 225 | Run the following commands on your rancher-master 226 | ``` 227 | sudo service docker stop 228 | rm -rf /var/lib/docker/* 229 | rm -rf /var/lib/mysql/* 230 | sudo service docker start 231 | ``` 232 | 233 | ### Make sure master is configured properly 234 | ``` 235 | ./ansible-playbook_wrapper create_master.yml -K 236 | ``` 237 | 238 | ### Restore mysql 239 | 240 | On the master enter into the container itself and run the restore script 241 | ``` 242 | sudo docker exec -ti mysql-backup-s3 bash 243 | ./restore.sh 244 | ``` 245 | 246 | This will restore the latest backup find on s3. 247 | 248 | If you want to restore a specific backup you can do 249 | ``` 250 | ID_BUCKET_RESTORE=2016-07-21T133544Z.dump.sql.gz ./restore.sh 251 | ``` 252 | 253 | ## Upgrading rancher 254 | 255 | To upgrade rancher you just need to change the rancher_version version in group vars. 256 | Exemple: in production/group_vars/all/vars 257 | ``` 258 | rancher_version: v1.1.1 259 | ``` 260 | 261 | We highly recommend to stick to a specific version for production rancher environment to make sure everything is repeatable. 262 | 263 | Then just run again the create_master.yml playbook. 264 | It will upgrade rancher-master smoothly. 265 | When rancher-master is up again it will contact environments host agents and update it if necessary, this operation is done by rancher itself. 266 | 267 | ## Upgrading docker 268 | 269 | To update docker you just need to set the docker_version variable. 270 | If you want to set this version to all rancher platforms, just update it in roles/docker/defaults/main.yml. 271 | If you want to set this version only for a particular rancher platform, add this variable in your group_vars/all/vars file. 272 | 273 | Then run again the configure_host.yml playbook. Note that you will get downtime if your application containers are not resilient as docker will be restarted. 274 | 275 | ### Docker version caveats 276 | 277 | WARNING: At the moment we use docker 1.10.3 which is not available for ubuntu-xenial. 278 | This was required to deploy gluster fs with convoy as there is a bug right now on docker 1.11 see https://github.com/rancher/rancher/issues/4411 279 | 280 | The docker version is currently configured in roles/docker/defaults/main.yml and we force the distribution name to wily to have access to the docker repository containing 1.10.3. 281 | To make sure ansible does try to discover ubuntu version on hosts itself we have disabled gather_facts in configure_host.yml: 282 | ``` 283 | - hosts: all 284 | #remove this when the glusterfs bug will be fixed 285 | gather_facts: no 286 | ``` 287 | 288 | We highly recommend to upgrade to docker 1.11 or higher (if supported by rancher) when this bug will be fixed. 289 | 290 | ### Example 291 | 292 | You can see an example of this project : https://github.com/AdopteUnOps/ansible-collect-data 293 | 294 | Ansible Collect Data it's a project to collect logs and metrics with the stack Kafka, Elasticsearch, Prometheus and other stuff. 295 | -------------------------------------------------------------------------------- /TROUBLESHOOTING.md: -------------------------------------------------------------------------------- 1 | # Troubleshooting 2 | 3 | 4 | To get live logs of a container (for example a crash looping one) you need to execute the following command on the docker host itself 5 | ``` 6 | docker logs --tail=1 -f DOCKERID 7 | ``` 8 | 9 | To get into a container you need to execute the following command on the docker host itself 10 | ``` 11 | docker exec -ti DOCKERID bash 12 | ``` 13 | 14 | # If Docker daemon doesn't start 15 | 16 | Check docker state 17 | ``` 18 | sudo service docker status 19 | ``` 20 | 21 | Check docker daemon logs 22 | ``` 23 | journalctl -u docker 24 | ``` 25 | 26 | If you get the following error it means you're missing aufs module in the kernel 27 | ``` 28 | 30.117934755Z" level=error msg="[graphdriver] prior storage driver \"aufs\" failed: driver not supported" 29 | ``` 30 | In that case you should run again configure_host.yml on your host. 31 | 32 | ``` 33 | ./ansible-playbook_wrapper configure_host.yml -e NAME_PROJECT=XXXX -l YOURHOSTNAME 34 | ``` 35 | 36 | 37 | ### On master side 38 | 39 | Check host global heatlh (freespace, memory, cpu). 40 | 41 | Check docker state 42 | ``` 43 | sudo service docker status 44 | ``` 45 | 46 | Check docker daemon logs 47 | ``` 48 | journalctl -u docker 49 | ``` 50 | 51 | Check system logs 52 | ``` 53 | dmesg 54 | ``` 55 | 56 | Check the logs of rancherServer container. 57 | ``` 58 | sudo docker logs --tail=1000 -f rancherServer 59 | ``` 60 | 61 | ### On environments hosts 62 | 63 | Check container logs, if it doesn't work check the log of network agent container. 64 | 65 | Check host global heatlh (freespace, memory, cpu). 66 | 67 | Check docker state 68 | ``` 69 | sudo service docker status 70 | ``` 71 | 72 | Check docker daemon logs 73 | ``` 74 | journalctl -u docker 75 | ``` 76 | 77 | Check system logs 78 | ``` 79 | dmesg 80 | ``` 81 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | Vagrant.configure("2") do |config| 2 | #note that global provisionners will be run FIRST as they are less precise, 3 | #this is where we will trigger configure-host and then on a per host basis we will setup rancher master or a rancher host 4 | config.vm.define "rancher-master1" do |ranchermaster| 5 | ranchermaster.vm.box = "ubuntu/xenial64" 6 | ranchermaster.vm.network "private_network", ip: "192.168.100.10", virtualbox__intnet: true 7 | ranchermaster.vm.network "forwarded_port", guest: 8080, host: 8080 8 | ranchermaster.vm.hostname = "rancher-master1" 9 | ranchermaster.vm.provider "virtualbox" do |vb| 10 | vb.memory = 2048 11 | end 12 | ranchermaster.vm.provision "install-rancher-master", type:'ansible' do |ansible| 13 | ansible.config_file = 'ansible.cfg' 14 | ansible.limit = "all" 15 | ansible.playbook = 'create_master.yml' 16 | ansible.groups = { 17 | "rancher-master" => [ "rancher-master1" ], 18 | "rancher-master:vars" => { 19 | "mysql_host" => "192.168.100.10", 20 | "mysql_database" => "rancher", 21 | "mysql_user" => "rancher", 22 | "mysql_password" => "rancher", 23 | "rancher_master_host" => "192.168.100.10", 24 | "rancher_master_port" => "8080", 25 | }, 26 | 27 | } 28 | end 29 | end 30 | 31 | config.vm.define "rancher-host1" do |rancherhost| 32 | rancherhost.vm.box = "ubuntu/xenial64" 33 | rancherhost.vm.network "private_network", ip: "192.168.100.20", virtualbox__intnet: true 34 | rancherhost.vm.hostname = "rancher-host1" 35 | rancherhost.vm.provider "virtualbox" do |vb| 36 | vb.memory = 2048 37 | end 38 | config.vm.provision "create-demo-project", type:'ansible' do |ansible| 39 | ansible.config_file = 'ansible.cfg' 40 | ansible.limit = "all" 41 | ansible.playbook = 'create_project.yml' 42 | ansible.groups = { 43 | "demo-project" => [ "rancher-host1"], 44 | "demo-project:vars" => { 45 | "rancher_master_host" => "192.168.100.10", 46 | "rancher_master_port" => "8080", 47 | "rancher_master_url" => "http://{{rancher_master_host}}:{{rancher_master_port}}", 48 | "rancher_project_name" => "{{NAME_PROJECT | lower }}" 49 | } 50 | } 51 | ansible.raw_arguments = ["-e NAME_PROJECT=demo-project"] 52 | end 53 | end 54 | 55 | config.vm.provision "configure-host", type:'ansible' do |ansible| 56 | ansible.config_file = 'ansible.cfg' 57 | ansible.limit = "all" 58 | ansible.playbook = 'configure_host.yml' 59 | ansible.raw_arguments = ["-e NAME_PROJECT=all -e use_docker_lvm=false"] 60 | end 61 | 62 | end 63 | -------------------------------------------------------------------------------- /ansible-playbook_wrapper: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | SCRIPTPATH=`dirname $0` 3 | export ANSIBLE_HOSTS="$SCRIPTPATH/production/inventory" 4 | export ANSIBLE_CONFIG=$SCRIPTPATH/ansible.cfg 5 | if [ -s $SCRIPTPATH/config_ssh ] 6 | then 7 | export ANSIBLE_SSH_ARGS="-F config_ssh -o ControlMaster=auto -o ControlPersist=30m" 8 | fi 9 | if [ -s $SCRIPTPATH/.ansible_vault_pass ] 10 | then 11 | export ANSIBLE_VAULT_PASSWORD_FILE=$SCRIPTPATH/.ansible_vault_pass 12 | fi 13 | ansible-playbook "$@" 14 | -------------------------------------------------------------------------------- /ansible-vault_wrapper: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | SCRIPTPATH=`dirname $0` 3 | export ANSIBLE_CONFIG=$SCRIPTPATH/ansible.cfg 4 | if [ -s $SCRIPTPATH/.ansible_vault_pass ] 5 | then 6 | export ANSIBLE_VAULT_PASSWORD_FILE=$SCRIPTPATH/.ansible_vault_pass 7 | fi 8 | ansible-vault "$@" 9 | -------------------------------------------------------------------------------- /ansible.cfg: -------------------------------------------------------------------------------- 1 | # config file for ansible -- http://ansible.com/ 2 | # ============================================== 3 | 4 | # nearly all parameters can be overridden in ansible-playbook 5 | # or with command line flags. ansible will read ANSIBLE_CONFIG, 6 | # ansible.cfg in the current working directory, .ansible.cfg in 7 | # the home directory or /etc/ansible/ansible.cfg, whichever it 8 | # finds first 9 | 10 | [defaults] 11 | # SSH timeout 12 | timeout = 30 13 | host_key_checking = False 14 | roles_path = roles 15 | 16 | [ssh_connection] 17 | 18 | # ssh arguments to use 19 | # Leaving off ControlPersist will result in poor performance, so use 20 | # paramiko on older platforms rather than removing it 21 | ssh_args = -o ControlMaster=auto -o ControlPersist=30m 22 | 23 | # Enabling pipelining reduces the number of SSH operations required to 24 | # execute a module on the remote server. This can result in a significant 25 | # performance improvement when enabled, however when using "sudo:" you must 26 | # first disable 'requiretty' in /etc/sudoers 27 | # 28 | # By default, this option is disabled to preserve compatibility with 29 | # sudoers configurations that have requiretty (the default on many distros). 30 | # 31 | pipelining = True 32 | 33 | # if True, make ansible use scp if the connection type is ssh 34 | # (default is sftp) 35 | scp_if_ssh = True 36 | 37 | [accelerate] 38 | accelerate_port = 5099 39 | accelerate_timeout = 30 40 | accelerate_connect_timeout = 5.0 41 | 42 | # The daemon timeout is measured in minutes. This time is measured 43 | # from the last activity to the accelerate daemon. 44 | accelerate_daemon_timeout = 30 45 | -------------------------------------------------------------------------------- /ansible_wrapper: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | SCRIPTPATH=`dirname $0` 3 | export ANSIBLE_HOSTS="$SCRIPTPATH/production/inventory" 4 | export ANSIBLE_CONFIG=$SCRIPTPATH/ansible.cfg 5 | if [ -s $SCRIPTPATH/config_ssh ] 6 | then 7 | export ANSIBLE_SSH_ARGS="-F config_ssh -o ControlMaster=auto -o ControlPersist=30m" 8 | fi 9 | if [ -s $SCRIPTPATH/.ansible_vault_pass ] 10 | then 11 | export ANSIBLE_VAULT_PASSWORD_FILE=$SCRIPTPATH/.ansible_vault_pass 12 | fi 13 | MY_ANSIBLE_HOST_PATTERN="$1" 14 | shift 15 | ansible "$MY_ANSIBLE_HOST_PATTERN" -i $ANSIBLE_HOSTS "$@" 16 | -------------------------------------------------------------------------------- /config_ssh.template: -------------------------------------------------------------------------------- 1 | 2 | ## 3 | ## Simple use case 4 | ## 5 | 6 | Host * 7 | User USER 8 | IdentityFile PATH_TO_YOUR_PRIVATE_KEY 9 | 10 | ## 11 | ## Bastion use case 12 | ## 13 | 14 | # this is a template file to configure ssh to do transparent proxy on a bastion host 15 | # to use it : 16 | # > cp config_ssh.template config_ssh 17 | # configure USER and IdentityFile lines 18 | # save and enjoy transparent proxy by using ssh_wrapper :) 19 | 20 | 21 | #Host bastion_host 22 | # User USER 23 | # Port 2222 24 | # HostName bastion_hostname_or_ip 25 | # IdentityFile PATH_TO_YOUR_PRIVATE_KEY 26 | # ForwardAgent yes 27 | # ProxyCommand none 28 | # BatchMode yes 29 | # 30 | #Host * 31 | # User USER 32 | # ProxyCommand ssh -F config_ssh -W %h:%p bastion_host 33 | -------------------------------------------------------------------------------- /configure_host.yml: -------------------------------------------------------------------------------- 1 | --- 2 | #first time ./ansible-playbook_wrapper configure_host.yml -u yourgenericaccount -Kk -e "NAME_PROJECT=myproject" 3 | #second time ./ansible-playbook_wrapper configure_host.yml -K -e "NAME_PROJECT=myproject" 4 | 5 | - hosts: "{{ NAME_PROJECT }}" 6 | gather_facts: false 7 | tasks: 8 | - name: Make sure python is installed 9 | raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal) 10 | become: true 11 | 12 | - hosts: "{{ NAME_PROJECT }}" 13 | roles: 14 | - {role: "AdopteUnOps.install-kernel", tags: "kernel"} 15 | - {role: "AdopteUnOps.configure-rsyslog", tags: "rsyslog"} 16 | - {role: "AdopteUnOps.configure-hostname", tags: "configure-hostname"} 17 | - {role: "AdopteUnOps.configure-ssh", tags: "configure-ssh"} 18 | - {role: "AdopteUnOps.install-debugging-tools", tags: "install-tools"} 19 | - {role: "AdopteUnOps.docker", tags: "install-docker"} 20 | - {role: "AdopteUnOps.docker-login", tags: "install-docker"} 21 | - {role: "AdopteUnOps.upgrade-packages", tags: "upgrade-packages"} 22 | - {role: "AdopteUnOps.configure-unix-accounts", tags: "ops"} 23 | -------------------------------------------------------------------------------- /create_master.yml: -------------------------------------------------------------------------------- 1 | --- 2 | #./ansible-playbook_wrapper create_master.yml -K 3 | - hosts: rancher-master 4 | roles: 5 | - {role: AdopteUnOps.mysql-docker, tags: mysql} 6 | - {role: AdopteUnOps.rancher-install-master, tags: rancher-master} 7 | - {role: AdopteUnOps.rancher-configure-catalog, tags: catalogs} 8 | -------------------------------------------------------------------------------- /create_project.yml: -------------------------------------------------------------------------------- 1 | --- 2 | #./ansible-playbook_wrapper create_project.yml -K -e "NAME_PROJECT=myproject" 3 | - hosts: "{{ NAME_PROJECT}}[0]" 4 | gather_facts: no 5 | roles: 6 | - {role: AdopteUnOps.rancher-create-project, tags: rancher-create-project} 7 | 8 | - hosts: "{{ NAME_PROJECT }}" 9 | pre_tasks: 10 | # force reload of apikey, as it's cached by ansible when we first trigger the playbook 11 | - include_vars: "{{ inventory_dir }}/group_vars/{{ NAME_PROJECT }}/apikey.yml" 12 | roles: 13 | - {role: AdopteUnOps.rancher-add-host, tags: add-host} 14 | 15 | - hosts: "{{ NAME_PROJECT}}[0]" 16 | gather_facts: no 17 | roles: 18 | - {role: AdopteUnOps.rancher-install-stack, stack_name: janitor, tags: install-stack} 19 | -------------------------------------------------------------------------------- /requirements.yml: -------------------------------------------------------------------------------- 1 | #configure host dependencies 2 | - src: AdopteUnOps.install-kernel 3 | - src: AdopteUnOps.configure-rsyslog 4 | - src: AdopteUnOps.configure-hostname 5 | - src: AdopteUnOps.configure-ssh 6 | - src: AdopteUnOps.install-debugging-tools 7 | - src: AdopteUnOps.docker 8 | - src: AdopteUnOps.docker-login 9 | - src: AdopteUnOps.upgrade-packages 10 | - src: AdopteUnOps.configure-unix-accounts 11 | 12 | # rancher stuff dependencies 13 | - src: AdopteUnOps.mysql-docker 14 | - src: AdopteUnOps.rancher-install-master 15 | - src: AdopteUnOps.rancher-create-project 16 | - src: AdopteUnOps.rancher-add-host 17 | - src: AdopteUnOps.rancher-install-stack 18 | - src: AdopteUnOps.rancher-configure-catalog 19 | - src: AdopteUnOps.clean-docker-host 20 | - src: AdopteUnOps.rancher-delete-host 21 | - src: AdopteUnOps.rancher-delete-project 22 | -------------------------------------------------------------------------------- /sample/group_vars/all/rancher: -------------------------------------------------------------------------------- 1 | DEVOPS_LOGIN: devops 2 | DEVOPS_PASSWORD: changeme 3 | rancher_master_host: "{{groups['rancher-master'][0]}}" 4 | rancher_master_port: 8080 5 | rancher_version: v1.6.10 6 | rancher_agent_version: v1.2.6 7 | 8 | rancher_catalogs: 9 | catalogs: 10 | library: 11 | url: https://git.rancher.io/rancher-catalog.git 12 | branch: "${RELEASE}" 13 | community: 14 | url: https://git.rancher.io/community-catalog.git 15 | branch: master 16 | adopteunops-catalog: 17 | url: https://github.com/AdopteUnOps/rancher-catalog.git 18 | branch: master 19 | -------------------------------------------------------------------------------- /sample/group_vars/all/unix_accounts: -------------------------------------------------------------------------------- 1 | ssh_users: 2 | - name: myuser 3 | key: "ssh-rsa ZZZZZYOURSSHKEY" 4 | password: "changeme" 5 | former_ops_users: 6 | - name: mygenericaccount 7 | -------------------------------------------------------------------------------- /sample/group_vars/all/vars: -------------------------------------------------------------------------------- 1 | normalized_project_name: "{{ NAME_PROJECT | lower}}" 2 | -------------------------------------------------------------------------------- /sample/group_vars/rancher-master/mysql-rancher: -------------------------------------------------------------------------------- 1 | mysql_host: "{{ groups.mysql[0] }}" 2 | mysql_database: rancher 3 | mysql_user: rancher 4 | mysql_password: pha8Queirierane 5 | 6 | mysql_backup_enabled: false 7 | 8 | mysql_s3_access_key: A_S3_TOKEN 9 | mysql_s3_secret_key: S3_SECRET 10 | mysql_s3_bucket: rancher-s3 11 | mysql_s3_prefix: rancher-master-backup 12 | mysql_s3_region: eu-central-1 13 | mysql_s3_schedule: "@daily" 14 | -------------------------------------------------------------------------------- /sample/inventory: -------------------------------------------------------------------------------- 1 | [rancher-master] 2 | host-1 3 | 4 | [my-first-project] 5 | host-3 6 | host-4 7 | -------------------------------------------------------------------------------- /scp_wrapper: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | SCRIPTPATH=`dirname $0` 3 | sshconfig_file=${SCRIPTPATH}/config_ssh 4 | if [ ! -d ${HOME}/.ansible/cp ]; then 5 | mkdir -p ${HOME}/.ansible/cp 6 | fi 7 | if [ -f ${sshconfig_file} ]; then 8 | scp -F ${sshconfig_file} -o ControlMaster=auto -o ControlPersist=30m -o ControlPath="${HOME}/.ansible/cp/ansible-ssh-%h-%p-%r" "$@" 9 | else 10 | scp -o ControlMaster=auto -o ControlPersist=30m -o ControlPath="${HOME}/.ansible/cp/ansible-ssh-%h-%p-%r" "$@" 11 | fi 12 | 13 | -------------------------------------------------------------------------------- /ssh_wrapper: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | SCRIPTPATH=`dirname $0` 3 | sshconfig_file=${SCRIPTPATH}/config_ssh 4 | if [ ! -d ${HOME}/.ansible/cp ]; then 5 | mkdir -p ${HOME}/.ansible/cp 6 | fi 7 | if [ -f ${sshconfig_file} ]; then 8 | ssh -F ${sshconfig_file} -o ControlMaster=auto -o ControlPersist=30m -o ControlPath="${HOME}/.ansible/cp/ansible-ssh-%h-%p-%r" "$@" 9 | else 10 | ssh -o ControlMaster=auto -o ControlPersist=30m -o ControlPath="${HOME}/.ansible/cp/ansible-ssh-%h-%p-%r" "$@" 11 | fi 12 | 13 | -------------------------------------------------------------------------------- /templates/janitor/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | cleanup: 4 | privileged: true 5 | image: meltwater/docker-cleanup:1.8.0 6 | environment: 7 | CLEAN_PERIOD: '3600' 8 | DEBUG: '0' 9 | DELAY_TIME: '900' 10 | KEEP_CONTAINERS: '*:*' 11 | KEEP_CONTAINERS_NAMED: '*-datavolume' 12 | KEEP_IMAGES: rancher/ 13 | LOOP: 'true' 14 | network_mode: none 15 | volumes: 16 | - /var/run/docker.sock:/var/run/docker.sock 17 | - /var/lib/docker:/var/lib/docker 18 | labels: 19 | io.rancher.scheduler.affinity:host_label_ne: janitor.exclude=true 20 | io.rancher.scheduler.global: 'true' 21 | -------------------------------------------------------------------------------- /templates/janitor/rancher-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | cleanup: 4 | start_on_create: true 5 | -------------------------------------------------------------------------------- /utils_clean_host.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # clean One HOST on project 3 | #./ansible-playbook_wrapper utils_clean_host.yml -K -e "NAME_PROJECT=myproject" -e "NAME_HOST=myHost" (check with inventory) 4 | # If you want delete all host of the project, you can't set NAME_HOST 5 | - hosts: rancher-masters 6 | gather_facts: no 7 | roles: 8 | - AdopteUnOps.rancher-delete-host 9 | 10 | - vars: 11 | NAME_HOST: "{{ NAME_PROJECT }}" 12 | hosts: "{{ NAME_HOST }}" 13 | gather_facts: no 14 | roles: 15 | - AdopteUnOps.rancher-clean-docker-host 16 | --------------------------------------------------------------------------------