1.kvm部署
系统 | ip | 主机名 |
centos7 | 192.168.59.142 | 142 |
centos7 | 192.168.59.143 | 143 |
部署前请确保你的CPU虚拟化功能已开启。分为两种情况:
- 虚拟机要关机设置CPU虚拟化
- 物理机要在BIOS里开启CPU虚拟化
#关闭防火墙和selinux [root@142 ~]# systemctl disable --now firewalld [root@142 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted #安装依赖 yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++ #验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的 [root@142 ~]# egrep -o 'vmx|svm' /proc/cpuinfo vmx vmx vmx vmx vmx vmx vmx vmx #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
因为虚拟机中网络,我们一般都是和公司的其他服务器是同一个网段,所以我们需要把KVM服务器的网卡配置成桥接模式。这样的话KVM的虚拟机就可以通过该桥接网卡和公司内部 其他服务器处于同一网段 此处我的网卡是ens33,所以用br0来桥接ens33网卡
[root@142 ~]# cd /etc/sysconfig/network-scripts/ [root@142 network-scripts]# ls ifcfg-br0 ifdown-eth ifdown-post ifdown-TeamPort ifup-eth ifup-plip ifup-sit init.ipv6-global ifcfg-ens33 ifdown-ib ifdown-ppp ifdown-tunnel ifup-ib ifup-plusb ifup-Team network-functions ifcfg-lo ifdown-ippp ifdown-routes ifup ifup-ippp ifup-post ifup-TeamPort network-functions-ipv6 ifdown ifdown-ipv6 ifdown-sit ifup-aliases ifup-ipv6 ifup-ppp ifup-tunnel ifdown-bnep ifdown-isdn ifdown-Team ifup-bnep ifup-isdn ifup-routes ifup-wireless [root@142 network-scripts]# cat ifcfg-br0 TYPE=Bridge BOOTPROTO=static NM_CONTROLLED=no NAME=br0 DEVICE=br0 ONBOOT=yes IPADDR=192.168.59.142 NETMASK=255.255.255.0 GATEWAY=192.168.59.2 DNS1=114.114.114.114 [root@142 network-scripts]# cat ifcfg-ens33 TYPE=Ethernet BOOTPROTO=static NAME=ens33 DEVICE=ens33 ONBOOT=yes BRIDGE=br0 NM_CONTROLLED=no
重启网络
[root@142 network-scripts]# systemctl restart network [root@142 network-scripts]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000 link/ether 00:0c:29:56:9e:d8 brd ff:ff:ff:ff:ff:ff inet6 fe80::20c:29ff:fe56:9ed8/64 scope link valid_lft forever preferred_lft forever 3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 00:0c:29:56:9e:d8 brd ff:ff:ff:ff:ff:ff inet 192.168.59.142/24 brd 192.168.59.255 scope global br0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe56:9ed8/64 scope link valid_lft forever preferred_lft forever 4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:78:c6:02 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000 link/ether 52:54:00:78:c6:02 brd ff:ff:ff:ff:ff:ff
启动服务
[root@142 network-scripts]# systemctl start libvirtd [root@142 network-scripts]# systemctl status libvirtd ● libvirtd.service - Virtualization daemon Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2021-07-22 12:27:59 EDT; 1 day 15h ago Docs: man:libvirtd(8) https://libvirt.org Main PID: 3186 (libvirtd) Tasks: 19 (limit: 32768) CGroup: /system.slice/libvirtd.service ├─3186 /usr/sbin/libvirtd ├─3301 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/... └─3302 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/... Jul 22 12:28:00 localhost.localdomain dnsmasq[3292]: listening on virbr0(#4): 192.168.122.1 Jul 22 12:28:00 localhost.localdomain dnsmasq[3301]: started, version 2.76 cachesize 150 Jul 22 12:28:00 localhost.localdomain dnsmasq[3301]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN DHCP DHC...tify Jul 22 12:28:00 localhost.localdomain dnsmasq-dhcp[3301]: DHCP, IP range 192.168.122.2 -- 192.168.122.254, lease time 1h Jul 22 12:28:00 localhost.localdomain dnsmasq-dhcp[3301]: DHCP, sockets bound exclusively to interface virbr0 Jul 22 12:28:00 localhost.localdomain dnsmasq[3301]: reading /etc/resolv.conf Jul 22 12:28:00 localhost.localdomain dnsmasq[3301]: using nameserver 114.114.114.114#53 Jul 22 12:28:00 localhost.localdomain dnsmasq[3301]: read /etc/hosts - 2 addresses Jul 22 12:28:00 localhost.localdomain dnsmasq[3301]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses Jul 22 12:28:00 localhost.localdomain dnsmasq-dhcp[3301]: read /var/lib/libvirt/dnsmasq/default.hostsfile Hint: Some lines were ellipsized, use -l to show in full.
验证并测试安装结果
[root@142 ~]# lsmod |grep kvm kvm_intel 188740 0 kvm 637289 1 kvm_intel irqbypass 13503 1 kvm [root@142 ~]# virsh -c qemu:///system list Id Name State ---------------------------------------------------- [root@142 ~]# virsh --version 4.5.0 [root@142 ~]# virt-install --version 1.5.0 [root@142 ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm [root@142 ~]# which kvm /usr/bin/which: no kvm in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin) [root@142 ~]# which qemu-kvm /usr/bin/qemu-kvm
#查看网桥信息 [root@142 ~]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c29569ed8 no ens33 virbr0 8000.52540078c602 yes virbr0-nic
2 安装kvm web管理界面安装
#安装依赖包 [root@143~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel #从github上下载webvirtmgr代码 [root@143 ~]# cd /usr/local/src/ [root@143 src]# git clone git://github.com/retspen/webvirtmgr.git Cloning into 'webvirtmgr'... #安装webvirtmgr [root@143 src]# cd webvirtmgr/ [root@143 webvirtmgr]# pip install -r requirements.txt #检查sqlite3是否安装 [root@143 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@143 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: 790650910@qq.com //设置超级管理员邮箱 Password: //设置超级管理员密码 Password (again): //再次输入超级管理员密码 Superuser created successfully. Installing custom SQL ... Installing indexes ... Installed 6 object(s) from 1 fixture(s) #拷贝网页到指定目录 [root@143 webvirtmgr]# cd .. [root@143 src]# mkdir -p /var/www [root@143 src]# cp -r webvirtmgr/ /var/www/ [root@143 src]# ll total 4 drwxr-xr-x 20 root root 4096 Jul 24 05:29 webvirtmgr [root@143 src]# chown -R nginx.nginx /var/www/webvirtmgr/ [root@143 src]# ll /var/www/ total 4 drwxr-xr-x 20 nginx nginx 4096 Jul 24 05:34 webvirtmgr #生成秘钥 [root@143 src]# 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:NdHNeHbGYVEcS++sKTbRjctrFvRtNpurxf6xdHwpl1A root@143 The key's randomart image is: +---[RSA 2048]----+ | .. + O*| | .o B B| | o o E.| | . . .o= | | S .oo.=| | o+*B| | +.=%B| | . oO+=| | +o++| +----[SHA256]-----+ [root@143 src]# ssh-copy-id root@192.168.59.142 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.59.142 (192.168.59.142)' can't be established. ECDSA key fingerprint is SHA256:RmUyUj8k7idYXko0DM/lQzl4d7ycRPlbhGbFdd1zgvI. ECDSA key fingerprint is MD5:88:36:21:bf:3b:88:3e:ae:04:7e:df:47:1b:fd:6c:1d. 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 ^[[A root@192.168.59.142's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.59.142'" and check to make sure that only the key(s) you wanted were added. [root@143 src]# ssh root@192.168.59.142 'date' Sat Jul 24 05:40:04 EDT 2021
配置nginx
[root@143 nginx]# vim 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@143 nginx]# 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; } } #设置supervisord [root@143 nginx]# vim /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 #启动supervisor并设置开机自启 [root@143 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@143 nginx]# 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 Sat 2021-07-24 06:51:36 EDT; 1min 47s ago Process: 3002 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS) Main PID: 3005 (supervisord) Tasks: 3 CGroup: /system.slice/supervisord.service ├─3005 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf └─3006 /usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console Jul 24 06:51:36 143 systemd[1]: Starting Process Monitoring and Control Daemon... Jul 24 06:51:36 143 systemd[1]: Started Process Monitoring and Control Daemon. [root@143 nginx]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port 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:6010 *:* LISTEN 0 128 *:8000 *:* LISTEN 0 100 *:6080 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 100 [::1]:25 [::]:* LISTEN 0 128 [::1]:6010 [::]:* LISTEN 0 128 [::]:111 [::]:*
配置Nginx用户
[root@143 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:KkK+usTho4uDmsdpTERv68wtC6f9qINPZqtEi05DbW0 nginx@143 The key's randomart image is: +---[RSA 2048]----+ | | | . | | . . | | o + | | =.+ E S | |*o= o . | |o#B=o.. | |@=XO=o. | |@@*o++. | +----[SHA256]-----+ -bash-4.2$ -bash-4.2$ ssh-copy-id root@192.168.59.142 /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.59.142 (192.168.59.142)' can't be established. ECDSA key fingerprint is SHA256:RmUyUj8k7idYXko0DM/lQzl4d7ycRPlbhGbFdd1zgvI. ECDSA key fingerprint is MD5:88:36:21:bf:3b:88:3e:ae:04:7e:df:47:1b:fd:6c:1d. 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.59.142's password: Authentication failed. -bash-4.2$ ssh-copy-id root@192.168.59.142 /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 root@192.168.59.142's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.59.142'" and check to make sure that only the key(s) you wanted were added. -bash-4.2$ ssh root@192.168.59.142 'date' Sat Jul 24 12:08:32 EDT 2021 #远程连接配置 [root@143 nginx]# 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@143 nginx]# systemctl restart nginx [root@142 ~]# systemctl restart libvirtd
3.kvm web界面管理
通过ip192.168.59.143访问kvm
创建ssh连接
创建并进入存储
通过远程连接软件传iso文件到/var/lib/libvirt/images下
添加镜像
添加桥接网络
实例(虚拟机)创建
虚拟机插入光盘
设置在 web *问虚拟机的密码
启动虚拟机