LoginSignup
1
1

More than 3 years have passed since last update.

AmazonLinux2にインストールしたGraphiteの設定を更新する

Posted at

はじめに

この記事は前回投稿した以下の記事の続きになります。
AmazonLinux2にDockerを使わずGraphite+Grafanaを構築する
前回はすべてデフォルト設定でインストールしたので、もう少し設定を見直してみるという趣旨の記事です。

設定概要

  1. Graphite DB を SQLite から PostgreSQL に変更
  2. local_settings.py(Graphiteの構成) を変更
  3. carbon保持期間を変更
  4. carbonをサービス化

環境

項目
OS AmazonLinux2
インスタンスタイプ t3.micro
Python 3.7.9 (デフォルトでインストール済み)
PostgreSQL 11.5
Graphite 1.1.8

Graphite設定

WebアプリDB変更

デフォルトのSQLiteからPostgreSQLに変更します。

PostgreSQL11のインストール

# インストール
amazon-linux-extras enable postgresql11
yum install postgresql-server postgresql-devel postgresql-contrib

# DBセットアップ
postgresql-setup initdb

# 起動
systemctl start postgresql.service
systemctl enable postgresql.service

# ユーザパスワード設定
sudo passwd postgres

# pythonからPostgreSQLに接続するためのライブラリをインストール
pip3 install psycopg2

PostgreSQLでパスワード認証を許可させます。
IPv4 local connections を ident から md5 に変更します。

/var/lib/pgsql/data/pg_hba.conf
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 ident

変更後、postgresqlを再起動します。

systemctl restart postgresql.service

次に、Graphiteが使用するためのデータベースを作成します。

# PostgreSQLにログイン
sudo -u postgres psql

# graphiteユーザを作成
postgres=# CREATE USER graphite WITH PASSWORD 'password';

# graphiteデータベースを作成
postgres=# CREATE DATABASE graphite WITH OWNER graphite;

Graphiteの構成を変更して、PostgreSQLを参照するようにします。
以下のファイルを新規作成し設定します。

/opt/graphite/webapp/graphite/local_settings.py
DATABASES = {
    'default': {
        'NAME': 'graphite',
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'USER': 'graphite',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '5432'
    }
}

データベースをセットアップします。

PYTHONPATH=/opt/graphite/webapp django-admin.py migrate --settings=graphite.settings --run-syncdb

# apacheも再起動
systemctl restart httpd.service

その他Webアプリの構成

最終的に、今回構成した設定ファイルは以下のようになりました。

/opt/graphite/webapp/graphite/local_settings.py
SECRET_KEY = 'MY_SECRET_KEY'
TIME_ZONE = 'Asia/Tokyo'

DASHBOARD_REQUIRE_AUTHENTICATION = True
DASHBOARD_REQUIRE_PERMISSIONS = True

DATABASES = {
    'default': {
        'NAME': 'graphite',
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'USER': 'graphite',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '5432'
    }
}

設定を反映させます。

PYTHONPATH=/opt/graphite/webapp django-admin.py migrate --settings=graphite.settings --run-syncdb

# apacheも再起動
systemctl restart httpd.service

Carbonの構成

メトリクスの保存期間を変更してみます。

  • 60秒のデータ - 30日
  • 5分のデータ - 90日
  • 1時間のデータ - 3年
/opt/graphite/conf/storage-schemas.conf
[default]
pattern = .*
retentions = 60s:30d,5m:90d,1h:3y

Carbonのサービス化

systemdを使用して、Carbonをサービス化します。
標準で用意されていないので、色々調べて作成しました。
以下のserviceファイルを新規作成します。

/etc/systemd/system/carbon-cache.service
[Unit]
Description=Graphite Carbon Cache
After=network.target

[Service]
Type=forking
StandardOutput=syslog
StandardError=syslog
ExecStart=/opt/graphite/bin/carbon-cache.py --config=/opt/graphite/conf/carbon.conf --pidfile=/var/run/carbon-cache.pid start
ExecReload=/bin/kill -USR1 $MAINPID
PIDFile=/var/run/carbon-cache.pid

[Install]
WantedBy=multi-user.target

起動してみてエラーにならないことを確認します。

systemctl enable carbon-cache.service
systemctl start carbon-cache.service

おわりに

まだまだ設定は不足していそうですが、
最低限ここまで設定しておけば、Graphiteを使用できそうです。

参考URL

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1