centos7下部署kvm並安裝web管理客戶端


參考鏈接

https://www.cnblogs.com/root0/p/9356205.html
https://www.cnblogs.com/kcxg/p/11049829.html
https://blog.csdn.net/weixin_43695104/article/details/88554443#31_kvm_60
https://www.cnblogs.com/nulige/p/9236191.html

 

1.基礎環境配置

setenforce 0
systemctl stop firewalld
systemctl disable firewalld
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
yum install -y wget
ls /etc/yum.repos.d/
rm -rf /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install epel-release -y
yum makecache
vi /etc/resolv.conf 

2.安裝kvm

yum makecache #上面沒有刷新yum源刷新一下
yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install virt-manager #virt-manager為圖形管理工具可以選擇安裝
systemctl enable libvirtd
systemctl start libvirtd
lsmod | grep -i kvm
brctl show #查看網絡
virsh net-list #查看網絡
virsh net-dumpxml default #默認的網絡連接方式

3.橋接設置

3.1vi /etc/sysconfig/network-scripts/ifcfg-ens192 #刪除ip的設置並添加紅字部分

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens192"
UUID="fe8d89d2-fe1a-418b-aea2-682643e8661a"
DEVICE="ens192"
ONBOOT="yes"
IPV6_PRIVACY="no"
BRIDGE=br0
NM_CONTROLLED=no

3.2 vi /etc/sysconfig/network-scripts/ifcfg-br0 #橋接ip設置

TYPE=Bridge
DEVICE=br0
NM_CONTROLLED=no
BOOTPROTO=static
NAME=br0
ONBOOT=yes
IPADDR="192.168.120.54"
PREFIX="24"
GATEWAY="192.168.120.1"
DNS1="223.5.5.5"

3.3 重啟網卡並測速網絡

systemctl restart network
ping www.baidu.com

3.4下載虛擬機鏡像為后面做好准備

 cd /var/lib/libvirt/boot/
wget https://mirrors.aliyun.com/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-Minimal-1810.iso

4.安裝webvirmgr  web管理工具

yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx
yum -y install gcc python-devel
pip install numpy -i https://mirrors.aliyun.com/pypi/simple/ #用阿里源安裝numpy組件
cd /server/tools/
mkdir /server/tools/ -p
git clone git://github.com/retspen/webvirtmgr.git #github克隆文件太慢可以下載zip文件解壓安裝
yum install -y unzip zip

unzip webvirtmgr-master.zip
mv webvirtmgr-master webvirtmgr
cd webvirtmgr/
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple #安裝所需的pip環境

5.設置連接信息並同步配置

5.1
./manage.py syncdb #設置登錄web登錄賬號密碼 admin可以換成root

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'): admin Email address: test@163.com Password: 123456 Password (again): 123456 Superuser created successfully. Installing custom SQL ... Installing indexes ... Installed 6 object(s) from 1 fixture(s)

5.2 .
/manage.py collectstatic #生成配置文件 輸入yes
Type 'yes' to continue, or 'no' to cancel: yes

6.配置nginx解析

6.1

cd ..
mkdir /var/www
mv webvirtmgr /var/www/
cd /etc/nginx/conf.d/

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; # or /srv instead of /var
        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 $scheme;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        client_max_body_size 1024M; # Set higher depending on your needs
    }
}

6.2 關閉默認解析

vi /etc/nginx/nginx.conf #從server行開始注釋 只留最后一個大括號

#    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
#        server_name  _;
#       root         /usr/share/nginx/html;

#       # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;

#       location / {
#        }

#        error_page 404 /404.html;
#            location = /40x.html {
#        }

#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

....
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

 }

6.3

ls /var/www/webvirtmgr/webvirtmgr
chown -R nginx:nginx /var/www/webvirtmgr
service nginx restart #重啟nginx生效

6.4 設置nginx開機啟動

vi /etc/supervisord.d/webvirtmgr.ini

[program:webvirtmgr]
command=/usr/bin/python /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/python /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
service supervisord stop
service supervisord start

http://192.168.120.54/

admin #上面設置的web登錄密碼
123456

7 配置宿主機-如果虛擬機比較多,該腳本執行時間會比較長

curl http://retspen.github.io/libvirt-bootstrap.sh | sudo sh

配置ssh連接

sudo su - nginx -s /bin/bash
-bash-4.2$ ssh-keygen

+-----------------+-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$ cat .ssh/id_rsa.pub 

-bash-4.2$ ssh-copy-id root@192.168.120.54 #拷貝密鑰到認證文件夾下

然后復制客戶端在root下執行

vi /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
chown -R root.root /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla
systemctl restart nginx
systemctl restart libvirtd

8. 然后登錄web連接kvm

http://192.168.120.54/login/

admin

123456

1.添加連接

 

用root用戶 因為上面拷貝密鑰用的root用戶的

-bash-4.2$ ssh-copy-id root@192.168.120.54 #拷貝密鑰到認證文件夾下

 注意 編輯完點擊這里其實是54 點擊其他地方w無法進入后台  之前添加做了lab ip 后期無法修改

 

 

 

9.創建虛擬機

9.1創建存儲池

 

拷貝之前下載虛擬機iso鏡像到默認目錄

cp /var/lib/libvirt/boot/CentOS-7-x86_64-Minimal-1810.iso /var/lib/libvirt/images/

進入存儲目錄

可以看到iso文件

 

9.2 添加系統使用的硬盤

 

 

 

9.3 網絡管理添加橋接網絡

 

網絡類型橋接 名稱br0 然后創建

 

 

准備工作做完開始創建實例

10.

10.1

 10.2

10.3設置連接cdrom

設置web 密碼 應該是vnc的密碼

啟動虛擬機  我已經開機 現在只能關閉虛擬機

web控制台

 

 

 

特別注意 我是在exsi環境做的測試 開始 無法和外網通信 是網卡需要設置混雜模式 設置后網絡正常

 

virt-manager 管理工具
https://www.cnblogs.com/weilu2/p/kvm_bridge_centos7.html
用雲鏡像創建虛擬機
https://www.cnblogs.com/kcxg/p/11049829.html
[root@localhost centos7-vm1]# ssh-keygen -t ed25519 -C "VM Login ssh key"

[root@localhost centos7-vm1]# virsh net-dhcp-leases default
 2019-07-16 18:32:04  52:54:00:98:46:59  ipv4      192.168.122.53/24         centos7-vm1     -


[root@localhost ~]# cat .ssh/id_ed25519.pub 
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBnkO6rNDbgZbtGsq69NvOfqpLtkEJWIxrC29ymjwTx0 VM Login ssh key

[root@localhost centos7-vm1]# ssh vivek@192.168.122.53

[vivek@centos7-vm1 ~]$ sudo passwd root
123456

web界面 可能花屏

修改默認的顯卡模式為

然后關閉虛擬機 再開機虛擬機就正常了

 虛擬機 壓縮和轉換

qemu-img convert -c -O qcow2 /var/lib/libvirt/images/centos7-vm1/centos7-vm1.qcow2 /var/lib/libvirt/images/centos7-vm1/centos7-new.qcow2

qemu-img convert -c -f raw -O qcow2 /path/old.raw /path/new.qcow2

https://www.cnblogs.com/sixloop/p/8515099.html

轉換

[root@localhost images]# qemu-img info linux66.img #名字是img 實際是qcow2格式

qemu-img convert -c -f raw -O qcow2 /path/old.raw /path/new.qcow2

qemu-img convert -c -O qcow2  /var/lib/libvirt/images/linux66.img /var/lib/libvirt/images/centos7-60.qcow2

virtio驅動

https://blog.csdn.net/yasyal515/article/details/77649486
https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.141-1/virtio-win-0.1.141.iso
https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM