歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 使用ISO文件制作OpenStack使用的CoreOS鏡像

使用ISO文件制作OpenStack使用的CoreOS鏡像

日期:2017/2/28 14:01:52   编辑:Linux教程

本篇文章是使用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的介紹: http://www.linuxidc.com/Linux/2014-12/110383.htm

新建一個開機啟動配置文件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實例教程 http://www.linuxidc.com/Linux/2014-07/104806.htm

服務器操作系統CoreOS初體驗 http://www.linuxidc.com/Linux/2014-07/104807.htm

CoreOS 實戰:剖析 etcd http://www.linuxidc.com/Linux/2014-11/109725.htm

CoreOS 實戰:CoreOS 及管理工具介紹 http://www.linuxidc.com/Linux/2014-11/109728.htm

[教程]在 CoreOS 上構建你的第一個應用 http://www.linuxidc.com/Linux/2014-12/110799.htm

CoreOS 的詳細介紹:請點這裡
CoreOS 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved