knife-softlayer 0.0.5 で SoftLayer を管理する #softlayer #opschef_ja #getchef_ja
この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。
knife-softlayer とは
knife-softlayerとは、SoftLayer を操作するための Knife プラグインです。
なお、knife-softlayer 0.0.5 (2014/4/16現在)では、仮想サーバのフレーバーの一覧表示、仮想サーバの作成と Chef ブートストラップ、仮想サーバの破棄の3つの機能しか持っていません。
比較的新しく公開されたソフトウェアなので、今後の機能拡張に期待しましょう。
現状では knife-softlayer を利用して SoftLayer を管理するには、SoftLayerコマンドライン・クライアントと併用が必須と思われます。
knife-softlayer のインストール
RubyGemsでインストールできます。
ubuntu@ubuntu:~$ sudo /opt/chef/embedded/bin/gem install knife-softlayer --no-rdoc --no-ri : : : Successfully installed eventmachine-1.0.3 Successfully installed ffi-1.9.3 Successfully installed gssapi-1.0.3 Successfully installed httpclient-2.3.4.1 Successfully installed mini_portile-0.5.3 Successfully installed nokogiri-1.6.1 Successfully installed rubyntlm-0.1.1 Successfully installed uuidtools-2.1.4 Successfully installed nori-1.1.5 Successfully installed httpi-0.9.7 Successfully installed wasabi-1.0.0 Successfully installed gyoku-1.1.1 Successfully installed akami-1.2.1 Successfully installed savon-0.9.5 Successfully installed little-plugger-1.1.3 Successfully installed logging-1.8.2 Successfully installed winrm-1.1.3 Successfully installed em-winrm-0.5.5 Successfully installed knife-windows-0.5.15 Successfully installed knife-softlayer-0.0.5 20 gems installed ubuntu@ubuntu:~$
ubuntu@ubuntu:~$ knife softlayer FATAL: Cannot find sub command for: 'softlayer' Available softlayer subcommands: (for details, knife SUB-COMMAND --help) ** SOFTLAYER COMMANDS ** knife softlayer flavor list (options) knife softlayer server create (options) knife softlayer server destroy (options) ubuntu@ubuntu:~$
これで knife-softlayer のインストールは完了です。
FATAL となっていますが適切なサブコマンドを与えていないだけなので問題ありません。
knife-softlayer の設定
knife.rb に SoftLayer のユーザ名と API キーを記載します。
knife[ :softlayer_username ] がユーザ名、knife[ :softlayer_api_key ] が API キーとなります。
ubuntu@ubuntu:~/chef-repo$ cat >> .chef/knife.rb knife[ :softlayer_username ] = 'XXXXXXXX' knife[ :softlayer_api_key ] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' ubuntu@ubuntu:~/chef-repo$
knife-softlayer の利用
knife softlayer flavor list
SoftLayer で利用できる仮想サーバのフレーバーの一覧を表示します。
ubuntu@ubuntu:~/chef-repo$ knife softlayer flavor list | FLAVOR | CORES | RAM | DISK | tiny | 1 | 1024 | 25GB [LOCAL] | small | 2 | 2048 | 100GB [LOCAL] | medium | 4 | 4096 | 150GB [LOCAL] | large | 8 | 8192 | 200GB [LOCAL] | xlarge | 16 | 16384 | 300GB [LOCAL] NOTICE: 'flavors' provided here for convenience; SoftLayer allows you to choose a configuration a la carte. For a full list of available instance options use --all with the `knife softlayer flavor list` subcommand. ubuntu@ubuntu:~/chef-repo$
注意書きにある通り、SoftLayer の「フレーバー」は仮想サーバ設定を簡単に選択できるように、CPU、メモリ、ディスクをまとめたものです。
次のように --all をつけて実行すると、SoftLayer の仮想サーバで可能な設定をすべて確認できます。
ubuntu@ubuntu:~/chef-repo$ knife softlayer flavor list --all | CORES | RAM | DISK | OS | NETWORK [MBS] | DATACENTER | 1 x 2.0 GHz Core | 1024 [1 GB] | 1,000 GB (SAN) | CENTOS_6_64 | 10 | ams01 | 2 x 2.0 GHz Cores | 2048 [2 GB] | 1,000 GB (SAN) | CENTOS_6_32 | 100 | dal01 | 4 x 2.0 GHz Cores | 4096 [4 GB] | 1,000 GB (SAN) | CENTOS_5_64 | 1000 | dal05 | 8 x 2.0 GHz Cores | 6144 [6 GB] | 1,000 GB (SAN) | CENTOS_5_32 | | dal06 | 12 x 2.0 GHz Cores | 8192 [8 GB] | 1,500 GB (SAN) | CLOUDLINUX_6_64 | | sea01 | 16 x 2.0 GHz Cores | 12288 [12 GB] | 1,500 GB (SAN) | CLOUDLINUX_6_32 | | sjc01 | Private 1 x 2.0 GHz Core | 16384 [16 GB] | 1,500 GB (SAN) | CLOUDLINUX_5_64 | | sng01 | Private 2 x 2.0 GHz Cores | 32768 [32 GB] | 1,500 GB (SAN) | CLOUDLINUX_5_32 | | wdc01 | Private 4 x 2.0 GHz Cores | 49152 [48 GB] | 10 GB (SAN) | DEBIAN_7_64 | | | Private 8 x 2.0 GHz Cores | 65536 [64 GB] | 10 GB (SAN) | DEBIAN_7_32 | | (以下省略)
knife softlayer server create
SoftLayerに仮想マシンを作成し、Chef のブートストラップを行います。
あらかじめホステッドサービス版Enterprise Chefに登録し、chef-repoに既に設定が行われているとします。
なお、内部では softlayer-api-ruby-client を通じて、SoftLayer API の SoftLayer_Virtual_Guest::createObject を利用して仮想マシンを作成しています。
ubuntu@ubuntu:~/chef-repo$ knife softlayer server create --help knife softlayer server create (options) --assign-global-ip IpAdress Assign an existing SoftLayer Global IP address. --bill-monthly Flag to bill monthly instead of hourly, minimum charge of one month. -B, --block-storage VALUE The size in GB of the block storage devices (disks) for this instance. Specify 1 - 5 entries in a comma separated list following the format "dev:size". Example: "0:25,2:500" would be a 25GB volume on device 0 (the root partition) and a 100GB volume on on device 2. [NOTE: SoftLayer VMs always reserve device 1 for a swap device.] --bootstrap-protocol protocol protocol to bootstrap windows servers. options: winrm/ssh --bootstrap-proxy PROXY_URL The proxy server for the node being bootstrapped --bootstrap-version VERSION The version of Chef to install -N, --node-name NAME The Chef node name for your new node --server-url URL Chef Server URL --chef-zero-port PORT Port to start chef-zero on -k, --key KEY API Client Key --[no-]color Use colored output, defaults to false on Windows, true otherwise -c, --config CONFIG The configuration file to use -C, --cores VALUE The number of virtual cores SoftLayer will assign to the VM instance. --datacenter VALUE Create a CCI VI in a particular datacenter. --defaults Accept default values for all questions --disable-editing Do not open EDITOR, just accept the data as is -d, --distro DISTRO Bootstrap a distro using a template; default is 'chef-full' -D, --domain VALUE The FQDN SoftLayer will assign to the VM instance. -e, --editor EDITOR Set the editor to use for interactive commands -E, --environment ENVIRONMENT Set the Chef environment -f, --flavor FLAVOR Pre-configured packages of computing resources. See `knife softlayer flavor list` for details. -F, --format FORMAT Which format to use for output --fqdn FQDN Pre-defined FQDN --hint HINT_NAME[=HINT_FILE] Specify Ohai Hint to be set on the bootstrap target. Use multiple --hint options to specify multiple hints. --[no-]host-key-verify Verify host key, enabled by default. -H, --hostname VALUE The hostname SoftLayer will assign to the VM instance. -i IDENTITY_FILE, The SSH identity file used for authentication --identity-file -j, --json-attributes JSON A JSON string to be added to the first run of chef-client -z, --local-mode Point knife commands at local repository instead of server --new-global-ip Order a new SoftLayer Global IP address and assign it to the instance. -n VALUE, The maximum speed of the public NIC available to the instance. --network-interface-speed -u, --user USER API Client Username -O, --os-code VALUE A valid SoftLayer operating system code. See `knife softlayer flavor list --all` for a list of valid codes. --prerelease Install the pre-release chef gems --print-after Show the data after a destructive operation -R, --ram VALUE The number of virtual cores SoftLayer will assign to the VM instance. -r, --run-list RUN_LIST Comma separated list of roles/recipes to apply --san-storage Create a CCI VM with SAN based block storage [disk]. -s, --secret The secret key to use to encrypt data bag item values --secret-file SECRET_FILE A file containing the secret key to use to encrypt data bag item values --single-tenant Create a CCI VM with a dedicated physical host. -K, --softlayer-api-key SECRET Your SoftLayer API Key --softlayer-credential-file FILE File containing SoftLayer credentials as used by `softlayer_api` Ruby gem. -U, --softlayer-username KEY Your SoftLayer Username -w, --ssh-gateway GATEWAY The ssh gateway server -P, --ssh-password PASSWORD The ssh password -p, --ssh-port PORT The ssh port -x, --ssh-user USERNAME The ssh username -T Tag=Value[,Tag=Value...], The tags for this server --tags --template-file TEMPLATE Full path to location of template to use -V, --verbose More verbose output. Use twice for max verbosity -v, --version Show chef version -y, --yes Say yes to all prompts for confirmation -h, --help Show this message ubuntu@ubuntu:~/chef-repo$
- CPUコア数: 1
- ドメイン名: example.com
- ホスト名: softlayer-knife-test01
- メモリ: 1024MB
- OS: Ubuntu 12.04 (amd64)
- データセンター: サンノゼ
- run_list: recipe[apache2]
で仮想サーバを作成します。
ubuntu@ubuntu:~/chef-repo$ knife softlayer server create -C 1 -D example.com -H softlayer-knife-test01 -R 1024 -O UBUNTU_12_64 --datacenter sjc01 -r 'recipe[apache2]' Launching SoftLayer CCI, this may take a few minutes. ......................................................................Connecting to 198.11.XXX.XXX 198.11.XXX.XXX bash: warning: setlocale: LC_ALL: cannot change locale (ja_JP.UTF-8) 198.11.XXX.XXX bash: warning: setlocale: LC_ALL: cannot change locale (ja_JP.UTF-8) 198.11.XXX.XXX Installing Chef Client... : : : 198.11.XXX.XXX Recipe: apache2::default 198.11.XXX.XXX * execute[a2dissite default] action run 198.11.XXX.XXX - execute /usr/sbin/a2dissite default 198.11.XXX.XXX 198.11.XXX.XXX * service[apache2] action start (up to date) 198.11.XXX.XXX * service[apache2] action restart 198.11.XXX.XXX - restart service service[apache2] 198.11.XXX.XXX 198.11.XXX.XXX 198.11.XXX.XXX Running handlers: 198.11.XXX.XXX Running handlers complete 198.11.XXX.XXX 198.11.XXX.XXX Chef Client finished, 17/20 resources updated in 52.166112342 seconds ubuntu@ubuntu:~/chef-repo$
このように仮想サーバの起動と Chef のブートストラップ、Recipeの適用までが行えました。
knife-softlayer 0.0.5 にはサーバ一覧を表示する機能がないので、sl コマンドを利用して確認します。
ubuntu@ubuntu:~/chef-repo$ sl cci list :.........:............:....................................:.......:........:................:...............:....................: : id : datacenter : host : cores : memory : primary_ip : backend_ip : active_transaction : :.........:............:....................................:.......:........:................:...............:....................: : XXXXXXX : sjc01 : softlayer-knife-test01.example.com : 1 : 1G : 198.11.XXX.XXX : 10.90.XXX.XXX : - : :.........:............:....................................:.......:........:................:...............:....................: ubuntu@ubuntu:~/chef-repo$ ubuntu@ubuntu:~/chef-repo$ sl cci detail XXXXXX :....................:....................................: : Name : Value : :....................:....................................: : id : XXXXXXX : : hostname : softlayer-knife-test01.example.com : : status : Active : : active_transaction : - : : state : Running : : datacenter : sjc01 : : os : Ubuntu : : os_version : 12.04-64 Minimal for CCI : : cores : 1 : : memory : 1G : : public_ip : 198.11.XXX.XXX : : private_ip : 10.90.XXX.XXX : : private_only : False : : private_cpu : False : : created : 2014-04-16T16:29:39+09:00 : : modified : 2014-04-16T16:31:27+09:00 : : vlans : :.........:........:........: : : : : type : number : id : : : : :.........:........:........: : : : : PUBLIC : XXXX : XXXXXX : : : : : PRIVATE : XXXX : XXXXXX : : : : :.........:........:........: : :....................:....................................: ubuntu@ubuntu:~/chef-repo$
Chef-Server への登録も確認します。
特に指定をしなかったので Node 名と Client 名は ID そのままとなっています。
ubuntu@ubuntu:~/chef-repo$ knife node list XXXXXXX ubuntu@ubuntu:~/chef-repo$ ubuntu@ubuntu:~/chef-repo$ knife client list XXXXXXX XXXXXXX-validator ubuntu@ubuntu:~/chef-repo$ ubuntu@ubuntu:~/chef-repo$ knife node show XXXXXXX Node Name: XXXXXXX Environment: _default FQDN: softlayer-knife-test01.example.com IP: 198.11.XXX.XXX Run List: recipe[apache2] Roles: Recipes: apache2, apache2::default, apache2::mod_status, apache2::mod_alias, apache2::mod_auth_basic, apache2::mod_authn_file, apache2::mod_authz_default, apache2::mod_authz_groupfile, apache2::mod_authz_host, apache2::mod_authz_user, apache2::mod_autoindex, apache2::mod_dir, apache2::mod_env, apache2::mod_mime, apache2::mod_negotiation, apache2::mod_setenvif Platform: ubuntu 12.04 Tags: ubuntu@ubuntu:~/chef-repo$
knife softlayer server destroy
SoftLayerの仮想マシンを破棄します。
-N, --node-name で Chef の Node 名を指定するか、-I, --ip-address でパブリック IP アドレスを指定します。
ubuntu@ubuntu:~/chef-repo$ knife softlayer server destroy --ip-address 198.11.XXX.XXX Decommissioning SoftLayer CCI, this may take a few minutes. WARNING: Deleted node XXXXXXX Chef node successfully deleted. WARNING: Deleted client XXXXXXX Chef client successfully deleted. SoftLayer CCI successfully deleted. You are no longer being billed for this instance. ubuntu@ubuntu:~/chef-repo$
まとめ
knife-softlayer はまだまだ発展途上のソフトウェアですが、Chef との連携については特に問題なく行うことができます。
筆者も独自に knife-softlayer を作成していましたが、現在は SoftLayer 公式へのコントリビュートに切り替えています。