#author("2024-07-31T10:05:40+09:00","default:tanak0to","tanak0to") #author("2024-07-31T10:08:30+09:00","default:tanak0to","tanak0to") RIGHT:[[Linux 関係覚え書き]] * Cloud-init [#l26408e2] vSphere 環境の AlmaLinux を cloud-init 対応にし,テンプレートとして保存する. また,テンプレートからのデプロイ方法 ** 参考 [#w51eb7ed] - https://changineer.info/vmware/hypervisor/vmware_cloud_init.html - https://qiita.com/pioka/items/85ba1d4889e21822cca2 - https://docs.redhat.com/ja/documentation/red_hat_enterprise_linux/8/html-single/configuring_and_managing_cloud-init_for_rhel_8/ * テンプレートとなる AlamLinux を用意する [#t8981047] AlmaLinux のインストール,設定自体は省略する. ** cloud-init [#qfc773fa] テンプレートとなる AlmaLinux に cloud-init をインストールする # dnf install cloud-init 必要があれば cloud-init の設定(/etc/cloud/cloud.cfg)を設定変更する. その後仮想マシンを停止し,テンプレートを作成しておく. * デプロイ時に必要な iso ファイルを作成 [#pdf2e513] もう一台の Linux を用意(操作用)して,適当なディレクトリを作成, 以下のファイルを同じディレクトリ内に作成する - meta-data # 必須 - user-data # 必須 - network-config # 必須ではない - vendor-data # 必須ではない 設定ファイルのサンプルは cloud-init をインストールしたサーバの以下のディレクトリにある. /usr/share/doc/cloud-init/examples ** metadata [#bfd5c70e] ホスト名などを記述 instance-id: ci-test-01 local-hostname: test01.example.com ** user-data [#a1551453] 初期パスワードや sshd の設定などを記述 1行目の "#cloud-config" は必須,固定行. 例1 #cloud-config password: 'my-P2ssword' chpasswd: {expire: False} ssh_pwauth: True ssh_authorized_keys: - 'ssh-rsa XXXX...xxxx xxxxxxxxxxxx@example.com' 例 2 #cloud-config ssh_pwauth: true users: - name: 'user01' plain_text_passwd: 'my-P2ssword' ssh_authorized_keys: - 'ssh-ed25519 XXXX...xxxx xxxxxxxxxxxx@example.com' groups: user01,wheel shell: '/bin/bash' lock_passwd: false "users:" を指定しないとデフォルトのユーザが作成される. デフォルトユーザ作成を抑制するには /etc/cloud/cloud.cnf でユーザモジュールを外しておくか users: [] と記載しておく. ** network-config [#n79d732f] ネットワークの設定を記述 記述方法は,netplan と同じ書式 例 1 version: 2 ethernets: interface0: match: name: '*ens192*' set-name: ens192 dhcp4: no dhcp6: no addresses: - 192.168.0.51/24 gateway4: 192.168.0.1 例 2 version: 2 ethernets: ens192: dhcp4: false addresses: ["192.168.0.52/24"] gateway4: "192.168.0.1" nameservers: addresses: - "192.168.0.1" - "8.8.8.8" ** ISOファイルの作成 [#j1f3fe2e] 操作用 Linux に genisoimage をインストール # dnf install genisoimage 作成したファイル(meta-data 等)を置いたフォルダで iso イメージを作成する. なお,ボリューム名(-volid)は "cidata" の固定名でないといけない $ genisoimage \ -output ci-test-01.iso \ -volid cidata \ -joliet -rock user-data meta-data network-config macOS で iso イメージを作成する場合は, ディスクユーティリティで指定したフォルダを - イメージフォーマット DVD/CD マスター を選んで ci-test-01.cdr ファイルを作成する. その後, hdiutil makehybrid -iso -joliet \ -joliet-volume-name cidata \ -o ci-test-01.iso ci-test-01.cdr として iso ファイルを作成する. * デプロイ [#l11d5db6] 作成した iso を ESXi のデータストアに保存する. その後,テンプレートから仮想マシンを作成し(この時点では起動させない), 最初の起動時にデータストアに保存した iso(上記の例では ci-test-01.iso) を 接続した状態で起動する. そうすると,起動時に cloud-init が走って各種設定が行われる. * cloud-init 後の更新 [#vde06a4a] 一度 cloud-init でデプロイしたマシンを cloud-init で更新するには meta-data の instance-id instance-id: ci-test-01 を変更して,最初と同じように iso を接続して再度読み込ませればよい. * テンプレートの更新 [#qcb20686] cloud-init が実行された記録は - /var/lib/cloud/instances/ - /var/lib/cloud/instance - /var/lib/cloud/data/ に残骸が残るので # rm -Rf /var/lib/cloud/instances/ # rm -Rf /var/lib/cloud/instance # rm -Rf /var/lib/cloud/data/ をするか # cloud-init clean をした後にシャットダウンして,テンプレートに変換すれば良い. なお,cloud-init の実行ログは /var/log/cloud-init.log に出力されるので, # grep Writing /var/log/cloud-init.log とすれば,どのファイルが更新されたかわかる. 更新したくない項目があれば(例えばバックアップから戻した場合など) # vi /etc/cloud/cloud.cfg を編集して cloud_init_modules: - disk_setup - migrator - bootcmd - write-files - growpart - resizefs - set_hostname - update_hostname - update_etc_hosts - rsyslog - users-groups # - ssh コメントアウトすればよい.