togatttiのエンジニアメモ

過度な期待はしないでください.

Elasticsearchクラスタの共有リポジトリ設定

Snapshot機能で、使用する共有リポジトリの設定方法を示す。

概要

クラスタに、共有リポジトリを設定するためには、どのノードからもアクセスできるようにファイルシステムを共有する必要がある。 今回は、NFSで、共有ファイルサーバを構築し、共有リポジトリを設定することにした。

ファイルシステムを共有しないと、下記のように怒られる。

  • RemoteTransportException...
  • This might indicate that the store [(snip)] is not shared between this node and the master node or that permissions on the store don't allow reading files written by the master node
  • RepositoryVerificationException[[snip] a file written by master to the store [snip] cannot be accessed on the node

環境

  • Ubuntu 16.04
  • Elasticsearch 5.0.0 GA
    • ノード1(192.168.0.2)
    • ノード2(192.168.0.3)

ノード1とノード2は、クラスタを組ませている。

手順

はじめに

リポジトリの置き場所をノード1、ノード2にそれぞれ生成する。

# mkdir -p /var/elasticsearch/snapshot
# chown nobody:nogroup /var/elasticsearch/snapshot

NFS周りの設定

今回は、ノード1上で、NFSサーバを起動させて、リポジトリデータを置くことにする。 そして、ノード2上では、NFSクライアントを起動させる。

ノード1から作業する。 NFSサーバをインストールする。

# apt-get update && apt-get install nfs-kernel-server

/etc/exportsに追記する。

/var/elasticsearch/snapshot  192.168.0.3(rw,sync,no_subtree_check)

NFSサーバを再起動する。

# systemctl restart nfs-kernel-server

次に、ノード2で、NFSクライアントの設定をする。 NFSクライアントをインストールする。

# apt-get update && apt-get install nfs-common

NFSサーバ側のディレクトリをマウントする。

# mount 192.168.0.2:/var/elasticsearch/snapshot /var/elasticsearch/snapshot
# df -h
Filesystem                                 Size  Used Avail Use% Mounted on
(snip)
192.168.0.2:/var/elasticsearch/snapshot   63G   15G   46G  25% /var/elasticsearch/snapshot

リポジトリ生成

ノード1、ノード2それぞれのelasticsearch.ymlに、下記を追記する。

path.repo: ["/var/elasticsearch/snapshot"]

Elasticsearchを再起動する。

# systemctl restart elasticsearch

ノード1で、リポジトリを生成する。 /var/elasticsearch/snapshotがnobody:nogroupのままだと、snapshot1の権限がelasticsearch:elasticsearchにならないので、手動で変更しています。

# mkdir /var/elasticsearch/snapshot/snapshot1
# chown elasticsearch:elasticsearch /var/elasticsearch/snapshot/snapshot1
# curl -XPUT 'http://192.168.0.2:9200/_snapshot/snapshot1?pretty' -d '{
  "type": "fs",
  "settings": {
    "location" : "/var/elasticsearch/snapshot/snapshot1",
    "compress": true
  }}'
# curl -XGET http://192.168.0.2:9200/_snapshot/snapshot1?pretty
{
  "snapshot1" : {
    "type" : "fs",
    "settings" : {
      "compress" : "true",
      "location" : "/var/elasticsearch/snapshot/snapshot1"
    }
  }
}

Elasticsearchのデータをバックアップして、別ノードにリストアする

Elasticsearchを運用していて、あるノードのデータをバックアップして、それを別のノードにリストアする方法を探した。

以前は、elasticsearch-knapsackやesclientといった方法があったらしい。

ただ、Elasticsearch自体の機能に、バックアップ、リストアの機能が備わっている。

技術の進歩って早いね!!

はじめに

  • 192.168.0.2
    • データ移動元ノード
  • 192.168.0.3
    • データ移動先ノード、インストール後の状態で用意。

共に、Ubuntu 16.04.1 LTS、 Elasticsearch 2.4.1で構築している。

データのバックアップ

バックアップは、Snapshotを使う。作業は、移動元ノードで行う。

www.elastic.co

バックアップディレクトリ作成

# mkdir -p /var/elasticsearch/backups
# chown -R elasticsearch:elasticsearch /var/elasticsearch

リポジトリ作成

# curl -XPUT -uadmin:admin 'http://192.168.0.2:9200/_snapshot/backup1' -d '{
  "type": "fs",
  "settings": {
    "location" : "/var/elasticsearch/backups/backup1",
    "compress": true
  }
}'

設定ファイル編集

# diff -u  /etc/elasticsearch/elasticsearch.yml.20161109/etc/elasticsearch/elasticsearch.yml
(snip)
+path.repo: ["/var/elasticsearch/backups"]
(snip)

Elasticsearchを再起動

# systemctl restart elasticsearch

Snapshot取得

# curl -XPUT 'http://192.168.0.2:9200/_snapshot/backup1/backup-2016.11.09?wait_for_completion=true' -d '{
"indices": "*",
"ignore_unavailable": true,
"include_global_state": false
}'
# curl -XGET -uadmin:admin 'http://192.168.0.2:9200/_snapshot/backup1/backup-2016.11.09?pretty'
{
  "snapshots" : [ {
    "snapshot" : "backup-2016.11.09",
    "version_id" : 2040199,
    "version" : "2.4.1",
    "indices" : [ (snip) ],
    "state" : "SUCCESS",
    "start_time" : "2016-11-09T01:47:12.035Z",
    "start_time_in_millis" : 1478656032035,
    "end_time" : "2016-11-09T01:52:47.587Z",
    "end_time_in_millis" : 1478656367587,
    "duration_in_millis" : 335552,
    "failures" : [ ],
    "shards" : {
      "total" : 717,
      "failed" : 0,
      "successful" : 717
    }
  } ]
}

stateが"SUCCESS"になっていることを確認する。

# ls -l /var/elasticsearch/backups/backup1/
total 32
-rw-r--r--   1 elasticsearch elasticsearch    59 11月  9 10:52 index
drwxr-xr-x 147 elasticsearch elasticsearch 12288 11月  9 10:46 indices
-rw-r--r--   1 elasticsearch elasticsearch   103 11月  9 10:45 meta-snapshot-2016.11.09.dat
-rw-r--r--   1 elasticsearch elasticsearch  2133 11月  9 10:46 snap-snapshot-2016.11.09.dat

バックアップディレクトリに、データが作られていることを確認する。

バックアップデータを移動先ノードに転送

# scp -pr /var/elasticsearch 192.168.0.3:/var/

データのリストア

以下、移動先ノードで行う。

所有権、グループ権変更

# chown -R elasticsearch:elasticsearch /var/elasticsearch

リポジトリ作成

# curl -XPUT 'http://192.168.0.3:9200/_snapshot/backup1' -d '{
  "type": "fs",
  "settings": {
    "location" : "/var/elasticsearch/backups/backup1",
    "compress": true
  }
}'

設定ファイル編集

# diff -u  /etc/elasticsearch/elasticsearch.yml.20161109 /etc/elasticsearch/elasticsearch.yml
(snip)
+path.repo: ["/var/elasticsearch/backups"]
(snip)

Elasticsearchを再起動

# systemctl restart elasticsearch

リストア

# curl -XPOST 'http://192.168.0.3:9200/_snapshot/backup1/backup-2016.11.09/_restore' -d '{
"indices": "*",
"ignore_unavailable": true,
"include_global_state": false
}'

参考

Elasticsearchのバックアップとリストア - Qiita

IPアドレスから、都道府県庁の緯度、経度を取得するスクリプト

Kibana4のグラフ描写で、緯度、経度が必要だったので取得するスクリプトを書いた。

書いたスクリプトはこれ。

IPアドレスから、緯度、経度を取得するスクリプト · GitHub

準備

$ gem install geoip
$ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
$ gzip -d GeoLiteCity.dat.gz
$ wget -O ip_to_lat_lon.rb https://gist.githubusercontent.com/kentatogashi/dcfac7362b847aba9b11998e33e9e3b3/raw/f60384745ac88b290004356f8c8eb91df534e5ef/ip_to_lat_lon.rb

使い方

IPアドレスは、はてなを使ってみる。

$ dig www.hatena.ne.jp +short
59.106.194.19
$ ruby ip_to_lat_lon.rb 59.106.194.19
59.106.194.19 -> 27:大阪府 (135.52, 34.686)

参考

https://gist.github.com/kentatogashi/dcfac7362b847aba9b11998e33e9e3b3

都道府県庁の経度緯度

koshigoeb.hateblo.jp

GitLab8.6.1にアップグレードで出たエラーの対処

運用しているGitLab (8.0.2)のバージョンが古かったので、 8.6.1にアップグレードしたところ、エラーが出たのでメモを残す。

環境

  • OS
  • GitLab
    • アップデート前 8.0.2
    • アップデート後 8.6.1

インストールは、

gitlab/gitlab-ce - Packages - packages.gitlab.com | packagecloud

から、Communication Editionのdebパッケージをダウンロードして、dpkg コマンドを実行していた。

# cd /tmp
# wget -O gitlab-ce_8.0.2-ce.0_amd64.deb https://packages.gitlab.com/gitlab/gitlab-ce/packages/ubuntu/trusty/gitlab-ce_8.0.2-ce.0_amd64.deb/download
# dpkg -i gitlab-ce_8.0.2-ce.0_amd64.deb

GitLab8.6.1にアップグレードで、500エラー

production.logを見ると、pg_trgm が足りないよと怒られていた。

GitLab ver 8.6.* からは、pg_trgmというpostgresqlの拡張モジュールの設定が必要なので、

入れてあげればよい。

$ sudo su gitlab-psql
$ /opt/gitlab/embedded/bin/psql --port 5432 -h /var/opt/gitlab/postgresql -d gitlabhq_production
psql (9.2.10)
Type "help" for help.
gitlabhq_production=# create extension pg_trgm with schema pg_catalog;
CREATE EXTENSION
gitlabhq_production=# \q

念のため、GitLabの設定読み込みと再起動

$ sudo gitlab-ctl reconfigure
$ sudo gitlab-ctl restart

sedで、範囲指定してから、文字列置換を使う

sedで、範囲指定してから、文字列置換する方法を備忘録として残しておく。

例えば、以下のようなファイルがあるとする。

$ cat sample_http.conf
<VirtualHost example.com:80>
  User www
  Group www
  Port 80
  SSLDisable
  ServerAdmin admin@example.com
  ServerName example.com
</VirtualHost>

<VirtualHost example.com:443>
  User www
  Group www
  Port 443
  SSLEnable
  ServerAdmin admin@example.com
  ServerName example.com
</VirtualHost>

そして、ServerAdminをそれぞれ、

に変更したい。

これをsedで、行うためには、こうする。

$ sed -i.bak -e '/<VirtualHost .*:80>/,/<\/VirtualHost>$/s|admin|http-admin|' sample_httpd.conf
$ sed -i.bak -e '/<VirtualHost .*:443>/,/<\/VirtualHost>$/s|admin|http-admin|' sample_httpd.conf

実行すると思い通りの挙動になる。

<VirtualHost example.com:80>
  User www
  Group www
  Port 80
  SSLDisable
  ServerAdmin http-admin@example.com
  ServerName example.com
</VirtualHost>

<VirtualHost example.com:443>
  User www
  Group www
  Port 443
  SSLEnable
  ServerAdmin https-admin@example.com
  ServerName example.com
</VirtualHost>

公式風に書くと、

sed -e '/範囲指定開始文字列/,/範囲指定終了文字列/s|置換前文字列|置換後文字列|' ファイル

実際に実行したコマンドを簡単に見てみると、

$ sed -i.bak -e '/<VirtualHost .*:80>/,/<\/VirtualHost>$/s|admin|http-admin|' sample_httpd.conf

について、/<VirtualHost .*:80>/,/<\/VirtualHost>$/ で、

作業の範囲が、80番のVirtualHostディレクティブ内の内容であることを明示している。

続けて、置換の構文を s|admin|http-admin| 指定している。