关于vagrant:开发环境的转变从-vagrant-到-docker

开发环境、生产环境: 开发环境:就是程序猿在本人的电脑上写代码,并测试生产环境:赚钱用的环境就是生产环境老旧的模式、新模式: 老模式,就是虚拟机新模式,就是容器 在远古期间,程序猿在本人的电脑上,应用 vagrant + vbox 搭建开发环境 然而新的 todo

September 24, 2022 · 1 min · jiezi

关于vagrant:使用vagrant-virtualBox快速构建Linux虚拟机

1、下载并装置vagrant2、指定门路下初始化:vagrant init centos/73、启动虚拟环境vagrant up(4、间接下载慢,能够先下载好镜像再加载进去:cd 到.box文件的目录下,执行 vagrant box add centos/7 CentOS-7-x86_64-Vagrant-1803_01.VirtualBox.box而后再vagrant up) 5、连贯虚拟机vagrant ssh6、之后启动linux虚拟机只须要在Vagrantfile文件所在门路下执行vagrant upvagrant ssh即可

February 28, 2022 · 1 min · jiezi

关于vagrant:Vagrant-局域网访问

开发中须要和前端或其余后端共事配合,须要间接拜访我本机开发环境 在vagrant文件夹中的Vagrantfile配置下即可: # -*- mode: ruby -*-# vi: set ft=ruby : Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| if File.exist? aliasesPath then config.vm.provision "file", source: aliasesPath, destination: "/tmp/bash_aliases" config.vm.provision "shell" do |s| s.inline = "awk '{ sub(\"\r$\", \"\"); print }' /tmp/bash_aliases > /home/vagrant/.bash_aliases && chown vagrant:vagrant /home/vagrant/.bash_aliases" end end #公有拜访地址 config.vm.network "private_network", ip: "192.168.33.10" if File.exist? homesteadYamlPath then settings = YAML::load(File.read(homesteadYamlPath)) elsif File.exist? homesteadJsonPath then settings = JSON::parse(File.read(homesteadJsonPath)) else abort "Homestead settings file not found in #{confDir}" end Homestead.configure(config, settings) if File.exist? afterScriptPath then config.vm.provision "shell", path: afterScriptPath, privileged: false, keep_color: true end if File.exist? customizationScriptPath then config.vm.provision "shell", path: customizationScriptPath, privileged: false, keep_color: true end if Vagrant.has_plugin?('vagrant-hostsupdater') config.hostsupdater.remove_on_suspend = false config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] } elsif Vagrant.has_plugin?('vagrant-hostmanager') config.hostmanager.enabled = true config.hostmanager.manage_host = true config.hostmanager.aliases = settings['sites'].map { |site| site['map'] } end end

June 23, 2021 · 1 min · jiezi

关于vagrant:vagrant常见问题总结

记录vagrant应用过程中遇到的问题,会继续补充 降级virtualbox后,呈现挂载host目录问题环境 host win10virtualbox 6.1.22vm centos 7问题景象:之前vm设置过与host的文件共享,Vagrantfile中配置 config.vm.synced_folder "D:/work/vagrant/data", "/vagrant_data"virtualbox从6.1.16降级到6.1.22, 降级后vagrant up显示如下问题 ==> pg12: Checking for guest additions in VM...==> pg12: Setting hostname...==> pg12: Configuring and enabling network interfaces...==> pg12: Mounting shared folders... pg12: /vagrant => D:/work/vagrant/vms/pg12Vagrant was unable to mount VirtualBox shared folders. This is usuallybecause the filesystem "vboxsf" is not available. This filesystem ismade available via the VirtualBox Guest Additions and kernel module.Please verify that these guest additions are properly installed in theguest. This is not a bug in Vagrant and is usually caused by a faultyVagrant box. For context, the command attempted was:mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrantThe error output from the command was:/sbin/mount.vboxsf: mounting failed with the error: Invalid argument解决办法:ssh登陆vm,将vagrant(vagrant为vm默认登陆用户,依据理论状况配置)增加到vboxsf,vboxusers 组 ...

June 15, 2021 · 1 min · jiezi

关于vagrant:从已有的vagrant虚拟机中打包镜像

应用 vagrant 的好一个益处就是能够想合作中分享咱们的 Box 打包上面就介绍一下打包形式 首先确定须要打包哪台虚拟机,进入装置 VirtualBox 的装置目录 默认在这个门路 C:\Program Files\Oracle\VirtualBoxcd "C:\Program Files\Oracle\VirtualBox"查看所有的虚拟机,能够看到有显示格局如下: "vmname" {vmid} ,咱们这里须要的是 vmname > ./VBoxManage.exe list vms"django-twitter_default_1619620682201_12310" {b3c74405-5d50-4f05-9f0e-98c9563f1fa5}将指标切换到你须要的门路中,而后执行以下的目录 vagrant package --base django-twitter_default_1619620682201_12310 --output demo_ubuntu18.box好了,这个时候你的门路下就会生成一个 demo_ubuntu18.box 的文件 应用打包的好的镜像光有打包是不够的,因为打包的目标是为了应用 box 镜像。应用的时候有两个留神点,那就是因为 ssh 密钥的缘故,你 add ,而后 up 之后会发现进不去,这是就须要去批改 vagrantfile 这个文件,将默认的密钥登录改为账号密码登录。 ssh 密钥谬误如下 > vagrant upBringing machine 'default' up with 'virtualbox' provider...==> default: Importing base box 'hashicorp/bionic64_demo_ubuntu18'...==> default: Matching MAC address for NAT networking...==> default: Setting the name of the VM: twitter-term-1_default_1623336326135_46711==> default: Clearing any previously set network interfaces...==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly==> default: You are trying to forward to privileged ports (ports <= 1024). Most==> default: operating systems restrict this to only privileged process (typically==> default: processes running as an administrative user). This is a warning in case==> default: the port forwarding doesn't work. If any problems occur, please try a==> default: port higher than 1024.==> default: Forwarding ports... default: 8000 (guest) => 80 (host) (adapter 1) default: 22 (guest) => 2222 (host) (adapter 1)==> default: Booting VM...==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying...Timed out while waiting for the machine to boot. This means thatVagrant was unable to communicate with the guest machine withinthe configured ("config.vm.boot_timeout" value) time period.If you look above, you should be able to see the error(s) thatVagrant had when attempting to connect to the machine. These errorsare usually good hints as to what may be wrong.If you're using a custom box, make sure that networking is properlyworking and you're able to connect to the machine. It is a commonproblem that networking isn't setup properly in these boxes.Verify that authentication configurations are also setup properly,as well.If the box appears to be booting properly, you may want to increasethe timeout ("config.vm.boot_timeout") value.然而账号密码登录不如密钥登录平安,所以第一次(账号+明码)登录之后,倡议改为密钥登陆初始化如果你还没有 vagrantfile 文件,才须要初始化操作 ...

June 10, 2021 · 3 min · jiezi

关于vagrant:mac-使用-vagrant-部署-linux-环境配置端口导出修复目录共享

装置 Vagrant装置很简略,去官网下载安装 DMG 文件,双击点开装置即可。下载链接:https://www.vagrantup.com/downloads。 启动 Centos 8$ mkdir ~/vagrant/centos8$ cd ~/vagrant/centos8$ vagrant init centos/8配置端口导出和共享目录vim ~/vagrant/centos8/Vagrantfile增加配置项: config.vm.network "forwarded_port", guest: 5671, host: 5671config.vm.synced_folder "/Users/kun/workspace", "/workspace"查看《残缺配置文件》。 启动$ vagrant up如果启动报如下谬误,请看下一节解决方案: Vagrant was unable to mount VirtualBox shared folders. This is usuallybecause the filesystem "vboxsf" is not available. This filesystem ismade available via the VirtualBox Guest Additions and kernel module.Please verify that these guest additions are properly installed in theguest. This is not a bug in Vagrant and is usually caused by a faultyVagrant box. For context, the command attempted was:mount -t vboxsf -o uid=1000,gid=1000 workspace /workspaceThe error output from the command was:mount: /workspace: unknown filesystem type 'vboxsf'.修复共享目录报错装置虚拟机依赖: ...

March 25, 2021 · 1 min · jiezi

关于vagrant:vagrant使用

装置vagrantvagrant_2.2.10 装置virtualboxvirtualbox-6.1.14 装置vagrant插件vagrant-vbguest插件 vagrant plugin install vagrant-vbguestvagrant-disksize插件 vagrant plugin install vagrant-disksizevagrant-share插件 vagrant plugin install vagrant-share 查看vagrant插件vagrant plugin listvagrant-disksize (0.1.3, global)vagrant-share (1.1.11, global)vagrant-vbguest (0.25.0, global) vagrant常用命令#列出本地的box文件vagrant box list#在空文件夹初始化虚拟机vagrant init NAME [URL]#在初始化完的文件夹内启动虚拟机vagrant up#重启虚拟机vagrant reload#以默认账号vagrant连贯虚拟机vagrant ssh#敞开虚拟机vagrant halt#挂起启动的虚拟机vagrant suspend#查找虚拟机的运行状态vagrant status#销毁以后虚拟机vagrant destory罕用的镜像Vagrant boxes search Ubuntu 18.04 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.boxCentOS 7 https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box应用举例 #启动一个 Ubuntu 18.04 的虚拟机vagrant init ubuntu-bionic https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box#启动一个 CentOS 7 的虚拟机vagrant init centos7 https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.boxVagrantfile罕用配置阐明网络配置 Private network(公有网络) 长处:能够应用一个固定IP连贯虚拟机 毛病:其余团队成员不能拜访你的虚拟机# 固定IPconfig.vm.network "private_network", ip: "192.168.50.4" #设置动静IP#config.vm.network "private_network", type: "dhcp"端口转发虚拟机参数配置共享目录Vagrantfile示例# -*- mode: ruby -*-# vi: set ft=ruby :# All Vagrant configuration is done below. The "2" in Vagrant.configure# configures the configuration version (we support older styles for# backwards compatibility). Please don't change it unless you know what# you're doing.# vagrant起始配置块Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. #定义box名称 config.vm.box = "ubuntu18" #定义虚拟机名字 config.vm.hostname = "rtm" #设置磁盘容量,须要装置vagrant-disksize插件 config.disksize.size = "80GB" #---------------------SSH相干配置-------------------------------------------------------- # config.ssh.username = "vagrant" #设置默认ssh用户(默认用户是vagrant) # config.ssh.password = "vagrant" #设置默认ssh明码(默认明码是vagrant) # config.ssh.port = 22 #设置ssh端口 # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # NOTE: This will enable public access to the opened port # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # Create a private network, which allows host-only access to the machine # using a specific IP. config.vm.network "private_network", ip: "192.168.33.10" # ---------------------基于virtualbox的一些配置-------------------------------------------- config.vm.provider "virtualbox" do |vb| vb.memory = "8192" # 在virtualbox中显示的名字 vb.name = "rtm" # 指定虚拟机内核数 vb.cpus = 2 end # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network "public_network" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # # View the documentation for the provider you are using for more # information on available options. # Enable provisioning with a shell script. Additional provisioners such as # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision "shell", inline: <<-SHELL # apt-get update # apt-get install -y apache2 # SHELLend驯服迷人的Vagrant!Vagrant应用国内镜像装置插件和box镜像 ...

October 20, 2020 · 3 min · jiezi

关于vagrant:vagrant使用

装置vagrantvagrant_2.2.10 装置virtualboxvirtualbox-6.1.14 装置vagrant插件vagrant-vbguest插件 vagrant plugin install vagrant-vbguestvagrant-disksize插件 vagrant plugin install vagrant-disksizevagrant-share插件 vagrant plugin install vagrant-share 查看vagrant插件vagrant plugin listvagrant-disksize (0.1.3, global)vagrant-share (1.1.11, global)vagrant-vbguest (0.25.0, global) vagrant常用命令#列出本地的box文件vagrant box list#在空文件夹初始化虚拟机vagrant init NAME [URL]#在初始化完的文件夹内启动虚拟机vagrant up#重启虚拟机vagrant reload#以默认账号vagrant连贯虚拟机vagrant ssh#敞开虚拟机vagrant halt#挂起启动的虚拟机vagrant suspend#查找虚拟机的运行状态vagrant status#销毁以后虚拟机vagrant destory罕用的镜像Vagrant boxes search Ubuntu 18.04 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.boxCentOS 7 https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box应用举例 #启动一个 Ubuntu 18.04 的虚拟机vagrant init ubuntu-bionic https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box#启动一个 CentOS 7 的虚拟机vagrant init centos7 https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.boxVagrantfile罕用配置阐明网络配置 Private network(公有网络) 长处:能够应用一个固定IP连贯虚拟机 毛病:其余团队成员不能拜访你的虚拟机# 固定IPconfig.vm.network "private_network", ip: "192.168.50.4" #设置动静IP#config.vm.network "private_network", type: "dhcp"端口转发虚拟机参数配置共享目录Vagrantfile示例# -*- mode: ruby -*-# vi: set ft=ruby :# All Vagrant configuration is done below. The "2" in Vagrant.configure# configures the configuration version (we support older styles for# backwards compatibility). Please don't change it unless you know what# you're doing.# vagrant起始配置块Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. #定义box名称 config.vm.box = "ubuntu18" #定义虚拟机名字 config.vm.hostname = "rtm" #设置磁盘容量,须要装置vagrant-disksize插件 config.disksize.size = "80GB" #---------------------SSH相干配置-------------------------------------------------------- # config.ssh.username = "vagrant" #设置默认ssh用户(默认用户是vagrant) # config.ssh.password = "vagrant" #设置默认ssh明码(默认明码是vagrant) # config.ssh.port = 22 #设置ssh端口 # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # NOTE: This will enable public access to the opened port # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # Create a private network, which allows host-only access to the machine # using a specific IP. config.vm.network "private_network", ip: "192.168.33.10" # ---------------------基于virtualbox的一些配置-------------------------------------------- config.vm.provider "virtualbox" do |vb| vb.memory = "8192" # 在virtualbox中显示的名字 vb.name = "rtm" # 指定虚拟机内核数 vb.cpus = 2 end # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network "public_network" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # # View the documentation for the provider you are using for more # information on available options. # Enable provisioning with a shell script. Additional provisioners such as # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision "shell", inline: <<-SHELL # apt-get update # apt-get install -y apache2 # SHELLend驯服迷人的Vagrant!Vagrant应用国内镜像装置插件和box镜像 ...

October 15, 2020 · 3 min · jiezi

关于vagrant:Vagrant更好的个人开发环境

简介一般来说,线上环境和预公布环境都有短缺的配置化,既能够分明的确认环境的组成细节,又能够高效疾速从0重建。然而集体的开发测试环境,可能就没有这么完满。兴许是公司专用的几台测试机,兴许是本人的本地环境。折腾出了问题,还是比拟麻烦的。我集体认为,Docker做开发环境,也是差强人意,还是本地环境或是虚拟机比拟适宜。显然、一个良好,可疾速从新构建的开发环境,能够极大的进步本人的效率。(开发环境折腾坏了,是常有的事。如果能疾速重建就很好了。)如果还能将你的环境,轻松分享给其他人。也能够解决“我那里还能跑,为什么到你这里就跑不了”的经典问题。而Vagrant就是这样一种工具,能够管制其余虚拟化工具比方VirtualBox和VMware,并且能配置文件从0重建环境。而且也能和Ansible、Chef、Puppet等自动化工具联动。(如果其余环境应用上述自动化工具构建,那么Vagrant能够轻松重用。最初,本文选用Win10 + Virtualbox + Vagrant 作为根底,在CentOS零碎中应用rpm形式繁难装置MySQL。 1、下载安装Vagrant的装置形式和虚拟机相似,除了软件自身,还须要镜像。Vagrant应用一种非凡的自制镜像,以.box结尾。须要从官网或第三方资源处获取。【留神,这种非凡镜相辨别虚拟机工具,比方Virtuabox和VMware的镜像不同】 软件本体间接从官网下载即可:https://www.vagrantup.com/downloads非凡镜像有如下几种获取形式: Vagrant官网: https://app.vagrantup.com/boxes/search【速度较慢操作系统官网: http://cloud.centos.org/centos/7/vagrant/x86_64/images/ 【这里是centos的例子第三方镜像站:http://www.vagrantbox.es/ 【官网可能太慢,能够思考从第三方站点获取镜像这里没有阐明虚拟化环境自身的装置,VirtualBox或者VMware均可,间接从对应官网装置即可。2、根底应用办法2.1、疾速上手:Vagrant是命令行工具,装置完之后,能够在powershell或者cmd中应用。想要疾速启动一个Vagrant虚拟机,能够应用如下操作。 倡议应用一个空文件夹进行操作,镜像不肯定要在这个目录中。小技巧:在文件夹空白处:Shift + 右键,即可在该地位关上Powershell或者cmdvagrant box add [env_name] [url/path] # 导入镜像,这里也能够应用本地下载好的镜像门路,不肯定是url。vagrant init [env_name] # 初始化。vagrant up #启动vagrant ssh #ssh进入虚拟机通过上述几条指令,就能够载入下载好的镜像,初始化、并启动一个虚拟机、ssh进入虚拟机。 第二步init要留神,命名和第一条命令的命名要统一。win10也可装置ssh,控制面板中能够关上。 2.2、其余常用命令、留神点:vagrant box add  #增加根底镜像配置vagrant init # 初始化虚拟机(初始化后,可自行批改配置文件进行定制vagrant up  # 创立并启动虚拟机vagrant halt  # 敞开虚拟机vagrant destroy #销毁以后虚拟机vagrant status  #查看虚拟机运行状态vagrant ssh  # SSH 至虚拟机vagrant package #导出镜像上述几条命令,根本就能够轻松应用vagrant了。不过还有几点须要留神: vagrant package 能够将本人的镜像打包,即可轻松分享给其他人。(实用于变动较少的镜像)PS:更倡议的形式是,共享根底镜像+vagrantfile配置文件。间接分享配置文件即可,更加快捷。(如果频繁变动,间接编辑配置文件会比拟轻松,不要在镜像里手动批改,难以确认到底做了什么)vagrant destroy会销毁实例,也就是virtualbox中看到的虚拟机。然而不会销毁配置,destroy之后,能够间接up重建。默认用户不是root,然而能够sudo执行命令,无需明码。(如果有其余须要,自行批改即可。随时都能够用vagrant status确认虚拟机状态,是否启动、进行等。参考:https://blog.csdn.net/hel12he/article/details/51089774 3、更多定制化vagrant init初始化完结后,会在当前目录生成一个 Vagrantfile,你能够对其进行批改、实现各种定制化。比方config.vm.network "private_network", ip: "192.168.33.10" 批改网卡默认ip。 一个根底的初始文件很简略,大略是这样: # -*- mode: ruby -*-# vi: set ft=ruby :Vagrant.configure("2") do |config|  config.vm.box = "MySQL-test00" # 在这里减少其余配置项end依据依据须要,减少各种配置项即可。【比方网卡采纳桥接、cpu核数,内存等等。更多配置项能够参考:https://www.vagrantup.com/docs/vagrantfile ...

August 6, 2020 · 1 min · jiezi

K8S集群学习笔记整理一

教程地址:《kubernetes + docker 全套教程》 1、vagrant创建5台机器#节点主机规划10.4.7.11 master-1110.4.7.12 master-1210.4.7.21 worker-2110.4.7.22 worker-2210.4.7.200 tool-200master-11='10.4.7.11'master-12='10.4.7.12'worker-21='10.4.7.21'worker-22='10.4.7.22'‘控制面板网络和 Internet网络连接’ 找不到 适配器,卸载、重新安装VirtualBox、选择加载 host-only驱动。重要:如果vagrant创建私有网络失败,卸载、重装virtual-box、选择加载net驱动。 1.1 环境初始化xshell 多窗口并行执行,机器批量初始化设置。 #!/bin/bash### 0 允许密码认证登录if [[ $(id | grep root) == "" ]]; then sudo su echo "go to root"fiid### 1、环境初始化# 1.1 关闭防火墙功能systemctl stop firewalldsystemctl disable firewalld# 1.2.关闭selinuxsed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/configsetenforce 0# 1.3 关闭swap,启动项swapoff -ased -i.bak '/swap/s/^/#/' /etc/fstab# 1.4 服务器规划result=$(cat /etc/hosts | grep "节点主机")if [[ "$result" != "" ]]; then echoelse cat <<EOF >> /etc/hosts#节点主机10.4.7.11 master-1110.4.7.12 master-1210.4.7.21 worker-2110.4.7.22 worker-2210.4.7.200 tool-200# GitHub githubusercontent 超时备用199.232.68.133 raw.githubusercontent.comEOFfi# 1.5 临时主机名配置方法,vagrant设置、略 hostnamectl set-hostname master1# 1.6 时间同步:ntp、chronytimedatectl set-timezone Asia/Shanghaiyum install chrony -ycat <<EOF > /etc/chrony.confserver ntp1.aliyun.com iburst minpoll 4 maxpoll 10server ntp2.aliyun.com iburst minpoll 4 maxpoll 10server ntp3.aliyun.com iburst minpoll 4 maxpoll 10EOFsystemctl start chronyd.servicesystemctl enable chronyd.service# 1.7 开启转发,即要求iptables不对bridge的数据进行处理cat <<EOF > /etc/sysctl.d/k8s.confnet.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1EOFsysctl -p /etc/sysctl.d/k8s.conf### 2、docker安装# 2.1 更新主机源yum install -y yum-utils device-mapper-persistent-data lvm2 wget bash-completion.noarchdocker-ce.repowget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repowget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repowget -O /etc/yum.repos.d/docker-ce.repo "https://download.docker.com/linux/centos/docker-ce.repo"sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repocat <<EOF > /etc/yum.repos.d/kubernetes.repo[kubernetes]name=Kubernetesbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/enabled=1gpgcheck=1repo_gpgcheck=1gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpgEOF# 2.2 安装docker,kubelet kubeadm kubectl#yum remove docker docker-common docker-selinux docker-engine -yyum clean all && yum makecache fastyum install -y docker-ce kubelet kubeadm kubectl systemctl start kubelet systemctl enable kubelet# 2.3 docker配置cgroup驱动mkdir -p /etc/docker cat <<EOF > /etc/docker/daemon.json{ "graph": "/data/docker", "storage-driver": "overlay2", "registry-mirrors": ["https://kuogup1r.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"]}EOF##-b, --bridge="" 桥接一个系统上的网桥设备到 Docker 容器里,当使用 none 可以停用容器里的网络##--bip="" 使用 CIDR 地址来设定网络桥的 IP。此参数和 -b 不能一起使用。每个容器使用一个段:172.7.xx.1--255,桥地址是1##"live-restore": true,//dockerd挂掉是否保活容器(避免了docker服务异常而造成容器退出)systemctl daemon-reload systemctl start docker systemctl enable dockerecho ">> 完成!"./core.sh 初始化脚本允许重复执行。以下操作,主要在操作主机 tool-200 进行。 ...

June 2, 2020 · 6 min · jiezi

突发HashiCorp禁止在中国使用企业版VAULT软件

前言昨天HashiCorp突然发布一则消息,禁止在中国使用Vault软件的企业版本,官方申明是这样的: HashiCorp的解释是因为中国的出口管制的原因导致无法出售HASHICORP软件或者使用企业版的Vault。所以在没有取得HashiCorp书面协议的前提下,不得在中国境内使用,部署和安装HashiCorp的Vault企业版本软件。 注意,这里只是禁止使用企业版本的Vault软件,个人版本和HashiCorp公司的其他软件并不在此限制之内。大家不要被网络上面的谣言所迷惑,一定要勇于探索真理。HashiCorp公司介绍那么这个影响到底对我们有多大呢?我们先看下HashiCorp公司的成长史。 更多精彩内容且看: 区块链从入门到放弃系列教程-涵盖密码学,超级账本,以太坊,Libra,比特币等持续更新Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新Spring 5.X系列教程:满足你对Spring5的一切想象-持续更新java程序员从小工到专家成神之路(2020版)-持续更新中,附详细文章教程 HashiCorp于2012年成立,由Mitchell Hashimoto和Armon Dadgar创办,并陆续推出了Vagrant、Packer 、 Terraform、Consul , Vault 和 Nomad以满足不同的需求。 HashiCorp专注于提供DevOps基础设施自动化工具,集开发、运营和安全性于一体,可以帮助开发者编写和部署应用程序,加速应用程序分发,助力企业提升开发效率。公司还推出了一个商业平台Atlas,为公共云服务供应商和私人云技术公司等提供支持。 HashiCorp于2014年获得了1000万美元A轮融资。并在最近,也就是2020-03-18月E轮融资获得了1.75亿美元。主要投资方包括:GGV纪源资本,红点投资,Mayfield Fund,IVP (Institutional Venture Partners)等知名机构。 HashiCorp采用开源的方式和云厂商合作,为云的使用提供了一套通用的工作流程。合作方包括2000多家上市公司。 在2019 胡润研究院发布《2019胡润全球独角兽榜》,HashiCorp排名第138位。 HashiCorp旗下的软件 HashiCor提供了一整套的技术服务,涵盖了云服务的每一层,帮助企业轻松在云环境中操作,每个产品都是为特定的云基础设置自动化来服务的。 区分下来,可以分为Provision,Secure,Connect和Run四个部分。 ProvisionProvision的意思就是安装。 Terraform可以实现用代码的形式来安装cloud或者infrastructure。基础结构即代码,使用 Terraform 配置语言可以轻松跨整个工作流实现资源管理自动化。 基本上大部分的公有云都支持使用Terraform。 Secure 安装好基础组件之后,那么就需要保证他们使用的安全性。那么就需要用到Vault。也就是今天被禁止使用的Vault。 Vault是一款企业级私密信息管理工具。 在企业级应用开发过程中,我们每时每刻都在使用到私密信息,包括密码,密钥,token等等。那么如果在公司内部的开发者之间共享这些密码,密钥,token就是一个很实在的问题。 而Vault就是这样的一套统一的管理私密信息的接口。 难道被禁的原因是Vault的安全性协议? Connect安全性也保证了,那么接下来就是连接服务了。 Consul是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件。在国内有大量的使用案例。 Run 最后就是运行了,Nomad可以用来对容器进行管理和调度。从而更加快捷的部署和更加方便的管理线上资源。 总结虽然目前被禁用的只是Vault的企业版本,但是还是让人感到深深的危机感,中国的企业什么时候能够做出世界级的软件平台,让我们拭目以待! 本文作者:flydean程序那些事本文链接:http://www.flydean.com/hashicorp-terms-of-vault/ 本文来源:flydean的博客 欢迎关注我的公众号:程序那些事,更多精彩等着您!

May 30, 2020 · 1 min · jiezi

virtualbox-解决文件挂载问题-相当详细

废话不多说首先,我们要知道,我们使用 vagrant init centos/7vagrant up会有下面的问题: Vagrant was unable to mount VirtualBox shared folders. This is usuallybecause the filesystem "vboxsf" is not available. This filesystem ismade available via the VirtualBox Guest Additions and kernel module.Please verify that these guest additions are properly installed in theguest. This is not a bug in Vagrant and is usually caused by a faultyVagrant box. For context, the command attempted was:mount -t vboxsf -o uid=1000,gid=1000 home_vagrant_labs /home/vagrant/labsThe error output from the command was:/sbin/mount.vboxsf: mounting failed with the error: No such device因为没有在 vagrant box 中安装 VBoxGuestAdditions ( centos/7 base box 就没装,不怪你们)虚拟机安装VBoxAdditions增强功能,该功能有如下作用: ...

July 9, 2019 · 2 min · jiezi

Vagrant-极简教程

Vagrant 可以理解为是一个管理虚拟机的工具,我们常用它快速创建虚拟镜像来进行开发或者测试等等。官方文档为 https://www.vagrantup.com/docs/ 安装# macOSbrew cask install virtualboxbrew cask install vagrant因为 Vagrant 只是个单纯管理虚拟机的工具,所以是需要先安装虚拟机的 快速入门这个 demo 将教你通过 Vagrantfile 快速创建 3 个 ubuntu 的虚拟镜像。首先需要你先添加 Ubuntu 的 box,具体有哪些 box ,可以到https://app.vagrantup.com/boxes/search 这里来查找 vagrant box add generic/ubuntu1804接下来就是一个漫长的等待过程(如果你没开代理的话),这种时候你可以复制它的下载链接,然后通过迅雷等下载工具来进行下载,下载完成后再来添加 box vagrant box add --name generic/ubuntu1804 /path/to/xxxxx编辑 Vagrantfile Vagrant.configure(2) do |config| # 指定使用哪个 box config.vm.box = "generic/ubuntu1804" # 指定使用哪个虚拟机 config.vm.provider "virtualbox" do |vb| # 给virtualbox虚拟机指定资源配置,其他配置项可以看https://www.vagrantup.com/docs/virtualbox/configuration.html # 记住,不同的虚拟机的配置是不太一样的,具体看官方文档 # 这里指定了1024M内存 vb.memory = "1024" end # 循环 2 次 (1..2).each do |i| # 创建虚拟机,用 i 下标来标识每一台虚拟机,虚拟机名称为 node1,node2 config.vm.define "node#{i}" do |node| # 配置hostname node.vm.hostname = "node#{i}" # 配置机网络 node.vm.network "private_network", ip: "192.168.33.2#{i}" # 配置端口映射,guest是虚拟机,host是本机 # 需要多个端口映射,就写多个,目录映射同理 node.vm.network "forwarded_port", guest: 80, host: "908#{i}" node.vm.network "forwarded_port", guest: 90, host: "909#{i}" # 配置目录映射,第一个参数是本机目录(这里用是相对路径,你也可以写绝对路径),第二个参数是虚拟机目录 # 第三个参数是 virtualbox 需要的,具体看https://www.virtualbox.org/manual/ch04.html#sharedfolders node.vm.synced_folder "app#{i}", "/vagrant", SharedFoldersEnableSymlinksCreate: false end end # 单独创建一个虚拟机 config.vm.define "manage" do |manage| manage.vm.hostname = "manage" manage.vm.network "private_network", ip: "192.168.33.11" # 使用 shell provision 执行一段脚本,其他 provision 可以看官方文档 # https://www.vagrantup.com/docs/provisioning/ manage.vm.provision "shell", path: "bootstrap-manage.sh" endend编辑 bootstrap-manage.sh ...

June 11, 2019 · 1 min · jiezi

使用Ansible和Vagrant设置Kubernetes

作者:Naresh L J(Infosys)目的此博客文章介绍了为开发而设的多节点Kubernetes集群所需的步骤。此设置提供了类似生产环境的群集,可以在本地计算机上进行。为什么需要多节点群集设置?多节点Kubernetes集群提供类似生产的环境,具有各种优势。尽管Minikube提供了很好的入门平台,但它并没有提供使用多节点集群的机会,帮助解决与应用程序设计和体系结构相关的问题或错误。例如,Ops可以在多节点集群环境中重现问题,测试者可以部署多个版本的应用程序来执行测试用例和验证更改。这些优势使团队能够更快地解决问题,从而提高敏捷性。为什么使用Vagrant和Ansible?Vagrant可以让我们轻松创建虚拟环境,消除导致“在我的机器能工作”现象的陷阱。它可以与多个供应程序一起使用,例如Oracle VirtualBox、VMware、Docker等。它允许我们通过使用配置文件来创建一次性环境。Ansible是自动化软件配置管理的基础架构自动化引擎。它是无代理的,允许我们使用SSH密钥连接到远程计算机。Ansible playbooks以yaml编写,以简单的文本文件提供库存管理。先决条件在你的机器上安装Vagrant。可以在此处找到安装二进制文件。Oracle VirtualBox可以作为Vagrant供应程序,也可以使用Vagrant官方文档描述的类似供应程序。Ansible安装在你的机器上。有关特定平台的安装,请参阅Ansible安装指南。设置概述我们将建立一个Kubernetes集群,包含一个主节点和两个工作节点。所有节点将运行Ubuntu Xenial 64位操作系统,配置使用Ansible playbooks。第1步:创建Vagrantfile使用你喜欢的文本编辑器,创建名为Vagrantfile的文件,插入下面的代码。N的值表示集群中存在的节点数,可以相应地进行修改。在下面的示例中,我们将N的值设置为2。IMAGE_NAME = “bento/ubuntu-16.04"N = 2Vagrant.configure(“2”) do |config| config.ssh.insert_key = false config.vm.provider “virtualbox” do |v| v.memory = 1024 v.cpus = 2 end config.vm.define “k8s-master” do |master| master.vm.box = IMAGE_NAME master.vm.network “private_network”, ip: “192.168.50.10” master.vm.hostname = “k8s-master” master.vm.provision “ansible” do |ansible| ansible.playbook = “kubernetes-setup/master-playbook.yml” end end (1..N).each do |i| config.vm.define “node-#{i}” do |node| node.vm.box = IMAGE_NAME node.vm.network “private_network”, ip: “192.168.50.#{i + 10}” node.vm.hostname = “node-#{i}” node.vm.provision “ansible” do |ansible| ansible.playbook = “kubernetes-setup/node-playbook.yml” end end end第2步:为Kubernetes master创建Ansible playbook。在Vagrantfile相同的目录中创建名为kubernetes-setup的目录。在kubernetes-setup目录中创建两个名为master-playbook.yml和node-playbook.yml的文件。在master-playbook.yml文件中,添加以下代码。步骤2.1:安装Docker及其相关组件。我们将安装以下软件包,然后将名为“vagrant”的用户添加到“docker”组。—- hosts: all become: true tasks: - name: Install packages that allow apt to be used over HTTPS apt: name: “{{ packages }}” state: present update_cache: yes vars: packages: - apt-transport-https - ca-certificates - curl - gnupg-agent - software-properties-common - name: Add an apt signing key for Docker apt_key: url: https://download.docker.com/linux/ubuntu/gpg state: present - name: Add apt repository for stable version apt_repository: repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable state: present - name: Install docker and its dependecies apt: name: “{{ packages }}” state: present update_cache: yes vars: packages: - docker-ce - docker-ce-cli - containerd.io notify: - docker status - name: Add vagrant user to docker group user: name: vagrant group: docker步骤2.2:如果系统启用了swap,Kubelet将无法启动,因此我们使用以下代码禁用swap。 - name: Remove swapfile from /etc/fstab mount: name: “{{ item }}” fstype: swap state: absent with_items: - swap - none - name: Disable swap command: swapoff -a when: ansible_swaptotal_mb > 0步骤2.3:使用以下代码安装kubelet、kubeadm和kubectl。 - name: Add an apt signing key for Kubernetes apt_key: url: https://packages.cloud.google.com/apt/doc/apt-key.gpg state: present - name: Adding apt repository for Kubernetes apt_repository: repo: deb https://apt.kubernetes.io/ kubernetes-xenial main state: present filename: kubernetes.list - name: Install Kubernetes binaries apt: name: “{{ packages }}” state: present update_cache: yes vars: packages: - kubelet - kubeadm - kubectl步骤2.3:使用以下代码使用kubeadm初始化Kubernetes集群(仅适用于主节点)。 - name: Initialize the Kubernetes cluster using kubeadm command: kubeadm init –apiserver-advertise-address=“192.168.50.10” –apiserver-cert-extra-sans=“192.168.50.10” –node-name k8s-master –pod-network-cidr=192.168.0.0/16步骤2.4:使用以下代码为vagrant用户设置kube配置文件以访问Kubernetes集群。 - name: Setup kubeconfig for vagrant user command: “{{ item }}” with_items: - mkdir -p /home/vagrant/.kube - cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config - chown vagrant:vagrant /home/vagrant/.kube/config步骤2.5:使用以下代码设置容器网络供应商和网络政策引擎。 - name: Install calico pod network become: false command: kubectl create -f https://docs.projectcalico.org/v3.4/getting-started/kubernetes/installation/hosted/calico.yaml步骤2.6:生成kube join命令将节点加入Kubernetes集群,并将该命令存储在名为join-command的文件中。 - name: Generate join command command: kubeadm token create –print-join-command register: join_command - name: Copy join command to local file local_action: copy content=”{{ join_command.stdout_lines[0] }}" dest="./join-command"步骤2.7:使用以下代码设置检查Docker守护程序的处理程序。 handlers: - name: docker status service: name=docker state=started步骤3:为Kubernetes节点创建Ansible playbook。在kubernetes-setup目录中创建名为node-playbook.yml的文件。将以下代码添加到node-playbook.yml中步骤3.1:开始添加步骤2.1到2.3的代码。步骤3.2:使用以下代码将节点加入Kubernetes集群。 - name: Copy the join command to server location copy: src=join-command dest=/tmp/join-command.sh mode=0777 - name: Join the node to cluster command: sh /tmp/join-command.sh步骤3.3:添加步骤2.7中的代码以完成此playbook。第4步:完成Vagrantfile和playbooks后,请按照以下步骤操作。$ cd /path/to/Vagrantfile$ vagrant up完成上述所有步骤后,Kubernetes集群应该已启动并运行。我们可以使用Vagrant登录主节点或工作节点,如下所示:$ ## Accessing master$ vagrant ssh k8s-mastervagrant@k8s-master:~$ kubectl get nodesNAME STATUS ROLES AGE VERSIONk8s-master Ready master 18m v1.13.3node-1 Ready <none> 12m v1.13.3node-2 Ready <none> 6m22s v1.13.3$ ## Accessing nodes$ vagrant ssh node-1$ vagrant ssh node-2KubeCon + CloudNativeCon + Open Source Summit大会日期:会议日程通告日期:2019 年 4 月 10 日会议活动举办日期:2019 年 6 月 24 至 26 日KubeCon + CloudNativeCon + Open Source Summit赞助方案KubeCon + CloudNativeCon + Open Source Summit多元化奖学金现正接受申请KubeCon + CloudNativeCon和Open Source Summit即将首次合体落地中国KubeCon + CloudNativeCon + Open Source Summit购票窗口,立即购票!CNCF邀请你加入最终用户社区 ...

March 18, 2019 · 3 min · jiezi

Vagrant (四) - Box 的用法

Box 是一个 bundle 或者 package,它将一台虚拟机所需要的一切设施捆绑到一个单一的、可交换的文件之中,从而提供了分享的便利。从这一点上说,Box 和 OVF 是一样的东西。不过 OVF 是具有公信力的标准格式,而 Box 则是 Vagrant 的私有格式。所以人们不免疑问既然如此、岂非我还是去 VMWare/VirtualBox 中导出 ovf 格式的比较好吧?事实上又并非这么简单,就好像 Microsoft Office 也是私有格式一样,它还是通行于全球。所以,一般地看,为着分享一个极为有效的基础设施的目的,你可能还是需要通过 Vagrant Box 特性建立你的基础设施的 VirtualBox/VMWare/KVM/Hyper-V 分发包,才能真的让世界上的其它人或许感谢你的友好;而如果你只是临时给LAN中某个人传递一个snapshot的话,就ovf好了;如果你的 team 统一使用 vagrant 为开发容器环境的话,那当然还是 box 了;如果你的 team 主力使用 docker,那就已经不在这个问题讨论范围内了。自己的box打包 VM 为 box一般的用法是:vagrant package –base dos7bc31 –output dos7bc31.box在这里,你首先在VirtualBox中制作了一个名为 dos7bc31 的虚拟机,并且在虚拟机中搭建了 DOS 7.1的环境和安装了 Borland C++ 3.1,然后发布此命令就能得到一个 box 文件了:$ vagrant package –base dos71bc31 –output dos71bc31.box==> dos7bc31: Exporting VM…==> dos7bc31: Compressing package to: /Users/hz/vmt/dos7/dos71bc31.box$ lltotal 113784-rw-r–r– 1 hz staff 3.0K Feb 21 10:05 Vagrantfile-rw-r–r–@ 1 hz staff 55M Feb 21 10:08 dos71bc31.box当然,你需要为那个虚拟机准备一份 Vagrantfile,这可以用 vagrant init 在当前的空文件夹中建议一份。分发 boxLAN中,直接传输 box 给他人即可。你也可以考虑发布 box 到公共目录中。使用 box对于一个本地的 box 文件,可以这样添加它:vagrant box add –name hedzr/dos71bc31 ./dos71bc31.box然后就可以初始化相应的实例了:mkdir test && cd test1vagrant init hedzr/doc71bc31vagrant up迭代当发布了你的box的新版本之后,接收人需要更新它。对于本地的 box 文件,更新的方法是:vagrant box add -f –name hedzr/dos72bc31 ‘./dos71bc31 (1).box’如果发布者在构建 box 时正确地进行了 config.vm.box_version 的管理的话,则 -f 标志不必使用。命令和概念参考相关的 vagrant 命令参考如下:packagepackage 命令用于从虚拟机提供商(例如 virtualbox)环境中已经创建了虚拟机实例中打包一个 .box 可交换文件出来。Usage: vagrant package [options] [name|id]Options: –base NAME Name of a VM in VirtualBox to package as a base box (VirtualBox Only) –output NAME Name of the file to output –include FILE,FILE.. Comma separated additional files to package with the box –vagrantfile FILE Vagrantfile to package with the box -h, –help Print this helpboxbox 包含一组子命令:add,list,outdated,prune,remove,repackage,update。命令描述add添加 box(从文件、url、短名字)到 vagrant boxes管理区,必要时自动下载它list列出 vagrant boxes 管理区中已有的本地 boxesoutdated检查本地 boxes 的时效性,有否新版本存在prune删除本地 boxes 的旧版本,仅保留最新的一个版本remove删除一个本地 boxrepackage重新打包一个本地 box,这多用于本地创建新box的过程中update更新一个本地 box,从远程来源你可以通过 vagrant box add –help 来获取相应的命令行参数参考。或者检查官网:Box。为免填充字数只说,这里不再罗列了。cd dos71bc31vagrant package –base dos71bc31 –output dos71bc31.box –vagrantfilevagrant box add -f –name hedzr/dos71bc31 dos71bc31.boxmkdir test1 && cd test1vagrant init hedzr/dos71bc31vagrant up# shutdown VM from virtualbox …vagrant destroy -fcd -vagrant box remove hedzr/dos71bc31 -fVagrantfile loader按照官方的说法,Vagrantfile能够被合并。这就是说,当你通过 vagrant init 建立了一个新的虚拟机环境时,vagrant将会一次检查一系列的可用的 Vagrantfile 并将它们合并到最终为你生成的 Vagrantfile 中。而当 vagrant up 当前虚拟机时,vagrant同样会检查这一系列可用的 Vagrantfile,并依据有否变更或虚拟机有否不符等实际情况对真实的虚拟机进行配置修订。这些可用的 Vagrantfile 包括:box 中自带的你在 ~/.vagrant.d/ 中自定义的一个 Vagrantfile你的当前虚拟机的 Vagrantfile。当 vagrant init 时,此文件尚未存在故略过,当 vagrant up 时它会被检视,并根据实际情况去修订虚拟机如果存在 Multi-machine overrides如果存在 Provider-specific overrides可以参考:Load Order and MergingGUI模式有多种方法启用或禁用 虚拟机的 GUI 界面。直接通过命令行:vagrant up –guivagrant up –headless修改 Vagrantfile 配置文件:config.vm.provider “virtualbox” do |v| v.gui = trueend对于大多数基于 Linux Server 的虚拟机来说,乃至于 vagrant 默认情况下,一个虚拟机总是被启动于 headless 无界面模式的。不过即使这样,你还是可以在 VirtualBox应用程序界面中双击虚拟机缩略图或者通过虚拟机子菜单显示其 gui 界面。有人曾钻研过这个问题,列出了一个更奇技的方法来方便你指定GUI模式与否。其方法是修改Vagrantfile加入如下片段:# Returns true if GUI environment variable is set to a non-empty value.# Defaults to falsedef gui_enabled? !ENV.fetch(‘GUI’, ‘’).empty?endVagrant.configure(‘2’) do |config| config.vm.provider ‘virtualbox’ do |v| v.gui = gui_enabled? endend然后你就可以这样来执行命令:# for *nix systemGUI=1 vagrant up # gui modevagrant up # -headless mode# for windowsSET GUI=1vagrant upSee also: https://stackoverflow.com/a/2…不过我觉得多余。至于我列举的原因呢,只是展示一种使用环境变量的方法,它可以被用于自行装配和分发 box。 ...

March 1, 2019 · 2 min · jiezi

Vagrant (三) - 网络配置

这个章节脱胎于我早期的blog文章,订正了一下句法和准确性。原始的文字在 VAGRANT 网络配置 (HIDE)网络配置Vagrant可以指定三种网络:端口转发(Forwarded Ports),私有网络(Private Network),公有网络(Public Network)。他们和多数虚拟机提供的网络是对应的。Vagrant 网络模型端口转发: config.vm.network :forwarded_port, guest: 80, host: 8080将虚拟机(被称作guest)的端口80映射为宿主机的端口8080。端口转发隐含着一个provider的NAT网络将被首先创建。所以,如果你单独定义一条端口转发的配置语句的话,VM将会自动建立NAT网络环境。私有网络: config.vm.network :private_network, ip: “192.168.1.104"你可以从宿主机自由访问虚拟机,但LAN网络中的其他人不需要也无法访问虚拟机。值得注意的是,ip地址“192.168.1.104”不是随便指定的。首先你可以不指定,这表示虚机启动时会DHCP到一个可用的IP地址(例如:192.168.33.101),这是vagrant通过virtualbox私有网络的DHCP机制获得的。如果你要自行指定明确的IP地址,要保证该地址是在恰当的网段中,例如192.168.33.71。多台虚拟机在私有网络模式下也可以互相访问,只要设置为相同的网段就可以。本质上说,这是使用provider的HostOnly模式。公有网络:公有网络实际上是表示将虚拟机暴露为LAN(例如你的宿主机所在的办公室网络)中的一台主机。例如使用LAN的DHCP自动获得IP地址: config.vm.network :public_network也可以指定LAN网段中的一个可用的地址,但需要注意不要和LAN中已有的主机或者保留的IP地址相冲突。本质上说,这是使用provider的桥接网络模式。Provider的网络模式对于vagrant的provider,例如VirtualBox来说,网络模式区分的更细,但vagrant并不能使用全部vbox网络模型。VirtualBox 的典型网络模型:NAT,Hostonly,Bridge以及Internal。这些模式的细节我们不再列举。借用一张表格来归纳: NATBridgedInternalHostonlyvm -> host√√××host -> vm×√××vm -> others hosts√√××others hosts => vm×√××vm <-> vm×√same subnet√这张表格描述了virtualbox的网络模型。实用的网络配置一般来说,端口转发足以满足开发需要了。但对于特殊的需要来说,你可能需要一台完全“真实”的虚机,这台虚机可以被稳定地从宿主机访问,并且可以访问LAN中的其他资源。这样的需求实际上可以通过配置多块网卡来解决问题,例如一块配置为私有网络模式,一块配置为公有网络模式。vagrant通过配置文件能够支持virtualbox的NAT,Bridge以及Hostonly网络模型。默认情况默认情况下,我们已经知道一个最简的流程来启动vagrant: mkdir /dev cd /dev vagrant box add ubuntu/trusty64 vagrant init ubuntu/trusty64 vagrant up vagrant ssh vagrant halt这样的步骤,可以得到一台ubuntu 14.04的虚拟机,采用Provider的NAT网络模式,在虚拟机中可以访问宿主机,也可以使用宿主机的外网路由上网。观察它生成的默认的Vagranfile,其网络配置是未指定的。此时,vagrant建立的vm具有一个NAT网卡。桥接网络当采用如下配置语句时,vagrant建立的vm具有一个Bridged网络:# Create a public network, which generally matched to bridged network.# Bridged networks make the machine appear as another physical device on# your network.config.vm.network “public_network"此时,vm在宿主机所在的LAN中等价于一台物理机器。假如你具有LAN Router的管理权的话,这是很简单的一种开发模型:通过路由器的mac绑定为vm保留一个固定的dhcp地址,这样vm无论何时启动都会获取到相同的IP地址,你的开发和调试将会很顺利很简单。私有网络当采用如下配置语句时,vagrant建立的vm具有两个hostonly网络:config.vm.network “private_network”, ip: “192.168.9.10"config.vm.network “private_network”, ip: “192.168.33.10"标识符“private_network”总是被映射为virtualbox的hostonly模型。私有网络模型是允许宿主机访问的,为了便于宿主机访问,我们也可以明确指定一个适当网段的地址。要知道适当的网段是多少,可以这样查证:通过进入虚机显示网卡信息和IP地址来了解网段。打开 VirtualBox 的网络配置,检查HostOnly网络的DHCP网段。混合网络当采用如下配置语句时,vagrant建立的vm具有一个NAT和一个hostonly网络:config.vm.network “private_network”, ip: “192.168.33.10"标识符“private_network”总是被映射为virtualbox的hostonly模型。注意 NAT 网络将被隐含地创建。vagrant在创建网卡时,如果配置文件仅配置了一个private_network,则vagrant自动创建NAT网卡,然后在创建配置文件所描述的网络;而如果配置文件指定了两个以上的private_network的话,vagrant不再自动创建 NAT 网卡了。混合网络非常适合开发和测试环境,你可以通过NAT和Internet相通,然后多个vm之间也能相互通信。内外网络内外网络 只是我随便命名的,也就是从vm的角度出发既有内网(VM的私有网络),又有外网(宿主机所在的局域网)。当采用如下配置语句时,vagrant建立的vm具有一个bridged和一个hostonly网络:config.vm.network “public_network"config.vm.network “private_network”, ip: “192.168.33.10"这是比较通用的配置模式,vm既有host主机所在局域网的ip,又有一个私有网络的ip地址,因此这些vm之间具有全连通性。不过,一般来说开发和测试使用较为封闭的网络模型是比较好的方式,通常不建议vm配置有 public_network 的网卡。小结任何config.vm.network “private_network”, ip: “192.168.33.10"语句都可以改为:config.vm.network “private_network”, type: “dhcp"这时virtualbox的网关负责dhcp应答和分配IP。实例nginx服务建立一个新的工作目录 sample-nginx使用 vagrant init ubuntu/xenial64 进行初始化修改 Vagrantfile 加入必要的声明 (见后)使用 vagrant up 启动该虚拟机,然后可以SSH进入或者 curl -i http://localhost:8080/ 来尝试访问它完整的 Vagrantfile 如下:# -- mode: ruby --# vi: set ft=ruby :# All Vagrant configuration is done below. The “2” in Vagrant.configure# configures the configuration version (we support older styles for# backwards compatibility). Please don’t change it unless you know what# you’re doing.Vagrant.configure(“2”) do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. config.vm.box = “ubuntu/xenial64” # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # vagrant box outdated. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing “localhost:8080” will access port 80 on the guest machine. # NOTE: This will enable public access to the opened port config.vm.network “forwarded_port”, guest: 80, host: 8080 # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access # config.vm.network “forwarded_port”, guest: 80, host: 8080, host_ip: “127.0.0.1” # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network “private_network”, ip: “192.168.33.10” # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network “public_network” # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder “../data”, “/vagrant_data” # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider “virtualbox” do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = “1024” # end # # View the documentation for the provider you are using for more # information on available options. # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the # documentation for more information about their specific syntax and use. config.vm.provision “shell”, inline: <<-SHELL apt-get update apt-get install -y nginx SHELLend这是一个相当简单的实例。更复杂的初始化,可以考虑使用一个独立的脚本文件来完成,而不是直接放在 Vagrantfile 中。Updates有时候,可能遇到网络配置失败的情况。例如:$ vagrant upBringing machine ‘default’ up with ‘virtualbox’ provider…==> default: Importing base box ‘ubuntu/bionic64’…==> default: Matching MAC address for NAT networking…==> default: Checking if box ‘ubuntu/bionic64’ is up to date…==> default: Setting the name of the VM: setup_bionic_default_1511875921207_73707==> default: Clearing any previously set network interfaces…==> default: Preparing network interfaces based on configuration… default: Adapter 1: nat default: Adapter 2: hostonly==> default: Forwarding ports… default: 22 (guest) => 2222 (host) (adapter 1)==> default: Running ‘pre-boot’ VM customizations…==> default: Booting VM…==> default: Waiting for machine to boot. This may take a few minutes… default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: Warning: Connection reset. Retrying… default: Warning: Remote connection disconnect. Retrying… default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest… default: Removing insecure key from the guest if it’s present… default: Key inserted! Disconnecting and reconnecting using new SSH key…==> default: Machine booted and ready!==> default: Checking for guest additions in VM… default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 5.1.28_Ubuntu r117968 default: VirtualBox Version: 5.2==> default: Configuring and enabling network interfaces…The following SSH command responded with a non-zero exit status.Vagrant assumes that this means the command failed!/sbin/ifdown ’enp0s8’ || true/sbin/ip addr flush dev ’enp0s8’# Remove any previous network modifications from the interfaces filesed -e ‘/^#VAGRANT-BEGIN/,$ d’ /etc/network/interfaces > /tmp/vagrant-network-interfaces.presed -ne ‘/^#VAGRANT-END/,$ p’ /etc/network/interfaces | tac | sed -e ‘/^#VAGRANT-END/,$ d’ | tac > /tmp/vagrant-network-interfaces.postcat \ /tmp/vagrant-network-interfaces.pre \ /tmp/vagrant-network-entry \ /tmp/vagrant-network-interfaces.post \ > /etc/network/interfacesrm -f /tmp/vagrant-network-interfaces.prerm -f /tmp/vagrant-network-entryrm -f /tmp/vagrant-network-interfaces.post/sbin/ifup ’enp0s8’Stdout from the command:Stderr from the command:bash: line 4: /sbin/ifdown: No such file or directorysed: can’t read /etc/network/interfaces: No such file or directorysed: can’t read /etc/network/interfaces: No such file or directorybash: line 20: /sbin/ifup: No such file or directory这是由于 vagrant 使用 ifupdown 软件包来管理虚拟机的网络配置问题,然而某些新版本的操作系统,例如 Ubuntu 18+ 已经放弃了 ifupdown,因而 vagrant 脚本会失败。解决的办法是两次启动虚拟机,并且在其间自行安装 ifupdown:$ vagrant up # 启动虚拟机,会报错网络配置不成功$ vagrant ssh # 不理睬错误直接登录到虚拟机中ubuntu@node1$ sudo apt install ifupdown; exit$ vagrant halt # 关闭虚拟机$ vargant up && vagrant ssh # 然后重新启动虚拟机,网络配置会再次被应用,并且应该会一切正常了Referenceshttps://www.virtualbox.org/ma…https://dev.to/isabolic99/how...https://www.vagrantup.com/doc… ...

March 1, 2019 · 6 min · jiezi

Vagrant (一) - 基本知识

介绍Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。它使用Oracle的开源VirtualBox虚拟化系统,使用 Chef 创建自动化虚拟环境。同时,Vagrant也支持其他类型的虚拟机系统,例如 kvm, vmware,甚至是容器系统,例如docker,等等。安装 VagrantVagrant 可以使用不同的后端虚拟机,例如 virtualbox,vmware,kvm,qemu 等等,它们被称作 provider。不过一般来说还是使用 virtualbox 居多。安装到 Windows下载 安装 VirtualBox :https://www.virtualbox.org/下载安装 Vagrant :http://www.vagrantup.com/下载所需的 Boxes :http://www.vagrantbox.es/首先依次安装 Virtualbox 和 Vagrant,视情况重启系统。virtualbox可以选择安装Extension Pack。安装到 MacmacOS中安装vagrant支持类似于Windows,首先安装 Virtualbox 及其 Extension Pack,然后是 安装 Vagrant 的 DMG 包 vagrant_1.9.8_x86_64.dmg。安装到 Ubuntu针对 Virtualbox 根据系统版本的不同,下载 对应的 deb 包到本机,然后安装 deb 包:# 14.04dpkg -i virtualbox-5.1_5.1.26-117224-Ubuntu-trusty_amd64.deb# 15.10dpkg -i virtualbox-5.1_5.1.26-117224-Ubuntu-wily_amd64.deb# 16.04dpkg -i virtualbox-5.1_5.1.26-117224-Ubuntu-xenial_amd64.deb# 16.10dpkg -i virtualbox-5.1_5.1.26-117224-Ubuntu-yakkety_amd64.deb# 17.04dpkg -i virtualbox-5.1_5.1.26-117224-Ubuntu-zesty_amd64.deb准备BoxesVagrant 的 box,是一个打包好的单一文件,其中包含了一个完整系统的虚拟机相关数据。添加box的方法很简单,例如Ubuntu 的box:vagrant box add ubuntu/xenial64 ubuntu-xenial64-20170503.1.0-virtualbox.boxvagrant box add ubuntu/trusty64 ubuntu-trusty64-20170424.0.0-virtualbox.box可以直接添加box,vagrant会尝试自动下载:vagrant box add chef/centos-6.5或者在初始化新vm时直接指定来源地址vagrant init ubuntu/trusty64 https://atlas.hashicorp.com/ubuntu/boxes/trusty64查看所有已经添加的boxes:vagrant box list小结安装本身都很简单,无需太多准备。 ...

March 1, 2019 · 1 min · jiezi

Vagrant (二) - 日常操作

立即上手上一节中,我们介绍了怎样安装 Vagrant,安装本身并不困难。本章节中我们首先要快速上手,以便获得一个直观的概念:建立一个工作目录打开命令行工具,终端工具,或者iTerm2等,建立一个我们将要新建虚机的项目目录,例如 first-vm:cd $HOMEmkdir -p vms/first-vmcd vms/first-vmWindows的命令可能像这样: cd %HOME% mkdir -p vms\first-vm cd vms\first-vm我们需要一个完全空白的工作目录作为开始。如上我们新建一个目录当做项目目录的基准位置。初始化一个操作系统以 Ubuntu 操作系统为虚拟机的例子,我们可以这样初始化:vagrant init ubuntu/xenial64这个命令将会去下载 ubuntu 的 box 镜像回来,然后以该镜像为核心,在工作目录下建立一份虚拟机规范描述文件 Vagrantfile。有关的描述我们稍后会加以介绍。在这里,你需要知道的是,你可以修改或查看这个文本格式的文件。本质上说,Vagrantfile采用ruby等价的语法,所以你可以选择文本编辑器按照ruby进行语法加亮。在启动这个vagrant虚机之前,可以修改Vagrantfile中的配置,例如添加更多网卡。启动虚拟机并操作它那么,我们可以启动这个虚机或者关闭它:vagrant up #启动虚机vagrant halt #停止虚机vagrant destroy [-f] #删除虚机删除虚机的指令将会全部删除虚机的后端vmm中的全部相关文件、快照等等,但并不会删除项目目录中的 Vagrantfile 。当虚机启动之后,在项目目录执行命令 vagrant ssh 可以登录到虚拟机中。虚机目录 /vagrant 和项目目录是自动同步的,在虚机中写入/vagrant中将会在宿主机中的项目目录中找到相应的内容。虚机是完整的 Ubuntu 操作系统,当使用 vagrant ssh 登录到虚拟机中之后,你得到了一个虚拟机的终端控制台,现在你可以使用 Ubuntu 自身的shell指令了,并且可以随时通过 exit 结束控制台返回到宿主机的控制台环境。$ vagrant up…$ vagrant sshvagrant@vm1> ll /vagrant…vagrant@vm1> exit$ …$ vagrant shutdown使用Vagrant常用命令vagrant init [name [url]] # 初始化当前目录到 vagrant 环境vagrant up # 启动虚拟机vagrant halt # 关闭虚拟机vagrant reload # 重启虚拟机vagrant status # 查看虚拟机运行状态vagrant destroy [-f] # 销毁当前虚拟机(但不删除Vagrantfile)vagrant suspend # 挂起当前虚拟机vagrant resume # 恢复被挂起的vmvagrant ssh # SSH 登录至虚拟机vagrant box add ADDRESS # 安装box文件/url到本地vagrant box list # 列出所有本地已安装的box列表vagrant box outdated # 检查已安装的box是否有更新的版本vagrant box remove NAME # 删除某个已安装的boxvagrant package # 打包当前虚拟机环境为box文件以用于分发vagrant plugin # 安装卸载vagrant插件vagrant provision # 执行专门的环境初始化脚本vagrant ssh-config # 输出ssh连接的一些信息vagrant version # 获取vagrant的版本更多命令可以查看 vagrant 官方的文档。转换现有虚拟机为box包对于刚建立的第一个虚拟机镜像first-vm,它的项目目录名称为“first-vm”,并且已经至少使用 vagrant up 启动过一次了,那么我们现在可以将这个虚拟机导出为box包:vagrant package –base=first-vm –output=first-vm-v1.box这个box同样可以被添加到本机的vagrant boxes缓存中:vagrant box add first-vm-v1.box –name=first-vm-v1box文件实际上是一个 OVF 包。开源虚拟化格式OVF文件是一种开源的文件规范,它描述了一个开源、安全、有效、可拓展的便携式虚拟打包以及软件分布格式,它一般有几个部分组成,分别是ovf文件、mf文件、cert文件、vmdk文件和iso文件。如果网络模式中使用 private_network 的话,在打包之前需要清除一下private_network的设置,避免不必要的错误:vm$ sudo rm -f /etc/udev/rule.d/70-persistent-net.rules这条bash指令应该在虚机中被执行,对于不同的操作系统来说,文件的位置可能是有变化的。将这个box文件分发给同事,则他们也可以创建一个完全等价的虚拟机。vagrant box add first-vm-v1.box –name=myteam/first-vm-v1vagrant init my-team/first-vm-v1vagrant upVagrantfile 介绍此前,我们通过 vagrant init 的指令创建了新的虚拟机环境,在工作目录中一个 Vagrantfile将被自动创建,如果它尚未存在的话。这个文件通常应该是这样子的:# -- mode: ruby --# vi: set ft=ruby :# All Vagrant configuration is done below. The “2” in Vagrant.configure# configures the configuration version (we support older styles for# backwards compatibility). Please don’t change it unless you know what# you’re doing.Vagrant.configure(“2”) do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. config.vm.box = “ubuntu/xenial64” # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # vagrant box outdated. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing “localhost:8080” will access port 80 on the guest machine. # NOTE: This will enable public access to the opened port # config.vm.network “forwarded_port”, guest: 80, host: 8080 # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access # config.vm.network “forwarded_port”, guest: 80, host: 8080, host_ip: “127.0.0.1” # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network “private_network”, ip: “192.168.33.10” # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network “public_network” # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder “../data”, “/vagrant_data” # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider “virtualbox” do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = “1024” # end # # View the documentation for the provider you are using for more # information on available options. # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision “shell”, inline: <<-SHELL # apt-get update # apt-get install -y apache2 # SHELLend这个文件的内容很容易读懂。box config.vm.box = “ubuntu/xenial64"这句话是指定操作系统用的。换句话说,你用文本编辑器修改它也就会随心替换系统了。只不过如果你真的要这么做的话,需要执行 vagrant destroy; vagrant up 这样的序列,以便清理旧的虚拟机设施和以新的虚机配置创建相应的设施。 对于 VirtualBox来说,它提供了 VBoxManage 命令行工具可以针对某个 VM 做很多事,modifyvm 就是一条可以修订 VM 配置参数的子命令。而Vagrant也可以在配置文件 Vagrantfile 中操作 virtualbox 以达到类似的目的。例如: config.vm.provider “virtualbox” do |v| v.customize [“modifyvm”, :id, “–name”, “bastion”, “–memory”, “2048”] end这个示例可以修改 VM 的内存为 2GB。网络设置默认情况下,vagrant通知provider创建 NAT 类型的网卡,这样虚拟机将能够和主机之间相互通讯,同时VM能够借助宿主机的外网环境直接访问公网。在这种情况下,我们可以指定VM的端口转发来暴露VM中的应用程序的端口,从而在宿主机上可以直接与该端口进行通信。 config.vm.network “forwarded_port”, guest: 80, host: 8080例如,我们在VM中安装了nginx的话,上面的端口转发设定可以将其暴露为宿主机的 :8080 ,这样我们从宿主机或者内网的其他主机都可以访问VM中的nginx服务了。 网络配置是个大话题,我们稍后会专列一章更多地讲述几种最佳实践。同步文件夹我们曾经提到过当 SSH 到 VM 中时,/vagrant 实际上和主机的VM工作目录是等价的,也就是 Vagrantfile 所在的那个目录。这种特性实际上就是 vagrant 建立的一个同步目录的机制。一般来说,它是通过 provider 的所谓 共享文件夹(Shared Folder)的特性来实现的。我们当然并不需要关心 provider 的特性,也不必关心 vagrant 是怎么做的。我们只需要知道,这个连接点是可以被自定义的: config.vm.synced_folder “../data”, “/vagrant_data"上面这一配置,会将宿主机的 VM工作目录/../data 映射为 VM中的 /vagrant_data。你还可以做到更多:同步文件夹支持符号链接,所以你可以通过符号链接聚合主机的其他位置到一个root连接点中。使用ruby循环语句建立多个 VMs在一个Vagrantfile配置中,建立一组VMs是很容易的事情: (1..3).each do |i| config.vm.define “node-#{i}” do |node| node.vm.provision “shell”, inline: “echo hello from node #{i}” end end值得注意的是,使用ruby的 for i in 1..3 do 循环,你无法得到想要的结果。原因是 for i in 语句在每次循环迭代时为 i 赋新的值,并重用循环体的语句块,这就导致 |node| 也并不得到新的副本,而是上一次循环时的 node,结果是你只能得到一个不断修改后的单一的 VM。如下所示: # THIS DOES NOT WORK! for i in 1..3 do config.vm.define “node-#{i}” do |node| node.vm.provision “shell”, inline: “echo hello from node #{i}” end end替换 VM 的 Locale一般情况下,SSH会话沿用宿主机控制台的Locale,但你可以通过预定义覆盖它,在会话中总是使用特定的语言: ENV[“LC_ALL”] = “en_US.UTF-8” Vagrant.configure(“2”) do |config| # … endProvisioningProvisioning 也是一个大话题。我会考虑单列一章。不过在这里,我们主要是提及这个机制能干些什么。简单地说,Provisioning是一个首次启动时的后初始化点,它发生在 VM 操作系统被安装就绪,并首次启动完成时。你可以编写 Shell 脚本在这个点自动完成你的专属环境初始化动作,例如安装标准服务、安装bash扩展组件(例如git-prompt)、建立必要的工作文件夹和拉取特定的分发包之类。如果有必要,在今后,即使VM已经完成了首次初始化了,你也可以通过 vagrant up –provision 这样的指令来强制 VM 开机和再次执行 Provisioning 脚本。在 Vagrantfile 中,典型地可以这样指定 Provioning: $script = <<-SCRIPT echo I am provisioning… date > /etc/vagrant_provisioned_at SCRIPT Vagrant.configure(“2”) do |config| config.vm.provision “shell”, inline: $script end也可以指定一个脚本文件,注意我们通过 /vagrant 传递 provision.sh 到 VM 中,而典型的 box 系统中,/vagrant 是在 $PATH 搜索路径中的: Vagrant.configure(“2”) do |config| config.vm.provision “shell”, path: “provision.sh” end你也可以指定脚本的全路径名,甚至可以在 path 中指定一个 url。参考vagrant 官方的文档http://note.axiaoxin.com/cont…OVF standardsOVF 格式 ...

March 1, 2019 · 4 min · jiezi

Windows7下Vagrant 启动失败的问题

最近vagrant不知道什么原因无法启动,日志如下:Bringing machine ‘default’ up with ‘virtualbox’ provider…/Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/io.rb:32:in encode': "x8B" followed by "x02" on GBK (Encoding::InvalidByteSequenceError) from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/io.rb:32:in read_until_block’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/subprocess.rb:194:in block in execute' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/subprocess.rb:192:in each’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/subprocess.rb:192:in execute' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/subprocess.rb:22:in execute’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/powershell.rb:174:in version' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/powershell.rb:195:in validate_install!’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/powershell.rb:96:in execute_cmd' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/platform.rb:145:in block (2 levels) in windows_hyperv_enabled?’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/platform.rb:142:in each' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/platform.rb:142:in block in windows_hyperv_enabled?’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/platform.rb:153:in windows_hyperv_enabled?' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/plugins/providers/virtualbox/action/check_virtualbox.rb:19:in call’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/action/warden.rb:34:in call' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/action/builder.rb:116:in call’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/action/runner.rb:66:in block in run' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/util/busy.rb:19:in busy’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/action/runner.rb:66:in run' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/machine.rb:239:in action_raw’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/machine.rb:208:in block in action' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/environment.rb:614:in lock’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/machine.rb:194:in call' from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/machine.rb:194:in action’ from /Vagrant/embedded/gems/2.2.1/gems/vagrant-2.2.1/lib/vagrant/batch_action.rb:82:in `block (2 levels) in run’ 参考了两篇网上找到文章,无法解决问题 1 日文网站的解决方案(认为是编码问题):https://github.com/NetCommons3/NetCommons3/issues/50 対策 「C:\opscode\chefdk\bin\berks」 を開き、3行目に Encoding.default_external = ‘utf-8’ 2 laravel-china上的一篇文章(认为是中文问题),很相似,可是问题原因不同。但也给了解决思路 https://laravel-china.org/articles/4082/a-wonderful-problem-encountered-in-the-use-of-vagrant 随后自己仔细看了下ruby的日志及相关源码,发现里面描述了powershell的问题,于是尝试启动windows的powershell果然是启动失败。这样的话问题就好说了,解决了powershell的问题就行了。(是.netFramework的问题,原来是自己之前由于visio安装不上,升级了.netFramework)。降级.netFramework即可之后Vagrant启动正常总结:遇到问题注意看日志,尽管是自己不熟悉的语言。文章结尾奶一下ruby:借用《Programming Ruby》推荐序中的一段话:事实上,执行性能与开发效率是软件开发中的一对矛盾,所有的程序设计语言都必须面对这个矛盾,作出自己的选择。 在当时,大多数新语言的选择是上下通吃。它们一方面提供了丰富多彩的高级抽象,另一方面又提供了强有力的底层操作能力,希望由此实现高性能与高效率的统一。C++、Java、C#和Delphi都是走的这条路线,甚至VB从5.0开始也强化了底层操作机制,并提供了编译模型,不落人后。 Ruby实现了最纯粹意义上的面向对象,让Smalltalk、Perl和Lisp的灵魂在新的躯壳里高歌。相比于Python,Ruby的思想更加清晰一致,形式更加灵活;相比于Perl,Ruby更简单质朴,绝少光怪陆离之举;相比于Smalltalk和Lisp,Ruby更富有现代感和实干气质;相比于庙堂之上的“工业语言”,Ruby自由挥洒、轻快锐利;而相比于JavaScript和PHP,Ruby从Smalltalk继承而来的深厚底蕴又大占优势。面对执行性能与开发效率的谜题,Ruby毫不犹豫地选择了开发效率,选择了对人脑的友好。 ...

January 8, 2019 · 1 min · jiezi