zoukankan      html  css  js  c++  java
  • 使用ISO文件制作openstack使用的coreOS镜像

    OpenStack源码交流群: 538850354

    本篇文章是使用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.ibm.com/developerworks/cn/linux/1407_liuming_init3/index.html

    新建一个开机启动配置文件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
  • 相关阅读:
    【深度学习Deep Learning】资料大全
    在谷歌中缓存下载视频离线观看,js代码
    asp.net mvc 中Html.ValidationSummary显示html
    asp.net网站访问时不能显示页面
    k8s install kubeadm网络原因访问不了谷哥and gpg: no valid OpenPGP data found. 解决办法
    火绒杀毒软件更安静
    Linux使用mount挂载Windows共享文件夹
    spark学习
    https://blog.csdn.net/tangdong3415/article/details/53432166
    正则表达式
  • 原文地址:https://www.cnblogs.com/jython/p/4225287.html
Copyright © 2011-2022 走看看