-
Notifications
You must be signed in to change notification settings - Fork 378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vagrant #36
Comments
单主机且使用帐号密码登录配置
|
最新box安装方法https://atlas.hashicorp.com/boxes/search 或者 Ubuntu box http://uec-images.ubuntu.com/vagrant/ CentOs box http://nrel.github.io/vagrant-boxes/ 下载 1. 首先选择你的目标box,这里以
|
Vagrant.configure("2") do |config|
config.vm.define :u1 do |u1|
u1.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "u1", "--memory", "1024"]
end
u1.vm.box = "ubuntu/trusty64"
u1.vm.hostname = "u1"
u1.vm.network :public_network, ip: "192.168.0.225"
end
config.vm.define :u2 do |u2|
u2.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "u2", "--memory", "1024"]
end
u2.vm.box = "ubuntu/trusty64"
u2.vm.hostname = "u2"
u2.vm.network :public_network, ip: "192.168.0.226"
end
end |
Vagrant.configure("2") do |config|
config.vm.box = "CentOS/7"
config.vm.hostname = "c"
config.vm.network :public_network, ip: "192.168.99.138"
config.vm.provider "virtualbox" do |v|
v.memory = "2048"
v.name = "c"
end
end 若报
|
windows下centos7镜像共享目录报错解决方法
修改 如下所示
下载 https://itefix.net/dl/cwRsync_5.5.0_x86_Free.zip 文件,解压后将
修改 如下所示
启动时若报如下错误
做如下操作即可
进入 yum update
yum install kernel-headers kernel-devel 重启后进入 sudo /opt/VBoxGuestAdditions-*/init/vboxadd setup 参考资料 hashicorp/vagrant#6769 (comment) |
使用metadata.json方式添加本地box使用该方式添加的box将带有版本信息 其
注意:
执行如下命令添加 vagrant box add metadata.json
使用时最好在
如下所示
参考资料 https://www.vagrantup.com/docs/boxes/format.html |
Windows 10下正常运行Vagrant的版本如下所示
切勿升级,切勿升级,切勿升级 |
CentOS 7单机自定义VagrantfileVagrant.configure("2") do |config|
config.vm.define "centos7" do |node|
node.vm.box = "centos/7"
node.vm.box_version = "1705.01"
node.vm.box_check_update = false
node.vm.provision "shell", inline: "echo This box contains CentOS 7 64-bit."
node.vm.hostname = "centos"
node.vm.network "public_network", ip: "192.168.31.216"
node.vm.provider "virtualbox" do |v|
v.name = "centos"
v.memory = 2048
v.cpus = 2
end
end
end 使用Vagrantfile批量创建多节点虚拟机Vagrant.configure("2") do |config|
(1..3).each do |i|
config.vm.define "node#{i}" do |node|
node.vm.box = "centos/7"
node.vm.box_version = "1705.01"
node.vm.box_check_update = false
node.vm.provision "shell", inline: "echo This box contains CentOS 7 64-bit."
node.vm.hostname = "node#{i}"
node.vm.network "public_network", ip: "192.168.31.#{i}"
node.vm.provider "virtualbox" do |v|
v.name = "node#{i}"
v.memory = 2048
v.cpus = 2
end
end
end
end |
离线方式添加 Box编写 metadata.json {
"name": "centos/7",
"versions": [{
"version": "1710.01",
"providers": [{
"name": "virtualbox",
"url": "file:///virtualbox.box"
}]
}]
} 添加 Box vagrant box add metadata.json |
Vagrant设置账号密码登录在虚拟机 vagrantfile 的目录位置 打开控制台,输入vagrant ssh-config
查看 HostName ,Port,IdentityFile 这三个位置。 有可能不能选择密码登录,默认是用密钥登录,浏览,文件,找到 刚刚的IdentityFile ,再输入密码vagrant 就可以使用vagrant 用户ssh登录。 设置root账号登录vagrant登陆后,切换到root账号 $ sudo -i 设置root的密码 passwd 编辑 /etc/ssh/sshd_config 文件,(注意,vagrant用户下这个文件是只读的,可能什么也看不见) 修改 PermitRootLogin 改为yes ,并把前面的# 去掉 修改 PasswordAuthentication 改为yes ,并把前面的# 去掉 保存退出,重启sshd服务 $ systemctl restart sshd |
Vagrant 管理# 全局管理
vagrant global-status
vagrant global-status --prune
vagrant destroy <vm_id>
vagrant halt <vm_id1> <vm_id2>
vagrant reload <vm_id1> <vm_id2>..
vagrant up <vm_id1> <vm_id2>.. Vagrant 插件
实现多台虚拟机之间直接通过名称访问,原理为更改 host文件 # 安装并验证插件
vagrant plugin install vagrant-hostmanager
vagrant plugin list
# 在 Vagrantfile 中修改
config.hostmanager.enabled = true
config.hostmanager.manage_guest = true
config.hostmanager.manage_host = true
# 执行命令,更新虚拟机上的hosts,同时更新主机上的 hosts
vagrant hostmanager
处理 VisualBox 中无法设置共享目录问题 vagrant plugin install vagrant-vbguest
vagrant vbguest --status
vagrant vbguest --do install node1
# 配置 vagrantfile
config.vbguest.auto_update=false
非使用 visualBox 自带的共享目录,自定义使用文件系统 nfs,性能更高 vagrant plugin install vagrant-bindfs
# 。。。
node1. vm. synced_folder "./app","/mnt/app-data", type:"nfs
node1. bindfs. bind_folder "/mnt/app-data","/app", force_user:"root", force_group:"root",o:"nonempty"
# 代理设置插件
# 在Vagrantfile中的config部分添加代理配置, 全部网络都走主机代理
vagrant plugin install vagrant-proxyconf
vim Vagrantfile
Vagrant.configure("2") do |config|
if Vagrant.has_plugin?("vagrant-proxyconf")
config.proxy.http = "<http://192.168.0.2:3128/>"
config.proxy.https = "<http://192.168.0.2:3128/>"
config.proxy.no_proxy = "localhost,127.0.0.1,.example.com"
end
# ... other stuff
end
# 复用虚拟机环境插件安装
vagrant plugin list
vagrant plugin install vagrant-scp
vagrant scp
# 处理虚拟机安装进行目录文件映射权限问题
vagrant plugin install vagrant-vbguest
vagrant plugin list 网络配置根据需要设置虚拟机网络 IP 地址固定,实现虚拟机中的软件根据名称访问,设置虚拟机可以访问外部的网络。
将宿主机的端口与虚拟机的端口绑定,从而让外部通过端口可以访问虚拟机 若 guest_ip 和 host_ip 两项配置为空,则局域网下的所有设备都可以访问该虚拟机 Vagrant.configure(2") do |config|
config.vm.network
"forwarded_port"(必选) //端口转发标示
,guest(必选): # 虚拟端口
,host(必选): # 宿主机端口,值必须大于1024
,gust_ip(可选): # 虚拟机端口绑定虚拟机ip地址
,host_ip(可选): # 虚拟机端口绑定宿主机端口ip
,protocol(可选): # 指定通信协议,可以使用tcp/udp,默认tcp
,auto_correct(可选): # ture/fasle,开机是否自动检测端口冲突
end
# 实际配置
# 配置2个端口映射,把物理机的8080映射到虚拟机80,物理机的2100映射到虚拟机的22
# host_ip 在主机 IP 较为固定情况下配置使用
config.vm.network :"forwarded_port", guest: 80, host: 8060,host_ip: "10.2.11.203"
config.vm.network :"forwarded_port", guest: 22, host: 2100, host_ip: "10.2.11.203"
虚拟机之间处在同一网段的地址可相互访问,主机可以访问虚拟机,无法通过虚拟机进行团队合作,不与宿主机的 IP 在同一个网段,防止冲突 配置 vagrant 里面的虚拟机的私有网段的时候,切记不能和企业(公司)内部的 DHCP 分配的 IP 地址在同一网段,否则会发生冲突 # 配置 Static IP
config.vm.network "private_network", ip: "192.168.50.10"
config.vm.network "private_network", ip: "192.168.55.20"
# 配置通过 DHCP 进行获取 IP,之后执行 `vagrant reload`
config.vm.network "private_network", type: "dhcp"
# 实际使用
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.10",
auto_config: false
end
与宿主机一样的网络配置, vagrant1.3+ 支持设置固定 IP,虚拟机 IP 与主机 IP 处在同一个网段时,实现局域网之间的互通,需要有路由器分配 IP.一般来说开发和测试使用较为封闭的网络模型是比较好的方式,通常不建议 vm 配置有 public_network 的网卡关联 配置虚拟机自动获取公司内部DHCP服务器分配的IP地址,在局域网任何一台电脑上,都可以ssh到虚拟机,或访问虚拟机上提供的服务 config.vm.network "public_network", ip: "192.168.1.120"
# 配置动态 IP
# 配置共有网络,使用主机上可以访问外网的接口(ipconfig)
# 配置默认网关
config.vm.network "public_network",bridge: "ens33"
config.vm.provision "shell",run: "always",inline: "route add default gw 10.2.11.1"
# 配置静态 IP
# auto_config:关闭自动配置
# ifconfig enp0s8 10.2.11.196 netmask 255.255.255.0 up: 配置静态ip(这里的ip不能和公司内部的地址冲突)
# route add default gw 10.2.11.1 指定网关(添加默认路由)
# bridge: 绑定接口(物理机哪个接口可以上网)
config.vm.network "public_network", auto_config: false ,bridge: "ens33"
config.vm.provision "shell",run: "always",inline: "ifconfig eth1 10.2.11.196 netmask 255.255.0.0 up"
config.vm.provision "shell",run: "alway",inline: "route add default gw 10.2.11.1"
config.vm.network "public_network", auto_config: false
config.vm.provision "shell",run: "always",inline: "ip addr add 172.17.10.51/21 dev eth1"
config.vm.provision "shell",run: "alway",inline: "ip route add 172.17.8.0/21 via 172.17.0.49" 共享文件配置宿主机中的数据与虚拟机的数据映射 # src: 是物理机的目录,相对路径,(相对于项目目录(/vagrant/ubuntu))
# /srv/website: 虚拟机的目录,绝对路径,如果没有,会自动创建
config.vm.synced_folder "src/", "/srv/website"
end
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.synced_folder "src/","/srv/website",owner: "root",group: "root" |
配置示例
参考文档
Discover Vagrant Boxes
Vgrant安装配置
使用 Vagrant 打造跨平台开发环境
Vagrant 三种网络配置详解
使用vagrant快速搭建环境
在 Mac/win7 下上使用 Vagrant 打造本地开发环境
解决Windows下使用Vagrant出现的Connection Timeout的问题
vagrant使用及trusty的配置记录
http://kiwenlau.com/2016/07/03/vagrant-vm-cluster/
The text was updated successfully, but these errors were encountered: