从集群内部访问 Vagrant VM (Virtualbox 运行 Centos/7)
Accessing Vagrant VM (Vitualbox running Centos/7) from inside cluster
我目前正在尝试使用 ansible,对于那个用例,我已经使用 VirtualBox 和 Vagrant 设置了一个由 3 个虚拟机组成的集群。现在我的 VM-Setup 看起来像这样
Vagrantfile
$inline_m1 = <<SCRIPT
yum -y update
yum install -y git
yum install -y ansible
SCRIPT
$inline_n1_n2 = <<SCRIPT
yum -y update
yum install -y git
SCRIPT
Vagrant.configure(2) do |config|
config.vm.define "master1" do |conf|
# conf.vm.box = "peru/my_centos-7-x86_64"
# conf.vm.box_version = "20181211.01"
conf.vm.box = "centos/7"
conf.vm.hostname = 'master1.vg'
conf.vm.network "private_network", ip: "192.168.255.100"
conf.vm.provider "virtualbox" do |v|
v.memory = 6144
v.cpus = 2
end
conf.vm.provision "shell", inline: $inline_m1
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc/hosts"
conf.vm.provision "file", source: "./master1/etc.ansible.hosts", destination: "~/etc/ansible.hosts"
end
config.vm.define "node1" do |conf|
conf.vm.box = "centos/7"
conf.vm.hostname = 'node1.vg'
conf.vm.network "private_network", ip: "192.168.255.101"
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc/hosts"
conf.vm.provision "shell", inline: $inline_n1_n2
end
config.vm.define "node2" do |conf|
conf.vm.box = "centos/7"
conf.vm.hostname = 'node2.vg'
conf.vm.network "private_network", ip: "192.168.255.102"
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc/hosts"
conf.vm.provision "shell", inline: $inline_n1_n2
end
end
所以是1主2节点。 master 应该安装了 ansible 并通过 ssh 访问节点。所以所有机器都启动并运行,我可以使用
连接到我的主机
vagrant ssh master1
我也修改了 etc/hosts,所以我可以到达 master1.vg、node1.vg 等
但是有一个问题。我应该通过 ssh 连接到主机内部的节点。但是
ssh node1.vg
将无法工作,因为要求输入密码后权限被拒绝。根据文档,默认密码应为 "vagrant" 但此处并非如此。 (我猜是因为访问方法已经设置为使用密钥的 ssh)。我在谷歌上搜索了很多,因为我认为这是一个常见问题,但没有找到令人满意的答案。您知道如何通过 ssh 从 master1 虚拟机连接到其中一个节点虚拟机吗?
我也已将配置上传到存储库 (https://github.com/relief-melone/vagrant-ansibletestingsetup)
好的,我现在解决了。现在 Vagrant 将生成您的私钥,您需要将该密钥以正确的权限获取到您的主虚拟机中。您还需要正确设置网络。所以让我们先解决网络点。
您的 /etc/hosts 必须进行设置。在我的设置中它看起来像这样
/etc/hosts
192.168.255.100 master1.me.vg
192.168.255.101 node1.me.vg
192.168.255.102 node2.me.vg
您的私钥将存储在./.vagrant/machines/nodeX/virtualbox/private_key中。您将需要您想要从您的主人访问的所有节点,所以这给我们留下了以下内容
Vagrantfile
Vagrant.configure(2) do |config|
config.vm.define "node1" do |conf|
conf.vm.box = "centos/7"
conf.vm.hostname = 'node1.me.vg'
conf.vm.network "private_network", ip: "192.168.255.101"
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc.hosts"
conf.vm.provision "shell", path: "./node/shell.sh"
end
config.vm.define "node2" do |conf|
conf.vm.box = "centos/7"
conf.vm.hostname = 'node2.me.vg'
conf.vm.network "private_network", ip: "192.168.255.102"
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc.hosts"
conf.vm.provision "shell", path: "./node/shell.sh"
end
config.vm.define "master1" do |conf|
conf.vm.box = "centos/7"
conf.vm.hostname = 'master1.me.vg'
conf.vm.network "private_network", ip: "192.168.255.100"
conf.vm.provider "virtualbox" do |v|
v.memory = 6144
v.cpus = 2
end
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc.hosts"
conf.vm.provision "file", source: "./master1/etc.ansible.hosts", destination: "~/etc.ansible.hosts"
conf.vm.provision "file", source: "./.vagrant/machines/node1/virtualbox/private_key", destination: "~/keys/node1"
conf.vm.provision "file", source: "./.vagrant/machines/node2/virtualbox/private_key", destination: "~/keys/node2"
conf.vm.provision "shell", path: "./master1/shell.sh"
end
end
最后你必须设置私钥的权限,因为过于开放的权限设置将在稍后的 ssh 上被拒绝。我的 shell 文件看起来像这样
./master1/shell.sh
yum
-y update
yum install -y git
yum install -y ansible
cp /home/vagrant/etc.hosts /etc/hosts
cp /home/vagrant/etc.ansible.hosts /etc/ansible/hosts
chmod 600 /home/vagrant/keys/*
./node/shell.sh
yum -y update
yum install -y git
cp /home/vagrant/etc.hosts /etc/hosts
完成之后
vagrant up
应该运行顺利,你可以使用
进入你的主虚拟机
vagrant ssh master1
在该主控中,您现在可以连接到例如使用
的 node2 机器
ssh -i ~/keys/node2
因为这是一个包含大量文件的集合,所以我也把它放到了一个 repo 中,可以在这里找到
https://github.com/relief-melone/vagrant-ansibletestingsetup/tree/working-no-comments
我目前正在尝试使用 ansible,对于那个用例,我已经使用 VirtualBox 和 Vagrant 设置了一个由 3 个虚拟机组成的集群。现在我的 VM-Setup 看起来像这样
Vagrantfile
$inline_m1 = <<SCRIPT
yum -y update
yum install -y git
yum install -y ansible
SCRIPT
$inline_n1_n2 = <<SCRIPT
yum -y update
yum install -y git
SCRIPT
Vagrant.configure(2) do |config|
config.vm.define "master1" do |conf|
# conf.vm.box = "peru/my_centos-7-x86_64"
# conf.vm.box_version = "20181211.01"
conf.vm.box = "centos/7"
conf.vm.hostname = 'master1.vg'
conf.vm.network "private_network", ip: "192.168.255.100"
conf.vm.provider "virtualbox" do |v|
v.memory = 6144
v.cpus = 2
end
conf.vm.provision "shell", inline: $inline_m1
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc/hosts"
conf.vm.provision "file", source: "./master1/etc.ansible.hosts", destination: "~/etc/ansible.hosts"
end
config.vm.define "node1" do |conf|
conf.vm.box = "centos/7"
conf.vm.hostname = 'node1.vg'
conf.vm.network "private_network", ip: "192.168.255.101"
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc/hosts"
conf.vm.provision "shell", inline: $inline_n1_n2
end
config.vm.define "node2" do |conf|
conf.vm.box = "centos/7"
conf.vm.hostname = 'node2.vg'
conf.vm.network "private_network", ip: "192.168.255.102"
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc/hosts"
conf.vm.provision "shell", inline: $inline_n1_n2
end
end
所以是1主2节点。 master 应该安装了 ansible 并通过 ssh 访问节点。所以所有机器都启动并运行,我可以使用
连接到我的主机vagrant ssh master1
我也修改了 etc/hosts,所以我可以到达 master1.vg、node1.vg 等
但是有一个问题。我应该通过 ssh 连接到主机内部的节点。但是
ssh node1.vg
将无法工作,因为要求输入密码后权限被拒绝。根据文档,默认密码应为 "vagrant" 但此处并非如此。 (我猜是因为访问方法已经设置为使用密钥的 ssh)。我在谷歌上搜索了很多,因为我认为这是一个常见问题,但没有找到令人满意的答案。您知道如何通过 ssh 从 master1 虚拟机连接到其中一个节点虚拟机吗?
我也已将配置上传到存储库 (https://github.com/relief-melone/vagrant-ansibletestingsetup)
好的,我现在解决了。现在 Vagrant 将生成您的私钥,您需要将该密钥以正确的权限获取到您的主虚拟机中。您还需要正确设置网络。所以让我们先解决网络点。
您的 /etc/hosts 必须进行设置。在我的设置中它看起来像这样
/etc/hosts
192.168.255.100 master1.me.vg
192.168.255.101 node1.me.vg
192.168.255.102 node2.me.vg
您的私钥将存储在./.vagrant/machines/nodeX/virtualbox/private_key中。您将需要您想要从您的主人访问的所有节点,所以这给我们留下了以下内容
Vagrantfile
Vagrant.configure(2) do |config|
config.vm.define "node1" do |conf|
conf.vm.box = "centos/7"
conf.vm.hostname = 'node1.me.vg'
conf.vm.network "private_network", ip: "192.168.255.101"
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc.hosts"
conf.vm.provision "shell", path: "./node/shell.sh"
end
config.vm.define "node2" do |conf|
conf.vm.box = "centos/7"
conf.vm.hostname = 'node2.me.vg'
conf.vm.network "private_network", ip: "192.168.255.102"
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc.hosts"
conf.vm.provision "shell", path: "./node/shell.sh"
end
config.vm.define "master1" do |conf|
conf.vm.box = "centos/7"
conf.vm.hostname = 'master1.me.vg'
conf.vm.network "private_network", ip: "192.168.255.100"
conf.vm.provider "virtualbox" do |v|
v.memory = 6144
v.cpus = 2
end
conf.vm.provision "file", source: "./etc.hosts", destination: "~/etc.hosts"
conf.vm.provision "file", source: "./master1/etc.ansible.hosts", destination: "~/etc.ansible.hosts"
conf.vm.provision "file", source: "./.vagrant/machines/node1/virtualbox/private_key", destination: "~/keys/node1"
conf.vm.provision "file", source: "./.vagrant/machines/node2/virtualbox/private_key", destination: "~/keys/node2"
conf.vm.provision "shell", path: "./master1/shell.sh"
end
end
最后你必须设置私钥的权限,因为过于开放的权限设置将在稍后的 ssh 上被拒绝。我的 shell 文件看起来像这样
./master1/shell.sh
yum
-y update
yum install -y git
yum install -y ansible
cp /home/vagrant/etc.hosts /etc/hosts
cp /home/vagrant/etc.ansible.hosts /etc/ansible/hosts
chmod 600 /home/vagrant/keys/*
./node/shell.sh
yum -y update
yum install -y git
cp /home/vagrant/etc.hosts /etc/hosts
完成之后
vagrant up
应该运行顺利,你可以使用
进入你的主虚拟机vagrant ssh master1
在该主控中,您现在可以连接到例如使用
的 node2 机器ssh -i ~/keys/node2
因为这是一个包含大量文件的集合,所以我也把它放到了一个 repo 中,可以在这里找到
https://github.com/relief-melone/vagrant-ansibletestingsetup/tree/working-no-comments