This repository contains the implementation for a DevOps task involving containerization, infrastructure provisioning, and configuration management for a Python metrics exporter. The solution uses Docker, Terraform, and Ansible and is designed to run on Yandex.Cloud.
Objective:
- Containerize a Python script (
exporter.py) that exports Prometheus metrics. - Provision two virtual machines using Terraform:
- Application Host (Ubuntu 22.04)
- Monitoring Host (Ubuntu 22.04)
- Configure the hosts using Ansible:
- Application Host:
- Configure a firewall.
- Install Docker.
- Deploy the metrics exporter container.
- Monitoring Host:
- Configure a firewall.
- Install Prometheus.
- Set up Prometheus to scrape metrics from the Application Host.
- Application Host:
Environment:
- Preferred: Yandex.Cloud.
- Alternative: Any other Russian cloud provider.
Deliverables:
- A mono-repository containing all necessary code.
- Deployed machines accessible via SSH.
- Demonstration of Prometheus collecting metrics.
/
|-- docker/
| |-- Dockerfile # Dockerfile to containerize the Python script
| |-- exporter.py # Python script exporting Prometheus metrics
|
|-- terraform/
| |-- main.tf # Terraform configuration for provisioning VMs
|
|-- ansible/
| |-- app_host.yml # Ansible playbook for the Application Host
| |-- monitoring_host.yml # Ansible playbook for the Monitoring Host
|
|-- README.md # Documentation (this file)
- Terraform installed on your local machine.
- Ansible installed on your local machine.
- Docker installed locally to build the Docker image.
- A Yandex.Cloud account with the necessary credentials:
- Cloud ID
- Folder ID
- Yandex.Cloud API token
- SSH key pair for accessing the provisioned VMs.
- Navigate to the
docker/directory:cd docker - Build the Docker image:
docker build -t exporter:latest . - Optionally, push the Docker image to a container registry if required.
- Navigate to the
terraform/directory:cd terraform - Initialize Terraform:
terraform init
- Apply the Terraform configuration:
terraform apply
- You will be prompted to input variables (e.g., Cloud ID, Folder ID, etc.).
- Terraform will provision two VMs: Application Host and Monitoring Host.
- Update the
inventoryfile to include the IPs of the provisioned VMs. Example:[application_host] <APPLICATION_HOST_IP> [monitoring_host] <MONITORING_HOST_IP>
- Run the playbook for the Application Host:
ansible-playbook -i inventory ansible/app_host.yml
- Run the playbook for the Monitoring Host:
ansible-playbook -i inventory ansible/monitoring_host.yml
- SSH into the Monitoring Host:
ssh ubuntu@<MONITORING_HOST_IP>
- Access the Prometheus web UI:
- URL:
http://<MONITORING_HOST_IP>:9090
- URL:
- Verify that Prometheus is scraping metrics from the Application Host:
- Navigate to Status > Targets.
- Ensure the exporter target is listed and in the "UP" state.
- Mono-repository with the following structure:
docker/(Dockerfile and Python script).terraform/(Terraform configuration).ansible/(Ansible playbooks).
- Deployed virtual machines accessible via SSH.
- Demonstration of metrics collection by Prometheus.
- This repository includes placeholders (e.g.,
<APPLICATION_HOST_IP>,<YOUR_SSH_PUBLIC_KEY>). Replace these with actual values before running the scripts. - For CI/CD, GitHub Actions can be integrated to automate Docker image builds and Terraform deployments.