install kvm hosts use vnc in CentOS 6

2 minute read

背景

本文描述在centos6中部署kvm虚拟机, 使用vnc连接到服务器, 再使用vncviewer连接到kvm vnc安装界面.

(原本打算直接连接到kvm vnc安装界面的, 没有成功, 会闪退)

安装过程

在服务器安装必要的包

# yum install -y qemu-img qemu-kvm virt-manager libvirt libvirt-python python-virtinst libvirt-client tigervnc-server libvirt libvirt-client virt-what virt-viewer  
# yum groupinstall -y desktop  

创建一个虚拟磁盘目录

# mkdir /data03/kvmdisk  

创建虚拟磁盘, 用于虚拟机的系统盘

# qemu-img create -f qcow2 -o encryption=off,cluster_size=2M,preallocation=full /data03/kvmdisk/disk01.img 32G  

下载安装镜像

# mkdir /data03/iso  
# cd iso  
# wget http://mirrors.aliyun.com/centos/6.6/isos/x86_64/CentOS-6.6-x86_64-bin-DVD1.iso  

启动libvirtd

# service libvirtd start  
# chkconfig libvirtd on  
# chkconfig libvirt-guests off  

查看当前启动的网桥

# brctl show  
bridge name     bridge id               STP enabled     interfaces  
virbr0          8000.5254001263b0       yes             virbr0-nic  
  
# ifconfig  
em1       Link encap:Ethernet  HWaddr 00:22:19:60:77:8F    
          inet addr:172.16.3.150  Bcast:172.16.3.255  Mask:255.255.255.0  
          inet6 addr: fe80::222:19ff:fe60:778f/64 Scope:Link  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:5469716 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:2830916 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:1000   
          RX bytes:5147311077 (4.7 GiB)  TX bytes:198552462 (189.3 MiB)  
  
lo        Link encap:Local Loopback    
          inet addr:127.0.0.1  Mask:255.0.0.0  
          inet6 addr: ::1/128 Scope:Host  
          UP LOOPBACK RUNNING  MTU:65536  Metric:1  
          RX packets:79073 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:79073 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:0   
          RX bytes:24506711 (23.3 MiB)  TX bytes:24506711 (23.3 MiB)  
  
virbr0    Link encap:Ethernet  HWaddr 52:54:00:12:63:B0    
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:0   
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)  

网桥地址配置

# grep -r 192.168.122 /etc/libvirt  
/etc/libvirt/qemu/networks/default.xml:  <ip address="192.168.122.1" netmask="255.255.255.0">  
/etc/libvirt/qemu/networks/default.xml:      <range start="192.168.122.2" end="192.168.122.254" />  
/etc/libvirt/qemu/networks/autostart/default.xml:  <ip address="192.168.122.1" netmask="255.255.255.0">  
/etc/libvirt/qemu/networks/autostart/default.xml:      <range start="192.168.122.2" end="192.168.122.254" />  

安装KVM虚拟机, 开启VNC.

# virt-install \  
   --name=centos6_6_x64 \  
   --disk path=/data03/kvmdisk/disk01.img,device=disk,bus=virtio,perms=rw,cache=writethrough \  
   --graphics vnc,listen=0.0.0.0,port=5901,password=digoal \  
   --vcpus=4 --ram=4096 \  
   --cdrom=/data03/iso/CentOS-6.6-x86_64-bin-DVD1.iso \  
   --network bridge=virbr0 \  
   --os-type=linux \  
   --os-variant=rhel6  
  
# ps -ewf|grep kvm  
root       967     2  0 15:05 ?        00:00:00 [kvm-irqfd-clean]  
qemu      3582     1  7 15:15 ?        00:00:00 /usr/libexec/qemu-kvm -name centos6_6_x64 -S -M rhel6.6.0 -enable-kvm -m 4096 -realtime mlock=off -smp 4,sockets=4,cores=1,threads=1 -uuid e55d318c-5b3c-a0c0-e694-799c618787ac -nodefconfig -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/centos6_6_x64.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-reboot -no-shutdown -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x4.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x4 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x4.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x4.0x2 -drive file=/data03/kvmdisk/disk01.img,if=none,id=drive-virtio-disk0,format=raw,cache=writethrough -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x5,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=2 -drive file=/data03/iso/CentOS-6.6-x86_64-bin-DVD1.iso,if=none,media=cdrom,id=drive-ide0-1-0,readonly=on,format=raw -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0,bootindex=1 -netdev tap,fd=22,id=hostnet0,vhost=on,vhostfd=23 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:5d:70:bd,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -device usb-tablet,id=input0 -vnc 0.0.0.0:1,password -vga cirrus -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x6 -msg timestamp=on  
root      3593     2  0 15:15 ?        00:00:00 [kvm-pit-wq]  
root      3605  2457  0 15:16 pts/2    00:00:00 grep kvm  
  
# netstat -anp|grep 5901  
tcp        0      0 0.0.0.0:5901                0.0.0.0:*                   LISTEN      3582/qemu-kvm      

虽然监听了5901, 但是在外部主机不能连到这个VNC, 会闪退.

在服务器本地可以连接正常.

在服务端启动vncserver

# vncserver :2  
# vi ~/.vnc/xstartup  
#twm &  
gnome-session &  
  
# vncserver -kill :2  
# vncserver :2  

使用笔记本连接到服务端vnc, 然后在服务端的vnc窗口中使用vncviewer再连接本地的vnc 5901.

安装过程略.

其他, 如果要删除虚拟机, 需要删除配置, 重启libvirtd.

# virsh list --all  
 Id    Name                           State  
----------------------------------------------------  
 1     centos6_6_x64                  running  
  
# virsh destroy 1  
  
# virsh list --all  
 Id    Name                           State  
----------------------------------------------------  
 -     centos6_6_x64                  shut off  
  
# rm -f /etc/libvirt/qemu/centos6_6_x64.xml  
# service libvirtd restart  
  
# virsh list --all  
 Id    Name                           State  
----------------------------------------------------  

问题

1. 无法直接使用笔记本连接virt-install提供的vnc端口, 必须在服务端连, 目前不知原因.

2. 当你启动libvirtd 服务失败的时候。你用status查看这个服务的状态的时候,会发现他提示的是“libvirtd dead but subsys locked” 这边你可能需要将其他几个关联服务给一同启动起来

/etc/init.d/messagebus start

/etc/init.d/avahi-daemon start

/etc/init.d/libvirted start

当时想使用KVM管理工具virsh时候提示/var/run/libvirt/libvirt-sock 找不到文件夹。

在你安装libvirt包的情况下还出现这个问题

你可能有几个服务没有安装。用yum安装

libdaemon avahi

参考

1. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Virtualization_Getting_Started_Guide/ch05.html#idp8381488
2. man virt-install
3. man virsh
4. man qemu-kvm
5. man qemu-img
6. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Installation_Guide/index.html

Flag Counter

digoal’s 大量PostgreSQL文章入口