Vagrant + Ansible 解析超级慢

7
我使用Vagrant和Ansible创建了一个虚拟机,但当我启动后使用时,发现它非常慢。我还在使用云中的远程数据库,但似乎不是缓慢响应的原因。所谓的缓慢响应是指大多数情况下加载页面需要约5秒钟。
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    # Set the box name and URL
    config.vm.box = "trusty-server"
    config.vm.box = "skeleton"
    config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"

    # Hostmanager plugin config
    config.hostmanager.enabled           = true
    config.hostmanager.manage_host       = true
    config.hostmanager.ignore_private_ip = false
    config.hostmanager.include_offline   = true


    # Fix TTY problem
    config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"

    # Basic box configuration
    config.vm.network "private_network", ip: "192.168.100.102"
    config.vm.hostname = "skeleton.ontherocks.dev"
    config.hostmanager.aliases = %w(en.skeleton.ontherocks.dev)
    config.vm.synced_folder ".", "/vagrant", :mount_options => ['dmode=777', 'fmode=777']

    # Provision using Ansible
    config.vm.provision "ansible" do |ansible|
        ansible.playbook = "provisioning/playbook.yml"
    end

    config.vm.provision :hostmanager
end

操作手册

- hosts: default
  remote_user: vagrant
  sudo: true
  sudo_user: root

  tasks:
    ##
    # Update apt packages
    #
    - name: General | Update apt packages.
      apt: update_cache=yes

    ##
    # Apt package instalation of required software
    #
    - name: General | Install required packages.
      apt: pkg={{ item }} state=present
      with_items:
        - build-essential
        - php5
        - apache2
        - php5-mysql
        - sendmail
        - php5-mcrypt
        - php5-curl
        - php5-gd

    ##
    # Apache2 setup
    #
    - name: Apache | Enable required modules
      action: command a2enmod rewrite vhost_alias

    - name: Apache | Configuration file for our site
      action: template src=templates/etc-apache2-sites-available-website-conf.j2 dest=/etc/apache2/sites-available/website.conf

    - name: Apache | Disable the default site
      action: command a2dissite 000-default

    - name: Apache | Enable our new site
      action: command a2ensite website
      notify:
        - Restart Apache

    - name: PHP | Enable required modules
      action: command php5enmod mcrypt
      notify:
        - Restart Apache

    ##
    # Website setup
    #
    - name: Website | Set up file and directory permissions
      file: path=/vagrant/craft/storage/ mode=0777 recurse=yes state=directory
      sudo: yes

  ##
  # Restart services
  #
  handlers:
    - name: Restart Apache
      service: name=apache2 state=restarted

你使用的是哪个提供程序,比如VirtualBox?你正在提供需要大量磁盘IO的内容吗? - Emil Davtyan
VirtualBox,绝对不会有太多的磁盘IO。主机电脑并不是问题,因为在任何使用虚拟机的电脑上都会出现这种情况。 - mbalparda
尝试使用rsync同步文件夹,然后告诉我问题是否解决:config.vm.synced_folder(".", "/vagrant", type: "rsync") - Emil Davtyan
你是在Windows电脑上吗? - Emil Davtyan
尝试使用rsync来查看是否有帮助,可能是同步的文件夹引起的。 - Emil Davtyan
显示剩余3条评论
1个回答

3

使用nfs加速共享文件夹

 config.vm.synced_folder ".", "/vagrant", :nfs => { :mount_options => ["dmode=777","fmode=777"] }

加速虚拟机网络
 config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--nictype1", "virtio"]        
 end

这给我带来了10-15倍的网络提速。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接