DBMS/MySQL

[MySQL] Grafana + influxDB + telegraf 모니터링 구성

RYEAN 2023. 11. 7. 10:08
반응형

 


 

 

Grafana + influxDB + telegraf 를 구성하여 MySQL 을 모니터링해보려고 한다.

각각의 역할은 아래와 같다.

 

ν Grafana 란?

Grafana는 데이터 시각화 및 대시보드 작성을 위한 오픈소스 도구이다. Prometheus, InfluxDB, Azure Monitor, CloudWatch 등 다양한 데이터 소스를 지원하며, 데이터를 가져와 원하는 시각적인 대시보드를 작성할 수 있다.

 

ν InfluxDB 란?

InfluxDB는 시계열 데이터를 저장하고 검색하기 위한 오픈소스 데이터베이스이다. InfluxDB는 대규모 데이터를 처리하고 저장할 수 있으며, SQL과 유사한 InfluxQL 쿼리 언어를 사용한다. InfluxDB는 Telegraf와 같은 데이터 수집 도구와 함께 사용되어 데이터를 수집하고 저장한다.

 

ν Telegraf 란?

Telegraf는 InfluxDB와 같은 데이터베이스에 데이터를 저장(수집)하는 오픈소스 데이터 수집 도구이다. CPU, 메모리, 디스크, 네트워크 등 다양한 시스템 리소스의 사용량을 수집할 수 있다.

 

전체적인 모니터링의 아키텍쳐는 아래와 같다.

 

모니터링용 데이터를 수집하는 Telegraf 가 모니터링할 VM 혹은 DB 에서 관련 데이터를 수집InfluxDB 에 전송(저장)하게 된다.

해당 데이터를 InfluxDB 에 저장한 후, Grafana 가 이 수집된 InfluxDB 에 연결하여 데이터를 조회할 수 있다.

이 데이터들을 통해 원하는 시각적인 대시보드를 구성할 수 있다.

 

 

 

 

1. Grafana 설치

# Grafana 를 설치하기 위한 필요한 utils 설치

sudo yum install -y yum-utils

 

 

# Grafana 저장소 추가

sudo bash -c 'cat << EOF > /etc/yum.repos.d/grafana.repo
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF'

 

 

# Grafana 설치

sudo yum install grafana -y

 

 

# Grafana 서비스 시작 및 자동 실행 설정

# systemd 데몬 재로드 (실행하지 않으면 이전 구성이 계속 사용됨)
sudo systemctl daemon-reload

# grafana 서비스 시작
sudo systemctl start grafana-server

# grafana 서비스 부팅 시 자동 실행 설정
sudo systemctl enable grafana-server

 

 

2. Reverse Proxy 구성

# Reverse proxy 란?

Reverse Proxy 는 클라이언트와 서버 사이에 위치하여 클라이언트로부터 요청을 받아 웹서버에 전달하는 서버이다.

클라이언트와 서버간의 연결을 중개하므로서, 외부에서 직접 서버에 접근하는 것을 막아 클라이언트로부터 서버를 보호하는 역할을 한다.

대표적인 Reverse Proxy 로는 Nginx, Apache 등이 있다. 또, 로드 밸런싱을 하고 캐싱을 하여 서버 부하를 줄이는 역할도 한다.

보통 Reverse Proxy 는 대규모 웹 사이트에서 사용되며 보안, 성능, 가용성 등을 향상시키는 데 도움이 된다.

 

 

# EPEL(Extra Packages for Enterprise Linux - 엔터프라이즈 리눅스용 추가 패키지) 설치

sudo yum install epel-release -y

 

 

# Nginx 설치

sudo yum install nginx -y

 

 

# Nginx 서버 실행 및 자동 실행 설정

# Nginx 서버 시작
sudo systemctl start nginx

# Nginx 서버 부팅 시 자동 실행 설정
sudo systemctl enable nginx

 

 

 

# Nginx 설정 파일 생성

그라파나를 외부에서 접근 가능한 웹 서비스로 제공하기 위한 설정 파일이며, 파일 내용은 아래와 같다.

 

(1) http(80) 포트로 접속하는 경우, https(443) 포트로 강제 리다이렉트로 시켜준다.

(2) https(443) 포트로 접속하는 경우, 그라파나의 Proxy 로 설정한다.

(3) SSL추가 및 위치를 설정하여 웹에서 접근 가능하도록 구성한다.

sudo bash -c 'cat << EOF > /etc/nginx/conf.d/grafana.conf
# HTTP(80) 포트로 접속하는 경우, HTTPS(443) 포트로 강제 리다이렉트
server {
    listen 80;
    server_name grafana.your_domain.com; # 도메인 이름 입력
    
    location / {
        return 301 https://$host$request_uri;
    }
}

# HTTPS(443) 포트로 접속하는 경우, Grafana가 실행 중인 주소로 Proxy Pass 설정
server {
    listen 443 ssl;
    server_name grafana.your_domain.com; # 도메인 이름 입력


    # SSL 설정 추가 (SSL 인증서 파일 경로를 기입)
    ssl_certificate /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;


    location / {
        proxy_pass http://localhost:3000; # Grafana가 실행 중인 주소
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
EOF'

 

 

# Nginx 재시작

sudo systemctl restart nginx

 

 

 

3. InfluxDB 설치

 

# InfluxDB 리포지토리(저장소) 추가

cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo
[influxdb]
name = InfluxDB Repository - RHEL \$releasever
baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdata-archive_compat.key
EOF

 

 

# influxDB 설치

sudo yum install -y influxdb

 

 

# influxDB 서비스 시작 및 자동 실행 설정

# influxdb 서비스 시작
sudo systemctl start influxdb

# influxdb 서비스 부팅 시 자동 실행 설정
sudo systemctl enable influxdb

 

 

 

4. Telegraf 설치

 

# 모니터링 대상 MySQL 서버에 Telegraf 수집용 계정 생성 및 권한 부여

  *계정이 있다면 생성하지 않아도 된다.

 

  [필요 권한]

     ㆍ PROCESS - 실행 중인 프로세스 정보 조회
     ㆍ REPLICATION CLIENT - 서버 복제 상태를 조회
     ㆍ SELECT - 데이터 조회

# telegraf 수집용 계정 생성
CREATE USER 'telegraf'@'%' IDENTIFIED BY 'password';

# 모니터링에 필요한 권한 부여
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'telegraf'@'%';

# 권한 적용
FLUSH PRIVILEGES;

 

 

 

# Telegraf 패키지 설치

sudo yum install -y telegraf

 

 

# Telegraf 설정 파일 생성

InfluxDB 와 Telegraf 를 사용하여 시스템 모니터링을 수행하기 위한 설정 파일이며, InfluxDB 에 전송할 데이터베이스 이름, 계정 정보, 데이터 수집 주기 등을 설정한다. 또, CPU, Disk, Memory, Network 등 시스템 정보를 수집하는 input plugin 도 설정할 수 있다.

 

설정은 agent, output plugin, input plugin 이 있다.

 

 ㆍ [agent] : 데이터 수집 및 전송 주기를 설정한다.

 ㆍ [outputs] : 데이터를 수집할 대상을 설정하는 것으로, 설치한 influxdb 정보를 입력한다.

 ㆍ [inputs] : 수집할 데이터 대상을 설정하는 것으로, 모니터링 대상의 mysql 서버 정보를 입력하며 원하는 모니터링 항목을 설정할 수 있다.

 

sudo bash -c 'cat <<EOF > /etc/telegraf/telegraf.conf
[global_tags]
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "15s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  logfile = "/var/log/telegraf/telegraf.log"
  hostname = ""
  omit_hostname = false

# 수집할 influxdb 대상 정보
[[outputs.influxdb]]
  urls = ["http://localhost:8086"]
  database = "telegraf"
  retention_policy = ""
  write_consistency = "any"
  timeout = "5s"
  username = "dbadmin"
  password = "password"

# 모니터링 대상 mysql 서버 정보 
[[inputs.mysql]]
 servers = ["username:password@tcp(127.0.0.1:3306)/?tls=false"]
 gather_process_list = true
 gather_slave_status = true

[[inputs.cpu]]
  percpu = true
  totalcpu = true
  fielddrop = ["time_*"]

[[inputs.disk]]
  ignore_fs = ["tmpfs", "devtmpfs"]

[[inputs.diskio]]
[[inputs.kernel]]
[[inputs.mem]]
[[inputs.processes]]
[[inputs.swap]]
[[inputs.system]]
[[inputs.net]]
[[inputs.netstat]]
[[inputs.interrupts]]
[[inputs.linux_sysctl_fs]]
EOF'

 

 

다수의 MySQL 서버를 모니터링할 경우, [[inputs.mysql]] 부분만 추가해주면 된다.

[[inputs.mysql]]
 servers = ["username:password@tcp(127.0.0.1:3306)/?tls=false"]
 gather_process_list = true
 gather_slave_status = true
 
[[inputs.mysql]]
 servers = ["username:password@tcp(127.0.0.2:3306)/?tls=false"]
 gather_process_list = true
 gather_slave_status = true
 
 [[inputs.mysql]]
 servers = ["username:password@tcp(127.0.0.3:3306)/?tls=false"]
 gather_process_list = true
 gather_slave_status = true

 

 

이 외에도 아래와 같은 다양한  input plugin 를 추가하여 설정할 수 있다.

## if the list is empty, then metrics are gathered from all databasee tables
# table_schema_databases = []

## gather metrics from INFORMATION_SCHEMA.TABLES for databases provided above list
# gather_table_schema = false

## gather thread state counts from INFORMATION_SCHEMA.PROCESSLIST
# gather_process_list = false

## gather user statistics from INFORMATION_SCHEMA.USER_STATISTICS
# gather_user_statistics = false

## gather auto_increment columns and max values from information schema
# gather_info_schema_auto_inc = false

## gather metrics from INFORMATION_SCHEMA.INNODB_METRICS
# gather_innodb_metrics = false

## gather metrics from SHOW SLAVE STATUS command output
# gather_slave_status = false

## gather metrics from SHOW BINARY LOGS command output
# gather_binary_logs = false

## gather metrics from SHOW GLOBAL VARIABLES command output
# gather_global_variables = true

## gather metrics from PERFORMANCE_SCHEMA.TABLE_IO_WAITS_SUMMARY_BY_TABLE
# gather_table_io_waits = false

## gather metrics from PERFORMANCE_SCHEMA.TABLE_LOCK_WAITS
# gather_table_lock_waits = false

## gather metrics from PERFORMANCE_SCHEMA.TABLE_IO_WAITS_SUMMARY_BY_INDEX_USAGE
# gather_index_io_waits = false

## gather metrics from PERFORMANCE_SCHEMA.EVENT_WAITS
# gather_event_waits = false

## gather metrics from PERFORMANCE_SCHEMA.FILE_SUMMARY_BY_EVENT_NAME
# gather_file_events_stats = false

## gather metrics from PERFORMANCE_SCHEMA.EVENTS_STATEMENTS_SUMMARY_BY_DIGEST
# gather_perf_events_statements = false

## the limits for metrics form perf_events_statements
# perf_events_statements_digest_text_limit = 120
# perf_events_statements_limit = 250
# perf_events_statements_time_limit = 86400

## Some queries we may want to run less often (such as SHOW GLOBAL VARIABLES)
##   example: interval_slow = "30m"
# interval_slow = ""

 

 

# Telegraf 서비스 시작 및 자동 실행 설정

# telegraf 서비스 시작
sudo systemctl start telegraf

# telegraf 서비스 부팅 시 자동 실행 설정
sudo systemctl enable telegraf

 

 

5. Grafana 에 InfluxDB 연결

 

# Grafana 의 Data sources 에서 구성한 InfluxDB 추가

 ㆍ Name : telegraf_test 

 ㆍ Query Language : InfluxQL

 ㆍ URL : http://your_domain:8086 (모니터링 서버에 그라파나를 설치하였다면, localhost 를 넣어주면 된다.)

 

 

# InfluxDB Details 에 해당 InfluxDB 의 DB 및 유저정보 입력

 ㆍ Database : telegraf (InfluxDB 에서 수집되고 있는 DB 정보 입력)

 ㆍ User : db_user (InfluxDB 에서 해당 DB 를 접근할 수 있는 계정 입력)

 

 

이로써, 그라파나에 InfluxDB 를 연결하여 구성이 끝났다.

이를 통해 원하는 지표를 그라파나 대시보드에 구성하면 된다!

 

 

 

반응형