GitHub EnterpriseをAWSで使おう – レプリケーションを利用したHA構成

2016.10.31

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

こんにちは、中山です。

GitHub Enterprise(以下GHE) on AWSシリーズの第4弾です。今回はレプリケーションを利用したHA構成をご紹介します。

GHEにおけるHA構成

GHEではプライマリ/セカンダリが1台ずつのシンプルなHA構成が構築可能です。プライマリとセカンダリはレプリケーションを通じて各種データストアをほぼリアルタイムで同期することができます。設定するための各種スクリプトはGHEに同梱されており、利用するだけであればかなり簡単に使うことができます。また、セットアップ用のドキュメントも充実しているので、上記ドキュメントを参照すれば難なく導入できると思います。今回はこのドキュメントをもとに、レプリケーションのセットアップ方法をご紹介します。

ただし、注意点としてHA構成はバックアップ目的で利用するものではないという点は認識しておく必要があります。レプリケーションはほぼ即時でセカンダリに反映されるので、破損したデータも反映されてしまいます。そうするとセカンダリ側もバックアップ用途としては利用できなくなってしまいます。万が一のためにgithub/backup-utilsなどを利用して、定期的にバックアップを取得するようにしましょう。

今回の構成

今回エントリを書くに当たり、前提とする構成は以下のとおりです。

ha

ご覧のようにVPC内にサブネットを2つ切り、その中にGHEを1台ずつ設置しています。もし実際に試してみたい場合は、こちらのエントリを参照していただくと良いかと思います。追加でサブネットとGHEインスタンスを1台構築すれば同じような構成が構築可能です。

今回はプライマリ/セカンダリとなるGHEインスタンスに以下のドメインを割り当てているものとします。設定方法についてはこちらのエントリを参照してください。

ドメイン レコードタイプ バリュー
git.<_YOUR_DOMAIN_> A プライマリのパブリックIP
*.git.<_YOUR_DOMAIN_> A プライマリのパブリックIP
git-standby.<_YOUR_DOMAIN_> A セカンダリのパブリックIP

また、プライマリの初期構築はすでに完了しているものとします。正常にセカンダリへレプリケーションが実施されているのか分かりやすくするために、適当なリポジトリを1つ作成しておいてください。

ghe4-1

セカンダリ側はこの時点ではインスタンスの起動のみでOKです。

使ってみる

説明が長くなりました。早速使ってみましょう。

1. セキュリティグループの修正

後述しますが、 ghe-repl-setup コマンドを利用したレプリケーションでは1194/udpでVPNが、22/tcpでSSHが実施されます。そのため、まだこのトラフィックを許可していない場合は、プライマリ/セカンダリでセキュリティグループを修正してポートを開放してください。

2. レプリケーションのセットアップ

セカンダリにsshログインしてレプリケーションの設定を行います。

$ ssh -i path/to/key admin@git-standby.<_YOUR_DOMAIN_> -p 122
       _ __  __        __              __                   _
 ___ _(_) /_/ /  __ __/ /    ___ ___  / /____ _______  ____(_)__ ___
/ _ `/ / __/ _ \/ // / _ \  / -_) _ \/ __/ -_) __/ _ \/ __/ (_-</ -_)
\_, /_/\__/_//_/\_,_/_.__/  \__/_//_/\__/\__/_/ / .__/_/ /_/___/\__/
/___/                                           /_/

Administrative shell access is permitted for troubleshooting and performing
documented operations procedures only. Modifying system and application files,
running programs, or installing unsupported software packages may void your
support contract. Please contact GitHub Enterprise technical support at
enterprise@github.com if you have a question about the activities allowed by
your support contract.
Last login: Fri Oct 28 09:12:00 2016 from ***.***.***.***
admin@ip-10-0-101-159:~$

まず初めに ghe-repl-setup コマンドでセカンダリをレプリケーションのスタンバイに設定します。渡す引数はプライマリのホスト名かIPアドレスです。今回の構成であればVPC内の通信にしたいので、このコマンドに渡す引数はプライマリのプライベートIPアドレスになります。

admin@ip-10-0-101-159:~$ ghe-repl-setup 10.0.100.127
Generating rsa key pair for communication with primary GitHub instance.
The primary GitHub Enterprise instance must be configured to allow replica access.
Visit http://10.0.100.127/setup/settings and authorize the following SSH key:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCnWwrXaSf6pNKGekw/X/xegVAnEZthmrRSEicLjG+zWcBR9IEG7rEWaTai51il+oIE9zKaq1i8txBEPeOufr7WBsuD6ClEZkTP3Df7CQnPBOu3OitmKHpPLlcb1cs80h3KFO7JL9MWAw09ffOAa6SvvYvnxYJt2i+o0umOumswxh3Q+lw8egVc8RBr4/MOIC4tGzFTYnjAmqegeKcTlGQgMsg0R4wJwIpyloGgigBELObS7HWUdlaq0rR44fKEC577OLBrkiwgG03leh4hH7ZvbzZ/hrQWOmBcx+IGigDJf4O6vjLlC6JWEgOvXbBRh89pSxVyXwRGYpjfphgULb/1 ha-replica-ip-10-0-101-159

Run `ghe-repl-setup 10.0.100.127' once the key has been added to continue replica setup.

この時点ではssh鍵のセットアップが完了していないので、標準出力に表示された公開鍵をプライマリに登録する必要があります。 https://git.<_YOUR_DOMAIN_>/setup にアクセスして公開鍵を登録してください。

ghe4-2

公開鍵の登録後、もう一度先程のコマンドを実行すると以下のようにレプリケーションのセットアップが完了した旨、表示されます。

admin@ip-10-0-101-159:~$ ghe-repl-setup 10.0.100.127
Verifying ssh connectivity with 10.0.100.127 ...
Connection check succeeded.
Copying license and settings from primary instance ...
 --> Importing SSH host keys...
 --> The SSH host keys on this instance have been replaced to match the primary.
 --> Please run `ssh-keygen -R 10.0.101.159; ssh-keygen -R "[10.0.101.159]:122"' on your client to prevent future ssh warnings.
unknown
Configuring database replication against primary ...
Configuring OpenVPN tunnel between instances ...
Setting up SSH key for Git replication ...
0+1 records in
0+1 records out
399 bytes (399 B) copied, 0.0113837 s, 35.0 kB/s
0+1 records in
0+1 records out
89 bytes (89 B) copied, 0.0110866 s, 8.0 kB/s
0+1 records in
0+1 records out
89 bytes (89 B) copied, 0.0111116 s, 8.0 kB/s
Verifying SSH access between primary and replica ...
Configuring Git replication ...
0+1 records in
0+1 records out
2 bytes (2 B) copied, 6.2451e-05 s, 32.0 kB/s
Success: Replica mode is configured against 10.0.100.127.
To disable replica mode and undo these changes, run `ghe-repl-teardown'.
Run `ghe-repl-start' to start replicating from the newly configured primary.

この時点ではまだレプリケーションはスタートしていません。セカンダリ側のセットアップが完了しただけです。この段階でセカンダリにアクセスすると以下のようにレプリケーションモードになっている旨、表示されます。

ghe4-3

実際にレプリケーションをスタートするには ghe-repl-start コマンドを実行します。

admin@ip-10-0-101-159:~$ ghe-repl-start
Verifying ssh connectivity with 10.0.100.127 ...
Starting OpenVPN tunnel ...
Starting MySQL replication ...
Starting Redis replication ...
Starting Elasticsearch replication ...
Starting Pages replication ...
Starting Alambic replication ...
Starting git-hooks replication ...
Starting Git replication ...
Success: replication is running for all services.
Run `ghe-repl-status' to monitor replication health and progress.

上記のように表示されたら正常にレプリケーションが実行されたと確認できます。レプリケーションの状態を確認したい場合は ghe-repl-status コマンドを実行します。 -v オプションを追加するとより詳細な情報が出力されます。2つまで( -vv まで)オプションが用意されているようです。

admin@ip-10-0-101-159:~$ ghe-repl-status
OK: mysql replication in sync
OK: redis replication is in sync
OK: elasticsearch cluster is in sync
OK: git replica is in sync at position 0.
OK: pages data is in sync
OK: alambic data is in sync
OK: git-hooks data is in sync

上記のように全てOKと表示されたらレプリケーションが正常に動作していると確認できます。

3. セカンダリの昇格

レプリケーションはこれだけで完了です。簡単ですね。今回はもう少し進めて、セカンダリをプライマリにする方法もご紹介します。参照するドキュメントはこちらです。

まず、はじめにプライマリをメンテナンスモードに設定します。GHEをメンテナンスモードに遷移させるには以下の3つの方法があります。

  • マネジメントコンソール
  • CLI
  • API

今回はCLIを利用した方法をご紹介します。以下のようにプライマリにsshでログインしてください。

$ ssh -i path/to/key admin@git.<_YOUR_DOMAIN_> -p 122
       _ __  __        __              __                   _
 ___ _(_) /_/ /  __ __/ /    ___ ___  / /____ _______  ____(_)__ ___
/ _ `/ / __/ _ \/ // / _ \  / -_) _ \/ __/ -_) __/ _ \/ __/ (_-</ -_)
\_, /_/\__/_//_/\_,_/_.__/  \__/_//_/\__/\__/_/ / .__/_/ /_/___/\__/
/___/                                           /_/

Administrative shell access is permitted for troubleshooting and performing
documented operations procedures only. Modifying system and application files,
running programs, or installing unsupported software packages may void your
support contract. Please contact GitHub Enterprise technical support at
enterprise@github.com if you have a question about the activities allowed by
your support contract.
Last login: Fri Oct 28 09:12:48 2016 from ***.***.***.***
admin@git-**************************:~$

CLIからGHEをメンテナンスモードに設定するには ghe-maintenance コマンドを実行します。 -s/--set でメンテナンスモードに、 -u/--unset で通常のモードに、 -q/--query で現在の状態を確認できます。

admin@git-**************************:~$ ghe-maintenance -s
admin@git-**************************:~$ ghe-maintenance -q
maintenance mode set

メンテナンスモード時にGHEへアクセスすると以下のような画面が表示されます。

ghe4-4

注意点として、 ghe-maintenance コマンドは即時でメンテナンスモードに設定してしまいます。数時間後など時間に余裕をもたせたい場合はマネジメントコンソールかAPIを利用してください。

続いて、セカンダリをプライマリに昇格させます。昇格には ghe-repl-promote コマンドをセカンダリで実行します。実行前に ghe-repl-status コマンドですべての項目がOKと表示されることを確認してください。

admin@ip-10-0-101-159:~$ ghe-repl-promote
Enabling maintenance mode on the primary to prevent writes ...
Stopping replication ...
  | Stopping Pages replication ...
  | Stopping Git replication ...
  | Stopping Alambic replication ...
  | Stopping git-hooks replication ...
  | Stopping MySQL replication ...
  | Stopping Redis replication ...
  | Stopping Elasticsearch replication ...
  | Stopping OpenVPN tunnel ...
  | Success: Replication was stopped for all services.
  | To disable replica mode and remove all replica configuration, run `ghe-repl-teardown'.
Switching out of replica mode ...
  | Success: Replication configuration has been removed.
  | Run `ghe-repl-setup' to re-enable replica mode.
Applying configuration and starting services ...
Success: Replica has been promoted to primary and is now accepting requests.

上記のように表示されたらセカンダリの昇格が完了したと確認できます。今回はRoute53を利用しているのでドメインの書き換えが必要です。プライマリ用ドメイン/セカンダリ用ドメインのレコードをそれぞれ逆に設定してください。

TTLが切れた後、 https://git.<_YOUR_DOMAIN_> にアクセスしてリポジトリを確認すると、元プライマリで作成したリポジトリが存在していること、つまりレプリケーションが正常に実行できていたことを改めて確認できます。以下の画像はレコードセットの書き換え後にアクセスしたものです。

ghe4-5

4. 元プライマリの昇格

せっかくなので元プライマリ(現セカンダリ)を再度プライマリに戻してみましょう。切り替え後分かりやすいようにテスト用のリポジトリを作成しておいてください。

ghe4-6

とは言え、内容は先程説明したものとほぼ同じなので簡易的に説明します。

  • 1. 元プライマリでレプリケーションの設定

元プライマリには各種データが残っているので途中で処理を進めていいか聞かれます。問題なければ y を入力します。

admin@git-**************************:~$ ghe-repl-setup 10.0.101.159
Warning: This instance is or has been a configured primary instance.
Proceding will overwrite data on this instance.

Proceed with initializing this instance as a replica? [y/N] y
Verifying ssh connectivity with 10.0.101.159 ...
Connection check succeeded.
Copying license and settings from primary instance ...
 --> Importing SSH host keys...
 --> The SSH host keys on this instance have been replaced to match the primary.
 --> Please run `ssh-keygen -R 10.0.100.127; ssh-keygen -R "[10.0.100.127]:122"' on your client to prevent future ssh warnings.
unknown
Configuring database replication against primary ...
Configuring OpenVPN tunnel between instances ...
Setting up SSH key for Git replication ...
0+1 records in
0+1 records out
399 bytes (399 B) copied, 0.0123235 s, 32.4 kB/s
0+1 records in
0+1 records out
89 bytes (89 B) copied, 0.0116537 s, 7.6 kB/s
0+1 records in
0+1 records out
89 bytes (89 B) copied, 0.0128773 s, 6.9 kB/s
Verifying SSH access between primary and replica ...
Configuring Git replication ...
0+1 records in
0+1 records out
2 bytes (2 B) copied, 6.6237e-05 s, 30.2 kB/s
Success: Replica mode is configured against 10.0.101.159.
To disable replica mode and undo these changes, run `ghe-repl-teardown'.
Run `ghe-repl-start' to start replicating from the newly configured primary.
  • 2. レプリケーションのスタート
admin@git-**************************:~$ ghe-repl-start
Verifying ssh connectivity with 10.0.101.159 ...
Starting OpenVPN tunnel ...
Starting MySQL replication ...
Starting Redis replication ...
Starting Elasticsearch replication ...
Starting Pages replication ...
Starting Alambic replication ...
Starting git-hooks replication ...
Starting Git replication ...
Success: replication is running for all services.
Run `ghe-repl-status' to monitor replication health and progress.
  • 3. プライマリへ昇格
admin@git-**************************:~$ ghe-repl-promote
Enabling maintenance mode on the primary to prevent writes ...
Stopping replication ...
  | Stopping Pages replication ...
  | Stopping Git replication ...
  | Stopping Alambic replication ...
  | Stopping git-hooks replication ...
  | Stopping MySQL replication ...
  | Stopping Redis replication ...
  | Stopping Elasticsearch replication ...
  | Stopping OpenVPN tunnel ...
  | Success: Replication was stopped for all services.
  | To disable replica mode and remove all replica configuration, run `ghe-repl-teardown'.
Switching out of replica mode ...
  | Success: Replication configuration has been removed.
  | Run `ghe-repl-setup' to re-enable replica mode.
Applying configuration and starting services ...
Success: Replica has been promoted to primary and is now accepting requests.
  • 4. Route53の書き換え

TTL超過後、 https://git.<_YOUR_DOMAIN_> にアクセスすれば以下のように元々セカンダリだったGHEで作成したリポジトリが表示されると思います。

ghe4-7

おまけ - レプリケーションを止めたい場合

ちなみに、一時的にレプリケーションを止めたい場合は ghe-repl-stop コマンドを実行すればOKです。

admin@ip-10-0-101-159:~$ ghe-repl-stop
Stopping Pages replication ...
Stopping Git replication ...
Stopping Alambic replication ...
Stopping git-hooks replication ...
Stopping MySQL replication ...
Stopping Redis replication ...
Stopping Elasticsearch replication ...
Stopping OpenVPN tunnel ...
Success: Replication was stopped for all services.
To disable replica mode and remove all replica configuration, run `ghe-repl-teardown'.

最後に出力されていますが、完全にレプリケーションの設定を消したい場合は ghe-repl-teardown コマンドを実行します。

admin@ip-10-0-101-159:~$ ghe-repl-teardown
Success: Replication configuration has been removed.
Run `ghe-repl-setup' to re-enable replica mode.

まとめ

いかがだったでしょうか。

GHEにはデフォルトでさまざまなCLIツールが同梱されています。これらのツールを利用すると簡単にHA構成が組めることを確認しました。中身は単なるシェルスクリプトなのでより詳細な動作を確認したい時に、ソースコードを簡単に確認できる点も管理者にとって嬉しいポイントではないでしょうか。

本エントリがみなさんの参考になれば幸いです。