Study/Study

cloud-init 이란 ?

TTOII 2022. 4. 6. 00:44
728x90

✔️ cloud-init 이란?

 

  • 클라우드 인스턴스의 초기화를 위한 스크립트
  • 클라우드 인스턴스의 최초 초기화를 위한 유틸리티들을 포함하는 패키지
  • 클라우드 인스턴스의 최초 초기화를 다루는 '사실상의 표준' 다중-배포판 패키지
  • 여러 리눅스 배포판에서 표준 Cloud 이미지를 제공하며 cloud-init으로 초기화할 수 있음

클라우드 인스턴스는 디스크 이미지 및 인스턴스 데이터에서 초기화된다.

  • Cloud metadata
  • User data (optional)
  • Vendor data (optional)

Cloud-init는 부팅하는 동안 실행 중인 클라우드를 식별하고 클라우드에서 제공된 메타데이터를 읽고 그에 따라 시스템을 초기화한다.

여기에는 SSH 액세스 키 및 시스템의 기타 여러 측면을 구성하기 위한 네트워크 및 저장 장치 설정이 포함될 수 있다.

나중에 cloud-init는 인스턴스에 전달된 선택적 user data 또는 vendor data도 구문 분석하고 처리한다.

 

✔️ 지원 형식

  • 쉘 스크립트 ( #!로 시작함 )
  • Cloud config 파일 ( #cloud-config로 시작함, yaml 형식을 따른다. )

✔️ 사용 예시

1. 인스턴스 생성시 공개키 등록 

ec2 인스턴스 생성시 공개키가 인스턴스에 등록 되는데 이 때 home 디렉토리의 .ssh/authorized_keys에 등록된다.

공개키를 인스턴스에 등록해주는 역할을 하는 것이 cloud-init이다.

 

2. 사용자 데이터 등록

인스턴스 생성시 고급 세부 정보 구성에서 사용자 데이터를 추가할 수 있다.

사용자 데이터에 작성된 셸 스크립트를 보면 명령에 sudo를 사용하지 않아도 정상적으로 수행된다.

이는 사용자 데이터를 이용해 인스턴스를 셋팅하는 주체가 cloud-init이기 때문이며 cloud-init은 관리자(즉, root)와 동등하다.

추가로 인스턴스에 접속했을 때 작성한 셸 스크립트가 정상적으로 수행되지 않았다면

/var/log 디렉토리에서 cloud-init-output.log 파일을 열어 user-data code 실행 로그를 볼 수 있다. 

[ec2-user@ip-172-31-12-236 instance]$ cd /var/log
[ec2-user@ip-172-31-12-236 log]$ ls
amazon    btmp                   cloud-init.log  grubby_prune_debug  lastlog   sa       tallylog
audit     chrony                 cron            httpd               maillog   secure   wtmp
boot.log  cloud-init-output.log  dmesg           journal             messages  spooler  yum.log
[ec2-user@ip-172-31-12-236 log]$ cat cloud-init-output.log 
cat: cloud-init-output.log: Permission denied
[ec2-user@ip-172-31-12-236 log]$ sudo cat cloud-init-output.log 
Cloud-init v. 19.3-45.amzn2 running 'init-local' at Tue, 05 Apr 2022 06:47:49 +0000. Up 4.80 seconds.
Cloud-init v. 19.3-45.amzn2 running 'init' at Tue, 05 Apr 2022 06:47:50 +0000. Up 5.86 seconds.
ci-info: ++++++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++++++
ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
ci-info: | Device |  Up  |           Address           |      Mask     | Scope  |     Hw-Address    |
ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
ci-info: |  eth0  | True |        172.31.12.236        | 255.255.240.0 | global | 06:94:8e:17:88:6b |
ci-info: |  eth0  | True | fe80::494:8eff:fe17:886b/64 |       .       |  link  | 06:94:8e:17:88:6b |
ci-info: |   lo   | True |          127.0.0.1          |   255.0.0.0   |  host  |         .         |
ci-info: |   lo   | True |           ::1/128           |       .       |  host  |         .         |
ci-info: +--------+------+-----------------------------+---------------+--------+-------------------+
ci-info: +++++++++++++++++++++++++++++++Route IPv4 info++++++++++++++++++++++++++++++++
ci-info: +-------+-----------------+------------+-----------------+-----------+-------+
ci-info: | Route |   Destination   |  Gateway   |     Genmask     | Interface | Flags |
ci-info: +-------+-----------------+------------+-----------------+-----------+-------+
ci-info: |   0   |     0.0.0.0     | 172.31.0.1 |     0.0.0.0     |    eth0   |   UG  |
ci-info: |   1   | 169.254.169.254 |  0.0.0.0   | 255.255.255.255 |    eth0   |   UH  |
ci-info: |   2   |    172.31.0.0   |  0.0.0.0   |  255.255.240.0  |    eth0   |   U   |
ci-info: +-------+-----------------+------------+-----------------+-----------+-------+
ci-info: +++++++++++++++++++Route IPv6 info+++++++++++++++++++
ci-info: +-------+-------------+---------+-----------+-------+
ci-info: | Route | Destination | Gateway | Interface | Flags |
ci-info: +-------+-------------+---------+-----------+-------+
ci-info: |   9   |  fe80::/64  |    ::   |    eth0   |   U   |
ci-info: |   11  |    local    |    ::   |    eth0   |   U   |
ci-info: |   12  |   ff00::/8  |    ::   |    eth0   |   U   |
ci-info: +-------+-------------+---------+-----------+-------+
Cloud-init v. 19.3-45.amzn2 running 'modules:config' at Tue, 05 Apr 2022 06:47:52 +0000. Up 7.71 seconds.
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Existing lock /var/run/yum.pid: another copy is running as pid 3135.
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum
    Memory :  34 M RSS (325 MB VSZ)
    Started: Tue Apr  5 06:47:52 2022 - 00:02 ago
    State  : Sleeping, pid: 3135
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum
    Memory :  82 M RSS (374 MB VSZ)
    Started: Tue Apr  5 06:47:52 2022 - 00:04 ago
    State  : Running, pid: 3135
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum
    Memory :  88 M RSS (381 MB VSZ)
    Started: Tue Apr  5 06:47:52 2022 - 00:06 ago
    State  : Running, pid: 3135
Another app is currently holding the yum lock; waiting for it to exit...
  The other application is: yum
    Memory : 160 M RSS (452 MB VSZ)
    Started: Tue Apr  5 06:47:52 2022 - 00:08 ago
    State  : Running, pid: 3135
No packages needed for security; 0 packages available
No packages marked for update
Cloud-init v. 19.3-45.amzn2 running 'modules:final' at Tue, 05 Apr 2022 06:48:03 +0000. Up 18.41 seconds.
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.52-1.amzn2 will be installed
--> Processing Dependency: httpd-tools = 2.4.52-1.amzn2 for package: httpd-2.4.52-1.amzn2.x86_64
--> Processing Dependency: httpd-filesystem = 2.4.52-1.amzn2 for package: httpd-2.4.52-1.amzn2.x86_64
--> Processing Dependency: system-logos-httpd for package: httpd-2.4.52-1.amzn2.x86_64
--> Processing Dependency: mod_http2 for package: httpd-2.4.52-1.amzn2.x86_64
--> Processing Dependency: httpd-filesystem for package: httpd-2.4.52-1.amzn2.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.52-1.amzn2.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.52-1.amzn2.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.52-1.amzn2.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.7.0-9.amzn2 will be installed
---> Package apr-util.x86_64 0:1.6.1-5.amzn2.0.2 will be installed
--> Processing Dependency: apr-util-bdb(x86-64) = 1.6.1-5.amzn2.0.2 for package: apr-util-1.6.1-5.amzn2.0.2.x86_64
---> Package generic-logos-httpd.noarch 0:18.0.0-4.amzn2 will be installed
---> Package httpd-filesystem.noarch 0:2.4.52-1.amzn2 will be installed
---> Package httpd-tools.x86_64 0:2.4.52-1.amzn2 will be installed
---> Package mailcap.noarch 0:2.1.41-2.amzn2 will be installed
---> Package mod_http2.x86_64 0:1.15.19-1.amzn2.0.1 will be installed
--> Running transaction check
---> Package apr-util-bdb.x86_64 0:1.6.1-5.amzn2.0.2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch      Version                   Repository     Size
================================================================================
Installing:
 httpd                  x86_64    2.4.52-1.amzn2            amzn2-core    1.3 M
Installing for dependencies:
 apr                    x86_64    1.7.0-9.amzn2             amzn2-core    122 k
 apr-util               x86_64    1.6.1-5.amzn2.0.2         amzn2-core     99 k
 apr-util-bdb           x86_64    1.6.1-5.amzn2.0.2         amzn2-core     19 k
 generic-logos-httpd    noarch    18.0.0-4.amzn2            amzn2-core     19 k
 httpd-filesystem       noarch    2.4.52-1.amzn2            amzn2-core     24 k
 httpd-tools            x86_64    2.4.52-1.amzn2            amzn2-core     88 k
 mailcap                noarch    2.1.41-2.amzn2            amzn2-core     31 k
 mod_http2              x86_64    1.15.19-1.amzn2.0.1       amzn2-core    149 k

Transaction Summary
================================================================================
Install  1 Package (+8 Dependent packages)

Total download size: 1.9 M
Installed size: 5.2 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                              8.1 MB/s | 1.9 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.7.0-9.amzn2.x86_64                                     1/9 
  Installing : apr-util-bdb-1.6.1-5.amzn2.0.2.x86_64                        2/9 
  Installing : apr-util-1.6.1-5.amzn2.0.2.x86_64                            3/9 
  Installing : httpd-tools-2.4.52-1.amzn2.x86_64                            4/9 
  Installing : generic-logos-httpd-18.0.0-4.amzn2.noarch                    5/9 
  Installing : mailcap-2.1.41-2.amzn2.noarch                                6/9 
  Installing : httpd-filesystem-2.4.52-1.amzn2.noarch                       7/9 
  Installing : mod_http2-1.15.19-1.amzn2.0.1.x86_64                         8/9 
  Installing : httpd-2.4.52-1.amzn2.x86_64                                  9/9 
  Verifying  : apr-util-1.6.1-5.amzn2.0.2.x86_64                            1/9 
  Verifying  : httpd-tools-2.4.52-1.amzn2.x86_64                            2/9 
  Verifying  : apr-util-bdb-1.6.1-5.amzn2.0.2.x86_64                        3/9 
  Verifying  : httpd-filesystem-2.4.52-1.amzn2.noarch                       4/9 
  Verifying  : httpd-2.4.52-1.amzn2.x86_64                                  5/9 
  Verifying  : mailcap-2.1.41-2.amzn2.noarch                                6/9 
  Verifying  : generic-logos-httpd-18.0.0-4.amzn2.noarch                    7/9 
  Verifying  : mod_http2-1.15.19-1.amzn2.0.1.x86_64                         8/9 
  Verifying  : apr-1.7.0-9.amzn2.x86_64                                     9/9 

Installed:
  httpd.x86_64 0:2.4.52-1.amzn2                                                 

Dependency Installed:
  apr.x86_64 0:1.7.0-9.amzn2                                                    
  apr-util.x86_64 0:1.6.1-5.amzn2.0.2                                           
  apr-util-bdb.x86_64 0:1.6.1-5.amzn2.0.2                                       
  generic-logos-httpd.noarch 0:18.0.0-4.amzn2                                   
  httpd-filesystem.noarch 0:2.4.52-1.amzn2                                      
  httpd-tools.x86_64 0:2.4.52-1.amzn2                                           
  mailcap.noarch 0:2.1.41-2.amzn2                                               
  mod_http2.x86_64 0:1.15.19-1.amzn2.0.1                                        

Complete!
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Cloud-init v. 19.3-45.amzn2 finished at Tue, 05 Apr 2022 06:48:07 +0000. Datasource DataSourceEc2.  Up 22.46 seconds
[ec2-user@ip-172-31-12-236 log]$

 

728x90