Welcome to the Monitoring Stack Deployment project! This repository provides an automated solution to deploy a comprehensive monitoring stack using Ansible. The stack includes Prometheus, Grafana, Loki, AlertManager, Node Exporter, and cAdvisor, orchestrated with Docker containers. The setup is designed for testing purposes and suitable for development environments.
Code in Action:
- DigitalOcean:
DG.1.mp4
- Vagrant:
vagrant.mp4
Effective monitoring is crucial for maintaining the health and performance of your infrastructure. This project automates the deployment of a robust monitoring stack using Ansible, ensuring that you can quickly set up and manage your monitoring tools.
The stack includes:
- Prometheus: Collects metrics from configured targets at given intervals.
- Grafana: Visualizes data through rich dashboards.
- Loki: Provides log aggregation and querying.
- AlertManager: Handles alerts sent by client applications like Prometheus.
- Node Exporter: Exposes machine metrics.
- cAdvisor: Collects container metrics.
- Automated Deployment: Use Ansible playbooks to deploy the entire monitoring stack with a single command.
- Dockerized Services: All components run in Docker containers for isolation and ease of management.
- Scalable Architecture: Easily add more nodes or services to your monitoring setup.
- Customizable Configuration: Templates and variables allow you to tailor configurations to your needs.
- Local Development with Vagrant: Test the stack locally using Vagrant and VirtualBox.
- Automated Testing: Ensure reliability with pytest and testinfra tests.
Before you begin, ensure you have the following installed on your control machine (the machine from which you'll run the Ansible playbooks):
-
Ansible
- Installation:
# For Ubuntu/Debian sudo apt update sudo apt install ansible -y # For macOS using Homebrew brew install ansible
- Version Check:
ansible --version
- Installation:
-
Python and pip
- Installation:
# For Ubuntu/Debian sudo apt install python3 python3-pip -y # For macOS using Homebrew brew install python
- Version Check:
python3 --version pip3 --version
- Installation:
-
Git
- Installation:
# For Ubuntu/Debian sudo apt install git -y # For macOS using Homebrew brew install git
- Version Check:
git --version
- Installation:
-
Vagrant (Optional, for local development)
- Installation: Download from Vagrant Downloads and follow the installation instructions for your operating system.
- Version Check:
vagrant --version
-
Docker (On Target Hosts)
- Note: The Ansible playbooks include roles to install Docker on target hosts. However, if you prefer to install Docker manually:
# For Ubuntu/Debian # Check https://docs.docker.com/engine/install/ubuntu/ # Add Docker's official GPG key: sudo apt-get update sudo apt-get install ca-certificates curl sudo install -m 0755 -d /etc/apt/keyrings sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc # Add the repository to Apt sources: echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- Note: The Ansible playbooks include roles to install Docker on target hosts. However, if you prefer to install Docker manually:
-
Clone the Repository
git clone https://github.com/Aazme/monitoring-stack-ansible.git cd monitoring-stack-ansible
-
Configure Ansible Inventory
-
Inventory File: Edit
inventory/inventory.yml
to include your target hosts.Example:
all: hosts: monitoring-server: ansible_host: your_server_ip ansible_user: your_ssh_user ansible_ssh_private_key_file: path_to_your_private_key
-
Vagrant Inventory (Optional): For local development with Vagrant, use
inventory/vagrant_inventory.yml
.
-
-
Update Variables (If Necessary)
- Ansible Configuration: Review and update
ansible.cfg
if needed. - Role Variables: Customize variables in
playbooks/roles/*/vars/main.yml
as required.
- Ansible Configuration: Review and update
-
Install Python Dependencies
pip3 install -r requirements.txt
Requirements File (
requirements.txt
):ansible pytest testinfra
-
(Optional) Set Up Vagrant for Local Testing
- Start the Vagrant VM:
vagrant up
- SSH into the VM:
vagrant ssh monitoring-server
- Start the Vagrant VM:
-
Run the Ansible Playbook
ansible-playbook playbooks/site.yml
playbooks/site.yml
is the main playbook that includes all roles.
- Prometheus:
- URL:
http://your_server_ip:9090
- URL:
- Grafana:
- URL:
http://your_server_ip:3000
- Default Credentials:
- Username:
admin
- Password:
admin
(change after first login)
- Username:
- URL:
- AlertManager:
- URL:
http://your_server_ip:9093
- URL:
- Loki:
- URL:
http://your_server_ip:3100
- URL:
- Node Exporter Metrics:
- Metrics endpoint:
http://your_server_ip:9100/metrics
- Metrics endpoint:
- cAdvisor Metrics:
- Metrics endpoint:
http://your_server_ip:8080/metrics
- Metrics endpoint:
- Grafana Dashboards:
- Import existing dashboards or create new ones.
- Use the provided dashboard template (
grafana_dashboard.json.j2
) as a starting point.
To configure Telegram alerts in AlertManager, you need to update the alertmanager/defaults/main.yml
file with your Telegram bot token and chat ID:
File: alertmanager/defaults/main.yml
---
telegram_bot_token: "7747059131:AAFZGqnsFFC7u-XXXXXXXXXXXXXXXX"
telegram_chat_id: "-100XXXXXXXXX"
Replace the placeholders with your actual Telegram bot token and chat ID. This configuration will allow AlertManager to send alerts to your specified Telegram chat.
-
Install Test Dependencies
pip3 install pytest testinfra
-
Run the Tests
pytest --ansible-inventory=./inventory/inventory.yml --force-ansible --connection=ansible
- SSH into Target Host
ssh your_ssh_user@your_server_ip
- Check Docker Containers
docker ps
- Verify Services
systemctl status docker
-
SSH Connection Errors
- Symptom: Ansible reports
UNREACHABLE
or SSH errors. - Solution: Verify SSH connectivity to the target host and ensure SSH user and key are correctly specified.
- Symptom: Ansible reports
-
Permission Denied Errors
- Symptom: Containers fail to access necessary resources.
- Solution: Check file permissions on configuration directories and ensure appropriate permissions for Docker socket.
-
Services Not Running
- Symptom: Docker containers or services are not running.
- Solution: Use
docker logs container_name
to view logs and check for configuration errors.
- Ansible Verbose Mode: Run playbooks with increased verbosity:
ansible-playbook playbooks/site.yml -vvv
- System Logs: Check system logs on the target host for errors:
journalctl -xe
- Docker Logs: View logs for individual containers:
docker logs prometheus
We welcome contributions to enhance the functionality and usability of this project.
-
Fork the Repository
- Create a personal fork of the repository on GitHub.
-
Clone Your Fork
git clone https://github.com/Aazme/monitoring-stack-ansible.git cd monitoring-stack-ansible
-
Create a Feature Branch
git checkout -b feature/your-feature-name
-
Make Your Changes
- Follow coding standards and best practices.
- Update or add documentation as necessary.
- Write or update tests to cover your changes.
-
Commit Your Changes
- Use Conventional Commits for commit messages.
git commit -m "feat: add new feature X"
-
Push to Your Fork
git push origin feature/your-feature-name
-
Open a Pull Request
- Go to the repository on GitHub and open a pull request.
- Provide a clear description of your changes.
Please read and adhere to the Code of Conduct.
Refer to the Contributing Guide for detailed instructions.
This project is licensed under the MIT License. See the LICENSE file for details.
- Ansible Community: For providing extensive documentation and support.
- Prometheus and Grafana Teams: For creating powerful monitoring tools.
- Open Source Contributors: For their valuable contributions to the tools used in this project.
For questions or support, please open an issue on the GitHub repository or contact the maintainer: