Jaybanuan's Blog

どうせまた調べるハメになることをメモしていくブログ

Vagrantを利用して同じ環境のVMを複数作成

以下のようにループを利用して必要な台数分の定義を生成するVagrantfileを準備すればよい。

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  MAX_VM_COUNT = 3
  
  (0 ... MAX_VM_COUNT).each do |counter|
    config.vm.define "node#{counter}" do |server|
      server.vm.box = "willyhu/ubuntu-16.04-server-amd64"
      server.vm.network "private_network", ip: "192.168.10.#{counter+100}", netmask: "255.255.255.0"
    end
  end
end

ここでは、node0 〜 node2の3台のVMを作成し、それぞれにIPアドレス192.168.10.100 〜 192.168.10.102を割り当てている。

Ubuntu 16.04で最新のAnsibleをインストール

Ubuntu 16.04の標準のリポジトリからapt-getで取得できるAnsibleは若干古い。 最新版をインストールするには、PPAのAnsibleのリポジトリを追加してapt-getすればよい。

$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

参考

Ansible Document - Instllation

Ubuntu 16.04でのVagrant + libvirtの環境構築

はじめに

Ubuntu 16.04でのVagrant + libvirtの環境構築に苦労したので、手順を残しておく。 VagrantKVMを扱えるようにするには、vagrant-libvirtというサードパーティ製のプラグインが必要になる。

以下の手順は、KVMの環境は構築済みであることが前提。

Vagrantインストール

$ sudo apt-get install vagrant

apt-getでインストールされるVagrantは1.8.1と若干古い(現時点での最新は1.9.1)が、後述のvagrant-libvirtのインストールに影響があるかもしれないので、これを利用する。

vagrant-libvirtのインストールの事前準備

/etc/apt/sources.listのdeb-srcのコメントアウトを外しておく。 コメントアウトを外しておかないと、後述のapt-get build-depの実行に失敗する。

$ sudo sed -i 's/^# deb-src/deb-src/g' /etc/apt/sources.list
$ sudo apt-get update

また、Vagrant 1.8.1にはプラグインのインストールに失敗するバグがあるため、(正式な対処ではないが)以下のように修正する。

$ sudo sed -i'' "s/Specification.all = nil/Specification.reset/" /usr/lib/ruby/vendor_ruby/vagrant/bundler.rb

vagrant-libvirtのインストール

まず、必要な依存物のインストールなどを行う。

$ sudo apt-get build-dep vagrant ruby-libvirt
$ sudo apt-get install qemu libvirt-bin ebtables dnsmasq
$ sudo apt-get install libxslt-dev libxml2-dev libvirt-dev zlib1g-dev ruby-dev

そして、vagrant-libvirtのインストールを行う。

$ vagrant plugin install vagrant-libvirt

動作確認

boxのホスティングサイトのAtlasから適当なboxを選ぶ。 数は少ないが、キーワード「libvirt」で検索すると、いくつかヒットする。 ここでは、willyhu/ubuntu-16.04-server-amd64を利用することにする。

適当にvagrant-testという作業ディレクトリを作成し、そのディレクトリの中で以下のようにboxを起動する。

$ mkdir vagrant-test
$ cd vagrant-test
$ vagrant init willyhu/ubuntu-16.04-server-amd64
$ vagrant up --provider libvirt

起動後、Vagrant経由でSSHでログインできれば成功。

$ vagrant ssh

参考

vagrant-libのREADME.md

https://github.com/vagrant-libvirt/vagrant-libvirt

Vagrantのバグで出力されるエラー

$ vagrant plugin install vagrant-libvirt
Installing the 'vagrant-libvirt' plugin. This can take a few minutes...
/usr/lib/ruby/2.3.0/rubygems/specification.rb:946:in `all=': undefined method `group_by' for nil:NilClass (NoMethodError)
        from /usr/lib/ruby/vendor_ruby/vagrant/bundler.rb:275:in `with_isolated_gem'
        from /usr/lib/ruby/vendor_ruby/vagrant/bundler.rb:231:in `internal_install'
        from /usr/lib/ruby/vendor_ruby/vagrant/bundler.rb:102:in `install'
        from /usr/lib/ruby/vendor_ruby/vagrant/plugin/manager.rb:62:in `block in install_plugin'
        from /usr/lib/ruby/vendor_ruby/vagrant/plugin/manager.rb:72:in `install_plugin'
        from /usr/share/vagrant/plugins/commands/plugin/action/install_gem.rb:37:in `call'
        from /usr/lib/ruby/vendor_ruby/vagrant/action/warden.rb:34:in `call'
        from /usr/lib/ruby/vendor_ruby/vagrant/action/builder.rb:116:in `call'
        from /usr/lib/ruby/vendor_ruby/vagrant/action/runner.rb:66:in `block in run'
        from /usr/lib/ruby/vendor_ruby/vagrant/util/busy.rb:19:in `busy'
        from /usr/lib/ruby/vendor_ruby/vagrant/action/runner.rb:66:in `run'
        from /usr/share/vagrant/plugins/commands/plugin/command/base.rb:14:in `action'
        from /usr/share/vagrant/plugins/commands/plugin/command/install.rb:32:in `block in execute'
        from /usr/share/vagrant/plugins/commands/plugin/command/install.rb:31:in `each'
        from /usr/share/vagrant/plugins/commands/plugin/command/install.rb:31:in `execute'
        from /usr/share/vagrant/plugins/commands/plugin/command/root.rb:56:in `execute'
        from /usr/lib/ruby/vendor_ruby/vagrant/cli.rb:42:in `execute'
        from /usr/lib/ruby/vendor_ruby/vagrant/environment.rb:268:in `cli'
        from /usr/bin/vagrant:173:in `<main>'

プラグインインストール不可のバグの報告

https://github.com/mitchellh/vagrant/issues/6911

プラグインインストール不可のバグの対処法など

http://stackoverflow.com/questions/36811863/cant-install-vagrant-plugins-in-ubuntu-16-04

Ubuntu 16.04でのKVM環境の構築

事前準備

CPUがハードウェア仮想化をサポートしているか検査。 以下のコマンドを実行した結果、1以上が表示されればOK。

$ egrep -c '(vmx|svm)' /proc/cpuinfo

KVM

$ sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils

virt-manager

$ sudo apt-get install virt-manager

参考

Community Help Wiki - KVM/Installation

Apache + mod_jk + 各種アプリケーションサーバの設定方法

はじめに

ほとんどのJava EEアプリケーションサーバは、Javaで実装した独自のWebサーバを持っている。 通常はそのWebサーバを利用すればよいのだが、運用の都合などにより、時々前段にApacheを配置することがある。 その時のために、mod_jkを利用してAJPApacheと各種アプリケーションサーバを連携させる設定方法をメモしておく。

ただ、前段にApacheを配置する場合でも、通常はmod_proxy_httpを利用してHTTPで繋げればよいのだが。。。

環境

OSはUbuntu 16.04 LTSを利用。

$ uname -srvm
Linux 4.4.0-47-generic #68-Ubuntu SMP Wed Oct 26 19:39:52 UTC 2016 x86_64

Javaは導入済みであることを前提とする。

設定手順

Apachemod_jk をインストール

ここでは、お手軽なのでapt-getでインストール。

$ sudo apt-get install apache2 libapache2-mod-jk

次にmod_jkの設定を行うために、jk.confを編集。 念のためバックアップをとった後に編集する。

$ cd /etc/apache2/mods-available
$ sudo cp jk.conf jk.conf.org 
$ sudo vi jk.conf

デフォルトのjk.confではまともに動作しないので、余分な項目を削って、以下の内容にしておく。

<IfModule jk_module>
    JkWorkersFile /etc/libapache2-mod-jk/workers.properties
    JkLogFile /var/log/apache2/mod_jk.log
    JkLogLevel info
</IfModule>

/etc/apache2/mods-enabledにシンボリックリンクを作成してmod_jkを有効化。

$ cd /etc/apache2/mods-enabled
$ sudo ln -s ../mods-available/jk.load jk.load
$ sudo ln -s ../mods-available/jk.conf jk.conf

現在有効なサイト(デフォルトでは/etc/apache2/sites-enabled/000-default.conf)に、AJPの転送元と転送先のマッピング(JkMount)を追加。

<VirtualHost *:80>
    # (略)

    JkMount /* ajp13_worker
</VirtualHost>

最後に、Apacheを再起動。

$ sudo systemctl restart apache2

Tomcat 8の場合

デフォルトでAJPのポート(8009)がオープンされているので、特段の設定は不要でTomcatを起動するだけ。

$ cd /path/to/tomcat
$ bin/catalina.sh start

参考: Apache Tomcat 8 Configuration Reference - The AJP Connector

GlassFish 4の場合

GlassFish 4のディレクトリに移動後、管理コマンドを利用してドメインの起動とAJPリスナを作成。

$ cd /path/to/glassfish4
$ bin/asadmin start-domain domain1
$ bin/asadmin create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true jk-connector

参考: GlassFish 4.0 Administration Guide

WildFly 10の場合

WildFly 10のディレクトリに移動後、WildFlyをバックグラウンドで起動し、そして管理コマンドを利用してAJPリスナを作成。

$ cd /path/to/wildfly10
$ bin/standalone.sh & 
$ bin/jboss-cli.sh --connect
[standalone@localhost:9990 /] /subsystem=undertow/server=default-server/ajp-listener=myListener:add(socket-binding=ajp, scheme=http, enabled=true)

参考: WildFly Admin Guide - AJP listeners

Jetty 9の場合

Jetty 9はAJPには未対応。 Jetty 8まではAJPに対応していたらしい。

参考: Jetty - Howto - Configure AJP13

動作確認

http://localhosthttp://localhost:8080にアクセスして、それぞれ同じ画面(各アプリケーションサーバのトップ画面)が表示されればOK。 ブラウザのキャッシュに引っかからないように、強制リフレッシュ(Google ChromeだとCTRL + F5)したほうがよい。

Vagrant + libvirtでprivate_networkの設定を行うと、NICが増える件

説明

Vagrant + libvirtでprivate_networkの設定を行うと、ゲスト側のNICが増えるのが気になったが、そういうものらしい。

That's not how private_network works. It will always create an extra interface. Vagrant needs the first interface that comes up to sit on the management_network so that it can connect to it and manage the VM.

Vagrantの設計思想とのこと。

No, because it is entirely against the design philosophy. The management network is there to allow Vagrant access to the VM and by not using DHCP we cannot know the IP address of the booted VM because we cannot alther the box VM network configuration until after it booted.

参考

https://github.com/vagrant-libvirt/vagrant-libvirt/issues/613

libvirtで、ネットワークdefaultのDHCPの範囲を変更

環境

Ubuntu 16.04 LTS

$ uname -srvm
Linux 4.4.0-45-generic #66-Ubuntu SMP Wed Oct 19 14:12:37 UTC 2016 x86_64

$ dpkg -l | grep libvirt0
ii  libvirt0:amd64    1.3.1-1ubuntu amd64         library for interfacing with different 

設定手順

ネットワークの設定の編集コマンドを実行。

$ virsh net-edit default

以下のようなXMLがエディタでオープンされるので、それを編集。

<network>
  <name>default</name>
  <uuid>d5e58ece-d152-4a87-a768-4339b82940d5</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:3b:67:93'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>

DHCPIPアドレスの範囲を変更したいので、<range>を以下のように修正。

<range start='192.168.122.128' end='192.168.122.254'/>

編集完了後、ネットワークの再起動。

$ virsh net-destroy default
ネットワーク default は強制停止されました

$ virsh net-start default
ネットワーク default が起動されました

確認

ネットワークの状態の確認。

$ virsh net-list
 名前               状態     自動起動  永続
----------------------------------------------------------
 default              動作中  はい (yes)  はい (yes)

<range>の変更が反映されているかの確認。

$ virsh net-dumpxml default
<network connections='2'>
  <name>default</name>
  <uuid>d5e58ece-d152-4a87-a768-4339b82940d5</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:3b:67:93'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.128' end='192.168.122.254'/>  ← 反映されている
    </dhcp>
  </ip>
</network>

補足

Ubuntuの場合、/etc/libvirt/qemu/networks/default.xmlにネットワークdefaultの設定があるが、コメント欄を読むと「直接編集するな、virsh使え」と書いてある。

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh net-edit default
or other application using the libvirt API.
-->

<network>
  <name>default</name>
  <uuid>d5e58ece-d152-4a87-a768-4339b82940d5</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:3b:67:93'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>