参考:https://cloud.tencent.com/document/product/213/12587
使用以下命令下载 cloud-init 源码包
官网下载地址:https://launchpad.net/cloud-init/+download
wget https://launchpad.net/cloud-init/trunk/17.1/+download/cloud-init-17.1.tar.gz
安装 cloud-init 依赖包
setuptools jinja2 prettytable oauthlib configobj pyyaml requests jsonpatch jsonschema six yum install python-pip -y pip install setuptools jinja2 prettytable oauthlib pyyaml requests jsonpatch jsonschema six --upgrade
安装
tar -zxvf cloud-init-17.1.tar.gz cd ./cloud-init-17.1 python setup.py build python setup.py install --init-system systemd
其中,--init-system 的可选参数有:(systemd,sysvinit,sysvinit_deb,sysvinit_freebsd,sysvinit_openrc,sysvinit_suse,upstart);[default: None],需要根据当前操作系统使用的自启动服务管理方式是什么进行选择,如果选择出错则 cloud-init 服务无法开机自启动,本例以 systemd 自启动服务管理为例。
centos/redhat 6用的是sysvinit, 7系列用的是systemd, ubuntu用的是是upstart
添加 syslog 用户
useradd syslog
设置 cloud-init 服务开机自启动
systemctl enable cloud-init-local.service systemctl start cloud-init-local.service systemctl enable cloud-init.service systemctl start cloud-init.service systemctl enable cloud-config.service systemctl start cloud-config.service systemctl enable cloud-final.service systemctl start cloud-final.service systemctl status cloud-init-local.service systemctl status cloud-init.service systemctl status cloud-config.service systemctl status cloud-final.service
CentOS 和 Red Hat 操作系统特殊执行:将/lib/systemd/system/cloud-init-local.service
文件内容替换为如下:
[Unit] Description=Initial cloud-init job (pre-networking) Wants=network-pre.target After=systemd-remount-fs.service Before=NetworkManager.service Before=network-pre.target Before=shutdown.target Conflicts=shutdown.target RequiresMountsFor=/var/lib/cloud [Service] Type=oneshot ExecStart=/usr/bin/cloud-init init --local ExecStart=/bin/touch /run/cloud-init/network-config-ready RemainAfterExit=yes TimeoutSec=0 # Output needs to appear in instance console output StandardOutput=journal+console [Install] WantedBy=cloud-init.target
将/lib/systemd/system/cloud-init.service
文件内容替换为如下:
[Unit] Description=Initial cloud-init job (metadata service crawler) Wants=cloud-init-local.service Wants=sshd-keygen.service Wants=sshd.service After=cloud-init-local.service After=systemd-networkd-wait-online.service After=networking.service After=systemd-hostnamed.service Before=network-online.target Before=sshd-keygen.service Before=sshd.service Before=systemd-user-sessions.service Conflicts=shutdown.target [Service] Type=oneshot ExecStart=/usr/bin/cloud-init init RemainAfterExit=yes TimeoutSec=0 # Output needs to appear in instance console output StandardOutput=journal+console [Install] WantedBy=cloud-init.target
但谈何容易。