Use ISO files to create CoreOS images for OpenStack

Source: Internet
Author: User
Tags test web server

Use ISO files to create CoreOS images for OpenStack

This article uses the CoreOS ISO file to manually create the qcow2 image file used by openstack. For more information about CoreOS, see here.

Servers used: CentOS6.5

1. Download The CoreOS image (version 444.5.0)

FQ may be required

# CoreOS Installation File
# CoreOS will automatically download the following two files during installation. However, due to network reasons, the download may take a long time. Therefore, download the files in advance (you may need to use a proxy to download the files)
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 Image File
# Use this ISO file to create an image
Wget http://stable.release.core-os.net/amd64-usr/444.5.0/coreos_production_iso_image.iso

 

 

2. Create a Virtual Disk

# Install coreOS Virtual Machine on this disk
Qemu-img create-f qcow2 coreOS_v1.qcow2 20G
# In the subsequent steps, we will use the downloaded ISO file to install the virtual machine to this disk, and then customize the configuration. This qcow2-format virtual disk is the final virtual machine image we need.

 

3. Use the virt-install tool to install from the ISO file (boot the system from the CD)

 

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: Virtual Machine name
-R: Memory Allocation
-C: used ISO file
-- Disk: Installs the disk.
-Vnc: vnc remote connection
-- Vncport: vnc client access port

 

The virt-install tool and vnc remote connection are used here.

 

4. Set cloud-config


Cloud-config introduction:

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

Because coreOS uses a key for login by default, we must try to inject a public key into the virtual machine so that we can use the key to access the image.

The simplest cloud-config.yaml contains only one ssh_authorized_keys field for key Injection

Create a new cloud-config.yaml file with ssh-rsa followed by the public key to be injected

Shell> cat cloud-config.yaml
# Cloud-config

Ssh_authorized_keys:
-Ssh-rsa aaaab3nzac1yc2eaaaadaqabaa109c0g + ZTxC7weoIJLUafOgrm + h...

 

5. Install coreOS to the Virtual Disk

The client uses the vnc viewer tool to connect to the virtual machine. The currently running system is the ISO image coreos_production_iso_image.iso we downloaded. Unlike centos, you can directly install the system to the disk. For coreOS, we need to use the Installation Tool coreos-install provided by this ISO to install coreOS to the disk.

Note: After executing the following command, coreos-install will automatically download the installation program, but it is easy to download errors. It is best to use the local Download Method in step 1.

 

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

#-D: The parameter specifies the disk to be installed, which is the virtual disk created in step 2.
#-C: version used, stable version
#-V: The version of The coreOS system to be installed. coreos-install will download the corresponding version installer from the Official Website Based on the specified version.
#-C: Specifies a cloud-config configuration file that can be executed after startup. It is used to inject keys.

 

 

6. Use a local Installation File

After executing the installation command in the previous step, coreos-install will automatically call the following command to download the required installation file and automatically 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

Due to network reasons, the download may not succeed, or the download is slow. We can set coreos-install to download from the local address instead of from the official website to save time.

All we need to do is replace this address: http://stable.release.core-os.net/amd64-usr/444.5.0 with a locally accessible address (self-built web Server)

6.1 first you need to set resolution so that the stable.release.core-os.net is resolved to a local IP

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

6.2 configure a web host to replace the official website address

 


# We need to use another virtual machine to build a web server on it to replace the address http://stable.release.core-os.net/amd64-usr/444.5.0and set its IP address to 192.168.11.166.

# Create a directory structure

Mkdir/data/coreos/amd64-usr/444.5.0-p
Cd/data/coreos/amd64-usr/444.5.0

# Copy the installation file downloaded in step 1 to the local directory.
Cp coreos_production_image.bin.bz2 coreos_production_image.bin.bz2.sig.

# Enter the/data/coreos directory and start an http service using python. Its root directory is the python running directory.
Cd/data/coreos
Python-m SimpleHTTPServer 80 (this command creates a web server and uses the command running directory as the web root directory)

 

6.3 test web Server

After the above two steps, visit http://stable.release.core-os.net/amd64-usr/444.5.on the server and the server will be redirected to our self-built httphost (192.168.11.166 ).

6.4 now coreos-install can use the local installation file, and re-execute the following command to install the Virtual Machine

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

 

7. Boot script cloud-init

After the installation is complete, you need to add a boot script to get the host name and key from openstack.

Create 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. Set startup

CoreOS uses systemd to manage startup items. Introduction to systemd:

Create a STARTUP configuration file cloudinit. service

Cd/etc/systemd/system
# Cat cloudinit. service
[Unit]
Description = OpenStack nova
Request = coreos-setup-environment.service
After = coreos-setup-environment.service
Beforedomainuser-config.tar get

[Service]
Type = oneshot
RemainAfterExit = yes
EnvironmentFile =-/etc/environment
ExecStart =/usr/bin/bash/etc/cloud-init.sh # Run the script file cloud-init.sh

[Install]
Wantedbypolicmulti-user.tar get

# Add to systemd Management (set startup)

Systemctl enable cloudinit. service

# After the execution of enable, cloudinit. service this service will start up, so that our script cloud-init.sh can be executed

# Verify whether the cloudinit. service is set to start at startup

Systemctl is-enabled cloudinit

How to deploy a WordPress instance in CoreOS

Initial server operating system CoreOS experience

CoreOS practice: Analyzing etcd

CoreOS practice: Introduction to CoreOS and management tools

[Tutorial] Build your first application on CoreOS

CoreOS details: click here
CoreOS: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.