从群集内部访问Vagrant VM(运行Centos / 7的Vitualbox)

时间:2018-12-28 18:54:20

标签: ssh login vagrant centos7

我目前正在尝试使用ansible,在该用例中,我使用VirtualBox和Vagrant设置了3个VM的集群。现在我的虚拟机设置看起来像这样

流浪文件

$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个节点。主机应该安装了ansible,并可以通过ssh访问节点。这样所有机器都启动并运行,我可以使用

连接到我的主服务器
vagrant ssh master1

我还修改了etc / hosts,以便可以访问master1.vg,node1.vg等。

但是有一个问题。我应该通过ssh从主机内部连接到节点。但是

ssh node1.vg

将不起作用,因为在询问密码后权限被拒绝。根据文档,默认密码应为“ vagrant”,但此处不是这种情况。 (我猜想访问方法已经通过密钥设置为ssh了)。我已经搜索了很多,因为我认为这将是一个常见的问题,但没有找到令人满意的答案。您是否知道如何通过ssh从master1 vm连接到节点vm之一?

我还将配置上传到了一个仓库(https://github.com/relief-melone/vagrant-ansibletestingsetup

1 个答案:

答案 0 :(得分:0)

好的,我现在解决了。现在,Vagrant将生成您的私钥,您将需要使用正确的权限将该私钥导入主VM。您还需要正确设置网络。因此,让我们首先解决网络问题。

您的/ 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中。您将需要从主节点访问的所有节点,因此,我们将获得以下内容

流浪文件

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中被拒绝。我的外壳文件看起来像这样

.// 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

完成所有操作

  

无所事事

应该运行平稳,您可以使用

进入主虚拟机
  

无用的ssh master1

在该主机中,您现在可以连接到使用

的node2机器
  

ssh -i〜/ keys / node2

因为这是一个包含大量文件的集合,所以我也将其放入一个仓库中,可以在这里找到

https://github.com/relief-melone/vagrant-ansibletestingsetup/tree/working-no-comments