はじめに
クラウドインスタンスは、デフォルトのクラウドイメージに基づいています。各インスタンスはテンプレートオペレーティングシステムを使用するため、ユーザーデータを使用して一意のインスタンスを構成および設定するのはユーザーの責任です。
実行後にそれぞれを構成する代わりに、初期化を自動化するオープンソースツールがあります- cloud-init 。
cloud-initとは何ですか?
Cloud-initは、クラウドでLinuxベースのオペレーティングシステムをカスタマイズするために使用されるサービスです。起動時に汎用OS構成を変更することで、クラウドベンダーが提供する仮想マシンをカスタマイズできます。 Canonicalは当初Ubuntu用のcloud-initを開発しましたが、ほとんどの主要なLinuxおよびFreeBSDオペレーティングシステムに拡張されました。現在、8つのUnix OS(Ubuntu、Arch Linux、CentOS、Red Hat、FreeBSD、Fedora、Gentoo Linux、openSUSE)を公式にサポートしています。
このサービスは、プロビジョニングされたVMの初期段階の初期化の業界標準として使用されます。したがって、最初の起動時に必要に応じて動作するようにVMを設定できます。
ほとんどのベンダーは、UnixOSイメージにサービスをプリインストールしています。クラウドベンダーダッシュボードを使用して仮想マシンを作成する場合、ほとんどの場合、 cloud-initがあります。 またはユーザーデータ 必要な構成を指定するセクション。
Cloud-initは、パッケージのインストール、ユーザーとセキュリティの構成、ファイルの書き込み、および初回または後続の起動時に自動的に処理するその他のタスクを実行するために使用されます。
cloud-initはどのように機能しますか?
Cloud-initは、必要な構成とソフトウェアをすぐに使用できるようにVMインスタンスをセットアップするサービスです。サービスは起動時に開始され、クラウドプロバイダーまたは直接ユーザーによって提供されたメタデータを使用します。
これは、最も一般的には cloud-configからスクリプトを実行することによって行われます。 ファイル。したがって、デフォルト設定を変更するには、VMイメージのcloud-configファイルを編集する必要があります。これは、次のような基本的なYAML構文ルールに従うYAMLファイルです。
- アイテム間の関係は、空白のインデントによって定義されます。 。
- パイプ文字 ( | )テキストの前に、そのまま解釈する必要があることを示します。
- テキストブロックはインデントされています 。
- 先頭のダッシュ (- )リストのメンバーを識別します。
- コロン (: )+スペース +値 連想配列エントリを作成するために使用されます。
cloud-configファイルを追加できます:
- コントロールプレーンインターフェイスで、追加のオプションを選択する場合。プロバイダーにはcloud-initがあります またはユーザーデータ 構成ファイルを貼り付けることができるオプション。
- APIリクエストのJSONオブジェクト内のファイルを介して。
cloud-init機能
cloud-initサービスは、次のようなさまざまな目的で使用されます。
- ユーザーとグループを追加します。
- 任意のファイルを書き出す。
- YUMリポジトリーの追加。
- 最初の起動時にコマンドを実行します。
各機能に使用される構文については、以下のサブセクションをお読みください。
cloud-initを使用してユーザーとグループを追加する
プロビジョニング中にユーザーとグループをVMに追加するには、cloud-configファイルで次の構文を使用します。
#cloud-config
# Add groups to the system.
groups:
- group_name: [member1,member2]
# Add users to the system.
users:
- default
- name: User’s login name.
gecos: User’s real name.
primary_group: The primary group the user belongs to. If omitted, a new group is created under the user’s name.
groups: Additional groups to which you want to add the user.
selinux_user: The SELinux user for the user's login. If omitted, the system selects the default SELinux user.
expiredate: 'year-month-day' - Data when the user’s account should be disabled.
ssh_import_id: SSH IDs which you want to import.
lock_passwd: Use true or false to define whether you want to lock the password to disable password login. By default, the value is set to true.
inactive: 'x' - The number of days until the account is disabled after a password expires.
passwd: The hash of the password you want to use.
ssh_authorized_keys: Add keys to user’s authorized keys file:
- <ssh pub key 1>
- <ssh pub key 2>
system: Use true or false to define the new user as a system user.
sudo: Use sudo rule string(s) to define the user privileges. The user has no privileges by default, but it can accept one or multiple sudo rule strings. For example, add ALL=(ALL) NOPASSWD:ALL to give the user unrestricted access. To prevent sudo access, type: False.
- snapuser: Specify the email for your Ubuntu SSO account to allow snap to import a username and public keys into the system.
新しいユーザーを追加するときに、すべてのオプションを含める必要はありません。上記の構成は、ユーザーが新規の場合にのみ適用されます。ユーザーがすでに存在する場合、システムは次のオプションのみを変更します。
- plain_text_passwd
- hashed_passwd
- lock_passwd
- sudo
- ssh_authorized_keys
- ssh_redirect_user
任意のファイルの書き込み
yaml構成構文を使用して任意のファイルを書き出すことができます。コンテンツはエンコード(base64またはgzip)して、プレーンテキストまたはバイナリで指定できます。データが定義されたパスに書き込まれる前に、データはデコードされます。このモジュールはすべてのディストリビューションをサポートし、インスタンスごとに1回の頻度モジュールを備えています。
cloud-initを使用して任意のファイルを書き出すには、次の構文を使用します。
#cloud-config
# Write out arbitrary files
write_files:
path: The file path to which the content is added.
content: The content you want to add to the path.
owner: The user/group that has ownership. By default, the owner is root:root.
permissions: The permission of the specified path, defined with the appropriate octal string. The default permission is ‘0644’.
append: Use true or false to choose whether to append the specified content to an existing file if the path provided exists. The default value is false.
YUMリポジトリーの追加
必要なソフトウェアをインストールするために正しいパッケージを使用していることを確認するために、yumリポジトリーを構成する必要がある場合があります。 cloud-initを使用して、yumリポジトリー構成をシステムに追加します。構成ファイルが/etc/yum.repos.dに追加されます 。
yumリポジトリー構成を追加するには、次の構文を使用します。
yum_repos:
<repo_name>:
baseurl: Repository URL.
name: Repository name.
enabled: true/false
+ any other repository configuration option
最初の起動時にコマンドを実行する
起動プロセスの早い段階で任意のコマンドを実行するには、 bootcmdを使用できます。 またはruncmd モジュール。
bootcmd
boothook を実行した後、起動するたびに特定のコマンドを実行します 。すべてのディストリビューションをサポートし、リストまたは文字列として指定されたコマンドを受け入れます。構文は次のとおりです。
bootcmd:
- array of (array of string)/(string)
例:
bootcmd:
- echo 192.168.1.130 us.archive.ubuntu.com >> /etc/hosts
- [cloud-init-per, once, mymkfs, mkfs, /dev/vdb ]
runcmd
初回起動時にのみコマンドを実行します。リストまたは文字列として指定されたコマンドを実行できます。すべてのコマンドはyaml構文である必要があります(したがって、問題のある文字は必ず引用符で囲んでください)。構文は次のとおりです。
runcmd:
- array of (array of string)/(string)
次の例のように:
runcmd:
- [ ls, -l, / ]
- [ sh, -xc, "echo $(date) ': hello world!'" ]
- [ sh, -c, echo "=========hello world'=========" ]
- ls -l /root
SSHキーの設定
cloud-initを使用してSSH構成を管理できます。
承認済みキーは、ユーザーのホームディレクトリの .ssh / authorized_keysに保存されているSSHキーです。 。これらは、システム上の特定のユーザーアカウントに接続できるキーを定義します。 ssh_authorized_key を使用してユーザーを構成するときに、公開鍵をリストとして追加します :
ssh_authorized_keys:
- ssh_pub_key_1
- ssh_pub_key_2
リストされたキーは、構成されたユーザーまたはユーザーのモジュールで定義された最初のキーに属します。
事前に生成された秘密SSHキーがある場合は、それらをサーバーに保存できます。 Cloud-initは、RSA、DSA、およびECDS公開鍵暗号システムをサポートします。 SSHキーを追加するときは、フォーマットに注意してください。改行、ブロック、パイプキーを使用し、常にBEGININGPRIVATEKEYとENDPRIVATEKEYを指定してください。
ssh_keys:
rsa_private: |
-----BEGIN RSA PRIVATE KEY-----
your_rsa_private_key
-----END RSA PRIVATE KEY-----
rsa_public: your_rsa_public_key
ロケールの設定
システムロケールをシステム全体に設定して適用するには、 cc_localeを利用します モジュール。次の構成シェマを使用して、次を定義します。
locale: What you want to configure as the system's locale.
locale_configfile: The path to the file where you want to write the locale configuration.
ホスト名を定義する
cloud-initを使用すると、ホスト名とFQDN(完全修飾ドメイン名)を設定できます。これを行うにはいくつかの方法があります:
- 完全なドメイン名を指定します
fqdn
を使用する キー。 - ホスト名を定義します
hostname
を使用する キー。 hostname
を使用します fqdnを定義するためのキー (非推奨)。hostname
の両方を使用します キーとfqdn
キー。
ホスト名を定義するための構成キーは次のとおりです。
preserve_hostname: Use true or false to set whether to preserve the host name or allow altering.
prefer_fqdn_over_hostname: Use true or false to set whether to force the use of FQDN in all distros.
fqdn: FQDN
hostname: FQDN/hostname