Customize the CentOS7 image of Openstack

Article Directory

 

What to consider in OpenStack image production

Prepare a centos system virtual machine on VMware Workstation, enable VT and CPU performance counters

The CentOS-7-x86_64-Minimal-2003.iso  image is the smallest version of centos

Minimal is the official smallest centos image file, and other normal-sized iso files can also be used.

1. Install kvm, start libvirtd and set it to boot

Determine whether the computing node supports virtual machine hardware acceleration (not 0)

egrep -c '(vmx|svm)' /proc/cpuinfo
  • 1

Install kvm, start libvirtd and set it to boot

yum install libvirt virt-install qemu-kvm -y 
systemctl start libvirtd && systemctl enable libvirtd

#使用ifconfig命令,可以看到多出一个virbr0的网卡

Create a directory to store the image and upload the image of CentOS7 to this directory

mkdir -p /opt/image

#上传镜像,上传完成后开始创建虚拟机;使用centos最小版镜像
wget -c http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-2003.iso -P /opt/image

Create a 5G virtual machine hard disk

According to the size of the image, if there is a need to convert the raw format in the future, it may cause the image to occupy the space of the virtual hard disk.
Although the raw image has good performance, it does not have the functions of scaling and snapshots. It takes a lot of time to create and migrate the virtual machine. , Does not apply to the use of the actual online cloud environment. It is recommended to use qcow2.

qemu-img create -f qcow2 /tmp/centos7.qcow2 5G

#这里的大小不是占用你当前磁盘的大小而是将要制作的云镜像的系统盘大小
chmod 777 centos7.qcow2
#添加权限

2.  Create a virtual machine

virt-install --virt-type kvm \
--os-type=linux \
--os-variant centos7.0 \
--name centos7 \
--ram 1024 \
--disk /tmp/centos7.qcow2,format=qcow2 \
--cdrom /opt/image/CentOS-7-x86_64-Minimal-2003.iso \
--network network=default \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole 

#参数含义请百度
  •  

Use VNC to connect
VNC Viewer remote desktop download link

[root@kvm image]# netstat -lntup|grep 5900
tcp    0    0 0.0.0.0:5900        0.0.0.0:*        LISTEN     25986/qemu-kvm

3.  Simple tips for system installation steps

Modify the network card name to eth

选择第一个 Install CentOS 7
敲Tab键出现字时 , 接着输入 net.ifnames=0 biosdevname=0 ,然后回车

Choose Shanghai time

Partition, all capacity is given to the root partition, and only one root partition

Complete the configuration and set the root password

After the installation is complete, click reboot, the system will shut down, and will not restart. At this time, you can use commands to view the virtual machine that has just been created on the host.

[root@kvm image]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7                        shut off

#启动虚拟机
[root@kvm image]# virsh start centos7
Domain centos7 started


#删除不用的虚拟机
 virsh undefine  centos7

#停止主机:virsh destroy linux65

4. Connect to the virtual machine for configuration

After startup, connect again through the VNC tool to perform IP configuration, system optimization, install common software, configure yum source, and do the initialization function of cloud mirroring, etc.

Operations on the newly created virtual machine:

Connect via host

Install common software; configure yum source

yum install -y wget 
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum install -y mlocate lrzsz tree vim nc nmap bash-completion bash-completion-extras cowsay sl htop iotop iftop lsof net-tools sysstat unzip bc psmisc ntpdate wc telnet-server bind-utils

Turn off the firewall, turn off selinux

setenforce 0
sed -i 's#enforcing#disabled#' /etc/sysconfig/selinux
systemctl stop firewalld && systemctl disable firewalld

sshd optimized and modified the sshd.config file

[root@centos7 ~]# vim /etc/ssh/sshd_config
GSSAPIAuthentication yes
UseDNS no

Restart the network card after editing the network
. The network card configuration file in the customized image must be configured with dhcp to ensure that the ip assigned when the openstack instance is created can be obtained.
The configuration in this figure is redundant, DEFROUTE=yesdelete the third line of configuration . Otherwise, the IP cannot be obtained after using this image to create an instance

TYPE=Ethernet
BOOTPROTO=dhcp
NAME=eth0
DEVICE=eth0
ONBOOT=yes

This picture shows the modified configuration file of the mirrored network card


If you need to install the graphical interface, please see the link below
https://www.jianshu.com/p/bdd29c1e1dd9

5. The use and configuration of cloud-init initialization※

Last article: Cloud server cloud-init initialization tool use and configuration
cloud-init official document
In the cloud platform, when creating a cloud host, I hope to be able to perform some initialization operations on the host, such as configuring ip, host name, password, ssh login Wait. Cloud-init can be used for initialization on openstack.

It is recommended to read the official cloud-init documentation and read it several times to make sure you know what is being configured, how these configuration items take effect, and what is the frequency of the module.

To achieve the initial configuration in the cloud host, the cloud host must first obtain the configuration data entered by the user, and read it in the cdrom mode in cloud-init, that is, openstack packages the user data into iso and mounts it to the cloud host. After the host is started, the internal cloud-init reads data from the cdrom by default, and then performs the initialization operation.

The module frequency of the Users and Groups module is executed once for each instance, which means that after the created image has the cloud host, modifying the module configuration and restarting the machine will no longer take effect.
Similar to the module frequency of Users and Groups module, there are Write_files and Set Passwords modules.
The module frequency of the Bootcmd module is that each instance can be executed multiple times, that is to say, after starting the cloud host, modifying the module configuration and restarting the machine will also take effect.
The module frequency of Bootcmd module is similar to Runcmd.

After the setting is complete, shut down the virtual machine and prepare for the next stage

history -c
shutdown -h now
  • 6. Upload the centos image to openstack

If docking ceph storage. You can convert the qcow2 format to a raw format image and upload
Ceph. The disadvantage is that it does not support booting from the qcow2 format image, so you need to convert the raw format image.

[root@kvm image]# ll -h /tmp/centos7.qcow2 
-rw-r--r--. 1 root root 1.6G May 20 17:27 /tmp/centos7.qcow2

#将镜像从qcow2格式转换为raw格式;并下载到本地 【可选】
[root@kvm image]# qemu-img convert -f qcow2 -O raw /tmp/centos7.qcow2 CentOS-7-x86_64-Minimal-2003.raw

Upload the image to the glance node of openstack (on the controller node)

openstack image create --container-format bare --disk-format raw --file /server/tools/CentOS-7-x86_64-Minimal-2003.raw --unprotected --public CentOS-7-x86_64-Minimal-2003


实际中使用的命令
openstack image create --container-format bare --disk-format raw --file /root/w_trove_pg_11_16.raw  --public  w_trove_pg_11_16.raw 
#从QEMU中检索块设备映像信息
[root@controller tools]# qemu-img info CentOS-7-x86_64-Minimal-2003.raw
image: CentOS-7-x86_64-Minimal-2003.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 5.0G

View the data of the mirror and glance pool on ceph

rbd ls images
e7439786-1143-43fe-a509-f84d666c3fbf

rbd info images/e7439786-1143-43fe-a509-f84d666c3fbf

Note: Modify the host name and IP after logging in to avoid duplication in production. In addition, you can write a script directly when creating the image, or you can configure the cloud-init initialization service in the image; after the new cloud host is completed, directly change The host name and IP address
configure the tenant's own network, and a floating IP is added

[root@controller ~]# openstack server list
+--------------------------------------+------------+--------+----------------------------------------+-------+--------+
| ID                                   | Name       | Status | Networks                               | Image | Flavor |
+--------------------------------------+------------+--------+----------------------------------------+-------+--------+
| e5891fc5-63ca-43a8-bb92-bb512c1ec7e3 | K8s-master | ACTIVE | selfservice=172.18.1.33, 192.168.0.205 |       | CentOS |
+--------------------------------------+------------+--------+----------------------------------------+-------+--------+

#镜像中的cloud-init已经配置了controller节点的ssh免秘钥登陆
[root@controller ~]# ssh [email protected]
Last login: Fri May 21 17:17:24 2020 from 192.168.0.10
[root@k8s-master ~]#

Create the instance environment you need

Guess you like

Origin blog.csdn.net/qq_42533216/article/details/113564734