entercloudsuite.consul

ConsulをデプロイするためのAnsibleロール

Consul

Consul

Build Status Galaxy

特徴

YAMLでConsulを設定

Consulサービスの設定は、YAMLからJSONへの変換を使います。以下のようにConsul設定を表現できます:

consul_master_token: myToken
consul_server: true
consul_configs:
  main:
    acl_datacenter: pantheon
    acl_master_token: "{{ consul_master_token | to_uuid }}"
    bootstrap: true
    bind_addr: 0.0.0.0
    client_addr: 0.0.0.0
    datacenter: pantheon
    data_dir: "{{ consul_data_dir }}"
    log_level: INFO
    node_name: master
    server: "{{ consul_server }}"
    ui: true

これはJinja2フィルターを使って行います。このロールには、設定のための事前定義されたエントリーはありません。したがって、Consulの設定をJSONで書く代わりに、YAMLに対応する構文で簡単に表現できます。これは、Ansibleの設定のどこにでも配置できます。
上記の例のように、これは非常に強力です。Ansibleで利用可能な他のフィルターを使って、任意のデータを構成し設定に使用できます。Ansibleのテンプレート機能で表現できることは、すべて設定で使用できます(インベントリからデータ/ホスト情報を取得するなど)。

上記の例ではシンプルな文字列のキー/バリューペアを示していますが、有効なタイプ(辞書やリストなど)をYAMLで定義することもできます。
ConsulのJSON設定をYAMLで表現する方法がわからない場合は、便利なコンバータがあります

要件

このロールはUbuntu 16.04でのみテストされていますが、他のLinuxディストリビューションでも動作することが期待されます。 必要なもの:

  • systemd
  • unzip

デフォルトのロール変数

---
consul_packer_provision: false
consul_group_name: consul
consul_group_gid: 3000
consul_user_name: consul
consul_user_uid: 3000
consul_user_home: /opt/consul
consul_config_dir: "{{ consul_user_home }}/conf.d"
consul_data_dir: "{{ consul_user_home }}/data"
consul_version: 1.6.3
# Consulがサーバーとして設定されている場合、systemdはbindポート53のためにcap_net_bind_serviceを設定します
consul_cap_net_bind_service: "{{ consul_configs.main.server | default('false') }}"
consul_server: false
consul_uri: "https://releases.hashicorp.com/consul/{{ consul_version }}/consul_{{ consul_version }}_linux_amd64.zip"
consul_config_src: main.json.j2
consul_config_validate: "{{ consul_user_home }}/bin/consul validate -config-format=json %s"
consul_extra_args: []
consul_service_file:
  src: consul.service.j2
  dest: /etc/systemd/system/consul.service
consul_service_status: started
# reloaded
# restarted
# started
# stopped

enable_on_boot: yes
# yes
# no

# 設定ステップを実行しないための変数を設定
# no_configure

# 設定ステップを実行しないための変数を設定
# no_install

consul_config:
  datacenter: dc-1
  data_dir: "{{ consul_data_dir }}"
  log_level: INFO
  node_name: node-1
  server: "{{ consul_server }}"

例のプレイブック

基本のロール設定

- hosts: consul_servers
  vars:
    consul_master_token: myToken
    consul_server: true
    consul_config:
      acl_datacenter: pantheon
      acl_master_token: "{{ consul_master_token | to_uuid }}"
      bootstrap: true
      bind_addr: 0.0.0.0
      client_addr: 0.0.0.0
      datacenter: pantheon
      data_dir: "{{ consul_data_dir }}"
      log_level: INFO
      node_name: master
      server: "{{ consul_server }}"
      ui: true
  roles:
      - entercloudsuite.consul

設定せずにインストールだけする

---
- name: メインロールを実行
  hosts: all
  roles:
    - role: entercloudsuite.consul
      configure: false
      install: true
      consul_service_status: "stopped"
      consul_master_token: myToken
      consul_server: true
      consul_configs:
        main:
          acl_datacenter: pantheon
          acl_master_token: "{{ consul_master_token | to_uuid }}"
          bootstrap: true
          bind_addr: 0.0.0.0
          client_addr: 0.0.0.0
          datacenter: pantheon
          data_dir: "{{ consul_data_dir }}"
          log_level: INFO
          node_name: master
          server: "{{ consul_server }}"
          ui: true

インストールせずに設定だけする

---
- name: メインロールを実行
  hosts: all
  roles:
    - role: entercloudsuite.consul
      configure: true
      install: false
      consul_service_status: "started"
      consul_master_token: myToken
      consul_server: true
      consul_configs:
        main:
          acl_datacenter: pantheon
          acl_master_token: "{{ consul_master_token | to_uuid }}"
          bootstrap: true
          bind_addr: 0.0.0.0
          client_addr: 0.0.0.0
          datacenter: pantheon
          data_dir: "{{ consul_data_dir }}"
          log_level: INFO
          node_name: master
          server: "{{ consul_server }}"
          ui: true

Consulエージェントの設定

エージェントの例の設定です。サーバーにグループ['server']として参加します。

    - role: ansible-consul
      configure: true
      install: true
      consul_service_status: "started"
      consul_version: 1.6.3
      consul_configs:
        main:
          bind_addr: "{{ ansible_default_ipv4['address'] }}"
          client_addr: 0.0.0.0
          node_name: "{{ ansible_hostname }}"
          data_dir: "{{ consul_data_dir }}"
          datacenter: "pantheon"
          enable_syslog: true
          server: false
          ui: true
          enable_script_checks: true
          rejoin_after_leave: true
          retry_join: "{{ groups['server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}"

Consulサーバーの例

サーバーの例の設定です。ホストはグループ['server']にあり、新しいConsulクラスターを作成します。

    - role: ansible-consul
      configure: true
      install: true
      consul_service_status: "started"
      consul_version: 1.6.3
      consul_configs:
        main:
          bind_addr: "{{ ansible_default_ipv4['address'] }}"
          client_addr: 0.0.0.0
          node_name: "{{ ansible_hostname }}"
          data_dir: "{{ consul_data_dir }}"
          datacenter: "pantheon"
          enable_syslog: true
          server: true
          ui: true
          enable_script_checks: true
          rejoin_after_leave: true
          retry_join: "{{ groups['server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}"
          ports:
            dns: 53
          dns_config:
            udp_answer_limit: 64
          bootstrap_expect: "{{ groups['server'] | length | int }}"
          recursors:
            - 1.1.1.1
            - 8.8.8.8

テスト

テストはMoleculeを使用して行います。

Moleculeをインストールするか、docker-compose run --rm moleculeを使ってローカルDockerコンテナを実行します。これは、enterclousuite/moleculeプロジェクトに基づいています。

  1. molecule createを実行してローカルエンジンでターゲットDockerコンテナを起動します。
  2. molecule loginを使用して実行中のコンテナにログインします。
  3. ロールファイルを編集します。
  4. molecule/default/requirements.ymlファイルに必要な他のロール(外部)を追加します。
  5. molecule/default/playbook.ymlを編集します。
  6. molecule/default/testsフォルダーの下で、goos verifierを使ってインフラテストを定義します。
  7. 準備ができたら、molecule convergeを使ってAnsibleプレイブックを実行し、molecule verifyを使ってテストスイートを実行します。 コンバージュプロセスは、ロールの構文チェックを開始します。 molecule destroyコマンドでDockerコンテナを破棄します。

すべてのステップを一つのコマンドで実行するには、molecule testを実行します。

仮想マシンをターゲットにするロールを実行するには、たとえば、次のコマンドを使用してplaybook_deploy.ymlファイルを使います:ansible-playbook ansible-docker/molecule/default/playbook_deploy.yml -i VM_IP_OR_FQDN, -u ubuntu --private-key private.pem

ライセンス

MIT

作者情報

作成者:

  • Calum MacRae
  • Jacopo Secchiero
  • Attilio Greco
プロジェクトについて

Role for deploying Consul

インストール
ansible-galaxy install entercloudsuite.consul
ライセンス
mit
ダウンロード
7.5k