1. 程式人生 > >搭建kvm環境,及批量自動化部署

搭建kvm環境,及批量自動化部署

qemu first ont ces proto to do 技術 def 實驗

本實驗使用兩臺主機:
10.0.91.8 作為虛擬機的宿主機
10.0.91.10 配置httpd服務,提供10.0.91.8安裝虛擬機要使用的鏡像及自動應答文件kickstart

主機環境:

[root@master ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@master ~]# uname -r
3.10.0-862.11.6.el7.x86_64

10.0.91.8主機上執行如下操作:

1、搭建kvm環境

1.1 linux從內核版本2.6.20之後,默認安裝kvm模塊,因此不需要安裝kvm模塊


1.2 安裝qemu工具

linux上自帶的qemu版本太老,最好是從官方下載最新的源碼包編譯安裝

在QEMU 1.3版本之前,QEMU和QEMU-KVM是有區別的,從2012年底GA的QEMU 1.3版本開始,兩者就完全一樣

下載頁面如下:
https://www.qemu.org/download/

這裏下載當前最新版本2.12.0,如果本地事先已經下載源碼,直接上傳即可

yum install gcc gcc-c++ autoconf automake libtool glib* zlib* pixman* virt-manager virt-install virt-viewer libvirt libvirt-client libvirt-python python-virtinst -y


tar xvf qemu-2.12.0.tar.xz

cd qemu-2.12.0

./configure #默認安裝路徑為/usr/local

make -j 4 #這裏指定編譯時使用幾個進程,設置為cpu數量

make install

which qemu-system-x86_64

qemu-system-x86_64 -version

為了使用上的便利,創建如下鏈接:
ln -s /usr/local/bin/qemu-system-x86_64 /usr/bin/qemu-kvm

qemu-kvm -version
systemctl status libvirtd
systemctl start libvirtd
systemctl enable libvirtd
systemctl status libvirtd

chgrp kvm /dev/kvm

sed -n ‘/#user = "root"/s/#user = "root"/user = "root"/p‘ /etc/libvirt/qemu.conf
sed -n ‘/#group = "root"/s/#group = "root"/group = "root"/p‘ /etc/libvirt/qemu.conf

systemctl restart libvirtd
systemctl status libvirtd

這裏讓虛擬機使用橋接網絡(等同於vmare中的橋接)

yum install bridge-utils -y

brctl addbr br0

cat >/etc/sysconfig/network-scripts/ifcfg-br0<<end

STP=yes
BRIDGING_OPTS=priority=32768
TYPE=Bridge
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=10.0.91.8
PREFIX=24
GATEWAY=10.0.91.1
DNS1=1.1.1.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=br0
UUID=1b77d18d-9929-4cd3-8a32-52b858315867
DEVICE=br0
ONBOOT=yes

end

cat >/etc/sysconfig/network-scripts/ifcfg-em1<<end

TYPE=Ethernet
NAME=em1
UUID=dea57870-7fb5-46da-b068-7c007a827e50
DEVICE=em1
ONBOOT=yes
BRIDGE=br0
BRIDGE_UUID=1b77d18d-9929-4cd3-8a32-52b858315867

end

/etc/init.d/network restart


10.0.91.10主機上執行如下操作:

1、安裝httpd
yum install httpd -y

systemctl start httpd
systemctl enable httpd
systemctl status httpd

mkdir /var/www/html/{os,ks}

mount /dev/sr0 /var/www/html/os

cat >> /var/www/html/ks/ks.cfg<<end
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
#url --url=http://10.0.91.10/os/
# Use graphical install
#graphical
cmdline
text
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=vda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts=‘us‘
# System language
lang en_US.UTF-8

# Network information
network --bootproto=dhcp --device=eth0 --onboot=on --ipv6=auto
#network --bootproto=static --device=eth0 --gateway=10.0.91.1 --ip=10.0.91.82 --nameserver=114.114.114.114 --netmask=255.255.255.0
network --hostname=test.com

# Root password
#rootpw --iscrypted $6$q4DFSYCTvcXUb14r$J/WTNLpNOlJbeDj3cGNM64oFMZB3sSEarJFbqNeZLK96LGG3snqE/SFMeA8lzRkM5upTaJxQgtR7K1X59dO6O1
rootpw china123
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
#user --groups=wheel --name=nick --password=$6$EOKAkARkDAe5WoLv$0ACn/K0QNrOP2cxnalhy/k1i666EMH/Hjt8R4kS2E48xZ6xTTbfebnCTkoZ40hCPXbUPE7ifdTNoWnxCGAn6s1 --iscrypted --gecos="nick"
user --name=webapp --groups=webapp --homedir=/home/webapp --password=china123 --shell=/usr/bin/bash --uid=1000

# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=vda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=vda --size=500
part / --fstype="xfs" --ondisk=vda --grow --size=1
part swap --fstype="swap" --ondisk=vda --recommended

logging --host=10.0.91.83 --port=514 --level=info
zerombr
firewall --disabled
selinux --disabled
reboot

%packages
@^minimal
@compat-libraries
@core
@debugging
@development
kexec-tools
%end


%addon com_redhat_kdump --enable --reserve-mb=‘auto‘
%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

end


在瀏覽器中訪問10.0.91.10/os(顯示鏡像中文件列表)及10.0.91.10/ks/ks.cfg(顯示ks.cfg文件內容)

技術分享圖片

技術分享圖片

在10.0.91.8上開始安裝虛擬機:

mkdir /kvm/os/ -p


qemu-img create -f qcow2 /kvm/os/vm-01.qcow2

virt-install \
--name=vm-01 \
--disk path=/kvm/os/vm-01.qcow2,device=disk,bus=virtio,perms=rw,cache=writethrough \
--graphics none \
--vcpus sockets=2,cores=2,threads=2 \
--ram=16000 \
--location="http://10.0.91.10/os" \
--network bridge=br0 \
--os-type=linux \
--os-variant=rhel7 \
--extra-args="ks=http://10.0.91.10/ks/ks.cfg console=tty0 console=ttyS0,115200n8"


如果要同時安裝臺,將上述指令包裝到for或while循環中放到腳本中,執行腳本即可

腳本示例:

cat >> auto_install.sh<<end

#!/bin/bash

for i in {01..10}
do
qemu-img create -f qcow2 /kvm/os/vm-$i.qcow2

virt-install \
--name=vm-$i \
--disk path=/kvm/os/vm-$i.qcow2,device=disk,bus=virtio,perms=rw,cache=writethrough \
--graphics none \
--vcpus sockets=2,cores=2,threads=2 \
--ram=16000 \
--location="http://10.0.91.10/os" \
--network bridge=br0 \
--os-type=linux \
--os-variant=rhel7 \
--extra-args="ks=http://10.0.91.10/ks/ks.cfg console=tty0 console=ttyS0,115200n8" &

virsh start vm-$i
done

執行腳本:
sh auto_install.sh

因為腳本安裝指令中指定了終端,有使用了後臺並發執行,這樣回導致不能再當前終端安裝過程而報錯,直接回車,之後使用串口登錄即可

[root@master ~]# virsh
Welcome to virsh, the virtualization interactive terminal.

Type: ‘help‘ for help with commands
‘quit‘ to quit

virsh # list
Id Name State
----------------------------------------------------
4 vm-01 running
8 vm-02 running
17 vm-03 running
18 vm-04 running
19 vm-05 running
20 vm-06 running
21 vm-07 running
22 vm-08 running
23 vm-09 running
24 vm-10 running

virsh # console 24 #使用串口登錄,註意數字24是每臺虛擬主機的Id
Connected to domain vm-10
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64

test login: root
Password:
[root@test ~]# #可以看到已經通過敞口登錄到虛擬機上,在虛擬節點中進行網絡及其他優化設置

搭建kvm環境,及批量自動化部署