Cloud-init cloud.cfg 파일

ohyujeong·2023년 12월 13일
0

linux

목록 보기
9/9

cloud.cfg

Cloud-init 의 Base Configuration을 구성할 때 인식되는 파일로, YAML 파일 형식으로 작성되었다. /etc/cloud/cloud.cfg 경로에 위치해있다. 여러 개의 모듈이 정의되어 있고 모듈들은 cloud-init 실행 단계를 나타내는 키(key)로 그룹지어진다.
다음과 같은 형태를 가진다.

Ubuntu의 cloud.cfg
# The top level settings are used as module
# and system configuration.

# A set of users which may be applied and/or used by various modules
# when a 'default' entry is found it will reference the 'default_user'
# from the distro configuration specified below
users:
   - default

# If this is set, 'root' will not be able to ssh in and they 
# will get a message to login instead as the above $user (ubuntu)
disable_root: true

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false

# Example datasource config
# datasource: 
#    Ec2: 
#      metadata_urls: [ 'blah.com' ]
#      timeout: 5 # (defaults to 50 seconds)
#      max_wait: 10 # (defaults to 120 seconds)

# The modules that run in the 'init' stage
cloud_init_modules:
 - migrator
 - ubuntu-init-switch
 - seed_random
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - ca-certs
 - rsyslog
 - users-groups
 - ssh

# The modules that run in the 'config' stage
cloud_config_modules:
# Emit the cloud config ready event
# this can be used by upstart jobs for 'start on cloud-config'.
 - emit_upstart
 - disk_setup
 - mounts
 - ssh-import-id
 - locale
 - set-passwords
 - snappy
 - grub-dpkg
 - apt-pipelining
 - apt-configure
 - package-update-upgrade-install
 - fan
 - landscape
 - timezone
 - lxd
 - puppet
 - chef
 - salt-minion
 - mcollective
 - disable-ec2-metadata
 - runcmd
 - byobu

# The modules that run in the 'final' stage
cloud_final_modules:
 - rightscale_userdata
 - scripts-vendor
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message
 - power-state-change

# System and/or distro specific settings
# (not accessible to handlers/transforms)
system_info:
   # This will affect which distro class gets used
   distro: ubuntu
   # Default user name + that default users groups (if added/used)
   default_user:
     name: ubuntu
     lock_passwd: True
     gecos: Ubuntu
     groups: [adm, audio, cdrom, dialout, dip, floppy, lxd, netdev, plugdev, sudo, video]
     sudo: ["ALL=(ALL) NOPASSWD:ALL"]
     shell: /bin/bash
   # Other config here will be given to the distro class and/or path classes
   paths:
      cloud_dir: /var/lib/cloud/
      templates_dir: /etc/cloud/templates/
      upstart_dir: /etc/init/
   package_mirrors:
     - arches: [i386, amd64]
       failsafe:
         primary: http://archive.ubuntu.com/ubuntu
         security: http://security.ubuntu.com/ubuntu
       search:
         primary:
           - http://%(ec2_region)s.ec2.archive.ubuntu.com/ubuntu/
           - http://%(availability_zone)s.clouds.archive.ubuntu.com/ubuntu/
           - http://%(region)s.clouds.archive.ubuntu.com/ubuntu/
         security: []
     - arches: [armhf, armel, default]
       failsafe:
         primary: http://ports.ubuntu.com/ubuntu-ports
         security: http://ports.ubuntu.com/ubuntu-ports
   ssh_svcname: ssh

출처 : https://github.com/dmsimard/cloud-init/blob/master/config/cloud.cfg

모듈

cloud-init 에서의 모듈은 클라우드 인스턴스의 초기화 및 설정 과정에서 특정 작업을 수행하는 컴포넌트 또는 서비스이며 독립적으로 작동한다. 각 모듈은 cloud-init 의 실행 단계인 init-config-final 중 특정 단계에서 실행된다.
모듈은 일종의 플러그인 형태로 작동하여 사용자가 원하는 모듈은 주석처리하거나 입력하여 비활성화하거나 활성화할 수 있다. cloud.cfg 파일에 작성된 순서대로 모듈이 실행된다.
예를 들어 아래와 같이 작성되어 있다면,

cloud_final_modules:
  - [module_1]
  - [module_2]
  - [module_3]

module_1 , module_2 , module_3 순서대로 모듈이 실행된다. 따라서 모듈 사이에 의존성이 있는 경우를 잘 고려하여 순서를 정해야한다.

https://github.com/canonical/cloud-init/tree/main/cloudinit/config 에서 모듈 파일을 확인할 수 있다. 모듈 이름은 모듈 파일명에서 가져온다. 파일명에서 cc prefix와 .py suffix를 제거하고 -_ 를 상호교환할 수 있다.
예) cc_write_files.py -> write_files , write-files 로 사용 가능

모듈 사용

cloud.cfg 에 모듈을 최상위 depth로 작성하여 사용할 수 있다. 다음은 bootcmd 모듈(시스템 부팅 시 실행되는 명령들을 정의)을 사용하여 시스템이 부팅된 날짜를 /etc/birth_certificate 파일에 작성하는 명령을 추가하는 예제이다.

bootcmd:
  - date > /etc/birth_certificate

https://www.youtube.com/watch?v=exeuvgPxd-E 참고

모듈 키

다음과 같이 3개의 모듈 키가 있다.

  • cloud_init_modules : 부팅 프로세스 초기에 cloud-init 서비스가 시작될 때 실행되는 모듈들
  • cloud_config_modules : 초기 부팅 후 cloud-init 구성(configuration)중에 실행되는 모듈들
  • cloud_final_modules : 구성(configuration)이 완료된 후 cloud-init의 최종 단계에서 실행되는 모듈들

파일에서는 아래 예시와 같이 정의된다.
init 단계를 나타내는 cloud_init_modules 키로 그룹지어진 migrator, ubuntu-init-switch, seed_random 등등의 모듈들을 정의한 것이다.

# The modules that run in the 'init' stage
cloud_init_modules:  # 모듈을 그룹짓는 키
 - migrator   # 모듈이 배열로 나열되어 있다.
 - ubuntu-init-switch
 - seed_random
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - ca-certs
 - rsyslog
 - users-groups
 - ssh

데이터소스 키

메타데이터 데이터소스를 얻는 데 필요한 설정으로 아래와 같은 형식을 가진다.

datasource:
  <datasource_name>:
    ...

다음은 AWS EC2 데이터소스를 정의하는 예시이다.

datasource:
  Ec2:
    # 메타데이터를 얻을 수 있는 url을 리스트
    metadata_urls: ["http://169.254.169.254:80", "http://instance-data:8773"]
    max_wait: 120
    timeout: 50

시스템 정보 키

cloud-init 의 기본 설정과 관련된 중요한 정보를 담고 있고, 이 정보는 외부 데이터소스(예-User data)에 의해 변경될 수 없다. 다음과 같이 system_info: 엔트리를 가진다.

system_info:
   # This will affect which distro class gets used
   distro: ubuntu
   # Default user name + that default users groups (if added/used)
   default_user:
     name: ubuntu
     lock_passwd: True
     gecos: Ubuntu
     groups: [adm, audio, cdrom, dialout, dip, floppy, lxd, netdev, plugdev, sudo, video]
     sudo: ["ALL=(ALL) NOPASSWD:ALL"]
     shell: /bin/bash
     
     ...

https://cloudinit.readthedocs.io/en/latest/reference/base_config_reference.html#system-info-keys 링크에서 설정 항목에 대한 정보를 자세하게 확인할 수 있다.

참고

cloud.cfg 파일
https://cloudinit.readthedocs.io/en/latest/reference/base_config_reference.html#base-config-reference
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_and_managing_cloud-init_for_rhel_8/red-hat-support-for-cloud-init_cloud-content#the-default-cloud-cfg-file_red-hat-support-for-cloud-init
https://cloudinit.readthedocs.io/en/latest/reference/examples.html#yaml-examples

모듈
https://www.youtube.com/watch?v=exeuvgPxd-E

profile
거친 돌이 다듬어져 조각이 되듯

0개의 댓글