Apr 252013
 

 

使用Openstack,基本都是通过nova来管理KVM,自己对kvm的各种管理命令,其实是很陌生。所以这次就抛弃各种管理工具,直接在CentOS下使用KVM,创建虚拟机和模板。

参考文档

http://jingshengsun888.blog.51cto.com/1767811/1180585

http://linuxvpsblog.com/virtualization-with-kvm-on-a-centos-6-3-server/

准备

  • CPU支持kvm
  • 最小化安装CentOS6.4
  • 关闭Selinux
  • 关闭iptables或者打开相应的端口,vnc需要用到
  • 源,需要用到epel源

在Centos下检查cpu是否支持VT,没Ubuntu那么方便,输入下面命令,有内容输出,就表示支持,没任何显示,表明不支持。

egrep '(vmx|svm)' –color=always /proc/cpuinfo

查看Selinux状态

# sestatus
SELinux status:                 disabled

KVM

yum install kvm libvirt python-virtinst qemu-kvm virt-viewer

启动libvirt服务

/etc/init.d/libvirtd start

查看kvm服务是否正常,目前没任何虚拟机,所以没有内容显示

virsh -c qemu:///system list

Bridge

我们需要对网卡进行设置一下 创建一个文件 /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE="br0"
ONBOOT=yes
TYPE=Bridge
BOOTPROTO=static
IPADDR=10.1.199.138
NETMASK=255.255.255.0

修改 /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
BRIDGE=br0

重启网络服务

/etc/init.d/network restart

查看桥接网络

# brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.00e081de016c       no              eth0
virbr0          8000.5254009219a2       yes             virbr0-nic

VNC

yum install tigervnc-server

设置

编辑 /etc/sysconfig/vncservers 增加一行

VNCSERVERS="1:root"

设置password

vncpasswd

启服动务

# /etc/init.d/vncserver start
Starting VNC server: 1:root xauth:  creating new authority file /root/.Xauthority

New 'g138.chenshake.com:1 (root)' desktop is g138.chenshake.com:1

Creating default startup script /root/.vnc/xstartup
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/g138.chenshake.com:1.log

                                                           [  OK  ]

查看服务端口

# netstat -nulpt | grep vnc
tcp        0      0 0.0.0.0:5901        0.0.0.0:*     LISTEN      6508/Xvnc           
tcp        0      0 0.0.0.0:6001        0.0.0.0:*     LISTEN      6508/Xvnc           
tcp        0      0 :::6001                  :::*              LISTEN      6508/Xvnc    

创建虚拟机

准备好iso

ls /home/
CentOS-6.4-x86_64-bin-DVD1.iso

创建一个20G qcow2格式的磁盘文件,虚拟机的image放在 /home/kvm 目录下

qemu-img create -f qcow2 -o preallocation=metadata /home/kvm/centos64.qcow2 20G

查看该磁盘文件,注意文件的大小

# ls -lash /home/kvm/
total 3.3M
4.0K drwxr-xr-x  2 root root 4.0K Apr 27 11:04 .
4.0K drwxr-xr-x. 3 root root 4.0K Apr 27 11:04 ..
3.3M -rw-r--r--  1 root root  21G Apr 27 11:04 centos64.qcow2

创建虚拟机

virt-install \
--name=centos6.4 \
--ram 1024 \
--vcpus=2 \
-f /home/kvm/centos64.qcow2 \
--cdrom /home/CentOS-6.4-x86_64-bin-DVD1.iso \
--graphics vnc,listen=0.0.0.0,port=5920,  \
--network bridge=br0  \
--force  --autostart

你可以看到下面输出

快照2

VNC连接

这个时候你在windows的客户端,就可以通过vnc连接过去,完成虚拟机的安装 vnc客户端很多。我是使用TightVNC,安装的时候最好选择定制,只安装客户端,而不安装server端。

IP:5920,就可以访问。

  5 Responses to “CentOS 6.4学习KVM”

  1. centos 6.4 安装openstack G版 没有任何错误:

    packstack –answer-file=packstack-answers-20130504-082703.txt
    Welcome to Installer setup utility
    xxx.xxx.xx.x2_postscript.pp : [ DONE ]

    **** Installation completed successfully ******
    Additional information:
    * Time synchronization installation was skipped. Please note that unsynchronized time on server instances might be problem for some OpenStack components.
    * Did not create a cinder volume group, one already existed
    * To use the command line tools you need to source the file /root/keystonerc_admin created on 182.140.139.82
    * To use the console, browse to http://xxx.xxx.xx.x2/dashboard
    * The installation log file is available at: /var/tmp/packstack/20130505-191837-qU3VMx/openstack-setup.log
    secrete
    问题:登录不了,登录http://xxx.xxx.xx.x2/dashboard/auth/login/
    用户名和密码由/usr/share/openstack-keystone/sample_data.sh决定:
    ADMIN_USER=$(get_id keystone user-create –name=admin \
    –pass=secrete),可以看出是admin和secrete。

    请教如何登录openstack G版的dashboard或者如何找到或者修改登录用户名与密码。谢谢!

  2. 陈老师,貌似安装VNCserver那部分可以不用做。
    ———————————–这virt-install过后直接可以用vncveiwer,登陆10.1.199.138:20就可以登陆实例了
    virt-install \
    –name=centos6.4 \
    –ram 1024 \
    –vcpus=2 \
    -f /home/kvm/centos64.qcow2 \
    –cdrom /home/CentOS-6.4-x86_64-bin-DVD1.iso \
    –graphics vnc,listen=0.0.0.0,port=5920, \
    –network bridge=br0 \
    –force –autostart

  3. 确实不用安装 vncserver 但是怎么设置 vnc 的连接密码呢?

  4. 设置vnc密码 –graphics vnc,password=foobar,port=5910,keymap=ja

 Leave a Reply

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.