Cloud-init란?
Cloud-init는 클라우드 환경을 제공할때 사용되는 오픈소스 솔루션이다.
인스턴스 초기화를 위해서 업계 표준으로 사용되는 다중 배포 오픈소스 솔루션으로 AWS와 같은 Public Cloud에서 사용된다.
OS(Image)가 부팅이 될때 제공된 메타데이터를 읽는다. 그리고 그 데이터값에 의거하여 시스템 초기화를 진행한다.
Cloud-init의 동작방식
동작방식은 OS가 부팅이 될때 총 5가지 과정으로 이루어지게 된다.
1. Generator
2. Local
3. Network
4. Config
5. Final
인스턴스 재부팅시에는 /etc/cloud/cloud.cfg의 정보를 읽고 5단계를 걸친뒤에 모듈, 설정 정보를 실행하여 반영한다.
아래의 코드내용은 CentOS기준으로 확인된 cloud.cfg 파일의 내용이다.
# cat /etc/cloud/cloud.cfg
users:
- default
disable_root: 1
ssh_pwauth: 1
mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys: 0
ssh_genkeytypes: ~
syslog_fix_perms: ~
disable_vmware_customization: false
cloud_init_modules:
- disk_setup
- migrator
- bootcmd
- write-files
- growpart
- resizefs
- set_hostname
- update_hostname
- update_etc_hosts
- rsyslog
- users-groups
- ssh
cloud_config_modules:
- mounts
- locale
- set-passwords
- rh_subscription
- yum-add-repo
- package-update-upgrade-install
- timezone
- puppet
- chef
- salt-minion
- mcollective
- disable-ec2-metadata
- runcmd
cloud_final_modules:
- rightscale_userdata
- 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_info:
default_user:
name: centos
lock_passwd: true
gecos: Cloud User
groups: [wheel, adm, systemd-journal]
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
shell: /bin/bash
distro: rhel
paths:
cloud_dir: /var/lib/cloud
templates_dir: /etc/cloud/templates
ssh_svcname: sshd
# vim:syntax=yaml
위 내용에 의거하여 초기화가 진행된다.
Cloud-init 관련 예시 상황 및 솔루션
OS상에서 Cloud-init 초기 모듈과 함께 Cloud 초기화 설정을 진행
/usr/bin/cloud-init -d init
OS상에서 모든 모듈을 실행
/user/bin/cloud-init -d modules
SSH Public Key를 분실하여 SSH에 접속하지 못하는 상황
PublicKey를 생성하여 EC2-Userdata에 추가하는 방식이다.
AWS에서 Keypair를 생성해 public key로 변환시켜 맨 밑 PublicKeyPair자리에 입력한다.
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [users-groups, once]
users:
- name: ec2-user
ssh-authorized-keys:
- PublicKeypair
UserData를 수정하여 적용하는 법
아래와 같은 양식을 사용한다.
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
chmod -R 755 /home/ec2-user/
--//--
chmod -R 755 /home/ec2-user/ 부분을 삭제하고 자신이 넣을 Userdata를 넣으면 된다.
그 뒤 재부팅을하면 잘 작동한다.
아래와 같이도 사용이 가능하다.
#cloud-config
#!/bin/bash
chmod -R 755 /home/ec2-user/
#cloud-boothook
#!/bin/bash
chmod -R 755 /home/ec2-user/