KVM虚拟机基础和实现小环境的架构




KVM虚拟机基本使用

1.确认指令集

[01:20:48 root@localhost data]#grep -E "vmx|svm" /proc/cpuinfo | wc -l
4

1.1安装KVM工具包

[01:21:00 root@localhost data]#yum install qemu-kvm qemu-kvm-tools libvirt virt-manager virt-install

[01:21:00 root@localhost data]#systemctl start libvirtd

[01:21:00 root@localhost data]#systemctl enable libvirtd

[01:21:00 root@localhost data]#ifconfig virbr0 #生成NAT网卡
virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
       inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
       ether 52:54:00:b9:97:fb  txqueuelen 1000  (Ethernet)
       RX packets 0  bytes 0 (0.0 B)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 0  bytes 0 (0.0 B)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[01:25:15 root@localhost data]#grep "192.168.122.1" /etc/ -R #查看虚拟网卡配置文件
/etc/libvirt/qemu/networks/autostart/default.xml:  <ip address='192.168.122.1' netmask='255.255.255.0'>
/etc/libvirt/qemu/networks/default.xml:  <ip address='192.168.122.1' netmask='255.255.255.0'>

2.创建nat网络虚拟机

2.1创建磁盘

[01:27:15 root@localhost data]#ll /var/lib/libvirt/images/ #默认保存虚拟机磁盘文件路径

[01:28:15 root@localhost data]#qemu-img create -f raw /var/lib/libvirt/images/centos7.raw
10G #创建一个格式为raw大小为10G的裸磁盘
Formatting '/var/lib/libvirt/images/centos7.raw', fmt=raw size=10737418240

[01:28:35 root@localhost data]#ll -h /var/lib/libvirt/images/CentOS-7-x86_64.raw
-rw-r--r-- 1 root root 10G Jun 15 2019 /var/lib/libvirt/images/CentOS-7-x86_64.raw

[01:29:10 root@localhost data]#qemu-img create -f qcow2 /var/lib/libvirt/images/centos7.qcow2 10G
Formatting '/var/lib/libvirt/images/centos7.qcow2', fmt=qcow2 size=10737418240
encryption=off cluster_size=65536 lazy_refcounts=off

[01:29:15 root@localhost data]#ll -h /var/lib/libvirt/images/centos.qcow2
-rw-r--r-- 1 root root 193K Jun 14 2019 /var/lib/libvirt/images/centos.qcow2

2.2上传镜像文件

[01:25:53 root@localhost data]#ll
total 940032
-rw-r--r-- 1 qemu qemu 962592768 Jun 16 16:52 CentOS-7-x86_64-Minimal-1810.iso

2.3创建默认网络虚拟机

[01:39:27 root@localhost ~]#virt-install --virt-type kvm --name centos7 --ram 1024 --vcpus 2 --cdrom=/data/CentOS-7-x86_64-Minimal-1810.iso --disk path=/var/lib/libvirt/images/centos7.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

2.4通过virt-manager管理虚拟机

[01:54:48 root@localhost ~]#virt-manager

spacer.gifimage.png

2.5修改虚拟机设置信息

spacer.gifimage.png

2.6修改网卡信息并查看

image.pngspacer.gif

2.7修改网卡名为eth#形式

image.pngspacer.gif

配置虚拟网卡,安装net-tools命令,此ip地址可以从出外网访问但是无法从外网主从访问到此虚拟机上的相关服务。

image.pngspacer.gif

2.9用virt-manager管理的必要条件

1.隧道打开Xmanager功能


image.pngspacer.gif

spacer.gifimage.png

扫描二维码关注公众号,回复: 6533971 查看本文章

2.xshell必须为企业版本

image.pngspacer.gif

这种nat模式虚拟网卡在实际的工作环境中使用的较少

3.创建bridge网络虚拟机

3.1安装环境

[02:28:49 root@localhost network-scripts]#yum install bridge-utils

3.2创建br0、br1桥接网卡(172.20.66.200服务器)

[02:33:43 root@localhost network-scripts]#vim /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=172.20.66.200
NETMASK=255.255.0.0
GATEWAY=172.18.0.1
DNS1=172.18.0.1
[02:33:43 root@localhost network-scripts]#vim /etc/sysconfig/network-scripts/ifcfg-br1
TYPE=Bridge
BOOTPROTO=static
NAME=br1
DEVICE=br1
ONBOOT=yes
IPADDR=192.168.66.7
NETMASK=255.255.255.0

3.3创建br0、br1桥接网卡(172.20.66.201服务器)

[02:33:43 root@localhost network-scripts]#vim /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=172.20.66.201
NETMASK=255.255.0.0
GATEWAY=172.18.0.1
DNS1=172.18.0.1
[02:33:43 root@localhost network-scripts]#vim /etc/sysconfig/network-scripts/ifcfg-br1
TYPE=Bridge
BOOTPROTO=static
NAME=br1
DEVICE=br1
ONBOOT=yes
IPADDR=192.168.66.17
NETMASK=255.255.255.0

3.4创建band0、1网卡

vim /etc/sysconfig/network-scripts/ifcfg-bond0 vim /etc/sysconfig/network-scripts/ifcfg-bond1
BOOTPROTO=static NAME=bond0 DEVICE=bond0 ONBOOT=yes BONDING_MASTER=yes BONDING_OPTS="mode=1 miimon=100" BRIDGE=br0 BOOTPROTO=static NAME=bond1 DEVICE=bond1 ONBOOT=yes BONDING_MASTER=yes BONDING_OPTS="mode=1 miimon=100" BRIDGE=br1

3.5修改eth0、1网卡的配置文件

vim /etc/sysconfig/network-scripts/ifcfg-eth0 vim /etc/sysconfig/network-scripts/ifcfg-eth1
NAME=eth0 DEVICE=eth0 ONBOOT=yes NM_CONTROLLED=no MASTER=bond0 USERCTL=no SLAVE=yes NAME=eth1 DEVICE=eth1 ONBOOT=yes NM_CONTROLLED=no MASTER=bond0 USERCTL=no SLAVE=yes

3.6修改eth2、3网卡的配置文件

vim /etc/sysconfig/network-scripts/ifcfg-eth2 vim /etc/sysconfig/network-scripts/ifcfg-eth3
NAME=eth2 DEVICE=eth2 ONBOOT=yes NM_CONTROLLED=no MASTER=bond1 USERCTL=no SLAVE=yes NAME=eth3 DEVICE=eth3 ONBOOT=yes NM_CONTROLLED=no MASTER=bond1 USERCTL=no SLAVE=yes

3.7重点注意:

在配置完如上的网卡设置后,重启network服务可能回到是配置不生效,这个时候我们可以输入下图的命令

image.pngspacer.gif

1.关闭NetworkManager

出现如上的提示后就能够用xshell连接虚拟机,该原因我通过观察日志,推测应该是NetworkManager不识别bond0和bond1.需要关闭后在重启服务network.

2.取消参数

还有一个解决方法在设置eth网卡的时候不要添加NM_CONTROLLED=no参数

3.8上传镜像文件并安装虚拟机

[06:09:30 root@localhost data]#ll
total 940032
-rw-r--r-- 1 root root 962592768 Jun 16 16:52 CentOS-7-x86_64-Minimal-1810.iso #镜像文件
[06:10:23 root@localhost data]#qemu-img create -f qcow2 /var/lib/libvirt/images/centos7-bridge.qcow2 10G
Formatting '/var/lib/libvirt/images/centos7-bridge.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off #创建虚拟磁盘
[18:28:36 root@localhost ~]#virt-install --virt-type kvm --name centos7-bridge --ram 1024 --vcpus 2 --cdrom=/data/CentOS-7-x86_64-Minimal-1810.iso  --disk path=/var/lib/libvirt/images/centos7.qcow2 --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole #安装系统

3.9虚拟机网卡配置文件并查看地址

image.pngspacer.gif

1. 查看是否能够连通外网

spacer.gifimage.png

2. 查看外网能否连接内网

spacer.gifimage.png

4.虚拟机管理工具

4.1安装管理工具包

[18:44:19 root@localhost ~]#yum install acpid

4.2管理指令

[18:55:56 root@localhost ~]#virsh list #列出当前开机的虚拟机
[18:56:23 root@localhost ~]#virsh list --all #列出所有虚拟机
[18:56:23 root@localhost ~]#virsh shutdowm centos7-bridge #正常关机
[18:57:16 root@localhost ~]#virsh start centos7-bridge #正常开机
[18:57:16 root@localhost ~]#virsh destroy centos7-bridge #强制/停止关机
[18:57:16 root@localhost ~]#virsh undefine centos7-bridge #强制删除
[18:57:16 root@localhost ~]#virsh autostart centos7-bridge #设置开机自启动

4.3补充:使用vnc来对虚拟机进行系统安装

4.3.1书写登陆端口IP地址

(在虚拟机安装时候会打开一个5900端口)

image.pngspacer.gif

4.3.2点击登陆端口访问

spacer.gifspacer.gifimage.png

5.KVM虚拟机实现小型的高可用的haproxy+apache 项目

实验概念图

image.pngspacer.gif

5.1安装系统磁盘文件

(172.20.66.200\172.20.66.201机器上重复如下操作,创建共4台虚拟机)

[21:31:30 root@localhost images]#ll
total 2827712
-rw-r--r-- 1 qemu qemu 1447755776 Jun 17 21:29 centos7.qcow2
-rw-r--r-- 1 qemu qemu 1447821312 Jun 17 21:29 centos7.qcow2.bak

5.2虚拟机安装centos7系统

[20:00:53 root@localhost images]#virt-install --virt-type kvm --name centos7-bridge --ram 1024 --vcpus 2 --cdrom=/data/CentOS-7-x86_64-Minimal-1810.iso  --disk path=/var/lib/libvirt/images/centos7.qcow2 --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

[20:00:53 root@localhost images]#virt-install --virt-type kvm --name centos7-bridge.bak --ram 1024 --vcpus 2 --cdrom=/data/CentOS-7-x86_64-Minimal-1810.iso  --disk path=/var/lib/libvirt/images/centos7.qcow2.bak --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

1.打开虚拟机操作工具,查看虚拟机(共四台虚拟机)

[21:37:44 root@localhost images]#virt-manager

image.pngspacer.gif

5.3负载均衡器

5.3.1安装软件包(172.20.200.200/172.20.200.201)

[root@localhost ~]# yum install haproxy keepalived -y

5.3.2修改配置参数并使其生效

[root@localhost ~]# vi /etc/sysctl.conf 
net.ipv4.ip_forward = 1 #打开转发功能
net.ipv4.ip_nonlocal_bind = 1 #打开绑定端口功能

[root@localhost ~]# sysctl -p #使写入配置文件中的参数生效
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1

5.3.3修改配置文件

[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
listen web_http
       balance roundrobin
       mode http
       bind 172.20.200.66:80
       server web1 192.168.66.199:80 check inter 3000 fall 2 rise 5
       server web2 192.168.66.202:80 check inter 3000 fall 2 rise 5
[root@localhost ~]# vi /etc/keepalived/keepalived.conf
  ! Configuration File for keepalived

global_defs {
  notification_email {
    [email protected]
    [email protected]
    [email protected]
  }
  notification_email_from [email protected]
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id lvs_web1
  vrrp_skip_check_adv_addr
  #vrrp_strict #使用单播形式要禁掉该项
  vrrp_iptables #取消掉发生vip转移自动生成的防火墙策略
  vrrp_garp_interval 0
  vrrp_gna_interval 0
}
vrrp_instance VI_1 {
   state MASTER
   interface eth0
   virtual_router_id 66 #在同一个网络内不能相同id
   priority 100 #优先级和从节点要有区别
   advert_int 1
   unicast_src_ip 172.20.200.200 #从节点源地址要交换
   unicast_peer {
       172.20.200.201
   }
   authentication {
       auth_type PASS
       auth_pass 123456
   }
   virtual_ipaddress {
       172.20.200.66 dev eth0 label eth0:0 #绑定的vip网卡
   }
}

5.3.4启动服务,并查看启动情况

[root@localhost ~]# systemctl start haproxy keepalived
[root@localhost ~]# ss -ntl

image.pngimage.png

5.4后端服务器

5.4.1安装软件包

[root@localhost ~]# yum intall httpd

5.4.2布置首页内容

192.168.66.199服务器

[root@localhost ~]# echo 192.168.66.199 > /var/www/html/index.html

192.168.66.202服务器

[root@localhost ~]# echo 192.168.66.202 > /var/www/html/index.html

5.4.3启动服务

[root@localhost ~]# systemctl start httpd

效果验证:最终是能够访问到首页的网页内容

spacer.gifimage.png

image.png


猜你喜欢

转载自blog.51cto.com/14234933/2410155