1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dkronとは何か

概要

Dkronは、分散ジョブスケジューリングシステムであり、クラスタ全体でスケジュールされたジョブを分散して実行することができます
これは耐障害性とスケーラビリティを提供し、クラウドネイティブな環境や大規模な分散システムで適しています
また、スケジュールされたジョブであるcronをGUIで管理することができます

image.png
公式ページから引用)

Dkronのインストール方法

バイナリの実行

  1. ダウンロード
    • プラットフォームに適したパッケージ化されたアーカイブをダウンロードページからダウンロードし、/opt/local/binなどの共有場所にパッケージを展開します
  2. Dkronの実行
    dkron agent --server --bootstrap-expect=1
    
  3. UIへのアクセス

Dockerでの実行

Dkronは公式のDockerイメージを提供しており、Docker Hubからダウンロードして任意のシステムでデプロイできます

新しいコンテナとしてのDkronの起動

  1. クイックスタート
    docker run -d -p 8080:8080 --name dkron dkron/dkron agent --server --bootstrap-expect=1 --node-name=node1
    
  2. ログの確認
    docker logs -f dkron
    
  3. アクセス
    • Dkronの起動が完了したら、http://localhost:8080 でアプリケーションにアクセスできます

起動後は以下のような画面が出てきます
image.png

基本的な使い方とコマンド例

Dkronの設定ファイル

Dkronの設定はYAMLファイルで行います
以下はサンプルの設定ファイルです

backend: "etcd"
backend_machine:
  - "127.0.0.1:2379"
log_level: "debug"
server: true

ジョブの作成と管理

ジョブの作成

公式のサンプルジョブを作成します
GUIから作成するよりcurlを使ってHTTPからAPI叩いた方が楽でした

curl localhost:8080/v1/jobs -XPOST -d '{
  "name": "job1",
  "schedule": "@every 10s",
  "timezone": "Europe/Berlin",
  "owner": "Platform Team",
  "owner_email": "platform@example.com",
  "disabled": false,
  "tags": {
    "server": "true:1"
  },
  "metadata": {
    "user": "12345"
  },
  "concurrency": "allow",
  "executor": "shell",
  "executor_config": {
    "command": "date"
  }
}'

ジョブの一覧表示

こちらで作成したジョブを確認できます

image.png

チェックボックスをクリックすると実行や削除などのアイコンが表示されます

image.png

具体的なユースケースと実例

データベースのバックアップ

企業ではデータベースの定期的なバックアップが重要です
以下は毎日深夜にバックアップを実行するジョブの例です

curl localhost:8080/v1/jobs -XPOST -d '{
  "name": "db-backup",
  "schedule": "@daily",
  "timezone": "Asia/Tokyo",
  "owner": "YourName",
  "owner_email": "youremail@example.com",
  "disabled": false,
  "tags": {
    "server": "true:1"
  },
  "metadata": {
    "user": "12345"
  },
  "concurrency": "allow",
  "executor": "shell",
  "executor_config": {
    "command": "pg_dump -U user -h db_host mydatabase > /backup/db_backup.sql"
  }
}'

ログローテーション

ログファイルが大きくなるのを防ぐために、ログの回転を定期的に実行します
以下は毎週ログローテーションをするジョブの例です

curl localhost:8080/v1/jobs -XPOST -d '{
  "name": "logrotate",
  "schedule": "@weekly",
  "timezone": "Asia/Tokyo",
  "owner": "YourName",
  "owner_email": "youremail@example.com",
  "disabled": false,
  "tags": {
    "server": "true:1"
  },
  "metadata": {
    "user": "12345"
  },
  "concurrency": "allow",
  "executor": "shell",
  "executor_config": {
    "command": "logrotate /etc/logrotate.conf"
  }
}'

まとめ

cronも良いですがCLI上での操作になるので理解が遅れることがありますよね
DkronならGUI上での操作なので見た目が良いしわかりやすいです
上記のようにコマンドでも実行できるのでそこはお好みですが、一覧での確認やステータスの確認ならGUIの方が理解しやすいと思うのでDkronの使用がおすすめです

参考リンク

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?