kvm部署
环境说明:
系统类型 | IP |
---|---|
redhat8 | 192.168.47.115 |
centos7 | 192.168.47.140 |
//关闭防火墙
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# reboot
//验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的
[root@localhost ~]# egrep -o 'vmx|svm' /proc/cpuinfo
vmx
vmx
//安装依赖包
[root@localhost ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
[root@localhost ~]# yum -y install qemu-kvm qemu-img virt-manager libvirt libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools qemu-kvm-common cockpit-bridge
//因为虚拟机中网络,我们一般都是和公司的其他服务器是同一个网段,所以我们需要把 \
KVM服务器的网卡配置成桥接模式。这样的话KVM的虚拟机就可以通过该桥接网卡和公司内部 \
其他服务器处于同一网段
//此处我的网卡是ens160,所以用br0来桥接ens160网卡
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens160 ifcfg-br0
[root@localhost network-scripts]# ls
ifcfg-br0 ifcfg-ens160
[root@localhost network-scripts]# vim ifcfg-br0
[root@localhost network-scripts]# cat ifcfg-br0
TYPE="Bridge" #修改模式
BOOTPROTO="static"
NAME="br0" #修改名字
DEVICE="br0" #修改名字
ONBOOT="yes"
IPADDR="192.168.47.115"
PREFIX="24"
GATEWAY="192.168.47.2"
DNS1="114.114.114.114"
[root@localhost network-scripts]# vim ifcfg-ens160
[root@localhost network-scripts]# cat ifcfg-ens160
TYPE="Ethernet"
BOOTPROTO="static"
NAME="ens160"
DEVICE="ens160"
ONBOOT="yes"
BRIDGE="br0" #加入这个
#删除剩下的
//重启网络
[root@localhost network-scripts]# systemctl restart NetworkManager
[root@localhost network-scripts]# ifdown ens160;ifup ens160
成功停用连接 "ens160"(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/3)
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP group default qlen 1000
link/ether 00:0c:29:51:ab:da brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:0c:29:51:ab:da brd ff:ff:ff:ff:ff:ff
inet 192.168.47.115/24 brd 192.168.47.255 scope global noprefixroute br0
valid_lft forever preferred_lft forever
inet6 fe80::f452:dbff:fe1b:346/64 scope link
valid_lft forever preferred_lft forever
//启动服务
[root@localhost ~]# systemctl enable --now libvirtd
//验证安装结果
[root@localhost ~]# lsmod | grep kvm
kvm_intel 294912 0
kvm 786432 1 kvm_intel
irqbypass 16384 1 kvm
//测试并验证安装结果
[root@localhost ~]# virsh -c qemu:///system list
Id 名称 状态
-------------------
[root@localhost ~]# virsh --version
6.0.0
[root@localhost ~]# virt-install --version
2.2.1
//查看网桥信息
[root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c2951abda no ens160
virbr0 8000.5254002ddf57 yes virbr0-nic
[root@localhost ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@localhost ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx 1 root root 21 10月 20 16:13 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
kvm web管理界面安装
//安装依赖包
[root@web ~]# yum -y install epel-release
[root@web ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
//从github上下载webvirtmgr代码
[root@web ~]# cd /usr/local/src/
[root@web src]# 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 | 1.52 MiB/s, done.
处理 delta 中: 100% (3606/3606), done.
[root@web src]# ls
webvirtmgr
//安装webvirtmgr
[root@localhost src]# cd webvirtmgr/
[root@localhost webvirtmgr]# ls
conf dev-requirements.txt interfaces networks serverlog templates
console hostdetail locale README.rst servers Vagrantfile
create images manage.py requirements.txt setup.py vrtManager
deploy instance MANIFEST.in secrets storages webvirtmgr
[root@web webvirtmgr]# pip install -r requirements.txt
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 172kB/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 2.1MB/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
//检查sqlite3是否安装
[root@web 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.
>>> import sqlite3 (没有输出就是正常的)
>>> exit()
//初始化帐号信息
[root@web ~]# cd /usr/local/src/webvirtmgr/
[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'): //指定超级管理员帐号用户名,默认留空为root
Email address: 1@qq.com //设置超级管理员邮箱
Password: //设置超级管理员密码
Password (again): //再次输入超级管理员密码
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)
//拷贝web网页至指定目录
[root@web webvirtmgr]# mkdir /var/www
[root@web webvirtmgr]# cp -r /usr/local/src/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:ZcHCrdYFcYTFSiddsHdaD5KGrmNhzYaZ6QS6LZrdel8 root@web
The key's randomart image is:
+---[RSA 2048]----+
| . o+O=o. |
| o *+=o |
| . =+== o o|
| . .o@o. o =.|
| . .S = . .|
| o + + |
| o . = E |
| + o.. o |
| o oo... |
+----[SHA256]-----+
//cp到kvm主机
[root@web ~]# ssh-copy-id 192.168.47.115
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.47.115 (192.168.47.115)' can't be established.
ECDSA key fingerprint is SHA256:47Zd9Uv2fxtFemLlzz9cOYt4g7gpZpre5p/4hPpvfy0.
ECDSA key fingerprint is MD5:83:58:13:1b:5f:fe:17:00:51:23:29:54:5d:d7:b6:ee.
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.47.115's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.47.115'"
and check to make sure that only the key(s) you wanted were added.
[root@web ~]# ssh 192.168.47.115 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
Last login: Thu Oct 21 03:11:07 2021 from 192.168.47.1
[root@web ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:6080 *:*
LISTEN 0 128 127.0.0.1:8000 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 ::1:6080 :::*
LISTEN 0 128 ::1:8000 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
//配置nginx
[root@web ~]# cd /etc/nginx/
[root@web nginx]# ls
conf.d fastcgi_params mime.types scgi_params win-utf
default.d fastcgi_params.default mime.types.default scgi_params.default
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
[root@web nginx]# mv nginx.conf nginx-beifen.conf
[root@web nginx]# vi /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]# vi /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]# vi /var/www/webvirtmgr/conf/gunicorn.conf.py
.....此处省略N行
bind = '0.0.0.0:8000' //确保此处绑定的是本机的8000端口,这个在nginx配置中定义了,被代理的端口
backlog = 2048
.....此处省略N行
[root@web ~]# systemctl restart nginx
[root@web ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:6080 *:*
LISTEN 0 128 127.0.0.1:8000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 ::1:6080 :::*
LISTEN 0 128 ::1:8000 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@web ~]# vi /etc/supervisord.conf
//加到最下面
[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@web ~]# 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 ~]# systemctl status supervisord
● supervisord.service - Process Monitoring and Control Daemon
Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
Active: active (running) since 四 2021-10-21 02:39:18 CST; 6s ago
Process: 42642 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
Main PID: 42645 (supervisord)
CGroup: /system.slice/supervisord.service
├─42645 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
└─42667 /usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webv...
10月 21 02:39:18 web systemd[1]: Starting Process Monitoring and Control Daemon...
10月 21 02:39:18 web systemd[1]: Started Process Monitoring and Control Daemon.
[root@web ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:6080 *:*
LISTEN 0 128 127.0.0.1:8000 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 ::1:6080 :::*
LISTEN 0 128 ::1:8000 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@web ~]# 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:Apm01mJE+6uj3umbBN+tAudELHOV9dlwABf0GuepftE nginx@web
The key's randomart image is:
+---[RSA 2048]----+
| .+ ooo*o. |
| o *o o * |
| .X.. + + |
| oo++ = . |
| .= o S . o. |
| .oo. + .. E |
| =o o . . . |
| o++ . . . |
| .ooB+. .. |
+----[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@192.168.47.115
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/nginx/.ssh/id_rsa.pub"
/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
Warning: Permanently added '192.168.47.115' (ECDSA) to the list of known hosts.
root@192.168.47.115's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.47.115'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$ exit
登出
[root@web nginx]# systemctl restart nginx
kvm主机
[root@localhost ~]# systemctl restart libvirtd
通过远程连接软件上传ISO镜像文件至存储目录/var/lib/libvirt/images/
[root@localhost ~]# cd /var/lib/libvirt/images
[root@localhost images]# ls
[root@localhost images]# ls
CentOS-7-x86_64-DVD-1804-7.5.iso
解决方法
[root@localhost ~]# yum update libgcrypt
[root@localhost ~]# systemctl restart libvirtd
kvm网络管理
添加桥接网络
实例(虚拟机)创建
问题解决
[root@localhost ~]# ll /etc/rc.local
lrwxrwxrwx 1 root root 13 8月 10 23:31 /etc/rc.local -> rc.d/rc.local
[root@localhost ~]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 474 8月 10 23:31 /etc/rc.d/rc.local
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 474 8月 10 23:31 /etc/rc.d/rc.local
[root@localhost ~]# vi /etc/rc.d/rc.local
......此处省略N行
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup novnc_server 192.168.47.115:5920 &
[root@localhost ~]# . /etc/rc.d/rc.local