おうち Nomad & Consul クラスタの構築 - Consul 編
はじめに
前回は物理構築までやりました。今回は OS の基本セットアップと Consul のセットアップをしていきます。
OS のインストール
以下を参考に Raspberry Pi に Ubuntu Server をインストールします。今回は Ubuntu LTS 20.04 (Pi 3/4) を選択します。
基本セットアップ
キーボード
JIS 配列のキーボードを使っているので、まずはキーボードの変更をします。
$ sudo dpkg-reconfigure keyboard-configuration
Generic 105-key (Intl) PC を選んで Japanese を選びます。それ以外の選択肢はデフォルトで OK です。
Capslock を Ctrl に変換
Capslock を ctrl に変更するために以下のファイルを変更します。
$ cat /etc/default/keyboard ... XKBOPTIONS="ctrl:nocaps" ...
変更後再起動します。
時間周り
timezone を設定します。
$ sudo ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime $ sudo dpkg-reconfigure --frontend noninteractive tzdata
次に、systemd-timesyncd を使って、ntp サーバの設定をします。設定ファイル /etc/systemd/timesyncd.conf を以下のように変更します。
$ cat /etc/systemd/timesyncd.conf ... [Time] NTP=ntp.nict.jp FallbackNTP=0.jp.pool.ntp.org ...
変更後に systemd-timesyncd を再起動します。
$ sudo systemctl restart systemd-timesyncd
Docker
Docker をインストールします。基本的には以下と同じ手順を実行します。
# リポジトリのセットアップ $ sudo apt-get update $ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg $ echo \ "deb [arch=arm64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null # docker のインストール $ sudo apt-get update $ sudo apt-get install docker-ce docker-ce-cli containerd.io
上記を実行したあとユーザーを docker グループに追加します。
$ sudo usermod -a -G docker <username>
IP アドレスの固定
Raspberry Pi の IP アドレスがころころ変わると困るので、それを固定します。今回は netplan を使います。
まずは以下のファイルを作成します。固定するアドレスは 192.168.1.203/24 とします。
$ cat /etc/netplan/99-config.yaml network: version: 2 renderer: networkd ethernets: eth0: dhcp4: false dhcp6: false addresses: [192.168.1.203/24] gateway4: 192.168.1.1
以下のコマンドで設定を反映します。
$ sudo netplan apply
Consul のセットアップ
そもそも Consul とは
いよいよ Consul です。Consul はクラスタ内でどのサービスがどのノード上で動作しているかを特定する、いわゆるサービスディスカバリの機能を備えた Hashicorp のプロダクトです。
consul の実行時プロセスは agent と呼ばれ、server と client の 2 種類に分類され、server と client は共に複数ノードあってよいということになっています。server は consul に登録されているサービスの情報である catalog というものを管理しており、catalog のマスターデータは server の中でも leader という役割のノードが持っています。また、leader は Raft という分散合意アルゴリズムにしたがって選出され、catalog の更新は leader を通すことになっています。
セットアップ
Raft の性質上、server は最低でも 3 台はないと耐障害性を持たないです(3 台なら 1 台までは壊れてもなんとかなる)。前回の記事では 1 台のみ server を用意すると書きましたが、どうせならとやはり 3 台用意することにしました。
なので、構成図は以下のようになります。
consul をちゃんとセットアップするには以下を参考にすればいいですが、いかんせん長いのでここでは必要最小限な手順だけをピックアップして紹介します。
共通
# インストール $ export CONSUL_VERSION="1.8.0" $ export CONSUL_URL="https://releases.hashicorp.com/consul" $ curl --silent --remote-name \ ${CONSUL_URL}/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_arm64.zip $ unzip consul_${CONSUL_VERSION}_linux_arm64.zip $ sudo chown root:root consul $ sudo mv consul /usr/bin/ # ユーザとデータ用/設定用ディレクトリを作成 $ sudo useradd --system --home /etc/consul.d --shell /bin/false consul $ sudo mkdir --parents /opt/consul $ sudo chown --recursive consul:consul /opt/consul $ sudo mkdir --parents /etc/consul.d $ sudo chown --recursive consul:consul /etc/consul.d # 設定ファイルの生成 ## サーバの場合 $ sudo touch /etc/consul.d/server.hcl $ sudo chown --recursive consul:consul /etc/consul.d $ sudo chmod 640 /etc/consul.d/server.hcl ## クライアントの場合 $ sudo touch /etc/consul.d/client.hcl $ sudo chown --recursive consul:consul /etc/consul.d $ sudo chmod 640 /etc/consul.d/client.hcl # サービスの定義、起動 $ sudo touch /usr/lib/systemd/system/consul.service $ sudo systemctl enable consul $ sudo systemctl start consul
なお、consul.service
の中身はこんな感じ
[Unit] Description="HashiCorp Consul - A service mesh solution" Documentation=https://www.consul.io/ Requires=network-online.target After=network-online.target ConditionFileNotEmpty=/etc/consul.d/consul.hcl [Service] Type=notify User=consul Group=consul ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d/ ExecReload=/bin/kill --signal HUP $MAINPID KillMode=process KillSignal=SIGTERM Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target
サーバ の設定ファイルはこんな感じで
datacenter = "home" server = true bootstrap_expect = 3 data_dir = "/opt/consul" bind_addr = "{{ GetInterfaceIP \"eth0\" }}" retry_join = ["192.168.1.200", "192.168.1.201", "192.168.1.202"] client_addr = "0.0.0.0" ui_config { enabled = true }
クライアントはこんな感じです。
datacenter = "home" data_dir = "/opt/consul" bind_addr = "{{ GetInterfaceIP \"eth0\" }}" retry_join = ["192.168.1.200", "192.168.1.201", "192.168.1.202"]
retry_join の中身はご自身の Raspberry Pi のアドレスに置き換えてください。
上記を終わらせると、http://<ui設定がenabledなIP アドレス>:8500
で以下のような consul の Web UI にアクセスすることができます。
これにて Consul のセットアップ終了です!
おわりに
今回は Consul のセットアップについて書きました。(やる気になれば)最後に Nomad のセットアップや Consul Connect についても書きたいと思います。