LoginSignup
6
7

More than 5 years have passed since last update.

Infrataster::Plugin::Dns(infrataster-plugin-dns)を活用して、ネームサーバ切り替えを堅実に実施する

Last updated at Posted at 2015-09-16

Infrataster::Plugin::Dns(infrataster-plugin-dns)を活用して、ネームサーバ切り替えを堅実に実施する

したいこと

example.comのネームサーバを、あるDNSサーバ(currentdns.com / 203.0.113.1)からRoute53に切り替えたい!

よくありますよね?

そんな切り替え作業のとき、レコードがちゃんとお互いに設定されているかを目視で確認していたり、ネームサーバの切り替え確認をdigで実施していたりしているのを、Infrataster::Plugin::Dnsを活用して堅実に実施してみます。

手順

Infrataster::Plugin::Dnsのインストール

適当なディレクトリを作成して、

$ mkdir infraspec/
$ cd infraspec/

Gemfile を記述します

# infraspec/Gemfile
source 'https://rubygems.org'
gem 'infrataster-plugin-dns'

で、bundle install でインストールします。

最低限の Rakefile もつくっておきましょう

# infraspec/Rakefile
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new('spec')
task :default => :spec

現行のネームサーバの設定のspecを作成

$ mkdir spec/

spec_helper.rbを設置します。

# infraspec/spec/spec_helper.rb
require 'infrataster/rspec'
require 'infrataster-plugin-dns'

# 現行のネームサーバ設定
Infrataster::Server.define(
  :currentdns,
  '203.0.113.1' # 現行のネームサーバのIPアドレス
)

現行のネームサーバの設定を記述します。

# infraspec/spec/currentdns_spec.rb
require 'spec_helper'

describe server(:currentdns) do
  describe dns('example.com') do
    it { is_expected.to have_entry.with_type('A').and_address('203.0.113.2') }
    it { is_expected.to have_entry.with_type('MX').and_preference(10).and_exchange('mail.example.com') }
  end
  describe dns('mail.example.com') do
    it { is_expected.to have_entry.with_type('CNAME').and_domainname('foobarmailservice.com') }
  end
end

ここで一旦rake specを実行して、 グリーン になることを確認します。

$ cd infraspec/
$ bundle exec rake spec

server 'currentdns'
  dns 'example.com'
    should have the correct dns entries with {:type=>"A", :address=>"203.0.113.2"}
    should have the correct dns entries with {:type=>"MX", :preference=>10, :exchange=>"mail.example.com"}
  dns 'mail.example.com'
    should have the correct dns entries with {:type=>"CNAME", :domainname=>"foobarmailservice.com"}

Finished in 0.17017 seconds (files took 2.48 seconds to load)
3 examples, 0 failures

グリーン になりました。

Route53のHosted Zoneにexample.comを追加

追加するとSOAレコードとNSレコードが追加された状態で作成されます。

Route53の設定を追加

spec_helper.rbにRoute53のネームサーバ情報を追記します。

# Route53のネームサーバ設定
Infrataster::Server.define(
  :route53dns,
  'XXX.XXX.XXX.XXX' # ns-1234.awsdns-56.org NSレコードに追加されたドメインのIPアドレスを1つ
)

Route53のネームサーバの設定のspecを作成

infraspec/spec/currentdns_spec.rbをコピーして作成します。

# infraspec/spec/route53dns_spec.rb
require 'spec_helper'

describe server(:route53dns) do
  describe dns('example.com') do
    it { is_expected.to have_entry.with_type('A').and_address('203.0.113.2') }
    it { is_expected.to have_entry.with_type('MX').and_preference(10).and_exchange('mail.example.com') }
  end
  describe dns('mail.example.com') do
    it { is_expected.to have_entry.with_type('CNAME').and_domainname('foobarmailservice.com') }
  end
end

ここで一旦rake specを実行して、 レッド になることを確認します。

Route53のネームサーバの設定にレコードを追加していく

rake specの実行結果が グリーン になるように、Route53のネームサーバの設定にレコードを追加していきます。

グリーン になったらRoute53のネームサーバの設定完了です。

ネームサーバ切り替え簡易確認用のspecを記述する

Google Public DNSを借ります。spec_helper.rbに追記します。

# Google Public DNS
Infrataster::Server.define(
  :google,
  '8.8.8.8'
)
# infraspec/spec/google_spec.rb
require 'spec_helper'

describe server(:google) do
  describe dns('example.com') do
     it { is_expected.to have_entry.with_type('NS').and_domainname('ns-1234.awsdns-56.org') } # Route53のネームサーバ
  end
end

ここで一旦rake specを実行して、 レッド になることを確認します。

ネームサーバを切り替える

ネームサーバを切り替えます。

ネームサーバが切り替わったか確認する

rake specを実行して、グリーン になったら少なくともGoogle Public DNSへ切り替え設定が反映されたことが確認できます。

終わり!

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7