使用ISO檔案製作OpenStack使用的CoreOS鏡像

來源:互聯網
上載者:User

使用ISO檔案製作OpenStack使用的CoreOS鏡像

本篇文章是使用CoreOS ISO檔案手動製作openstack使用的qcow2鏡像檔案,關於CoreOS的介紹,可以看這裡

使用伺服器:CentOS6.5

1.下載CoreOS鏡像(444.5.0版本)

可能需要FQ

#coreOS安裝檔案
#下面兩個檔案在安裝過程中,coreOS會自動下載,但由於網路的原因,下載可能很耗時,所以這裡提前下載好(可能需要使用代理才能下載)
wget http://stable.release.core-os.net/amd64-usr/444.5.0/coreos_production_image.bin.bz2
wget http://stable.release.core-os.net/amd64-usr/444.5.0/coreos_production_image.bin.bz2.sig

#iso鏡像檔案
#使用這個ISO檔案製作鏡像
wget http://stable.release.core-os.net/amd64-usr/444.5.0/coreos_production_iso_image.iso

 

 

2.建立虛擬磁碟

#安裝coreOS虛擬機器到這塊磁碟上
qemu-img create -f qcow2 coreOS_v1.qcow2 20G
#在之後步驟中我們會使用下載的ISO檔案安裝虛擬機器到這塊磁碟上,然後再經過自訂配置,這塊qcow2格式的虛擬磁碟就是我們最終需要的虛擬機器鏡像

 

3.使用virt-install工具從ISO檔案安裝(從光碟片引導系統)

 

virt-install -n core -r 1024 -c /data_lij/coreOS/coreos_production_iso_image.iso --disk path=/data/coreOS/coreos_test.qcow2,device=disk,bus=virtio,size=5,format=qcow2 --vnc --vncport=5900 --vnclisten=0.0.0.0 -v

-n:虛擬機器名稱
-r:分配記憶體
-c:使用的ISO檔案
--disk:安裝磁碟
-vnc:使用vnc遠端連線
--vncport:vnc用戶端訪問連接埠

 

這裡使用到了virt-install工具,以及vnc遠端連線

 

4.設定cloud-config


cloud-config介紹:

CoreOS allows you to declaratively customize various OS-level items, such as network configuration, user accounts, and systemd units. This document describes the full list of items we can configure. The coreos-cloudinit program uses these files as it configures the OS after startup or during runtime.

Your cloud-config is processed during each boot. Invalid cloud-config won't be processed but will be logged in the journal. You can validate your cloud-config with the CoreOS validator or by running coreos-cloudinit -validate

由於coreOS預設使用密鑰登陸,所以我們必須想辦法注入一個公開金鑰到虛擬機器中,這樣製作出來的鏡像我們才可以使用密鑰訪問

最簡單的cloud-config.yaml只包括一個ssh_authorized_keys欄位,用於密鑰注入

建立一個cloud-config.yaml檔案,ssh-rsa 後面粘貼需要注入的公開金鑰

shell> cat cloud-config.yaml
#cloud-config

ssh_authorized_keys:
  - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0g+ZTxC7weoIJLUafOgrm+h...

 

5.安裝coreOS到虛擬磁碟

用戶端使用vnc viewer工具串連虛擬機器,當前啟動並執行系統是我們下載的ISO鏡像coreos_production_iso_image.iso,不同於centos可以直接安裝系統到磁碟,對於coreOS,我們需要使用這個ISO提供的安裝工具coreos-install去安裝coreOS到磁碟

注意:執行下面的命令後coreos-install會自動下載安裝程式,但是很容易下載出錯,最好使用第6步中的本地下載方法

 

coreos-install -d /dev/vda -C stable -V 444.5.0 -c cloud-config.yaml

#-d:參數指定安裝磁碟,這裡指第二步建立的虛擬磁碟
#-C:使用版本,stable穩定版
#-V:要安裝的coreOS系統版本,coreos-install會根據這裡指定的版本去官網下載相應版本安裝程式
#-c:指定一個啟動後可以執行的cloud-config設定檔,用於注入密鑰

 

 

6.使用本地安裝檔案

執行上一步的安裝命令後,coreos-install會自動調用下面命令下載所需安裝檔案,並自動安裝

wget http://stable.release.core-os.net/amd64-usr/444.5.0/coreos_production_image.bin.bz2
wget http://stable.release.core-os.net/amd64-usr/444.5.0/coreos_production_image.bin.bz2.sig

由於網路的原因,下載可能不會成功,或者下載很慢,我們可以設定讓coreos-install從本地地址下載代替從官網下載,從而節省時間

所有我們需要做的就是把這個地址:http://stable.release.core-os.net/amd64-usr/444.5.0 用本地可以訪問的地址替換(自建web伺服器)

6.1 首先需要設定解析,使stable.release.core-os.net解析為本地IP

echo "192.168.11.166 stable.release.core-os.net" >> /etc/hosts

6.2 下面配置一台web主機代替官網地址

 


#我們需要另外使用一台虛擬機器,在其上搭建一個web伺服器,替代http://stable.release.core-os.net/amd64-usr/444.5.0這個地址,假設其IP為192.168.11.166

#建立目錄結構

mkdir /data/coreos/amd64-usr/444.5.0 -p
cd /data/coreos/amd64-usr/444.5.0

#複製第一步下載的安裝檔案到本目錄
cp coreos_production_image.bin.bz2 coreos_production_image.bin.bz2.sig .

#進入/data/coreos目錄,使用python啟動一個http服務,其根目錄為python運行目錄
cd /data/coreos
python -m SimpleHTTPServer 80(這個命令建立一個web伺服器,並把命令運行目錄作為web根目錄)

 

6.3 測試web伺服器

經過上面兩步,現在在伺服器上訪問http://stable.release.core-os.net/amd64-usr/444.5.0,會被解析到我們自建的http主機(192.168.11.166)上去

6.4 現在coreos-install可以使用本地安裝檔案了,重新執行下面命令安裝虛擬機器

coreos-install -d /dev/vda -C stable -V 444.5.0 -c cloud-config.yaml

 

7.開機啟動指令碼cloud-init

安裝完成之後,為了使其可以從openstack擷取主機名稱,密鑰等,需要添加一個開機啟動指令碼

建立cloudinit.sh

 

#!/bin/bash

#get the env
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin

STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://169.254.169.254/latest`
if [ ! "$STATUS_CODE" -eq "200" ]; then
    /bin/sleep 3
fi

# set the root password using user data
STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://169.254.169.254/latest/user-data`
if [ "$STATUS_CODE" -eq "200" ]; then
    PASS=`curl -m 10 -s http://169.254.169.254/latest/user-data | awk -F '"' '{for(i=1;i<=NF;i++){if($i ~ /password/) print $(i+2)}}'`
    if [ "$PASS" != " " ]; then
    /usr/bin/echo "root:${PASS}" > tmp.txt
    /usr/sbin/chpasswd < tmp.txt
    rm -f tmp.txt
    fi
fi

# set the hostname using the meta-data service
STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://169.254.169.254/latest/meta-data/hostname`
if [ "$STATUS_CODE" -eq "200" ]; then
    curl -f http://169.254.169.254/latest/meta-data/hostname > /tmp/metadata-hostname 2>/dev/null
    if [ $? -eq 0 ]; then
        TEMP_HOST=`cat /tmp/metadata-hostname | awk -F '.novalocal' '{print $1}'`
        /usr/bin/hostnamectl set-hostname ${TEMP_HOST}
        /usr/bin/hostname $TEMP_HOST
        rm -f /tmp/metadata-hostname
    fi
fi

# get the user ssh key using the meta-data service
STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key`
if [ "$STATUS_CODE" -eq "200" ]; then
    mkdir -p /root/.ssh
    /usr/bin/echo >> /root/.ssh/authorized_keys
    curl -m 10 -s http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key | grep 'ssh-rsa' >> /root/.ssh/authorized_keys
    chmod 0700 /root/.ssh
    chmod 0600 /root/.ssh/authorized_keys
fi

8.設定開機啟動

coreOS使用systemd管理啟動項,關於systemd的介紹:

建立一個開機啟動設定檔cloudinit.service

cd /etc/systemd/system
#cat cloudinit.service
[Unit]
Description=OpenStack nova
Requires=coreos-setup-environment.service
After=coreos-setup-environment.service
Before=user-config.target

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=-/etc/environment
ExecStart=/usr/bin/bash /etc/cloud-init.sh          #執行的指令檔cloud-init.sh

[Install]
WantedBy=multi-user.target

#加入systemd管理(設定開機啟動)

systemctl enable cloudinit.service

#執行enable之後,cloudinit.service這個服務就會開機啟動,從而我們的指令碼cloud-init.sh就可以執行

#驗證cloudinit.service服務是否設為開機啟動

systemctl is-enabled cloudinit

在CoreOS下部署WordPress執行個體教程  

伺服器作業系統CoreOS初體驗  

CoreOS 實戰:剖析 etcd 

CoreOS 實戰:CoreOS 及管理工具介紹 

[教程]在 CoreOS 上構建你的第一個應用 

CoreOS 的詳細介紹:請點這裡
CoreOS 的:請點這裡 

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.