Libvirt集成Ceph RBD详解

环境说明

Ubuntu 16.04 LTS
Ceph 10.2.x(Jewel)

Libvirt配置详解

确认宿主机是否支持KVM虚拟化:

# egrep '(vmx|svm)' --color /proc/cpuinfo

安装libvirt相关工具包:

# apt-get install -y qemu libvirt-bin virtinst

基于共享存储的动态迁移的说明:需要设定一个共享存储空间,让源主机和目的主机都能够连接到共享存储空间上的虚拟媒体文件,包括虚拟磁盘、虚拟光盘和虚拟软盘。否则,即使迁移完成以后,也会因为无法连接虚拟设备,导致无法启动迁移后的虚拟机。动态迁移实际上是把虚拟机的配置封装在一个文件中,然后通过高速网络,把虚拟机配置和内存运行状态从一台物理机迅速传送到另外一台物理机上,期间虚拟机一直保持运行状态。

配置桥接网卡:

安装桥接工具:

# apt-get install bridge-utils

编辑/etc/network/interface配置文件,添加以下内容:

auto br0
iface br0 inet static
address 172.16.17.195
netmask 255.255.254.0
gateway 172.16.16.1
dns-nameservers 172.16.0.9
bridge_ports eth0
bridge_stp off
bridge_fd 0

重启网络服务:

# systemctl disable NetworkManager
# systemctl stop NetworkManager
# /etc/init.d/networking restart

安装图形化桌面管理工具:virt-manager(可选)

# apt-get install virt-manager

安装Web端管理工具:webvirtmgr(推荐)

安装软件及依赖:

# apt-get install git python-pip python-libvirt python-libxml2 novnc supervisor nginx

拉取代码及Django相关环境配置(使用豆瓣源):

# git clone git://github.com/retspen/webvirtmgr.git
# cd webvirtmgr
# pip install -r requirements.txt -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
# ./manage.py syncdb

# ./manage.py collectstatic  #创建超级用户密码并保存
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 (Put: yes)
Username (Leave blank to use 'admin'): admin (Put: your username or login)
E-mail address: username@domain.local (Put: your email)
Password: xxxxxx (Put: your password)
Password (again): xxxxxx (Put: confirm password)
Superuser created successfully.

添加额外的超级用户(可选):

# ./manage.py createsuperuser

附:升级pip后出现ImportError: cannot import name main提示解决办法

在Ubuntu中,升级了pip,再次使用pip 安装相关的python包的时候就出现以下错误:

ImportError: cannot import name main

解决:pip文件在usr/bin目录下,cd进去,进行以下修改。

把下面的三行:

    from pip import main
    if __name__ == '__main__':
        sys.exit(main())

换成下面的三行:

    from pip import __main__
    if __name__ == '__main__':
        sys.exit(__main__._main())

然后问题就解决了。

设置nginx反向代理webvirtmgr

准备webvirtmgr:

# cd ..
# mv webvirtmgr /var/www/

修改web模板(可选):

/var/www/webvirtmgr/templates/base.html
/var/www/webvirtmgr/templates/base_auth.html

编辑/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 
    }
}

编辑/etc/nginx/nginx.conf文件,注释以下内容:

#    server {
#        listen       80 default_server;
#        server_name  localhost;
#        root         /usr/share/nginx/html;
#
#        #charset koi8-r;
#
#        #access_log  /var/log/nginx/host.access.log  main;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        # redirect server error pages to the static page /40x.html
#        #
#        error_page  404              /404.html;
#        location = /40x.html {
#        }
#
#        # redirect server error pages to the static page /50x.html
#        #
#        error_page   500 502 503 504  /50x.html;
#        location = /50x.html {
#        }
#    }

重启代理服务:

# service nginx restart

设置supervisor

编辑/etc/insserv/overrides/novnc文件,修改以下内容:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          nova-novncproxy
# Required-Start:    $network $local_fs $remote_fs $syslog
# Required-Stop:     $remote_fs
# Default-Start:     
# Default-Stop:      
# Short-Description: Nova NoVNC proxy
# Description:       Nova NoVNC proxy
### END INIT INFO

修改文件权限:

# chown -R www-data:www-data /var/www/webvirtmgr

创建/etc/supervisor/conf.d/webvirtmgr.conf文件,添加以下内容使用supervisor管理webvirtmgr服务:

[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
stdout_logfile=/var/log/supervisor/webvirtmgr.log
redirect_stderr=true
user=www-data

[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=www-data

重启服务生效配置:

# /etc/init.d/supervisor restart

更新webvirtmgr(可选)

# cd /var/www/webvirtmgr
# git pull
# ./manage.py collectstatic
# service supervisor restart

设置SSH认证,nginx的用户“www-data”通过用户“webvirtmgr”免密ssh到libvirt服务器

  • 为www-data用户创建.ssh配置文件(webvirtmgr所在主机)
# mkdir /var/www/.ssh
# chmod 700 /var/www/.ssh
# vim /var/www/.ssh/config
StrictHostKeyChecking=no
UserKnownHostsFile=/dev/null
  • 创建SSH公钥,注意必须指定存放路径为/var/www/.ssh/id_rsa(webvirtmgr所在主机)
# ssh-keygen
Enter file in which to save the key (/root/.ssh/id_rsa): /var/www/.ssh/id_rsa
  • 更改文件权限及属主(webvirtmgr所在主机)
# chmod -R 0600 /var/www/.ssh/config
# chown -R www-data:www-data /var/www/.ssh
  • 添加webvirtmgr用户并加入libvirtd组(libvirt所在主机)
# useradd webvirtmgr
# passwd webvirtmgr
# usermod -G libvirtd -a webvirtmgr
  • 为用户webvirtmgr配置.ssh目录(libvirt所在主机)
# mkdir -p /home/webvirtmgr/.ssh
# chmod 700 /home/webvirtmgr/.ssh
# chown -R webvirtmgr:webvirtmgr /home/webvirtmgr
  • 切换到nginx用户并拷贝www-data的公钥到libvirt所在主机(webvirtmgr所在主机)
# su - www-data -s /bin/bash
$ ssh-copy-id webvirtmgr@qemu-kvm-libvirt-host
  • 修改文件权限(libvirt所在主机)
# chmod 0600 /home/webvirtmgr/.ssh/authorized_keys
# chown -R webvirtmgr:webvirtmgr /home/webvirtmgr/.ssh
  • 验证免密SSH登录(webvirtmgr所在主机)
# su - www-data -s /bin/bash
$ ssh webvirtmgr@qemu-kvm-libvirt-host
  • 设置管理libvirt的权限
Create file /etc/polkit-1/localauthority/50-local.d/50-libvirt-remote-access.pkla (permissions for user webvirtmgr):

[Remote libvirt SSH access]
Identity=unix-user:webvirtmgr
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes
  • 重启libvirt服务
# /etc/init.d/libvirtd restart

浏览器访问webvirtmgr

http://<your_webvirtmgr_host>/login/

遇到的问题

通过SSH连接虚拟机会出现web页面过了20s就自动断开连接,一个临时性的解决方法如下:

编辑/usr/lib/python2.7/dist-packages/websockify/websocket.py文件,注释掉以下4行配置:

if not multiprocessing:  <---
        # os.fork() (python 2.4) child reaper
        signal.signal(signal.SIGCHLD, self.fallback_SIGCHLD)  <---
    else:  <---
        # make sure that _cleanup is called when children die
        # by calling active_children on SIGCHLD
        signal.signal(signal.SIGCHLD, self.multiprocessing_SIGCHLD)  <---

使用supervisorctl重启webvirtmgr服务:

# supervisorctl
webvirtmgr                       RUNNING   pid 1162, uptime 8:07:41
webvirtmgr-console               RUNNING   pid 1161, uptime 8:07:41
supervisor> restart webvirtmgr
webvirtmgr: stopped
webvirtmgr: started
supervisor> restart webvirtmgr-console
webvirtmgr-console: stopped
webvirtmgr-console: started
supervisor> status
webvirtmgr                       RUNNING   pid 31908, uptime 0:00:16
webvirtmgr-console               RUNNING   pid 32040, uptime 0:00:08

webvirtmgr接入Ceph块设备参数说明

类型:rbd
名称:rbdpool
Ceph User: admin
Ceph Pool: rbd
Ceph Host: <your_mon_ip>
Secrets: <访问密钥>

存在的问题:

  • secret的用量含义
  • rbd不支持镜像克隆
提示:this function is not supported by the connection driver: storage pool does not support volume creation from an existing volume
posted @ 2020-11-03 10:37  Varden  阅读(778)  评论(0)    收藏  举报