Kvm虚拟化部署

需求说明

使用Kvm部署一台主机

环境说明

确保CPU虚拟化功能已开启
物理机要在BIOS里开启CPU虚拟化(现在一般默认开启)
虚拟机要关机设置CPU虚拟化
①.关闭防火墙

[root@lizihan ~]# systemctl stop firewalld.service 

②.关闭selinux

[root@lizihan ~]# setenforce 0
[root@lizihan ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config 

③.配置网络源和epel源

[root@lizihan ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@lizihan ~]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo 

操作步骤

①.验证CPU是否支持KVM,支持就安装kvm

[root@lizihan ~]# egrep -o 'vmx|svm' /proc/cpuinfo 
vmx    //出现vmx或者svm既表示支持虚拟化
[root@lizihan ~]# yum -y install qemu-kvm qemu-kvm-tools qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools

②.使用br0来桥接本机网卡

[root@lizihan ~]# cd /etc/sysconfig/network-scripts/
[root@lizihan network-scripts]# cp ifcfg-eth0 ifcfg-br0
[root@lizihan network-scripts]# vim ifcfg-br0 
TYPE=Bridge
DEVICE=br0
NM_CONTROLLED=no
BOOTPROTO=static
NAME=br0
ONBOOT=yes
IPADDR=172.16.11.100
NETMASK=255.255.255.0
GATEWAY=172.16.11.1
DNS1=114.114.114.114
DNS2=8.8.8.8
[root@lizihan network-scripts]# vim ifcfg-eth0 
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no
[root@lizihan ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000
    link/ether 00:0c:29:35:dd:b8 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::20c:29ff:fe35:ddb8/64 scope link 
       valid_lft forever preferred_lft forever
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether 00:0c:29:35:dd:b8 brd ff:ff:ff:ff:ff:ff
    inet 172.16.11.100/24 brd 172.16.11.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe35:ddb8/64 scope link 
       valid_lft forever preferred_lft forever

③.测试并验证安装结果

[root@lizihan ~]# systemctl start libvirtd
[root@lizihan ~]# systemctl enable libvirtd
[root@lizihan ~]# virsh -c qemu:///system list
 Id    Name                           State
----------------------------------------------------

[root@lizihan ~]# virsh -v 
4.5.0
[root@lizihan ~]# virt-install --version
1.5.0
[root@lizihan ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/    
[root@lizihan ~]# lsmod | grep kvm
kvm_intel             170086  0 
kvm                   566340  1 kvm_intel
irqbypass              13503  1 kvm
[root@lizihan ~]# brctl show      //查看网桥信息
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c2935ddb8	no		eth0
virbr0		8000.525400afae89	yes		virbr0-nic

④.kvm的web管理界面安装

[root@lizihan ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel     //安装依赖包
[root@lizihan ~]# pip install --upgrade pip     //升级pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 17kB/s 
Installing collected packages: pip
  Found existing installation: pip 8.1.2
    Uninstalling pip-8.1.2:
      Successfully uninstalled pip-8.1.2
Successfully installed pip-18.1

⑤.从github上下载webvirtmgr代码并安装

[root@lizihan ~]# cd /usr/local/src
[root@lizihan src]# git clone git://github.com/retspen/webvirtmgr.git       //下载webvirtmgr
Cloning into 'webvirtmgr'...
remote: Enumerating objects: 5614, done.
remote: Total 5614 (delta 0), reused 0 (delta 0), pack-reused 5614
Receiving objects: 100% (5614/5614), 2.98 MiB | 248.00 KiB/s, done.
Resolving deltas: 100% (3603/3603), done.
[root@lizihan src]# cd webvirtmgr/
[root@lizihan webvirtmgr]# pip install -r requirements.txt        //安装webviregmr
Collecting django==1.5.5 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/38/49/93511c5d3367b6b21fc2995a0e53399721afc15e4cd6eb57be879ae13ad4/Django-1.5.5.tar.gz (8.1MB)
    100% |████████████████████████████████| 8.1MB 127kB/s 
Collecting gunicorn==19.5.0 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/f9/4e/f4076a1a57fc1e75edc0828db365cfa9005f9f6b4a51b489ae39a91eb4be/gunicorn-19.5.0-py2.py3-none-any.whl (113kB)
    100% |████████████████████████████████| 122kB 123kB/s 
Collecting lockfile>=0.9 (from -r requirements.txt (line 5))
  Downloading https://files.pythonhosted.org/packages/c8/22/9460e311f340cb62d26a38c419b1381b8593b0bb6b5d1f056938b086d362/lockfile-0.12.2-py2.py3-none-any.whl
Installing collected packages: django, gunicorn, lockfile
  Running setup.py install for django ... done
Successfully installed django-1.5.5 gunicorn-19.5.0 lockfile-0.12.2
[root@lizihan webvirtmgr]# python      //检查sqlite3模块是否安装
Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3       //这里不报错即表示安装
>>> exit()

⑥.初始化账号信息,拷贝网页到指定目录

[root@lizihan webvirtmgr]# python manage.py syncdb
WARNING:root:No local_settings file found.
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table servers_compute
Creating table instance_instance
Creating table create_flavor

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes        //是否创建管理员账号
Username (leave blank to use 'root'):        //指定用户名,默认root
Email address: 18591993920@163.com     //邮箱地址
Password:       //管理员密码
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
[root@lizihan webvirtmgr]# mkdir /var/www
[root@lizihan webvirtmgr]# cp -r /usr/local/src/webvirtmgr/ /var/www/
[root@lizihan webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/

⑦.生成密钥并拷贝到kvm服务器

由于这里webvirtmgr和kvm服务部署在同一台机器,所以这里本地信任。如果kvm部署在其他机器,那么就要把公钥拷贝到部署kvm的服务器上面去

[root@lizihan ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:iMvPwgisiUrNyT+fjp4GrjNyc79dUgwNaXubaRBKdKc root@lizihan
The key's randomart image is:
+---[RSA 2048]----+
|     .. o..      |
|      ..+=       |
|     . oEo.      |
|     ...oo.      |
|.   . . Soo+     |
|..+o..   .=      |
|o+.B+   ...      |
|*o+.==o..o       |
|+o+ooBO=.        |
+----[SHA256]-----+
[root@lizihan ~]# ssh-copy-id 172.16.11.100

[root@lizihan ~]# ssh 172.16.11.100  -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60      //配置端口转发
Last login: Sat Dec 15 22:45:57 2018 from 172.16.11.1
[root@lizihan ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128              *:111                          *:*                  
LISTEN      0      5      192.168.122.1:53                           *:*                  
LISTEN      0      128              *:22                           *:*                  
LISTEN      0      100      127.0.0.1:25                           *:*                  
LISTEN      0      128      127.0.0.1:6080                         *:*                  
LISTEN      0      128      127.0.0.1:8000                         *:*                  
LISTEN      0      128             :::111                         :::*                  
LISTEN      0      128             :::22                          :::*                  
LISTEN      0      100            ::1:25                          :::*                  
LISTEN      0      128            ::1:6080                        :::*                  
LISTEN      0      128            ::1:8000                        :::*  

⑧.配置nginx

[root@lizihan ~]# vim /etc/nginx/nginx.conf
......
    server {
        listen       80;      //删除后面的defaults
        server_name  _;
        root         /usr/share/nginx/html;
......
[root@lizihan ~]# vim /etc/nginx/conf.d/webvirtmgr.conf
server {
    listen 80 default_server;

    server_name $hostname;
    #access_log /var/log/nginx/webvirtmgr_access_log;

    location /static/ {
        root /var/www/webvirtmgr/webvirtmgr;
        expires max;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-Proto $remote_addr;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        client_max_body_size 1024M;
    }
}

[root@lizihan ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py 
bind = '0.0.0.0:8000'     //确保此处绑定的是本机的8000端口,这个在nginx配置中定义了,被代理的端口
backlog = 2048
[root@lizihan ~]# systemctl restart nginx

[root@lizihan ~]# vim /etc/supervisord.conf      //设置supervisor,在配置文件的最后添加
.......
[program:webvirtmgr]
command=/usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
logfile=/var/log/supervisor/webvirtmgr.log
log_stderr=true
user=nginx

[program:webvirtmgr-console]
command=/usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console
directory=/var/www/webvirtmgr
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
redirect_stderr=true
user=nginx

[root@lizihan ~]# systemctl start supervisord.service 
[root@lizihan ~]# systemctl enable supervisord.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.

⑨.配置nginx用户

[root@lizihan ~]# mkdir /home/nginx
[root@lizihan ~]# chown -R nginx.nginx /home/nginx/
[root@lizihan ~]# chmod -R 700 /home/nginx/
[root@lizihan ~]# su - nginx -s /bin/bash      //因为nginx用户默认使用的shell是nologbin,这里使用bash来登陆nginx
-bash-4.2$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/nginx/.ssh/id_rsa): 
Created directory '/var/lib/nginx/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/lib/nginx/.ssh/id_rsa.
Your public key has been saved in /var/lib/nginx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:PQqLHtYD89vtuseW7Oasoo2JRZVxfq87ZlqCO55cdyY nginx@lizihan
The key's randomart image is:
+---[RSA 2048]----+
|      . .        |
|       =         |
|      o . .      |
|     .   o .     |
|    + . S o .    |
|   . * + . o     |
|    = * +oE.o    |
|   = *oB *@=     |
|  . =oBo*#B.     |
+----[SHA256]-----+
-bash-4.2$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.2$ chmod 0600 ~/.ssh/config 
-bash-4.2$ ssh-copy-id root@172.16.11.100

[root@lizihan ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[Remote libvirt SSH access]
Identity=unix-user:root
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes

[root@lizihan ~]# systemctl restart nginx
[root@lizihan ~]# systemctl restart libvirtd


到这里接配置完成了,可以使用web界面添加虚拟机了
在这里插入图片描述

登陆进去以后就开始添加ssh主机链接
在这里插入图片描述

在这里插入图片描述

添加存储池
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

通过远程链接将ISO镜像上传至这个目录即可

[root@localhost ~]# cd /var/lib/libvirt/images/
[root@localhost images]# 
[root@localhost images]# ls
rhel-server-7.4-x86_64-dvd.iso

在这里插入图片描述

创建系统安装镜像
在这里插入图片描述

在这里插入图片描述

添加桥接网络
在这里插入图片描述

在这里插入图片描述

添加虚拟机实例
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

虚拟机插入光盘
在这里插入图片描述

设置web上访问虚拟机的密码
在这里插入图片描述

开启虚拟机,打开控制台
在这里插入图片描述

要在CentOS 7上进行KVM虚拟化部署,可以按照以下步骤进行操作: 1. 首先,部署KVM环境。这可以通过安装libvirt、virt和qemu-kvm等软件包来实现。 2. 将CentOS 7的ISO镜像拉入宿主机。这可以通过将ISO镜像文件复制到宿主机上的任意位置来完成。 3. 使用VMM GUI或命令行工具(如virt-install)创建KVM虚拟机。在创建虚拟机时,需要指定虚拟机的名称、内存大小、CPU数量和磁盘路径等参数。 4. 确保虚拟机的网络使用桥接模式,这样虚拟机可以与宿主机和其他虚拟机进行通信。 5. 可以在虚拟机关机状态下创建快照,以便在需要时可以恢复到该状态。这可以通过VMM GUI或命令行工具来完成。 需要注意的是,虚拟机的定义文件通常存储在`/etc/libvirt/qemu/`目录中。如果需要克隆虚拟机,可以复制定义文件并进行相应的修改。 总结起来,CentOS 7 KVM虚拟化部署的步骤包括:部署KVM环境、拉入ISO镜像、创建虚拟机、配置网络和创建快照。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [CentOS部署kvm虚拟化机器](https://blog.csdn.net/weixin_45052781/article/details/125988357)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Centos7 安装KVM](https://blog.csdn.net/justlpf/article/details/126720554)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值