操作場景
為了保證使用私有鏡像創建的新云主機可以通過“用戶數據注入”功能注入初始化自定義信息(例如為云主機設置登錄密碼),請在創建私有鏡像前安裝Cloud-Init工具。
- 安裝Cloud-Init工具時需要從官網下載并安裝,因此,需要提前為云主機綁定彈性公網IP。
- 不安裝Cloud-Init工具,將無法對云主機進行自定義配置,只能使用鏡像原有密碼登錄云主機。
- 使用公共鏡像創建的云主機,默認已經安裝Cloud-Init,不需要執行安裝及配置操作。
- 用戶導入鏡像創建的云主機,請按照指導安裝及配置Cloud-Init。配置Cloud-Init操作請參考配置Cloud-Init工具章節。
前提條件
- 已為云主機綁定彈性公網IP。
- 已登錄云主機。
- 云主機的網卡屬性為DHCP方式。
安裝步驟說明
1.請先檢查是否已安裝Cloud-Init工具。
具體操作請參考檢查是否已經安裝Cloud-Init工具。
2.安裝Cloud-Init工具。
Cloud-Init安裝方式分為:采用官方提供的包源安裝Cloud-Init工具(優先推薦)、采用官方提供的Cloud-Init源碼包通過pip方式安裝Cloud-Init工具和采用源碼編譯安裝方法。
檢查是否已經安裝Cloud-Init工具
請先執行如下步驟檢查是否已安裝Cloud-Init工具。
在不同的操作系統下,查看是否已經安裝Cloud-Init工具的方法不同,以CentOS 6系列為例,執行以下命令查看是否安裝Cloud-Init工具。
rpm -qa |grep cloud-init
回顯類似如下,表示已經安裝Cloud-Init工具,無需重復安裝。
cloud-init-0.7.5-10.el6.centos.2.x86_64
無回顯信息表示未安裝Cloud-Init工具。
說明:
由于Cloud-Init工具存在多種安裝方法,如果無回顯信息,請再次使用rpm -qa |grep cloud-init命令進行檢查。which cloud-init和rpm -qa |grep cloud-init任何一個命令顯示已安裝,表示操作系統已安裝Cloud-Init工具。
如果已安裝Cloud-Init工具,還需要執行以下操作:
- 請確認當前云主機操作系統中的證書是否繼續使用。如果不再使用該證書,請刪除證書。
?root用戶對應目錄下的文件(如“/path/to/$root/.ssh/authorized_keys”),執行以下命令:
cd /root/.ssh
rm authorized_keys
?非root用戶對應目錄下的證書文件(如“/path/to/$none-root/.ssh/authorized_keys”),執行以下命令:
cd /home/centos/.ssh
rm authorized_keys
- 執行以下命令,清除Cloud-Init工具產生的緩存,確保使用該私有鏡像創建的云主機可以使用證書方式登錄。
sudo rm -rf /var/lib/cloud/*
說明:
設置完成后請勿重啟云主機,否則,需重新設置。
采用官方提供的包源安裝Cloud-Init工具(優先推薦)
在不同操作系統的云主機上安裝Cloud-Init工具的方法不同,請在root用戶下執行相關安裝操作。
以下將介紹SUSE、CentOS、Fedora、Debian和Ubuntu操作系統安裝Cloud-Init工具的具體方法。其他操作系統類型,請安裝并配置對應類型的Cloud-Init工具,例如,使用CoreOS操作系統時需安裝并配置coreos-cloudinit。
- SUSE操作系統
SUSE操作系統的Cloud-Init網絡安裝地址:
//ftp5.gwdg.de/pub/opensuse/repositories/Cloud:/Tools/
說明:
在上述提供的網絡安裝地址下選擇相應操作系統版本的repo安裝包進行安裝。
以SUSE Enterprise Linux Server 12為例,Cloud-Init工具的安裝步驟如下。
a.登錄到創建Linux私有鏡像所使用的云主機。
b.執行以下命令,安裝SUSE 12的網絡安裝源。
zypper ar //ftp5.gwdg.de/pub/opensuse/repositories/Cloud:/Tools/SLE_12_SP3/Cloud:Tools.repo
c.執行以下命令,更新網絡安裝源。
zypper refresh
d.執行以下命令,安裝Cloud-Init。
zypper install cloud-init
e.執行以下命令,設置Cloud-Init為開機自啟動服務。
- SUSE 11:
chkconfig cloud-init-local on; chkconfig cloud-init on; chkconfig cloud-config on; chkconfig cloud-final on
service cloud-init-local status; service cloud-init status; service cloud-config status; service cloud-final status
- SUSE 12以及openSUSE 12/13/42:
systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
注意:
對于SUSE和openSUSE操作系統,請執行以下步驟禁止動態修改云主機名稱。
1.執行以下命令,使用vi編輯器打開“dhcp”文件。
vi etc/sysconfig/network/dhcp
2.將“dhcp”文件中的“DHCLIENT_SET_HOSTNAME”的值修改為“no”。
- CentOS操作系統
CentOS操作系統的Cloud-Init網絡安裝地址如下表所示。請在提供的網絡安裝地址下選擇相應的epel-release安裝包進行安裝。
表 Cloud-Init網絡安裝地址
操作系統類型 版本 網絡安裝地址 CentOS 6系列32位 6系列64位 7系列64位
1、執行以下命令安裝Cloud-Init:
yum install 網絡安裝地址/epel-release-x-y.noarch.rpm
yum install cloud-init
說明:
“網絡安裝地址”表示Cloud-Init對應的epel-release安裝包的地址,“x-y”表示當前操作系統對應的Cloud-Init epel-release版本號,執行命令時需參考表1以及實際使用的安裝包版本進行替換。
以CentOS 6系列64位為例,當前版本號為6.8,則命令修改為:
yum install //archives.fedoraproject.org/pub/archive/epel/6/x86_64/epel-release-6-8.noarch.rpm
以CentOS 7系列64位為例,當前版本號為7.14。則命令修改為:
yum install //archives.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-14.noarch.rpm
2、執行以下命令,設置Cloud-Init為開機自啟動服務。
systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
- Fedora操作系統
安裝Cloud-Init前,請確認操作系統已經配置好對應的網絡安裝源地址,請查看“/etc/yum.repo.d/fedora.repo”文件中是否已配置相關軟件包安裝源地址,如果沒有配置相關地址源,請參考相關Fedora官網信息配置軟件包安裝源。
1、執行以下命令,安裝Cloud-Init。
yum install cloud-init
2、執行以下命令,設置Cloud-Init為開機自啟動服務。
systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
- Debian和Ubuntu操作系統
安裝Cloud-Init前,請確認操作系統已經配置好對應的網絡安裝源地址,請查看“/etc/apt/sources.list”文件中是否已配置相關軟件包安裝源地址,如果沒有配置相關地址源,請參考Debian或者Ubuntu官網信息配置軟件包安裝源。
1、執行以下命令,安裝Cloud-Init。
apt-get update
apt-get install cloud-init
2、執行以下命令,設置Cloud-Init為開機自啟動服務。
systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
采用官方提供的Cloud-Init源碼包通過pip方式安裝Cloud-Init工具
以cloud-init-0.7.9版本為例,Cloud-Init工具的安裝步驟如下。
1.下載cloud-init-0.7.9.tar.gz源碼包(推薦優先選用0.7.9版本),上傳到云主機指定目錄“/home/”下。
cloud-init-0.7.9.tar.gz源碼包下載地址:
//launchpad.net/cloud-init/trunk/0.7.9/+download/cloud-init-0.7.9.tar.gz
2.在“~/.pip/”目錄下新建pip.conf文件,編輯內容如下。
說明:
“~/.pip/”若不存在,可使用命令mkdir ~/.pip命令新建。
[global]
index-url = //<$mirror>/simple/
trusted-host = <$mirror>
說明:
編輯內容中<$mirror>部分可以選擇公網PyPI源或教育網PyPI源進行替換。
- 公網PyPI源://pypi.python.org/
- 教育網PyPI源://pypi.tuna.tsinghua.edu.cn/
//pypi.mirrors.ustc.edu.cn/
3.執行以下命令,安裝本地下載的Cloud-Init源碼包,安裝過程中根據需要選擇--upgrade參數。
pip install [--upgrade] /home/cloud-init-0.7.9.tar.gz
4.執行命令cloud-init -v,如回顯如下類似信息表示安裝Cloud-Init成功。
cloud-init 0.7.9
5.設置Cloud-Init相關服務為開機自啟動。
?若操作系統是sysvinit自啟動管理服務,則執行以下命令進行設置。
chkconfig --add cloud-init-local; chkconfig --add cloud-init; chkconfig --add cloud-config; chkconfig --add cloud-final
chkconfig cloud-init-local on; chkconfig cloud-init on; chkconfig cloud-config on; chkconfig cloud-final on
service cloud-init-local status; service cloud-init status; service cloud-config status; service cloud-final status
?若操作系統是systemd自啟動管理服務,則執行以下命令進行設置。
systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
注意:
采用官方提供的Cloud-Init源碼包通過pip方式進行安裝時要注意以下兩點。
1.Cloud-Init安裝時需要添加syslog用戶到adm組。存在syslog用戶時直接添加syslog用戶到adm組。不存在syslog用戶時(如CentOS和SUSE),執行下列命令創建syslog用戶,添加到adm組:
useradd syslog
groupadd adm
usermod -g adm syslog
2.在“/etc/cloud/cloud.cfg”中system_info部分的distro要根據具體操作系統發行版本做相應修改(如根據具體操作系統發行版相應修改為:distro: ubuntu,distro: sles,distro: debian,distro: fedora)。
采用源碼編譯安裝方法
由于Cloud-Init配置的相關內容已在源碼包編譯完成,執行以下操作步驟安裝Cloud-Init成功即可,無需執行Cloud-Init配置操作。源碼編譯安裝方法的Cloud-Init工具Github開源地址://github.com/canonical/cloud-init/
1.執行以下命令,下載Cloud-Init壓縮包,并將其復制至新建的“/tmp/CLOUD-INIT”文件夾。
說明:
0.7.6版本安裝包下載地址://github.com/canonical/cloud-init/archive/refs/tags/0.7.6.zip
0.7.9版本安裝包下載地址:
Cloud-Init所有版本安裝包下載地址://github.com/canonical/cloud-init/releases
wget //github.com/canonical/cloud-init/archive/refs/tags/0.7.6.zip
mkdir /tmp/CLOUD-INIT
cp cloud-init-0.7.6.zip /tmp/CLOUD-INIT
cd /tmp/CLOUD-INIT
2.執行如下命令,解壓Cloud-Init壓縮包。
unzip cloud-init-0.7.6.zip
3.執行如下命令進入cloud-init-0.7.6文件夾。
cd cloud-init-0.7.6
4.按照操作系統類型,執行相應的命令安裝Cloud-Init安裝包。
? CentOS6.x/SUSE11.x:
python setup.py build
python setup.py install --init-system sysvinit
? CentOS7.x/SUSE12.x:
python setup.py build
python setup.py install --init-system systemd
Cloud-Init安裝時需要添加syslog用戶到adm組。存在syslog用戶時直接添加syslog用戶到adm組。不存在syslog用戶時(如CentOS和SUSE),執行下列命令創建syslog用戶,添加到adm組:
useradd syslog
groupadd adm
usermod -g adm syslog
5.設置Cloud-Init相關服務為開機自啟動。
?若操作系統是sysvinit自啟動管理服務,則執行以下命令進行設置。
chkconfig --add cloud-init-local; chkconfig --add cloud-init; chkconfig --add cloud-config; chkconfig --add cloud-final
chkconfig cloud-init-local on; chkconfig cloud-init on; chkconfig cloud-config on; chkconfig cloud-final on
service cloud-init-local status; service cloud-init status; service cloud-config status; service cloud-final status
?若操作系統是systemd自啟動管理服務,則執行以下命令進行設置。
systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
systemctl status cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service
6.執行如下命令檢查Cloud-Init是否安裝成功。
cloud-init -v
cloud-init init --local
回顯如下類似信息所示表示安裝Cloud-Init成功。
cloud-init 0.7.6