KVM虚拟化安装部署及管理教程

时间:2022-03-02 12:00:00
目录
  • 1.kvm部署
    • 1.1 kvm安装
    • 1.2 kvm web管理界面安装
    • 1.3 kvm web界面管理
      • 1.3.1 kvm连接管理
      • 1.3.2 kvm存储管理
      • 1.3.3 kvm网络管理
      • 1.3.4 实例管理
  • 故障

1.kvm部署

 

1.1 kvm安装

  1. //关闭防火墙和selinux
  2. [root@kvm ~]# systemctl disable --now firewalld.service
  3. Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
  4. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  5. [root@kvm ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  6. [root@kvm ~]# reboot
  7.  
  8. //下载epel源和工具包
  9. [root@kvm ~]# yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++
  10.  
  11. //验证CPU是否支持KVM;如果结果中有vmx(Intel)或svm(AMD)字样,就说明CPU的支持的
  12. [root@kvm ~]# egrep -o 'vmx|svm' /proc/cpuinfo
  13.  
  14. //安装kvm
  15. [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
  16.  
  17. //桥接网卡,用br0来桥接ens160网卡
  18. [root@kvm ~]# cd /etc/sysconfig/network-scripts/
  19. [root@kvm network-scripts]# cp ifcfg-ens33 ifcfg-br0
  20. [root@kvm network-scripts]# cat ifcfg-br0
  21. TYPE=Bridge
  22. DEVICE=br0
  23. NM_CONTROLLED=no
  24. BOOTPROTO=static
  25. NAME=br0
  26. ONBOOT=yes
  27. IPADDR=192.168.237.131
  28. NETMASK=255.255.255.0
  29. GATEWAY=192.168.237.2
  30. DNS1=114.114.114.114
  31. DNS2=8.8.8.8
  32. [root@kvm network-scripts]# cat ifcfg-ens33
  33. TYPE=Ethernet
  34. BOOTPROTO=static
  35. NAME=ens33
  36. DEVICE=ens33
  37. ONBOOT=yes
  38. BRIDGE=br0
  39. NM_CONTROLLED=no
  40.  
  41. //重启网络
  42. [root@kvm ~]# systemctl restart network
  43. [root@kvm ~]# ip a
  44. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  45. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  46. inet 127.0.0.1/8 scope host lo
  47. valid_lft forever preferred_lft forever
  48. inet6 ::1/128 scope host
  49. valid_lft forever preferred_lft forever
  50. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
  51. link/ether 00:0c:29:7b:10:a5 brd ff:ff:ff:ff:ff:ff
  52. inet6 fe80::20c:29ff:fe7b:10a5/64 scope link
  53. valid_lft forever preferred_lft forever
  54. 3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  55. link/ether 00:0c:29:7b:10:a5 brd ff:ff:ff:ff:ff:ff
  56. inet 192.168.237.131/24 brd 192.168.237.255 scope global br0
  57. valid_lft forever preferred_lft forever
  58. inet6 fe80::20c:29ff:fe7b:10a5/64 scope link
  59. valid_lft forever preferred_lft forever
  60. 4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
  61. link/ether 52:54:00:1c:33:d6 brd ff:ff:ff:ff:ff:ff
  62. inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
  63. valid_lft forever preferred_lft forever
  64. 5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
  65. link/ether 52:54:00:1c:33:d6 brd ff:ff:ff:ff:ff:ff
  66.  
  67. //启动服务
  68. [root@kvm ~]# systemctl enable --now libvirtd
  69.  
  70. //验证安装结果
  71. [root@kvm ~]# lsmod|grep kvm
  72. kvm_intel 188740 0
  73. kvm 637289 1 kvm_intel
  74. irqbypass 13503 1 kvm
  75.  
  76. //测试
  77. [root@kvm ~]# virsh -c qemu:///system list
  78. Id 名称 状态
  79. ----------------------------------------------------
  80.  
  81. [root@kvm ~]# virsh --version
  82. 4.5.0
  83. [root@kvm ~]# virt-install --version
  84. 1.5.0
  85. [root@kvm ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
  86. [root@kvm ~]# ll /usr/bin/qemu-kvm
  87. lrwxrwxrwx 1 root root 21 10 20 23:14 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
  88.  
  89. //查看网桥信息
  90. [root@kvm ~]# brctl show
  91. bridge name bridge id STP enabled interfaces
  92. br0 8000.000c297b10a5 no ens33
  93. virbr0 8000.5254001c33d6 yes virbr0-nic

1.2 kvm web管理界面安装

kvm 的 web 管理界面是由 webvirtmgr 程序提供的。

  1. //安装依赖包
  2. [root@kvm ~]# yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx python-devel
  3. //从github上下载webvirtmgr代码
  4. [root@kvm ~]# cd /usr/local/src/
  5. [root@kvm src]# git clone git://github.com/retspen/webvirtmgr.git
  6. 正克隆到 'webvirtmgr'...
  7. remote: Enumerating objects: 5614, done.
  8. remote: Total 5614 (delta 0), reused 0 (delta 0), pack-reused 5614
  9. 接收对象中: 100% (5614/5614), 2.97 MiB | 29.00 KiB/s, done.
  10. 处理 delta 中: 100% (3606/3606), done.
  11. //安装webvirtmgr
  12. [root@kvm src]# cd webvirtmgr/
  13. [root@kvm webvirtmgr]# pip install -r requirements.txt
  14. Collecting django==1.5.5 (from -r requirements.txt (line 1))
  15. Downloading https://files.pythonhosted.org/packages/38/49/93511c5d3367b6b21fc2995a0e53399721afc15e4cd6eb57be879ae13ad4/Django-1.5.5.tar.gz (8.1MB)
  16. 100% |████████████████████████████████| 8.1MB 49kB/s
  17. ......
  18. //检查sqlite3是否安装
  19. [root@kvm webvirtmgr]# python
  20. Python 2.7.5 (default, Nov 16 2020, 22:23:17)
  21. [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
  22. Type "help", "copyright", "credits" or "license" for more information.
  23. >>> import sqlite3
  24. >>> exit()
  25. //初始化账号信息
  26. [root@kvm webvirtmgr]# python manage.py syncdb
  27. WARNING:root:No local_settings file found.
  28. Creating tables ...
  29. Creating table auth_permission
  30. Creating table auth_group_permissions
  31. Creating table auth_group
  32. Creating table auth_user_groups
  33. Creating table auth_user_user_permissions
  34. Creating table auth_user
  35. Creating table django_content_type
  36. Creating table django_session
  37. Creating table django_site
  38. Creating table servers_compute
  39. Creating table instance_instance
  40. Creating table create_flavor
  41. You just installed Django's auth system, which means you don't have any superusers defined.
  42. Would you like to create one now? (yes/no): yes
  43. Username (leave blank to use 'root'): admin
  44. Email address: 123@qq.com
  45. Password:
  46. Password (again):
  47. Superuser created successfully.
  48. Installing custom SQL ...
  49. Installing indexes ...
  50. Installed 6 object(s) from 1 fixture(s)
  51. //拷贝web网页至指定目录
  52. [root@kvm webvirtmgr]# mkdir /var/www
  53. [root@kvm webvirtmgr]# cp -r /usr/local/src/webvirtmgr /var/www/
  54. [root@kvm webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
  55. //生成密钥
  56. [root@kvm ~]# ssh-keygen -t rsa
  57. Generating public/private rsa key pair.
  58. Enter file in which to save the key (/root/.ssh/id_rsa):
  59. Created directory '/root/.ssh'.
  60. Enter passphrase (empty for no passphrase):
  61. Enter same passphrase again:
  62. Your identification has been saved in /root/.ssh/id_rsa.
  63. Your public key has been saved in /root/.ssh/id_rsa.pub.
  64. The key fingerprint is:
  65. SHA256:icyLAYmyxABKsogsIHmJqGjSby0ogFwf1p2zeiPwuxY root@kvm
  66. The key's randomart image is:
  67. +---[RSA 2048]----+
  68. |O+ . . . . |
  69. |/ooo o . + |
  70. |&*+ o . o |
  71. |X+.. = . o |
  72. |= o..* S |
  73. |. . +o.E o |
  74. | . .... = . |
  75. | o |
  76. | ... |
  77. +----[SHA256]-----+
  78. [root@kvm ~]# ssh-copy-id 192.168.237.131
  79. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  80. The authenticity of host '192.168.237.131 (192.168.237.131)' can't be established.
  81. ECDSA key fingerprint is SHA256:/AR9dYUN0PN9LOHYWfHeUe5LgyczVMH9mYv9+2GcAbM.
  82. ECDSA key fingerprint is MD5:30:f6:de:5a:7d:c2:08:b5:b7:31:61:4a:4e:dd:32:73.
  83. Are you sure you want to continue connecting (yes/no)? yes
  84. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  85. /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
  86. root@192.168.237.131's password:
  87. Number of key(s) added: 1
  88. Now try logging into the machine, with: "ssh '192.168.237.131'"
  89. and check to make sure that only the key(s) you wanted were added.
  90. //配置端口转发
  91. [root@kvm ~]# ssh 192.168.237.131 -L localhost:8000:localhost:8000 -L localhost:6080:localhost:60
  92. Last login: Wed Oct 20 23:12:00 2021 from 192.168.237.1
  93. [root@kvm ~]# ss -anlt
  94. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  95. LISTEN 0 128 127.0.0.1:6080 *:*
  96. LISTEN 0 128 127.0.0.1:8000 *:*
  97. LISTEN 0 128 *:111 *:*
  98. LISTEN 0 5 192.168.122.1:53 *:*
  99. LISTEN 0 128 *:22 *:*
  100. LISTEN 0 100 127.0.0.1:25 *:*
  101. LISTEN 0 128 [::1]:6080 [::]:*
  102. LISTEN 0 128 [::1]:8000 [::]:*
  103. LISTEN 0 128 [::]:111 [::]:*
  104. LISTEN 0 128 [::]:22 [::]:*
  105. LISTEN 0 100 [::1]:25 [::]:*
  106. //配置nginx
  107. [root@kvm ~]# cd /etc/nginx/
  108. [root@kvm nginx]# ls
  109. conf.d fastcgi_params mime.types scgi_params win-utf
  110. default.d fastcgi_params.default mime.types.default scgi_params.default
  111. fastcgi.conf koi-utf nginx.conf uwsgi_params
  112. fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
  113. [root@kvm nginx]# cp nginx.conf nginx.conf-bak //备份
  114. [root@kvm nginx]# cat nginx.conf
  115. user nginx;
  116. worker_processes auto;
  117. error_log /var/log/nginx/error.log;
  118. pid /run/nginx.pid;
  119. include /usr/share/nginx/modules/*.conf;
  120. events {
  121. worker_connections 1024;
  122. }
  123. http {
  124. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  125. '$status $body_bytes_sent "$http_referer" '
  126. '"$http_user_agent" "$http_x_forwarded_for"';
  127. access_log /var/log/nginx/access.log main;
  128. sendfile on;
  129. tcp_nopush on;
  130. tcp_nodelay on;
  131. keepalive_timeout 65;
  132. types_hash_max_size 2048;
  133. include /etc/nginx/mime.types;
  134. default_type application/octet-stream;
  135. include /etc/nginx/conf.d/*.conf;
  136. server {
  137. listen 80;
  138. server_name localhost;
  139. include /etc/nginx/default.d/*.conf;
  140. location / {
  141. root html;
  142. index index.html index.htm;
  143. }
  144. error_page 404 /404.html;
  145. location = /40x.html {
  146. }
  147. error_page 500 502 503 504 /50x.html;
  148. location = /50x.html {
  149. }
  150. }
  151. }
  152. [root@kvm conf.d]# pwd
  153. /etc/nginx/conf.d
  154. [root@kvm conf.d]# vi webvirtmgr.conf
  155. [root@kvm conf.d]# cat webvirtmgr.conf
  156. server {
  157. listen 80 default_server;
  158. server_name $hostname;
  159. #access_log /var/log/nginx/webvirtmgr_access_log;
  160. location /static/ {
  161. root /var/www/webvirtmgr/webvirtmgr;
  162. expires max;
  163. }
  164. location / {
  165. proxy_pass http://127.0.0.1:8000;
  166. proxy_set_header X-Real-IP $remote_addr;
  167. proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
  168. proxy_set_header Host $host:$server_port;
  169. proxy_set_header X-Forwarded-Proto $remote_addr;
  170. proxy_connect_timeout 600;
  171. proxy_read_timeout 600;
  172. proxy_send_timeout 600;
  173. client_max_body_size 1024M;
  174. }
  175. }
  176. //确保bind绑定的是本机的8000端口
  177. [root@kvm ~]# vim /var/www/webvirtmgr/conf/gunicorn.conf.py
  178. bind = '0.0.0.0:8000' //修改此行
  179. backlog = 2048
  180. //启动nginx
  181. [root@kvm ~]# systemctl enable --now nginx
  182. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
  183. [root@kvm ~]# ss -anlt
  184. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  185. LISTEN 0 128 127.0.0.1:6080 *:*
  186. LISTEN 0 128 127.0.0.1:8000 *:*
  187. LISTEN 0 128 *:111 *:*
  188. LISTEN 0 128 *:80 *:*
  189. LISTEN 0 5 192.168.122.1:53 *:*
  190. LISTEN 0 128 *:22 *:*
  191. LISTEN 0 100 127.0.0.1:25 *:*
  192. LISTEN 0 128 [::1]:6080 [::]:*
  193. LISTEN 0 128 [::1]:8000 [::]:*
  194. LISTEN 0 128 [::]:111 [::]:*
  195. LISTEN 0 128 [::]:22 [::]:*
  196. LISTEN 0 100 [::1]:25 [::]:*
  197. //设置supervisor
  198. [root@kvm ~]# vim /etc/supervisord.conf
  199. #在最后添加下面的内容
  200. [program:webvirtmgr]
  201. command=/usr/bin/python2 /var/www/webvirtmgr/manage.py run_gunicorn -c /var/www/webvirtmgr/conf/gunicorn.conf.py
  202. directory=/var/www/webvirtmgr
  203. autostart=true
  204. autorestart=true
  205. logfile=/var/log/supervisor/webvirtmgr.log
  206. log_stderr=true
  207. user=nginx
  208. [program:webvirtmgr-console]
  209. command=/usr/bin/python2 /var/www/webvirtmgr/console/webvirtmgr-console
  210. directory=/var/www/webvirtmgr
  211. autostart=true
  212. autorestart=true
  213. stdout_logfile=/var/log/supervisor/webvirtmgr-console.log
  214. redirect_stderr=true
  215. user=nginx
  216. //启动supervisor
  217. [root@kvm ~]# systemctl enable --now supervisord
  218. Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
  219. [root@kvm ~]# systemctl status supervisord
  220. ● supervisord.service - Process Monitoring and Control Daemon
  221. Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
  222. Active: active (running) since 三 2021-10-20 23:53:33 CST; 12s ago
  223. Process: 46734 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
  224. Main PID: 46737 (supervisord)
  225. //配置nginx用户
  226. [root@kvm ~]# su - nginx -s /bin/bash
  227. -bash-4.2$ ssh-keygen -t rsa
  228. Generating public/private rsa key pair.
  229. Enter file in which to save the key (/var/lib/nginx/.ssh/id_rsa):
  230. Created directory '/var/lib/nginx/.ssh'.
  231. Enter passphrase (empty for no passphrase):
  232. Enter same passphrase again:
  233. Your identification has been saved in /var/lib/nginx/.ssh/id_rsa.
  234. Your public key has been saved in /var/lib/nginx/.ssh/id_rsa.pub.
  235. The key fingerprint is:
  236. SHA256:S46h+CYFvCGW+6z68PXZgbKNLLdPdiPD6LmzPOpYBwI nginx@kvm
  237. The key's randomart image is:
  238. +---[RSA 2048]----+
  239. | |
  240. | |
  241. |E.. |
  242. |oo+ |
  243. |o.o+ . S |
  244. | o.o.+ * . |
  245. |. =.* O * |
  246. | =oX=X * o |
  247. |++**%B= . |
  248. +----[SHA256]-----+
  249. -bash-4.2$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=no\nUserKnownHostsFile=/dev/null" >> ~/.ssh/config
  250. -bash-4.2$ chmod 0600 ~/.ssh/config
  251. -bash-4.2$ ssh-copy-id root@192.168.237.131
  252. /bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/nginx/.ssh/id_rsa.pub"
  253. /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  254. /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  255. Warning: Permanently added '192.168.237.131' (ECDSA) to the list of known hosts.
  256. root@192.168.237.131's password:
  257. Number of key(s) added: 1
  258. Now try logging into the machine, with: "ssh 'root@192.168.237.131'"
  259. and check to make sure that only the key(s) you wanted were added.
  260. -bash-4.2$ exit
  261. 登出
  262. [root@kvm ~]# vim /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
  263. [Remote libvirt SSH access]
  264. Identity=unix-user:root
  265. Action=org.libvirt.unix.manage
  266. ResultAny=yes
  267. ResultInactive=yes
  268. ResultActive=yes
  269. [root@kvm ~]# chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
  270. [root@kvm ~]# systemctl restart nginx
  271. [root@kvm ~]# systemctl restart libvirtd

1.3 kvm web界面管理

通过ip地址在浏览器*问kvm

KVM虚拟化安装部署及管理教程

1.3.1 kvm连接管理

创建SSH连接:

KVM虚拟化安装部署及管理教程

KVM虚拟化安装部署及管理教程

KVM虚拟化安装部署及管理教程

1.3.2 kvm存储管理

创建存储:

KVM虚拟化安装部署及管理教程

KVM虚拟化安装部署及管理教程

进入存储:

KVM虚拟化安装部署及管理教程

KVM虚拟化安装部署及管理教程

通过远程连接软件上传ISO镜像文件至存储目录/var/lib/libvirt/images/

  1. [root@kvm ~]# ls /var/lib/libvirt/images/
  2. CentOS-8.4.2105-x86_64-dvd1.iso

在 web 界面查看ISO镜像是否存在

KVM虚拟化安装部署及管理教程

创建系统安装镜像

KVM虚拟化安装部署及管理教程

KVM虚拟化安装部署及管理教程

KVM虚拟化安装部署及管理教程

1.3.3 kvm网络管理

添加桥接网络

KVM虚拟化安装部署及管理教程

KVM虚拟化安装部署及管理教程

KVM虚拟化安装部署及管理教程

1.3.4 实例管理

实例(虚拟机)创建

KVM虚拟化安装部署及管理教程

KVM虚拟化安装部署及管理教程

KVM虚拟化安装部署及管理教程

虚拟机插入光盘

KVM虚拟化安装部署及管理教程

设置在 web *问虚拟机的密码

KVM虚拟化安装部署及管理教程

启动虚拟机

KVM虚拟化安装部署及管理教程

打开控制台

KVM虚拟化安装部署及管理教程

安装虚拟机

KVM虚拟化安装部署及管理教程

安装完成

KVM虚拟化安装部署及管理教程

故障

web界面无法访问,命令行报错(accept: Too many open files)

  1. nginx进行配置
  2. [root@kvm ~]# vim /etc/nginx/nginx.conf
  3. user nginx;
  4. worker_processes auto;
  5. error_log /var/log/nginx/error.log;
  6. pid /run/nginx.pid;
  7. worker_rlimit_nofile 655350; //添加此行
  8. [root@kvm ~]# systemctl restart nginx.service
  9.  
  10. 对系统参数进行设置
  11. [root@kvm ~]# vim /etc/security/limits.conf
  12. # End of file //添加下面两行
  13. * soft nofile 655350
  14. * hard nofile 655350
  15.  
  16. 重启虚拟机,就能成功访问
  17. [root@kvm ~]# reboot

以上就是KVM虚拟化安装部署及管理教程的详细内容,更多关于KVM虚拟化安装部署及管理的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/tianwailaiwu_/article/details/120872354