Sometimes we want to be able to configure the virtual machine when the boot virtual machine is configured, such as configuring the network, writing files, downloading some packages and installing, and so on, OpenStack provides a way to implement these, that is user-data and cloud-init.
User-data
Before talking about User-data, first say Nova's metadata API, the so-called metadata is about the virtual machine metadata, providing this API is mainly to be able to implement some information initialization when the virtual machine is started.
Using the Curl command inside a virtual machine, you can get the following results, which are expressed in different versions, each containing Meta-data and user-data, and Meta-data are various basic information including virtual machines, such as IP, disk, memory, hostname,public Key, security group and other settings, User-data is mainly to do some parameter settings, and some scripts, such as Python script, when using heat boot virtual machine, you can see such user-data example, User-data is able to achieve instance personalized customization of the foundation , it has a lot of written format, look at Cloud-init chapters in detail.
[HTML]View PlainCopy
- $ Curl 169.254.169.254
- 1.0
- 2007-01-19
- 2007-03-01
- 2007-08-29
- 2007-10-10
- 2007-12-15
- 2008-02-01
- 2008-09-01
- 2009-04-04
- Latest
For information about Metadata, you can also look at EC2 documentation Instance Metadata and User Data
Cloud-init
When the data is ready, the final action must be cloud-init, and it will be able to execute the data at boot of the virtual machine. See Ubuntu Cloudinit for Cloud-init related introduction and the User-data format you use.
The next step is to actually use these techniques to do a test.
Guaranteed connectivity to the Nova metadata API
Back to opnestack itself, to use the user-data to ensure that you can connect to the Nova metadata API IP address within the created virtual machine, metadata app IP inherits the 169.254.169.254 used by Amazon. In the OpenStack production environment, we do not have this IP, we need to use the following command to route to the 169.254.169.254 request to the Nova metadata API actual listening IP and port, as shown below
[HTML]View PlainCopy
- sudo iptables-t nat-a prerouting-d 169.254.169.254/32-p tcp-m tcp--dport 80-j DNAT--to-destination 10.11.0.16:8775
10.11.0.16 is metadata API listen IP address
Note Two related configuration items
/etc/nova/nova.conf
[HTML]View PlainCopy
- metadata_listen=10.11.0.16
/etc/neutron/metadata_agent.ini
[HTML]View PlainCopy
- NOVA_METADATA_IP = 10.11.0.16
Nova boot: Upload user-data, and cloud-init inject data
Write the script we need, parameter configuration, and so on to the text file, upload it to the Nova metadata server via--user-data, and then inject the data using the mirrored Cloud init, which is the syntax in Nova:
[HTML]View PlainCopy
- Nova Boot--user-data/filepath/filename ...
First look for an image with the Cloud init package installed, this article using Fedora 20, can be downloaded in http://cloud.fedoraproject.org/,
First step: glance Create image
[HTML]View PlainCopy
- Glance image-create--name F20 --disk-format=qcow2 --container-format=bare --is-public=True --file=fedora-x86_64-20-20131211.1-sda.qcow2
Step Two: Create the User-data file
The following is an example of User-data, for a script, other formats see references at the end of the article
[Plain]View PlainCopy
- #!/bin/bash
- echo "One Test about user data" >>userdata
- chmod 777 UserData
- Useradd-m Me
- Echo-e ' me\tall= (All) \tnopasswd:all ' >>/etc/sudoers
Step three: Boot virtual machine, injection of initialization data
Note: In this example, the--key-name parameter is used to pass in the public key, implementing no password ssh, the user name is fedora, in this case Root_key is a keypair, added with Nova Keypair-add.
[HTML]View PlainCopy
- Nova boot--key-name root_key--user-data/user-data.txt--flavor 2--image aad51d83-6398-4d18-89c8-5302993363b5 test_f 20
Fourth step: SSH into the virtual machine for verification
[Plain]View PlainCopy
- [[email protected] ~]# ssh [email protected]
- Last Login:thu Apr 3 02:07:24 from 10.20.1.3
- [Email protected] ~]$ Curl 169.254.169.254/latest/user-data
- #!/bin/bash
- echo "One Test about user data" >>userdata
- chmod 777 UserData
- Useradd-m Me
- Echo-e ' me\tall= (All) \tnopasswd:all ' >>/etc/sudoers
- [Email protected] ~]$ Cat/userdata
- One Test about user data
- [Email protected] ~]$ Ll/userdata
- -rwxrwxrwx. 1 root root Apr 3 02:07/userdata
- [email protected] ~]$ su me
- Password:
You can also check the/etc/sudoers
Other related references:
Supported formats for User-data: Https://help.ubuntu.com/community/CloudInit
openstack resolves a non-UEC mirrored virtual machine Cloud-init does not work automatically modifying the host name cannot be injected into user data
The following article describes how to install the Cloud init package using Cirros as an example http://eccp.csdb.cn/blog/?p=68
File/meta/user-data Injecting Data http://docs.openstack.org/grizzly/openstack-compute/admin/content/instance-data.html# Inserting_sshkeys
Resources:
Http://cloudinit.readthedocs.io/:http://cloudinit.readthedocs.io/en/latest/index.html
Cloud Platform OpenStack, cloudinit installation using: http://www.it165.net/os/html/201404/7848.html
Image root created with Cloud-init cannot log on: http://linuxfun.me/?p=1552
Basic Hardening with User data/cloud-init:http://openstack.prov12n.com/basic-hardening-with-user-data-cloud-init/
OpenStack Nova: Virtual machine initialization User-data & cloud-init:http://blog.csdn.net/juvxiao/article/details/22664457
Cloudinit:https://help.ubuntu.com/community/cloudinit
openstack resolves a non-UEC mirrored virtual machine Cloud-init does not work automatically modifying the host name cannot be injected into user data:http://blog.csdn.net/networm3/article/details/8559504
"OpenStack Virtual machine initialization User-data & Cloud-init"