반응형
이전에는 호스트에서 수동으로 설치 및 구성하여 prometheus / node_exporter / grafana 설치 가이드를 정리했습니다.
이번에는 ansible을 통해 3개 프로그램을 각 노드에 설치 및 설정할 수 있게 playbook을 작성 및 테스트해봤고, playbook 작성한 내용과 작성한 파일들을 github에 업로드하였습니다.
hk-controller 라는 호스트에서 ansible을 통해 다른 노드에 prometheus 설치를 위한 작성한 yaml에 대한 내용입니다.
작성한 ansible-playbook 파일은 아래 github에서 다운받아서 사용할 수 있습니다.
https://github.com/hkjeon/ansible-prometheus
## 환경 정보
ansible-controller : xx.xx.xx.5 (RHEL 8.4)
prometheus : xx.xx.xx.6 (RHEL 8.4)
1. main.yml 생성
[root@hk-controller ansible-prometheus]#cat main.yaml
---
- hosts: prometheus
become: false
gather_facts: false
roles:
- install
2. install roles 생성
ansible-galaxy init roles/install
3. role에서 사용할 변수 내용 구성
[root@hk-controller defaults]#cat main.yaml
---
prometheus_db_dir: /var/lib/prometheus
prometheus_version: 2.30.3
prometheus_ip: xx.xx.xx.6
prometheus_binary_install_dir: '/usr/local/bin'
4. templates 파일 구성 (systemd용과 prometheus.yml 파일)
[root@hk-controller templates]#cat prometheus.systemd.j2
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
[root@hk-controller templates]#cat prometheus.yml.j2
# Global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
scrape_timeout: 15s # scrape_timeout is set to the global default (10s).
# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['{{ prometheus_ip }}:9090']
5. task 작성
[root@hk-controller tasks]#cat main.yml
---
- name: create prometheus system group
group:
name: prometheus
system: true
state: present
- name: prometheus User add
user:
name: prometheus
system: true
shell: "/usr/sbin/nologin"
group: prometheus
createhome: false
home: "{{ prometheus_db_dir }}"
- name: make directory for prometheus
file:
path: "{{ prometheus_db_dir }}"
state: directory
owner: prometheus
group: prometheus
mode: 0755
- block:
- name: download prometheus package to localhost
become: false
get_url:
url: "https://github.com/prometheus/prometheus/releases/download/v{{ prometheus_version }}/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz"
dest: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz"
- name: unpack prometheus binaries
unarchive:
src: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz"
dest: "/tmp"
remote_src: yes
- name: copy prometheus and promtool binaries files
copy:
src: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64/{{ item }}"
dest: "{{ prometheus_binary_install_dir }}/{{ item }}"
mode: 0755
owner: root
group: root
remote_src: yes
with_items:
- prometheus
- promtool
- name: Make Directory /etc/prometheus
file:
path: /etc/prometheus
state: directory
- name: Configuration for Prometheus.yml
template:
src: prometheus.yml.j2
dest: /etc/prometheus/prometheus.yml
owner: root
group: root
mode: 0644
- name: Configuration systemd Files for Prometheus
template:
src: prometheus.systemd.j2
dest: /etc/systemd/system/prometheus.service
owner: root
group: root
mode: 0644
notify:
- restart prometheus
- name: reload for Prometheus
systemd:
name: prometheus
enabled: true
state: started
- name: Check Port 9090 for Prometheus
shell:
cmd: "/usr/bin/curl --connect-timeout 10 --silent --show-error '{{ prometheus_ip }}':9090"
warn: no
executable: /bin/bash
register: check_port
- name: Check port 9090 resule
debug:
msg:
- "{{ check_port.stdout }}"
6. handler 작성
[root@hk-controller handlers]#cat main.yml
---
- name: restart prometheus
become: true
systemd:
daemon_reload: true
name: prometheus
state: restarted
7. hosts 작성
[root@hk-controller ansible-prometheus]#pwd
/root/ansible-playbook/prometheus/ansible-prometheus
[root@hk-controller ansible-prometheus]#cat hosts
[prometheus]
xx.xx.xx.6
8. 실행방법
[root@hk-controller ansible-prometheus]#ansible-playbook main.yaml
728x90
'IaC (Automation) > Ansible-Playbook' 카테고리의 다른 글
[playbook] grafana 설치 playbook (0) | 2021.10.08 |
---|---|
[playbook] node_exporter 설치 playbook (0) | 2021.10.08 |
[Ansible] import vs include 차이점 (0) | 2021.07.29 |
[Ansible] AWS and Openstack Modules (0) | 2021.07.28 |
[Ansible] Roles 사용법 및 샘플 (0) | 2021.07.27 |