基于KVM的

实验图示:
基于KVM的

跳板机:(以跳板机为宿主机)

[[email protected](zxl) ~]# yum -y install libvirt-daemon-kvm qemu-kvm virt-manager libvirt
[[email protected](zxl) ~]# systemctl restart libvirtd

创建NAT桥:

#创建http-fpm网桥
[email protected](zxl) ~]# cd /etc/libvirt/qemu/networks/
[[email protected](zxl) networks]# cp default.xml httpfpm.xml
[[email protected](zxl) networks]# vim httpfpm.xml 
<network>
  <name>httpfpm</name>
  <uuid>06a6d52e-6c03-4b29-bdf7-2cfe6d5e937a</uuid>
  <bridge name='httpfpm' stp='on' delay='0'/>
  <mac address='52:54:00:c6:21:33'/>
  <ip address='10.0.0.1' netmask='255.255.255.0'>  #网桥自身ip
    <dhcp>
      <range start='10.0.0.2' end='10.0.0.254'/>        #网桥分配的ip地址范围
    </dhcp>
  </ip>
</network>
[[email protected](zxl) ~]# virsh net-define /etc/libvirt/qemu/networks/httpfpm.xml   #使这个域生效,但是不启动
[[email protected](zxl) ~]# virsh net-start httpfpm           #开启这个域
[[email protected](zxl) ~]# virsh net-autostart httpfpm     #使这个域开机自启动
#创建php-mysql网桥
[[email protected](zxl) ~]# cd /etc/libvirt/qemu/networks/
[[email protected](zxl) networks]# cp httpfpm.xml phpmysql.xml 
<network>
  <name>phpmysql</name>
  <uuid>06a6d52e-6c03-4b29-bdf7-2cfe6d5e937b</uuid>
  <bridge name='phpmysql' stp='on' delay='0'/>
  <mac address='52:54:00:c6:21:34'/>
  <ip address='20.0.0.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='20.0.0.2' end='20.0.0.254'/>
    </dhcp>
  </ip>
</network>
[[email protected](zxl) ~]# virsh net-define /etc/libvirt/qemu/networks/phpmysql.xml 
Network phpmysql defined from /etc/libvirt/qemu/networks/phpmysql.xml

[[email protected](zxl) ~]# virsh net-start phpmysql
Network phpmysql started

[[email protected](zxl) ~]# virsh net-autostart phpmysql
Network phpmysql marked as autostarted

创建物理桥:

[[email protected](zxl) ~]# nmcli connection add type bridge con-name br0 ifname br0
[[email protected](zxl) ~]# nmcli connection modify br0 ipv4.method manual ipv4.addresses 172.18.254.39/16 ipv4.gateway 172.18.0.1 ipv4.dns 172.18.0.1
[[email protected](zxl) ~]# nmcli connection up br0
[[email protected](zxl) ~]# nmcli connection add type bridge-slave ifname ens33 master br0
[[email protected](zxl) ~]# nmcli connection up bridge-slave-ens33

创建虚拟磁盘文件:(其作用是:记录安装的过程,以便可以用作模板来多次安装)

[[email protected](zxl) ~]# mkdir -pv /virtualmachines/centos7_base
[[email protected](zxl) ~]# qemu-img create -f qcow2 -o size=80G,preallocation=metadata,compat=1.1 /virtualmachines/centos7_base/centos7.img  #这个centos7.img是在创建centos7虚拟机时用的。(第二个图有显示)

安装centos7:(下图是安装的重要的点)
基于KVM的
基于KVM的

进去httpd的虚拟机:

[[email protected] conf.d]# vim /etc/httpd/conf/httpd.conf 
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

[[email protected] conf.d]# vim pma.conf 
#DirectoryIndex index.php
<VirtualHost *:80>
ServerName bbs.magedu.com
DocumentRoot /vhosts/pma/htdocs
<Directory /vhosts/pma/htdocs>
Require all granted
</Directory>
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$  fcgi://10.0.0.4:9000/vhosts/pma/htdocs/$1
</VirtualHost>

进入php虚拟机:(将包都在php里运行)

[[email protected] htdocs]# vim /etc/php-fpm.d/www.conf 
listen = 0.0.0.0:9000
;listen.allowed_clients = 127.0.0.1
#这个目录是httpd给限制的
[[email protected] htdocs]#  /vhosts/pma/htdocs        
#然后将wordpress包下到这个目录即可。  

mysql虚拟机装一个mysql服务即可。

笔记:
[[email protected] ~]# yum install libvirt-daemon-kvm qemu-kvm virt-manager libvirt

qemu-kvm ----模拟计算机的工具,为KVM虚拟机提供IO设备

       libvirt    ----管理虚拟机

       virt-install    ----命令行的虚拟机创建安装工具

       bridge-utils   ----网桥工具

用户网络(User Networking):NAT方式,让虚拟机访问主机、互联网或本地网络上的资源的简单方法,但是不能从网络或其他的客户机访问客户机,性能上也需要大的调整。
虚拟网桥(Virtual Bridge):Bridge方式,这种方式要比用户网络复杂一些,但是设置好客户机与互联网,客户机与主机之间的通信都很容易。
[[email protected] ~]# systemctl start libvirtd
[[email protected] yum.repos.d]# brctl addbr mybr0 #增加一个网桥 brctl show 查看
brctl delbr mybr0 #删除mybr0
[[email protected] yum.repos.d]# brctl stp mybr0 on #开启stp
[[email protected] yum.repos.d]# ip link add veth0.0 type veth peer name veth0.1 #添加虚拟以太网
[[email protected] yum.repos.d]# ip link set dev veth0.0 up 启动veth0.0
[[email protected] yum.repos.d]# ip link set dev veth0.1 up 启动veth0.1
[[email protected] yum.repos.d]# brctl addif mybr0 veth0.1 将mybr0和veth0.1关联起来(mybr0是交换机,veth是虚拟以太网)
[[email protected] yum.repos.d]# ip netns add myns #相当于建立一个隔离的虚拟机
[[email protected] yum.repos.d]# ip netns list #显示netns
[[email protected] yum.repos.d]# ip link set veth0.0 netns myns #将一个虚拟以太网连接到这个虚拟机中
[[email protected] yum.repos.d]# ip netns exec myns ifconfig -a #显示ip
[[email protected] yum.repos.d]# ip netns exec myns ip link set veth0.0 name eth0 #将veth0.0重命名为eth0
[[email protected] yum.repos.d]# ip netns exec myns ifconfig eth0 10.10.0.1/24 up #给eth0设置ip地址并将其**
[[email protected] yum.repos.d]# ip netns exec myns ifconfig #查看ip地址
[[email protected] app]# ip link set mybr0 up #开启mybr0
[[email protected] app]# ip addr add 10.10.0.254/24 dev mybr0 #给mybr0ip地址
[[email protected] app]# ifconfig #查看ip地址
virsh attach-interface c1 bridge mybr0 #这个c1虚拟机,给c1一个桥接的mybr0网络
virsh detach-interface c1 bridge --mac mac地址 #virsh domiflist c1这个可以查到mac地址,,这个命令是拔出一个桥接的网络
virsh console c1 #在终端上连接kvm创建的虚拟机,退出为ctrl+]
创建多个kvm虚拟机
[[email protected] qemu]# cd /etc/libvirt/qemu/
复制上边进入的目录里的 .xml文件,想创建几个就复制几份
修改复制的文件将

#这个是你复制的文件的修改,这个c3.img镜像文件,这个要看自己设置的 /root/c3.img 是镜像位置,至于这个c3.img是我复制来的 [[email protected] qemu]# virsh create .xml #创建并启动虚拟机 网桥就是vm中的vmnet# 桥接和仅主机这些