Salt StackでAWSをマネージメントしたい

目的

インフラ構成管理ソフトについて勉強中。とりあえずSaltStack使ってみるよ。

 

この記事でやること

SaltStackの公式ドキュメント [Getting Started With AWS EC2] をやってみる。

 

Salt StackでAWSをマネージメントするために用意しておくこと

  • EC2 access credentialsの作成
  • Key Pairの作成
  • Security Groupの作成
  • リージョンをどこにするか考える

 

Salt Stackを実行するEC2インスタンスを用意する

次のEC2インスタンスを作成した。Key PairやSecurity Groupはデフォルトではなく用意したものを使用した。

 

Salt Stackのインストール

用意したEC2インスタンスにログインし、SaltStackの公式ドキュメント [Ubuntu] を参照してインストールを行う。このsalt-masterをインストールした環境が管理サーバとなる。

#一般的な ネットワークアクセスを行うためのパッケージ
sudo apt-get install salt-api

# public cloud VM をマネージメントするパッケージ
sudo apt-get install salt-cloud

# 管理サーバに入れるパッケージ
sudo apt-get install salt-master

# クライアントサーバに入れるパッケージ
sudo apt-get install salt-minion

# リモート操作をするためのパッケージ
sudo apt-get install salt-ssh

# 分散リモート実行システムパッケージ
sudo apt-get install salt-syndic

# おまじない
sudo salt-cloud -u

[salt-cloud] Minion doesn't install on some providers after upgrading to 2015.8.0 · Issue #26699 · saltstack/salt · GitHub

おまじないは必須です。公式ドキュメントに書いておいてほしいですね。

 

Salt Cloudを使用してクライアントサーバを構築する

2つの設定ファイルを用意する。ファイルに構築環境を記録しておくことにより再現可能となる。さっそく[Getting Started With AWS EC2] を参考にして書いてみよう。

1つめ:/etc/salt/cloud.providers


# プロバイダー名。設定をこの名前を使って取り込む重要な名前。
my-ec2-southeast-private-ips:
 # salt-masterをインストールした(管理側)のEC2のプライベートIPアドレス。
 # ドキュメントではEC2インスタンスNAMEでもよさげだったけどできなかった。
  # Set up the location of the salt master
  #
  minion:
    master: 172.31.7.10

 # EC2はプライベートIPで接続する
  # Specify whether to use public or private IP for deploy script.
  #
  # Valid options are:
  #     private_ips - The salt-master is also hosted with EC2
  #     public_ips - The salt-master is hosted outside of EC2
  #
  ssh_interface: private_ips

 # とりあえずデフォルト
  # Optionally configure the Windows credential validation number of
  # retries and delay between retries.  This defaults to 10 retries
  # with a one second delay betwee retries
  win_deploy_auth_retries: 10
  win_deploy_auth_retry_delay: 1

 # AWSで作ったEC2 access credentialsを設定する
  # Set the EC2 access credentials (see below)
  #
  id: '*****************'
  key: '*****************'

  # AWSで作ったキーペアを設定する
  # Make sure this key is owned by root with permissions 0400.
  #
  private_key: '/root/.ssh/プライベートキー.pem'
  keyname: 'キーネーム'

  # AWSで作ったセキュリティーグループを設定する
  # This one should NOT be specified if VPC was not configured in AWS to be
  # the default. It might cause an error message which says that network
  # interfaces and an instance-level security groups may not be specified
  # on the same request.
  #
  securitygroup: 'セキュリティーグループ名'

 # リージョンを指定する。うちはオレゴンを使った。
  # Optionally configure default region
  #
  location: us-west-2
  availability_zone: us-west-2c

 # ubuntuを使っているのでubuntuにする
  # Configure which user to use to run the deploy script. This setting is
  # dependent upon the AMI that is used to deploy. It is usually safer to
  # configure this individually in a profile, than globally. Typical users
  # are:
  #
  # Amazon Linux -> ec2-user
  # RHEL         -> ec2-user
  # CentOS       -> ec2-user
  # Ubuntu       -> ubuntu
  #
  ssh_username: ubuntu

  # オプションなのでとりあえず指定しないでコメントアウトしておく
  # Optionally add an IAM profile
  # iam_profile: 'my other profile name'

  driver: ec2

2つめ:/etc/salt/cloud.profiles

# プロファイル名。salt-cloud実行時にこの名前を使って指定する。
base_ec2_private:
 # 1つめのcloud.providersで指定したプロバイダー名
  provider: my-ec2-southeast-private-ips 
  image: ami-835b4efa
  size: t2.micro
  ssh_username: ubuntu

設定終わり。本家のマニュアルを見ればもっと細かいことをやっているが、こちらは最小設定。

 

Salt Cloudを実行する

# Usage: salt-cloud -p <プロファイル名> <新たに作られるEC2インスタンスのName(任意)>
# 2つインスタンスを立てる
salt-cloud -p base_ec2_private ami.slave01
salt-cloud -p base_ec2_private ami.slave02

 AWSのコンソールを見ると、新しいEC2インスタンスができてる。

 

MasterからMinionのコマンドを起動する

 # minionサーバのコマンドが実行できた。
root@ip-172-31-2-47:/etc/salt# salt 'ami.slave*' cmd.run 'ls -la /etc/hostname'
ami.slave02:
    -rw-r--r-- 1 root root 17 Oct 24 04:20 /etc/hostname
ami.slave01:
    -rw-r--r-- 1 root root 15 Oct 24 04:18 /etc/hostname    
 # pythonも実行できる
root@ip-172-31-2-47:/etc/salt# salt 'ami.slave*' cmd.run 'python --version'
ami.slave02:
    Python 2.7.12
ami.slave01:
    Python 2.7.12

便利っぽい。

 

おかたづけ(EC2インスタンスを削除する)

 #EC2をdestroyするコマンドの実行
root@ip-172-31-2-47:/etc/salt# salt-cloud -d ami.slave01
The following virtual machines are set to be destroyed:
  my-ec2-southeast-public-ips:
    ec2:
      ami.slave01

Proceed? [N/y] y

root@ip-172-31-2-47:/etc/salt# salt-cloud -d ami.slave02
The following virtual machines are set to be destroyed:
  my-ec2-southeast-public-ips:
    ec2:
      ami.slave02

Proceed? [N/y] y
... proceeding
 # Masterの設定も初期化されたかをみてみる。
root@ip-172-31-2-47:/etc/salt# salt-key
Accepted Keys:
# ami.slave01, ami.slave02が設定されていたのが消えている

EC2を削除すると同時にsalt-masterとsalt-minionの関係も解消してくれた。

 

使ってみて感想

EC2インスタンスを簡単に作成できる。設定ファイルもわかりやすい。

AWSサービスがどこまでsalt-cloudで使用できるか調べる必要はある。

EC2を作成すると同時にsalt-masterとsalt-minionの関係が作られるのが楽ちん。

日本で使っている人少ないかもしれない。

 

おわり。