1. 程式人生 > >單磁碟(根和資料放在不同分割槽)建立第一臺kvm虛擬機器、克隆虛擬機器

單磁碟(根和資料放在不同分割槽)建立第一臺kvm虛擬機器、克隆虛擬機器

一,建立第一臺虛擬機器( 此步是在宿主機系統上進行的操作; 目的是作為克隆虛擬機器的模板): 1,虛擬機器磁碟規劃 磁碟A:大小260G 分割槽規劃,swap分割槽2048M,boot分割槽300M,根分割槽60G(lvm+xfs),資料分割槽(lvm+xfs)將剩餘的空間建立擴充套件分割槽,再建立邏輯分割槽,初步為150G,剩餘空間備用; 2,建立磁碟映像檔案 # qemu-img create -f qcow2 -o size=260G /data/kvm/imgs/template-centos7-3-1611.img 3,virt-install建立第一臺kvm虛擬機器:
# virt-install \ --connect qemu:///system \ --virt-type kvm \ --name "template" \ --metadata title="template1" \ --memory 2048,maxmemory=4096 \ --vcpus 4,maxvcpus=8 \ --disk path=/data/kvm/imgs/template-centos7-3-1611.img,bus=virtio,format=qcow2,size=100,sparse,cache=none \ --location=/data/kvm/os/CentOS-7-x86_64-DVD-1611.iso \ --network bridge=br0,model=virtio \ --os-type=linux \ --os-variant=rhel7 \ --graphics vnc \ --autostart \ --accelerate \ --noautoconsole \ --force 開始安裝...... 搜尋檔案 .treeinfo...... | 366 B 00:00:00 搜尋檔案 vmlinuz...... | 5.1 MB 00:00:00 搜尋檔案 initrd.img...... | 41 MB 00:00:00 建立域...... | 0 B 00:00:00 域安裝仍在進行。您可以重新連線 到控制檯以便完成安裝程序。 引數說明:
--name:必不可少,虛擬機器例項名稱,virsh可以通過虛擬機器例項的執行ID號及該名稱對例項程序操作; --metadata:title選項是虛擬機器例項的簡短描述資訊,可自行定義,通過virsh list --title進行檢視; --memeory:設定maxmemory,目的是後續可以對虛擬機器的記憶體進行熱擴容; --vcpus:設定maxvcpus,目的是後續可以對虛擬機器的VCPU數量進行熱擴容; 4,虛擬機器作業系統說明 guest OS發行版:CentOS Linux release 7.3.1611 (Core) guest OS核心版本:3.10.0-514.el7.x86_64 5,使用virt-manager+xmanager來圖形化介面安裝作業系統
1)在遠端宿主機上配置變數如下: # LANG=en_US.utf8 # export DISPLAY='192.168.13.105:0.0' 2)在本地電腦安裝了xmamager,並且配置xshell如下,通過xshell連線的遠端宿主機: 3)啟動xmanager-passive

4)然後,在遠端宿主機上執行virt-manager命令,會在本地開啟圖形化管理介面來安裝作業系統 # virt-manager !!!提示:若本地窗口出現亂碼,則需要在遠端宿主機上執行yum -y install xorg-x11-font*這些字型包;



通過圖形介面視窗控制檯做下面的二和三操作:
以下第二到第十步的操作是在建立好的第一臺虛擬機器中進行的; 二,關閉NetworkManager、firewalld和SELinux服務; # systemctl stop NetworkManager # systemctl disable NetworkManager # systemctl stop firelwalld # systemctl disable firelwalld # setenforce 0 # sed -i '/^SELINUX=/c\SELINUX=permissive' /etc/selinux/config
三,配置網路引數 1,網絡卡配置,這裡配置一個臨時地址使用,用於ssh連線過去方便複製貼上及檔案傳輸 # cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bak # vi /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes PEERDNS=no PEERROUTES=no IPV4_FAILURE_FATAL=no IPV6INIT=no IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth0 #UUID=2030e1ef-3890-4f56-a3e6-47f9de376d58 DEVICE=eth0 ONBOOT=yes IPADDR=10.1.73.9 NETMASK=255.255.255.0 GATEWAY=10.1.73.1 NM_CONTROLLED=no 2,主機名配置 # hostnamectl set-hostname YOUR_HOSTNAME 3,DNS配置:/etc/hosts和/etc/resolv.conf(根據實際需要看是否要配置) 重啟network # systemctl restart network
四,配置虛擬機器可以在宿主機上通過virsh console命令連線控制檯: # grep -i "ttyS0" /etc/securetty || echo "ttyS0" >> /etc/securetty # vi /boot/grub2/grub.cfg linux16 /vmlinuz-3.10.0-514.el7.x86_64 root=/dev/mapper/rootvg-rootlv ro crashkernel=auto rd.lvm.lv=rootvg/rootlv rhgb quiet LANG=zh_ CN.UTF-8 console=ttyS0 linux16 /vmlinuz-0-rescue-98a6cc2774a14f4fad9b048d99d54d37 root=/dev/mapper/rootvg-rootlv ro crashkernel=auto rd.lvm.lv=rootvg/rootlv rhgb quiet console=ttyS0 # reboot # virsh console template1 連線成功
五,建立目錄結構: # mkdir -pv /data/{logs,src,scripts}
六,上傳檔案 上傳 iso系統映象檔案所有元件安裝包配置檔案模板到第一臺虛擬機器 /data/src目錄下; 上傳部署指令碼到 /data/scripts目錄下;
七,系統初始化配置 # sh -x /data/scripts/osInit_centos7.sh
八,配置本地yum源(可選) 注意:務必確保有yum源可以正常使用。 # mkdir -pv /media/cdrom # mount -t iso9660 -o loop /data/src/CentOS-7-x86_64-DVD-1611.iso /media/cdrom # sed -i '$a\/data/src/CentOS-7-x86_64-DVD-1611.iso /media/cdrom iso9660 defaults,ro,loop 0 0' /etc/fstab # mount -a # cd /etc/yum.repos.d/ # for i in $(ls);do mv ./$i ./${i}.bak;done # cat > /etc/yum.repos.d/CentOS-Local.repo <<EOF [local] name=CentOS7.3.1611 baseurl=file:///media/cdrom enabled=1 gpgcheck=0 EOF # yum repolist
九,安裝zabbix_agent和zabbix_proxy,根據需要啟用 # sh -x /data/scripts/zabbix_proxy_and_agent_centos7.sh
十,ssh升級及相關配置: # sh -x /data/scripts/sshUpdate_centos7.sh 注意:任何指令碼在執行之前要保證指令碼的格式是linux系統的格式 # dos2unix script_name.sh

十一,在宿主機1上準備虛擬機器的克隆模板到指定目錄: 在宿主機1上: # cp /data/kvm/imgs/template-centos7-3-1611.img /data/kvm/templates/ # cp /etc/libvirt/qemu/template.xml /data/kvm/templates/template-centos7-3-1611.xml 然後將宿主機1上的模板傳輸拷貝到宿主機2上: # cd /data/kvm/templates/ # scp ./* [email protected]:/data/kvm/templates/ 克隆虛擬機器通過shell指令碼實現,對應的指令碼存放在/data/scripts目錄下,指令碼名稱分別為:7sys.sh和mac.py;
十二,克隆新的虛擬機器: # cd /data/scripts/ # sh -x /data/7sys_single.sh 根據提示輸入相應的資訊;
十三,克隆出來的虛擬機器需要做的操作: # virsh list --all Id 名稱 狀態 ---------------------------------------------------- - 10.1.73.17 關閉 啟動虛擬機器 # virsh start 10.1.73.17 連線到虛擬機器的控制檯 # virsh console 10.1.73.17 連線到域 10.1.73.17 換碼符為 ^]--------到這個介面之後,按回車,即可; Authorized users only. All activity may be monitored and reported localhost login:root 配置虛擬機器的網路引數: # cd /etc/sysconfig/network-scripts/ # vi ./ifcfg-eth0----修改ip地址即可; IPADDR=10.1.73.17 NETMASK=255.255.255.0 GATEWAY=10.1.73.1 另一種修改方法:注意這裡的ip地址改成自己實際需要配置的的ip地址; sed -i '/IPADDR=/c\IPADDR=10.1.73.19' /etc/sysconfig/network-scripts/ifcfg-eth0 # systemctl restart network 修改主機名: # hostnamectl set-hostname proxyserver2---代理層伺服器名稱 配置DNS(可選,根據需要來配置)

十四,元件部署 以福建電信節點為例,這裡首先需要部署nginx+squid的代理層: 部署nginx: 通過xshell連線到新開的虛擬機器上:使用admin賬號 $ su root # sh -x /data/scripts/nginx_centos7.sh # exit $ nginx的啟動、關閉等可以使用sudo systemctl,nginx程序執行身份為admin;
部署squid: 通過xshell連線到新開的虛擬機器上:使用admin賬號 $ su root # sh -x /data/scripts/squid_centos7.sh # exit $ squid的啟動、關閉等可以使用sudo systemctl,squid程序執行身份為admin;
部署keepalived: $ su root # sh -x /data/scripts/keepalived_centos7.sh # exit $ keepalived的啟動、關閉等必須使用root許可權,可以使用sudo systemctl;
十五,FAQ 1,有時候使用virsh連線虛擬機器的控制檯時會有報錯如下: [[email protected] ~]# virsh console 10.1.73.12 連線到域 10.1.73.12 換碼符為 ^] 錯誤:操作失敗: 這個域有活躍控制檯會話 原因及解決辦法:如果你之前連線過去,而後面沒有退出,但是你與宿主機的ssh斷了,則會出現這種問題,也就是你之前的連線控制檯的連線還存在; [[email protected] ~]# ps -ef | grep virsh root 13749 13734 0 22:48 pts/0 00:00:09 virsh console 10.1.73.12 root 14184 14159 0 23:26 pts/2 00:00:00 grep --color=auto virsh [[email protected] ~]# kill -15 13749 問題解決。
虛擬機器和宿主機登入規範: 首登入必須使用普通使用者admin賬號,日常管理都使用admin賬號; 必要的時候可以切換到root賬號;
附錄1: 使用--cdrom可以顯示如下截圖Install CentOS Linux7的介面,這樣可以向核心傳遞引數,而使用--location則直接進入安裝過程,當磁碟空間小於2T的時候,預設就是用MBR分割槽方法,這種情況下就不能修改核心分割槽方法了,無法輸入核心引數inst.gpt了; 已經驗證 --cdrom OPTIONS File or device used as a virtual CD-ROM device. It can be path to an ISO image or a URL from which to fetch/access a minimal boot ISO image. The URLs take the same format as described for the "--location" argument. If a cdrom has been specified via the "--disk" option, and neither "--cdrom" nor any other install option is specified, the "--disk" cdrom is used as the install media. -l LOCATION --location OPTIONS Distribution tree installation source. virt-install can recognize certain distribution trees and fetches a bootable kernel/initrd pair to launch the install. With libvirt 0.9.4 or later, network URL installs work for remote connections. virt-install will download kernel/initrd to the local machine, and then upload the media to the remote host. This option requires the URL to be accessible by both the local and remote host. --location allows things like --extra-args for kernel arguments, and using --initrd-inject. If you want to use those options with CDROM media, you have a few options: * Run virt-install as root and do --location ISO * Mount the ISO at a local directory, and do --location DIRECTORY * Mount the ISO at a local directory, export that directory over local http, and do --location http://localhost/DIRECTORY The "LOCATION" can take one of the following forms: http://host/path An HTTP server location containing an installable distribution image. ftp://host/path An FTP server location containing an installable distribution image. nfs:host:/path or nfs://host/path An NFS server location containing an installable distribution image. This requires running virt-install as root. DIRECTORY Path to a local directory containing an installable distribution image. Note that the directory will not be accessible by the guest after initial boot, so the OS installer will need another way to access the rest of the install media. ISO Mount the ISO and probe the directory. This requires running virt-install as root, and has the same VM access caveat as DIRECTORY. Some distro specific url samples: Fedora/Red Hat Based http://download.fedoraproject.org/pub/fedora/linux/releases/21/Server/x86_64/os Debian http://ftp.us.debian.org/debian/dists/stable/main/installer-amd64/ Ubuntu http://us.archive.ubuntu.com/ubuntu/dists/wily/main/installer-amd64/ Suse http://download.opensuse.org/distribution/11.0/repo/oss/ Mandriva ftp://ftp.uwsg.indiana.edu/linux/mandrake/official/2009.0/i586/ Mageia ftp://distrib-coffee.ipsl.jussieu.fr/pub/linux/Mageia/distrib/1