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
2.5修改虛擬機設置信息
2.6修改網卡信息並查看
2.7修改網卡名爲eth#形式
配置虛擬網卡,安裝net-tools命令,此ip地址可以從出外網訪問但是無法從外網主從訪問到此虛擬機上的相關服務。
2.9用virt-manager管理的必要條件
2.xshell必須爲企業版本
這種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服務可能回到是配置不生效,這個時候我們可以輸入下圖的命令
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虛擬機網卡配置文件並查看地址
1. 查看是否能夠連通外網
2. 查看外網能否連接內網
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端口)
4.3.2點擊登陸端口訪問
5.KVM虛擬機實現小型的高可用的haproxy+apache 項目
實驗概念圖
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
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
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
效果驗證:最終是能夠訪問到首頁的網頁內容