cloud-init 的工作流程原理

如有错误欢迎指正,期待您的宝贵意见!

cloud-init 是一个能够激活和初始化虚拟机的脚本,在虚拟机启动的时候,对虚拟机进行操作,他的功能很强,广泛适用于 OpenStack。


ubuntu14.04   /etc/cloud/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 'defa ult' 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
 - 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
 - grub-dpkg
 - apt-pipelining
 - apt-configure
 - package-update-upgrade-install
 - landscape
 - timezone
 - 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, 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/
         security: []
     - arches: [armhf, armel, default]
       failsafe:
         primary: http://ports.ubuntu.com/ubuntu-ports
         security: http://ports.ubuntu.com/ubuntu-ports
   ssh_svcname: ssh


 

1. cloud-init 资料 3

2. cloud-init 模板安装 3

2.1. linux 3

2.1.1. Rhel6.5安装cloud-init 3

2.1.2. 安装验证 4

2.1.3. 配置修改 4

2.2. AIX 5

2.2.1. Aix7.1安装cloud-init 5

2.2.2. 安装验证 5

2.2.3. 配置修改 7

3. cloud-init模板制作使用 8

3.1. 模板制作 8

3.1.1. 用命令行进行转模板 8

3.1.2. 用horizon进行转模板 8

3.2. 模板使用 9

3.2.1. 命令行创建虚机 9

3.2.2. Horizon创建虚机 9

4. Cloud-init工作原理 11

4.1. 理论介绍 11

4.2. 实例讲解 13

5. 部分脚本、配置示例 15

5.1. 修改root用户密码 15

5.2. 创建新用户、ssh登陆 16

5.3. 全域名、主机名写入hosts文件 16

 



1. cloud-init资料

激活引擎和 cloud-init 是获取用户输入以及在部署的虚拟机上配置操作系统和软件的方法。Cloud-init OpenStack 中广泛使用。安装在虚机内,形成模板后随模板使用。

cloud-init用于在创建虚拟机时通过元数据服务对虚拟机基本配置,包括常见的主机名,用户,密码等等。同时用户可以通过user_data自定义一些数据对虚拟机进行配置,可以有配置文件cloud-config或者直接给定脚本,支持常见的shell cmd python脚本(第5部分会有示例)。

程序首先判断操作系统类型,加载对应的模块,加载服务。

 

注:虚机里不可同时安装激活引擎和cloud-init。

2. cloud-init模板安装

2.1. linux

rhel6.5为例进行安装。

Cloud中所有模块都放在/usr/lib/python2.6/site-packages/cloudinit/config

2.1.1. Rhel6.5安装cloud-init

准备rhel6.5虚机,安装操作系统,配置网络后获取cloud-initrpm安装包(也可以在powervc /opt/ibm/powervc/images/cloud-init 下获取)。

使用 yumEPEL或者任何其他软件包管理器来安装存储库中cloud-init所必需的依赖项。

安装 ftp://ftp.unicamp.br/pub/linuxpatch/cloud-init-ppc64 存储库中 rhel6 cloud-init依赖项。

添加 EPEL yum 存储库以获得相关RPM。请运行以下命令以设置 Red Hat Enterprise Linux 6的存储库。

wget http://dl.fedoraproject.org/pub/epel/6Server/ppc64/epel-release-6-8.noarch.rpm

rpm -Uvh epel-release-6*.rpm

 

yum install  进行安装以下依赖包: 

libselinux-python

policycoreutils-python

python-argparse

python-boto

python-cheetah

python-configobj

python-jsonpatch

python-prettytable

python-requests

最后安装cloud-init rpm安装包:

rpm -ivh cloud-init-0.7.4-2.el6.noarch.rpm

2.1.2. 安装验证

执行 cloud-init init 命令

 

2.1.3. 配置修改

安装 cloud-init 之后,请使用下列值来修改 /etc/cloud/cloud.cfg 中提供的 cloud.cfg 文件。

#add

disable_ec2_metadata: True

#set

disable_root: 0

ssh_pwauth:   1

ssh_deletekeys:   1

 

清除MAC信息:

1)第一个文件,清空/etc/udev/rules.d/70-persistent-net.rules文件内容。

2)第二个文件,清空/lib/udev/rules.d/75-persistent-net-generator.rules内容。

3)第三个文件,去除/etc/sysconfig/network-scripts/ifcfg-eth0HWADDR一行

(4)清除 /var/log/cloud-init.log log信息。

2.2. AIX

AIX7.1为例进行安装。

2.2.1. Aix7.1安装cloud-init

准备创建AIX7.1虚机,安装操作系统,配置IP信息链接外网获取rpm安装包。

获取rpm包后,进行包安装。

安装依赖包:

        bash is needed by cloud-init-0.7.5-4.1

        python-argparse is needed by cloud-init-0.7.5-4.1

        python-boto >= 2.7 is needed by cloud-init-0.7.5-4.1

        python-cheetah is needed by cloud-init-0.7.5-4.1

        python-configobj is needed by cloud-init-0.7.5-4.1

        python-jsonpatch is needed by cloud-init-0.7.5-4.1

        python-oauth is needed by cloud-init-0.7.5-4.1

        python-prettytable is needed by cloud-init-0.7.5-4.1

        python-pyserial is needed by cloud-init-0.7.5-4.1

        python-PyYAML is needed by cloud-init-0.7.5-4.1

        python-requests is needed by cloud-init-0.7.5-4.1

        python-setuptools is needed by cloud-init-0.7.5-4.1

        python-xml is needed by cloud-init-0.7.5-4.1

        python-yaml is needed by cloud-init-0.7.5-4.1

        /usr/bin/python is needed by cloud-init-0.7.5-4.1

rpm -ivh cloud-init-0.7.5-4.1.aix6.1.ppc.rpm

 

2.2.2. 安装验证

安装完成后可以利用rpm -qa查看已安装的rpm包。

如:

# rpm -qa

AIX-rpm-7.1.3.30-1

tcl-8.4.7-3

tk-8.4.7-3

expect-5.42.1-3

bash-4.2-9

python-argparse-1.2.1-1

libyaml-0.1.4-1

unzip-6.0-2

bzip2-1.0.6-2

info-5.0-2

libiconv-1.14-1

db-4.8.24-4

expat-2.1.0-1

gmp-5.1.3-1

libffi-3.0.11-1

gettext-0.17-8

openssl-1.0.1g-1

ncurses-5.9-3

readline-6.2-3

sqlite-3.7.15.2-2

zlib-1.2.5-6

gdbm-1.10-1

python-2.7.6-1

python-setuptools-0.9.8-1

python-boto-2.34.0-1

python-cheetah-2.4.4-2

python-configobj-5.0.5-1

python-jsonpointer-1.0.c1ec3df-1

python-jsonpatch-1.8-1

python-oauth-1.0.1-1

python-prettytable-0.7.2-1

python-pyserial-2.7-1

python-PyYAML-3.11-2

python-requests-2.4.3-1

python-xml-0.8.4-1

cloud-init-0.7.5-4.1

glib2-2.33.2-1

python-devel-2.7.6-1

fdupes-1.51-1

python-six-1.3.0-1

 

执行/opt/freeware/bin/cloud-init init 进行验证

 

 

2.2.3. 配置修改

修改 /opt/freeware/etc/cloud/cloud.cfg

 - update_hostname           删除此行

datasource_list: ['ConfigDrive']  删除

disable_ec2_metadata: True   添加此行

ssh_pwauth: 1     设置为1

disable_root: 0    设置为0

 

设置虚机网络模式

利用 smit tcpip 命令将虚机设置为dhcp模式。

 

清除cloud-init 的log信息

清除 /var/log/cloud-init-output.log 日志文件内容

# >/var/log/cloud-init-output.log

3. cloud-init模板制作使用

3.1. 模板制作

3.1.1. 用命令行进行转模板

curl -i  'http://172.30.128.231:8774/v2/a8314cebd08140029b149e27c2bb37cc/servers/3721184d-981c-4763-9e1d-5b741c08eb7e/action' -X POST -H "Accept: application/json"  -H "Content-Type: application/json" -H "User-Agent: python-novaclient" -H "X-Auth-Project-Id: admin" -H "X-Auth-Token:d300686b417649ac8c48f72b9ef6d1d7" -d '{"finishOnboard": {"boot_volume_id": null,"os_distro": "aix"}}'

 

 

nova image-create 3721184d-981c-4763-9e1d-5b741c08eb7e cloud_dlpar_rhel7

 

3.1.2. 用horizon进行转模板

利用实例的快照按钮进行转模板操作,虚机必须关机断电进行快照操作。

如下图所示:

 

3.2. 模板使用

3.2.1. 命令行创建虚机

利用 --user-data 、--key-name-meta 等参数传输数据;

--user-data :指定脚本配置,实现修改密码等操作。

--key-name :指定密钥对name,实现无密码登陆

-meta :写入元数据。

创建虚机命令:

nova boot powerkvm --flavor 6 --image a48c96fd-d49a-4d0d-b6d9-1ddec52b65ba    --nic net-id=f4889f14-33ab-4345-b480-92db1d411a67 --key-name key1 --user-data /root/img/pass.txt --meta abc=def

 

3.2.2. Horizon创建虚机

部分功能使用方法如下图所示:

 

eg:    ssh -i cloud_key [email protected]

 

 

4. Cloud-init工作原理

4.1. 理论介绍

注:以rhel6.5为例介绍(涉及到部分文件路径)。

 

Cloudinit通过云平台的metadata服务或ConfigDrive等数据源拿到该userdata后,首先会对其进行切分,并分别供各模块处理。Cloud中所有模块都放在/usr/lib/python2.6/site-packages/cloudinit/config。有配置文件可知主要分为如下三大类:cloud_init_modulescloud_config_modulescloud_final_modulessystem_info

这里需要特别提到的一个模块是scripts-user,该模块负责执行userdata中的user scripts内容以及其他模块(如runcmd)生成的脚本,因此cloudinit的配置文件将其放在了cloud_final_modules阶段的近乎最后。

Cloud配置文件主要分为两部分:

1、参数、变量定义部分。

2、模块列表部分。

常见的配置包括:设定虚拟机的hostnamehosts文件、设定用户名密码、更新aptget的本地缓存、调整文件系统的大小(注意不是调整分区的大小)等。

配置文件:/etc/cloud/cloud.cfg

cloudinit会在虚拟机启动的过程中分四个阶段运行,按照时间顺序分为:cloud-init-local, cloud-init, cloud-config, cloud-final

cloud-init-local阶段主要是运行本地的一些初始化脚本。cloud-init, cloud-config, cloud-final阶段分别执行配置文件中名为cloud_init_modulescloud_config_modulescloud_final_modules下的所有模块,如果模块列表为空,则什么都不运行。

各模块在运行时,会根据之前定义的变量/参数的值,配置虚拟机的状态。

update_etc_host模块,/usr/lib/python2.6/site-packages/cloudinit/config/cc_update_etc_hosts.py。该模块用来设置主机的hosts文件,其中就用到了hostnamefqdnmanage_etc_hosts等变量的值。模块首先尝试从cloudinit的配置文件中读取这些变量的值,如果没有定义,则尝试从其他的数据源中获取变量的值,例如对于openstac来讲,可以从metadata servicehttp://169.254.169.254/latest/meta-data/hostname)获取虚拟机的主机名。如果初始模板没有配置此参数,则可以通过创建虚机时的--user-data参数获取相应的参数值)。

 

4.2. 实例讲解

以主机名写入hosts文件为例进行介绍:

一、模板初始配置

未经设置的hosts文件

 

/etc/cloud/cloud.cfg配置文件里设置

manage_etc_hosts: True

preserve_hostname: False

manage_etc_hosts设置为true,虚机启动的时候cloud_init_modules阶段运行update_etc_hos模块,获取变量值和相应的参数值。模板文件/etc/cloud/templates/hosts.redhat.tmpl被利用,

 

重写hosts文件如下:

 

二、以自定义脚本添加

如果出事模板里没有设置,可以通过nova boot创建虚机的时候指定--user-data参数获取配置值。

nova  boot testhostname --flavor 6 --image d892728d-3652-49ca-a943-e8006b0cbbb2 --nic net-id=f4889f14-33ab-4345-b480-92db1d411a67 --user-data /root/img/pass.txt

或通过horizon页面创建虚机时指定脚本如下:

#cloud-config

chpasswd:

  list: |

    root:123456

  expire: False

ssh_pwauth: True

manage_etc_hosts: True

preserve_hostname: False

 

将全域名、主机名成功写入hosts文件:

 

5. 部分脚本、配置示例

可以有配置文件cloud-config或者直接以脚本形式给出.

5.1. 修改root用户密码

#cloud-config

chpasswd:

  list: |

    root:123456

  expire: False

ssh_pwauth: True

 

5.2. 创建新用户、ssh登陆

#!/bin/bash

echo "one test about user data" >>userdata  

chmod 777 userdata  

useradd -m me  

echo -e 'me\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers  

 

passwd me <

abcabc

abcabc

EOF

sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config

service ssh restart

 

5.3. 全域名、主机名写入hosts文件

#cloud-config

chpasswd:

  list: |

    root:123456

  expire: False

ssh_pwauth: True

manage_etc_hosts: True

preserve_hostname: False

 

 

 

 





你可能感兴趣的:(openstack,cloud-init)