| 系统选择 | ip | 主机名 | 网卡配置 |
|---|---|---|---|
| CentOS7 | 192.168.149.146 | kmv | NAT |
| CentOS7 | 192.168.149.147 | web | NAT |
kvm部署

配置网络
[root@kvm ~]# cd /etc/sysconfig/network-scripts/
[root@kvm network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@kvm network-scripts]# vim ifcfg-br0
[root@kvm network-scripts]# cat ifcfg-br0
TYPE=Bridge
OTPROTO=static
NM_CONTROLLED=no
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.149.146
NETMASK=255.255.255.0
GATEWAY=192.168.149.2
DNS1=114.114.114.114
[root@kvm network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
OTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
NM_CONTROLLED=no
关闭防火墙
[root@kvm ~]# systemctl disable --now firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@kvm ~]# setenforce 0
[root@kvm ~]# reboot
[root@kvm ~]# yum install bridge-utils
[root@kvm ~]# brctl addbr br0
[root@kvm ~]# brctl addif br0 ens33
[root@kvm ~]# brctl stp br0 on

安装工具,验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的
[root@kvm ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
[root@kvm ~]# egrep -o 'vmx|svm' /proc/cpuinfo
svm
svm
svm
sv
svm
svm
svm
svm
安装KVM
[root@kvm ~]# 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
启动服务
[root@kvm ~]# systemctl enable --now libvirtd
[root@kvm ~]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since 一 2021-06-21 01:26:18 CST; 6s ago
Docs: man:libvirtd(8)
https://libvirt.org
Main PID: 13741 (libvirtd)
Tasks: 19 (limit: 32768)
CGroup: /system.slice/libvirtd.service
├─13741 /usr/sbin/libvirtd
├─13901 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-scr...
└─13902 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-scr...
6月 21 01:26:18 kvm systemd[1]: Started Virtualization daemon.
6月 21 01:26:19 kvm dnsmasq[13901]: started, version 2.76 cachesize 150
6月 21 01:26:19 kvm dnsmasq[13901]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN DHCP DHCPv6 no-...otify
6月 21 01:26:19 kvm dnsmasq-dhcp[13901]: DHCP, IP range 192.168.122.2 -- 192.168.122.254, lease time 1h
6月 21 01:26:19 kvm dnsmasq-dhcp[13901]: DHCP, sockets bound exclusively to interface virbr0
6月 21 01:26:19 kvm dnsmasq[13901]: reading /etc/resolv.conf
6月 21 01:26:19 kvm dnsmasq[13901]: using nameserver 114.114.114.114#53
6月 21 01:26:19 kvm dnsmasq[13901]: read /etc/hosts - 2 addresses
6月 21 01:26:19 kvm dnsmasq[13901]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
6月 21 01:26:19 kvm dnsmasq-dhcp[13901]: read /var/lib/libvirt/dnsmasq/default.hostsfile
Hint: Some lines were ellipsized, use -l to show in full.
验证安装结果
[root@kvm ~]# lsmod|grep kvm
kvm_amd 2177260 0
kvm 621480 1 kvm_amd
irqbypass 13503 1 kvm
[root@kvm ~]# virsh -c qemu:///system list
Id 名称 状态
----------------------------------------------------
[root@kvm ~]# virsh --version
4.5.0
[root@kvm ~]# virt-install --version
1.5.0
[root@kvm ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@kvm ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx. 1 root root 21 6月 21 01:27 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
查看网桥信息
[root@kvm ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29b18cad yes ens33
virbr0 8000.52540052bfdf yes virbr0-nic
kvm web 页面安装
[root@web ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
[root@web ~]# yum -y install epel-release
从github上下载webvirtmgr代码
[root@web ~]# git clone git://github.com/retspen/webvirtmgr.git
正克隆到 'webvirtmgr'...
remote: Enumerating objects: 5614, done.
remote: Total 5614 (delta 0), reused 0 (delta 0), pack-reused 5614
接收对象中: 100% (5614/5614), 2.97 MiB | 982.00 KiB/s, done.
处理 delta 中: 100% (3606/3606), done.
安装webvirtmgr并检查sqlite3是否安装
[root@web ~]# ls
anaconda-ks.cfg webvirtmgr
[root@kvm ~]# cd webvirtmgr/
[root@kvm webvirtmgr]# pip install -r requirements.txt
[root@kvm webvirtmgr]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
#没有出现其他的就表示安装好了
初始化账号信息
[root@web 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'):
Email address: 2574610915@qq.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
将网站移动到指定目录并设置属组和属主
[root@web webvirtmgr]# mkdir -p /var/www
[root@web webvirtmgr]# cp -r /root/webvirtmgr/ /var/www/
[root@web webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
做免密登录
[root@web ~]# 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:RmMupccb4dFt7+ewUsE6KTK1x4XywIwBW5XZpV4RQD0 root@web
The key's randomart image is:
+---[RSA 2048]----+
| .....=o++. |
| o..o...E. |
| . B=. +o.. |
| O.+*o.o+ |
| o S. *.+.. |
| +ooo B.. |
| .o o oo .|
| . = |
| .. .|
+----[SHA256]-----+
[root@web ~]# ssh-copy-id root@192.168.149.146
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.149.146 (192.168.149.146)' can't be established.
ECDSA key fingerprint is SHA256:Pht7GyMDMgqMQnLc1zhkzF44ElrHbSsgQesqf86uDkY.
ECDSA key fingerprint is MD5:df:73:a3:89:58:f3:49:0a:2c:97:f6:64:8b:c0:bd:e9.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.149.146's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.149.146'"
and check to make sure that only the key(s) you wanted were added.
[root@web ~]# ssh 192.168.149.146
Last login: Mon Jun 21 01:23:43 2021 from 192.168.149.200
配置nginx
[root@web ~]# cd /etc/nginx/
[root@web nginx]# mv nginx.conf nginx.conf-bak
[root@web nginx]# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;
include /etc/nginx/default.d/*.conf;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
[root@web nginx]# vim /etc/nginx/conf.d/webvirtmgr.conf
[root@web nginx]# cat /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;
}
}
修改bind绑定的是本机的8000端口
[root@web nginx]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
......
bind = '0.0.0.0:8000'
backlog = 2048
......
修改supervisord.conf文件
[root@web nginx]# cat >> /etc/supervisord.conf <<EOF
> [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
> EOF
启动supervisord
[root@web nginx]# systemctl enable --now supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
[root@web nginx]# systemctl restart nginx
[root@web nginx]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:8000 *:*
LISTEN 0 100 *:6080 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 128 [::]:22 [::]:*
配置nginx账户,做免密登录
[root@web nginx]# su - nginx -s /bin/bash
-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:ea3Ev7EuVWCJLcbPiuh6VQAaLzYfqdKJ3reA42fvcks nginx@web
The key's randomart image is:
+---[RSA 2048]----+
| . .. . o . |
| + .. = = |
| = + o = . |
| + * . o..o . |
| o + ..So+... |
| . + . oo.o. |
| + o.E. ..o |
| . .+++. . + |
| .o.B=. o+ |
+----[SHA256]-----+
-bash-4.2$ ssh-copy-id root@192.168.149.146
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/nginx/.ssh/id_rsa.pub"
The authenticity of host '192.168.149.146 (192.168.149.146)' can't be established.
ECDSA key fingerprint is SHA256:Pht7GyMDMgqMQnLc1zhkzF44ElrHbSsgQesqf86uDkY.
ECDSA key fingerprint is MD5:df:73:a3:89:58:f3:49:0a:2c:97:f6:64:8b:c0:bd:e9.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.149.146's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.149.146'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$ exit
[root@web nginx]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
[root@web nginx]# cat /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@web nginx]# systemctl restart nginx
[root@kvm ~]# systemctl restart libvirtd
关闭防火墙
[root@web ~]# setenforce 0
[root@web ~]# systemctl stop firewalld
web页面配置



[root@kvm ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 38G 2.4G 35G 7% /
/dev/sda1 1014M 151M 864M 15% /boot
/dev/mapper/centos-home 19G 33M 19G 1% /home
tmpfs 378M 0 378M 0% /run/user/0
/dev/sr0 4.4G 4.4G 0 100% /mnt
[root@kvm ~]# cd /
[root@kvm /]# mkdir kvm

记得把镜像拖进自己选择的文件里面
[root@kvm /]# cd kvm/
[root@kvm kvm]# ls
CentOS-7-x86_64-DVD-1908.iso














1526



被折叠的 条评论
为什么被折叠?



