You need to enable JavaScript to run this app.
导航
Linux系统中安装Cloud-Init
最近更新时间:2023.07.07 20:05:36首次发布时间:2021.10.29 18:50:38

Cloud-Init工具主要提供边缘实例首次初始化时自定义配置的能力。如果导入的镜像没有安装Cloud-Init,基于该镜像创建的边缘实例将无法被正常初始化。本文指导您在Linux系统中安装Cloud-Init。

前提条件

需要安装Cloud-Init的服务器可正常访问外网。

操作步骤

安装软件包

Centos 7/8系列:

  1. (可选)配置软件源
    CentOS的软件源配置文件是 /etc/yum.repos.d/CentOS-Base.repo,请先备份软件源配置文件后再修改成以下内容:

    • CentOS 7
    wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.volces.com/repo/Centos-7.repo
    

    或者,

    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.volces.com/repo/Centos-7.repo
    
    • CentOS 8
    [base]
    name=CentOS-8.5.2111 - Base
    baseurl=http://mirrors.volces.com/centos/8.5.2111/BaseOS/$basearch/os/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
    
    #additional packages that may be useful
    [extras]
    name=CentOS-8.5.2111 - Extras
    baseurl=http://mirrors.volces.com/centos/8.5.2111/extras/$basearch/os/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
    
    #additional packages that extend functionality of existing packages
    [centosplus]
    name=CentOS-8.5.2111 - Plus
    baseurl=http://mirrors.volces.com/centos/8.5.2111/centosplus/$basearch/os/
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
    
    [AppStream]
    name=CentOS-8.5.2111 - AppStream
    failovermethod=priority
    baseurl=http://mirrors.volces.com/centos/8.5.2111/AppStream/$basearch/os/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
    
  2. 安装软件包

    yum install cloud-utils-growpart
    

Ubuntu系列:

  1. (可选)配置软件源

Ubuntu的软件源配置文件是 /etc/apt/sources.list,请先备份软件源配置文件后再修改成以下内容:

  • Ubuntu 16.04 (Xenial)

    deb http://mirrors.volces.com/ubuntu/ xenial main
    deb-src http://mirrors.volces.com/ubuntu/ xenial main
    
    deb http://mirrors.volces.com/ubuntu/ xenial-updates main
    deb-src http://mirrors.volces.com/ubuntu/ xenial-updates main
    
    deb http://mirrors.volces.com/ubuntu/ xenial universe
    deb-src http://mirrors.volces.com/ubuntu/ xenial universe
    deb http://mirrors.volces.com/ubuntu/ xenial-updates universe
    deb-src http://mirrors.volces.com/ubuntu/ xenial-updates universe
    
    deb http://mirrors.volces.com/ubuntu/ xenial-security main
    deb-src http://mirrors.volces.com/ubuntu/ xenial-security main
    deb http://mirrors.volces.com/ubuntu/ xenial-security universe
    deb-src http://mirrors.volces.com/ubuntu/ xenial-security universe
    
  • Ubuntu 18.04 (Bionic)

    deb http://mirrors.volces.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.volces.com/ubuntu/ bionic main restricted universe multiverse
    
    deb http://mirrors.volces.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.volces.com/ubuntu/ bionic-security main restricted universe multiverse
    
    deb http://mirrors.volces.com/ubuntu/ bionic-updates main restricted universe multiverse
    deb-src http://mirrors.volces.com/ubuntu/ bionic-updates main restricted universe multiverse
    
    deb http://mirrors.volces.com/ubuntu/ bionic-proposed main restricted universe multiverse
    deb-src http://mirrors.volces.com/ubuntu/ bionic-proposed main restricted universe multiverse
    
    deb http://mirrors.volces.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.volces.com/ubuntu/ bionic-backports main restricted universe multiverse
    
  • Ubuntu 20.04 (Focal)

    deb http://mirrors.volces.com/ubuntu/ focal main restricted universe multiverse
    deb-src http://mirrors.volces.com/ubuntu/ focal main restricted universe multiverse
    
    deb http://mirrors.volces.com/ubuntu/ focal-security main restricted universe multiverse
    deb-src http://mirrors.volces.com/ubuntu/ focal-security main restricted universe multiverse
    
    deb http://mirrors.volces.com/ubuntu/ focal-updates main restricted universe multiverse
    deb-src http://mirrors.volces.com/ubuntu/ focal-updates main restricted universe multiverse
    
    deb http://mirrors.volces.com/ubuntu/ focal-proposed main restricted universe multiverse
    deb-src http://mirrors.volces.com/ubuntu/ focal-proposed main restricted universe multiverse
    
    deb http://mirrors.volces.com/ubuntu/ focal-backports main restricted universe multiverse
    deb-src http://mirrors.volces.com/ubuntu/ focal-backports main restricted universe multiverse
    
  1. 安装软件包

    apt update
    apt-get install net-tools ethtool
    apt-get install ifupdown resolvconf
    

Debian系列

  1. (可选)配置软件源

Debian 的软件源配置文件是 /etc/apt/sources.list,请先备份软件源配置文件后再修改成以下内容:

  • Debian 9 Stretch

    deb http://mirrors.volces.com/debian stretch main contrib non-free
    deb http://mirrors.volces.com/debian stretch-backports main contrib non-free
    deb http://mirrors.volces.com/debian-security stretch/updates main contrib non-free
    #deb-src http://mirrors.volces.com/debian stretch main contrib non-free
    #deb-src http://mirrors.volces.com/debian stretch-backports main contrib non-free
    #deb-src http://mirrors.volces.com/debian-security stretch/updates main contrib non-free
    
  • Debian 10 Buster

    deb http://mirrors.volces.com/debian buster main contrib non-free
    deb http://mirrors.volces.com/debian buster-backports main contrib non-free
    deb http://mirrors.volces.com/debian-security buster/updates main contrib non-free
    #deb-src http://mirrors.volces.com/debian buster main contrib non-free
    #deb-src http://mirrors.volces.com/debian buster-backports main contrib non-free
    #deb-src http://mirrors.volces.com/debian-security buster/updates main contrib non-free
    
  1. 安装软件包

    apt-get update
    apt-get install cloud-guest-utils cloud-utils cloud-initramfs-growroot parted dnsutils
    apt-get install net-tools ethtool tcpdump ifupdown
    

安装/更新Python版本

说明

请确保Python版本为3.6.5及以上,版本太低可能导致cloud-init执行失败。

CentOS 7/8系列,执行以下命令:

#安装python
yum install python3-pip
pip3 install --upgrade pip

#查看python版本
python3 -V

Ubuntu系列,执行以下命令:

#安装python
apt-get install python3-pip
pip3 install --upgrade pip

#查看python版本
python3 -V

Debian系列,执行以下命令:

#安装python
apt-get install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev
cd /opt/
wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz
tar zxvf Python-3.9.1.tgz
cd Python-3.9.1
./configure --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3.9 /usr/local/bin/python3
ln -s /usr/local/python3/bin/python3.9 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
pip3 install --upgrade pip

#查看python版本
python3 -V

下载cloud-init源码包

说明

Ubuntu系列无需下载,请直接进入安装流程。

在正常安装的情况下,cloud-init-21.1版本与火山引擎的兼容性较佳,建议选择cloud-init-21.1.tar.gz安装版本。您也可以点此获取其他版本的cloud-init源码包,但不保证实例可以正常运行。本文以cloud-init-21.1版本为例。

执行以下命令,下载cloud-init源码包:

wget https://launchpad.net/cloud-init/trunk/21.1/+download/cloud-init-21.1.tar.gz

安装cloud-init

说明

Ubuntu系列直接通过以下命令完成cloud-init安装以及开机自启动配置:
apt-get install cloud-init -y

其他Linux系列安装过程,参见以下步骤。

  1. 执行以下命令,解压cloud-init安装包:
tar -zxvf cloud-init-21.1.tar.gz
  1. 执行以下命令,进入已解压的cloud-init安装包目录:
cd cloud-init-21.1
  1. 执行以下命令,安装cloud-init:
  • CentOS 7/8系列(除7.2、7.3外)
pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
python3 setup.py build
python3 setup.py install --init-system systemd
cd ..
ln -s /usr/local/bin/cloud-init /usr/bin/cloud-init
ln -s /usr/local/bin/cloud-init-per /usr/bin/cloud-init-per
ln -s /usr/local/bin/cloud-id /usr/bin/cloud-id
  • CentOS 7.2/7.3
pip3 install MarkupSafe jinja2 oauthlib six configobj pyyaml urllib3 idna certifi charset-normalizer requests jsonpointer jsonpatch attrs pyrsistent zipp typing-extensions importlib-metadata jsonschema -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn

python3 setup.py build
python3 setup.py install --init-system systemd
cd ..
ln -s /usr/local/bin/cloud-init /usr/bin/cloud-init
ln -s /usr/local/bin/cloud-init-per /usr/bin/cloud-init-per
ln -s /usr/local/bin/cloud-id /usr/bin/cloud-id
  • Debian系列
pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install six==1.16.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
python3 setup.py build
python3 setup.py install --init-system systemd
cd ..
ln -s /usr/local/python3/bin/cloud-init /usr/local/bin/cloud-init
ln -s /usr/local/python3/bin/cloud-init /usr/bin/cloud-init
ln -s /usr/local/python3/bin/cloud-init-per /usr/local/bin/cloud-init-per
ln -s /usr/local/python3/bin/cloud-init-per /usr/bin/cloud-init-per
ln -s /usr/local/python3/bin/cloud-id /usr/local/bin/cloud-id
ln -s /usr/local/python3/bin/cloud-id /usr/bin/cloud-id
  1. 执行以下命令,使cloud-init开机自启动:
  • CentOS 7/8和Debian系列
systemctl enable cloud-init-local.service
systemctl enable cloud-init.service
systemctl enable cloud-config.service
systemctl enable cloud-final.service
  1. 执行以下命令,查看cloud-init版本,验证是否安装成功:
cloud-init --version

修改cloud-init配置

执行以下命令修改cloud-init配置,使其符合平台规范。

CentOS 7/8系列,执行以下命令:

cat > /etc/cloud/cloud.cfg.d/90_dpkg.cfg << EOF
datasource_list: [ ConfigDrive, NoCloud, None ]
EOF

    cat > /etc/cloud/cloud.cfg << EOF
users:
 - default

disable_root: 0
ssh_pwauth:   1
growpart:
    mode: auto
    devices: ['/']
    ignore_growroot_disabled: false

mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys:   1
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:
  distro: rhel
  paths:
    cloud_dir: /var/lib/cloud
    templates_dir: /etc/cloud/templates
  ssh_svcname: sshd

EOF

Ubuntu系列,执行以下命令:

cat > /etc/cloud/cloud.cfg.d/90_dpkg.cfg << EOF
datasource_list: [ ConfigDrive, NoCloud, None ]
EOF

    cat > /etc/cloud/cloud.cfg << EOF
users:
 - default

disable_root: 0
ssh_pwauth:   1
growpart:
    mode: auto
    devices: ['/']
    ignore_growroot_disabled: false

mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys:   1
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:
  paths:
    cloud_dir: /var/lib/cloud
    templates_dir: /etc/cloud/templates
  ssh_svcname: sshd

EOF

Debian系列,执行以下命令:

cat > /etc/cloud/cloud.cfg.d/90_dpkg.cfg << EOF
datasource_list: [ ConfigDrive, NoCloud, None ]
EOF

    cat > /etc/cloud/cloud.cfg << EOF
users:
 - default

disable_root: true
ssh_pwauth:   1
growpart:
    mode: auto
    devices: ['/']
    ignore_growroot_disabled: false

mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys:   1
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:
  distro: debian
  paths:
    cloud_dir: /var/lib/cloud
    templates_dir: /etc/cloud/templates
  ssh_svcname: sshd
EOF