栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

kvm虚拟化

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

kvm虚拟化

kvm虚拟化 kvm部署

环境:

主机IP地址
server1192.168.244.143
node3192.168.244.142
在server1上操作

关闭防火墙

[root@server1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@server1 ~]# vim  /etc/selinux/config 

SELINUX=disabled
[root@server1 ~]# reboot

安装epel源和软件

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@server1 ~]#  yum -y install epel-release vim wget net-tools unzip zip gcc gcc-c++

下载软件

[root@server1 ~]#  yum -y install qemu-kvm qemu-kvm-common qemu-img virt-manager libvirt python3-libvirt libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools

配置桥接网卡

[root@server1 network-scripts]# cp  ifcfg-ens33 ifcfg-br0
[root@server1 network-scripts]# ls
ifcfg-br0  ifcfg-ens33
[root@server1 network-scripts]# vim  ifcfg-br0 
[root@server1 network-scripts]# 
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
onBOOT=yes
IPADDR=192.168.244.136
PREFIX=24
GATEWAY=192.168.244.2
DNS1=114.114.114.114

[root@server1 network-scripts]# vim ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
onBOOT=yes
BRIDGE=br0

重启网卡

[root@server1 ~]# systemctl restart NetworkManager

[root@server1 ~]# ip  a
1: lo:  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:  mtu 1500 qdisc fq_codel master br0 state UP group default qlen 1000
    link/ether 00:0c:29:7e:0b:9b brd ff:ff:ff:ff:ff:ff
3: br0:  mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:7e:0b:9b brd ff:ff:ff:ff:ff:ff
    inet 192.168.244.136/24 brd 192.168.244.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe7e:b9b/64 scope link 
       valid_lft forever preferred_lft forever
4: virbr0:  mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:3a:01:4f 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:  mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:3a:01:4f brd ff:ff:ff:ff:ff:ff

启动服务

[root@server1 ~]# systemctl start libvirtd
[root@server1 ~]# systemctl enable libvirtd

查看服务以及版本号

[root@server1 ~]#  lsmod|grep kvm
kvm_intel             315392  0
kvm                   847872  1 kvm_intel
irqbypass              16384  1 kvm
[root@server1 ~]# 
[root@server1 ~]# virsh -c qemu:///system list
 Id   名称   状态
-------------------

[root@server1 ~]# virsh --version
6.0.0
[root@server1 ~]#  virt-install --version
2.2.1
[root@server1 ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
[root@server1 ~]# ll /usr/bin/qemu-kvm
lrwxrwxrwx. 1 root root 21 10月 20 19:34 /usr/bin/qemu-kvm -> /usr/libexec/qemu-kvm
[root@server1 ~]# lsmod |grep kvm
kvm_intel             315392  0
kvm                   847872  1 kvm_intel
irqbypass              16384  1 kvm
[root@server1 ~]# brctl show
bridge name     bridge id               STP enabledinterfaces
br0             8000.000000000000       no   
在node3上操作 kvm web管理界面安装

安装依赖包

[root@node3 ~]# yum -y install git python2-pip python36-libvirt libxml2-python python-websocket-client supervisor nginx python2-devel

从github上下载webvirtmgr代码

[root@node3 src]# git clone git://github.com/retspen/webvirtmgr.git
正克隆到 'webvirtmgr'...
remote: Enumerating objects: 5614, done.
接收对象中:  19% (1067/5614), 204.01 KiB | 289.00 Ki
.....

安装webvirtmgr

[root@node3 webvirtmgr]# pip2 install -r requirements.txt

检查sqlite3是否安装

##[root@node3 webvirtmgr]# ln -s /usr/local/python2 /usr/local/python

[root@node3 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@node3 webvirtmgr]# python manage.py syncdb  ||  ./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: 123@qq.com          //设置超级管理员邮箱
Password:                        //设置超级管理员密码
Password (again):                 //再次输入超级管理员密码
Superuser created successfully. 
Installing custom SQL ...
Installing indexes ...
Installed 6 object(s) from 1 fixture(s)

拷贝web网页至指定目录

[root@node3 webvirtmgr]# mkdir /var/www
[root@node3 webvirtmgr]# cp -r /usr/local/src/webvirtmgr /var/www/
[root@node3 webvirtmgr]# chown -R nginx.nginx /var/www/webvirtmgr/
[root@node3 webvirtmgr]# 

生成密钥

[root@node3 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:Y82SD1PgOnvrVlbhwcL9mZwc8xmTOwtqO3h15seA2HU root@node3
The key's randomart image is:
+---[RSA 2048]----+
|        .. o    .|
|       . .o = o+ |
|        . .o * B=|
|       . =  o.O+E|
|      o S o+.o..o|
|       + *+oo..+ |
|      . .o+ o +o |
|       ..o +   .o|
|       oo . .   .|
+----[SHA256]-----+

[root@node3 ~]# ssh-copy-id root@192.168.244.143
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/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.244.143's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.244.143'"
and check to make sure that only the key(s) you wanted were added.

[root@node3 ~]# ssh  root@192.168.244.143
Last login: Wed Oct 20 20:32:37 2021 from 192.168.244.1
[root@server1 ~]# 

配置nginx

[root@node3 ~]# vim  /etc/nginx/nginx.conf

......
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
......
[root@server1 ~]# 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端口

....
bind = '0.0.0.0:8000'
backlog = 2048
...

重启服务

[root@node3 ~]# systemctl enable --now nginx

设置supervisor

[root@node3 ~]# 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@node3 ~]# systemctl start supervisord
[root@node3 ~]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
[root@node3 ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128     *:8000                *:*                  
LISTEN     0      128     *:80                  *:*                  
LISTEN     0      128     *:22                  *:*                  
LISTEN     0      100    127.0.0.1:25                  *:*                  
LISTEN     0      128    :::22                 :::*                  
LISTEN     0      100       ::1:25                 :::*    

配置nginx用户

[root@node3 home]# 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:/P0RcwRe9lp1mC5dNVjB7kvH0krbOSjzmwB42g2I+4M nginx@node3
The key's randomart image is:
+---[RSA 2048]----+
|              =*O|
|             oo=*|
|             oo.+|
|      ..o   . o= |
|     . oS+   .=o.|
|      . +.+.  o*+|
|     ... ..o..+=+|
|     E..   o.o+=.|
|       ..   ++o .|
+----[SHA256]-----+
-bash-4.2$ touch ~/.ssh/config && echo -e "StrictHostKeyChecking=nonUserKnownHostsFile=/dev/null" >> ~/.ssh/config
-bash-4.2$ chmod 0600 ~/.ssh/config
-bash-4.2$ ssh-copy-id root@192.168.244.143
/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.244.143' (ECDSA) to the list of known hosts.
root@192.168.244.143's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.244.143'"
and check to make sure that only the key(s) you wanted were added.

-bash-4.2$ exit
logout

[root@node3 home]#  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

重启服务

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/338663.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号