|
| 1 | +# Deployment |
| 2 | + |
| 3 | +## Contents: |
| 4 | + |
| 5 | +- [Quick Start](#quick-start) |
| 6 | + |
| 7 | +## Quick Start: |
| 8 | + |
| 9 | +- If you are deploying from scratch, start at [Setting up localhost](#setting-up-localhost) |
| 10 | +- If you already have the repository cloned, the vault password saved and the projects clouds.yaml then start |
| 11 | + at [Deploy infrastructure](#deploy-infrastructure). |
| 12 | +- If you only need to make changes to an existing deployment then start |
| 13 | + at [Configure infrastructure](#configure-infrastructure) |
| 14 | +- To destroy all infrastructure, see [Destroy infrastructure](#destroy-infrastructure) |
| 15 | + |
| 16 | +## OpenStack Project Requirements: |
| 17 | + |
| 18 | +The project `Cloud-MicroServices` is already setup with all the required requisites. The variables in this repository |
| 19 | +reference that project. If you are using a different project for a deployment not used by the Cloud Team you will |
| 20 | +require the following: |
| 21 | + |
| 22 | +- A floating IP (e.g. 130.246.X.Y) |
| 23 | +- DNS records: |
| 24 | + - `<your-domain> CNAME host-130-246-X-Y.nubes.stfc.ac.uk` |
| 25 | + - **AND** |
| 26 | + - ``` |
| 27 | + # EITHER |
| 28 | + *.<your-domain> CNAME host-130-246-X-Y.nubes.stfc.ac.uk |
| 29 | + # OR |
| 30 | + kibana.<your-domain>. CNAME host-130-246-X-Y.nubes.stfc.ac.uk. |
| 31 | + grafana.<your-domain>. CNAME host-130-246-X-Y.nubes.stfc.ac.uk. |
| 32 | + prometheus.<your-domain>. CNAME host-130-246-X-Y.nubes.stfc.ac.uk. |
| 33 | + alertmanager.<your-domain>. CNAME host-130-246-X-Y.nubes.stfc.ac.uk. |
| 34 | + chatops.<your-domain>. CNAME host-130-246-X-Y.nubes.stfc.ac.uk. |
| 35 | + ``` |
| 36 | +- Ports 80 and 443 open inbound from the internet |
| 37 | +- OpenStack Volume for the VM ~10GB |
| 38 | +
|
| 39 | +### Deploying the Infrastructure: |
| 40 | +
|
| 41 | +You can run the deployment from any machine (including your local laptop). |
| 42 | +However, we suggest you make a dedicated "seed VM" in OpenStack as the |
| 43 | +deployment will create files such as SSL certificates and SSH keys which you |
| 44 | +will need to keep for further maintenance. |
| 45 | +
|
| 46 | +Machine requirements: |
| 47 | +
|
| 48 | +- Python3 |
| 49 | +- Snap (to install Terraform) |
| 50 | +- Pip or equivalent (to install Ansible) |
| 51 | +
|
| 52 | +#### Setting up localhost: |
| 53 | +
|
| 54 | +1. Install Ansible and collections |
| 55 | + ```shell |
| 56 | + # Install venv and Ansible |
| 57 | + apt install python3-venv ansible |
| 58 | + |
| 59 | + # Create a virtual environment |
| 60 | + python3 -m venv venv |
| 61 | + source venv/bin/activate |
| 62 | + |
| 63 | + # Install collections using Ansible Galaxy |
| 64 | + ansible-galaxy install -r requirements.yml |
| 65 | +
|
| 66 | + # Install dependencies |
| 67 | + pip install -r requirements.yml |
| 68 | + ``` |
| 69 | + |
| 70 | +2. Create a vault password file to avoid repeated inputs |
| 71 | + ```shell |
| 72 | + # Either |
| 73 | + |
| 74 | + echo "chatops_vault_password" >> ~/.chatops_vault_pass |
| 75 | + |
| 76 | + # or |
| 77 | + |
| 78 | + vim ~/.chatops_vault_pass # and enter the vault password as plain text |
| 79 | + ``` |
| 80 | + |
| 81 | +3. Change permissions and attributes to protect the file |
| 82 | + ```shell |
| 83 | + chmod 400 ~/.chatops_vault_pass |
| 84 | + chattr +i ~/.chatops_vault_pass |
| 85 | + ``` |
| 86 | + |
| 87 | +4. Copy the projects clouds.yaml to the `~/.config/openstack/clouds.yaml` |
| 88 | + ```shell |
| 89 | + cp <path-to>/clouds.yaml ~/.config/openstack/clouds.yaml |
| 90 | + ``` |
| 91 | + |
| 92 | +#### Deploy infrastructure: |
| 93 | + |
| 94 | +You can deploy both development and production environments on the same machine but not at the same time. |
| 95 | + |
| 96 | +1. Clone this repository |
| 97 | + ```shell |
| 98 | + git clone https://github.com/stfc/SCD-OpenStack-Utils |
| 99 | + ``` |
| 100 | + |
| 101 | +2. Change into the `ansible` directory |
| 102 | + ```shell |
| 103 | + cd SCD-OpenStack-Utils/chatops_deployment/ansible |
| 104 | + ``` |
| 105 | + |
| 106 | +3. Deploy infrastructure. Using -i to specify which inventory to use, dev or prod |
| 107 | + ```shell |
| 108 | + ansible-playbook deploy.yml --vault-password-file=~/.chatops_vault_pass -i <environment> |
| 109 | + ``` |
| 110 | + |
| 111 | +#### Configure infrastructure |
| 112 | + |
| 113 | +1. Configure the VMs. This step will take ~15 minutes |
| 114 | + ```shell |
| 115 | + ansible-playbook configure.yml --vault-password-file=~./chatops_vault_pass -i <environment> |
| 116 | + ``` |
| 117 | + |
| 118 | +#### Destroy infrastructure |
| 119 | + |
| 120 | +To destroy the infrastructure and all locally generated files run the destroy playbook. |
| 121 | + |
| 122 | +1. Destroy the infrastructure and locally generated files |
| 123 | + ```shell |
| 124 | + ansible-playbook destroy.yml --vault-password-file=~./chatops_vault_pass -i <environment> |
| 125 | + ``` |
| 126 | + |
| 127 | +## Debugging: |
| 128 | + |
| 129 | +### Terraform |
| 130 | + |
| 131 | +To debug the Terraform deployment, it is best to use the Terraform directly rather than through Ansible. |
| 132 | +When you run the deploy.yml playbook, a `terraform.tfvars` file is created which allows you to run the Terraform modules |
| 133 | +separate to Ansible. |
| 134 | + |
| 135 | +1. Ensure you have run deploy.yml at least once to generate the variables file `terraform.tfvars` |
| 136 | + |
| 137 | +2. Change to the terraform directory |
| 138 | + ```shell |
| 139 | + # Assuming you are in the ansible directory |
| 140 | + cd ../terraform |
| 141 | + ``` |
| 142 | + |
| 143 | +3. Check and change Terraform workspace. Terraform separates environments into workspaces. Make sure you are using the |
| 144 | + correct workspace before making changes. |
| 145 | + ```shell |
| 146 | + # List all workspaces. You should see at most "default, dev, prod" |
| 147 | + terraform workspace list |
| 148 | + |
| 149 | + # Select the workspace you want to affect |
| 150 | + terraform workspace select <environment> |
| 151 | + ``` |
| 152 | +
|
| 153 | +4. Now you can make changes to the deployment. It is advisable you only use the Terraform commands directly if there is |
| 154 | + something very wrong. The Ansible playbooks should be the first choice. |
| 155 | + ```shell |
| 156 | + # For example, plan and apply changes |
| 157 | + terraform plan -out plan |
| 158 | + terraform apply plan |
| 159 | + |
| 160 | + # Refresh the state to check API connections |
| 161 | + terraform refresh |
| 162 | + |
| 163 | + # Validate the config |
| 164 | + terraform validate |
| 165 | + ``` |
| 166 | + |
| 167 | +### Ansible |
| 168 | +
|
| 169 | +Each role in the Ansible playbook is tagged in its play. This enables you to run only parts of the playbooks. This is |
| 170 | +important as it takes ~15 minutes to run the entire playbook. So, when you only want to make changes to certain parts |
| 171 | +of the deployment you can use `--tags <some-tag>` to run only that part of the play. |
| 172 | +
|
| 173 | +For example, if you change the Prometheus config file template you can just run the playbook with the **prometheus** tag |
| 174 | +. |
| 175 | +```shell |
| 176 | +ansible-playbook configure.yml --vault-password-file=~./chatops_vault_pass -i dev --tags prometheus |
| 177 | +``` |
| 178 | +
|
| 179 | +It is not recommended to use tags when making changes to the production deployment. As changes are promoted to |
| 180 | +production the entire playbook should be run. This avoids any changes being missed out and ensures the entire deployment |
| 181 | +is running the latest configuration. |
0 commit comments