vagrant创建多节点虚拟机

使用Vagrant创建多节点虚拟机

  • 环境:

    • windows 7
    • vagrant 1.9.5
    • virtualbox 4.3.40
  • 需求:

    • 创建1个manager节点 (加装ansible)
    • 创建2个client节点
    • 3台虚拟机都配置两张网卡,1个NAT,一个Host-Only

这里我们主要写vagrantfile,来自动配置多台虚拟机

主要就是给manager节点加装ansible

* 修改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://atlas.hashicorp.com/search.
  config.vm.box = "ubuntu/trusty64"
  #给每台虚拟换为163源
  config.vm.provision "shell", path: "sources.sh"

  #配置manager节点
  config.vm.define "manager" do |manager|
    manager.vm.provider "manager" do |m|
      m.memory = 512
      m.cpus = 2
      m.name = "manager"
    end
    #端口转发
    manager.vm.network "forwarded_port", guest: 80, host: 8082 
    #私有网
    manager.vm.network "private_network", ip: "192.168.56.110"
    #安装ansible的脚本,放在vagrantfile同目录下
    manager.vm.provision "shell", path: "ansible.sh"
    manager.vm.hostname = "manager"
  end

  config.vm.define "client1" do |client1|
    client1.vm.provider "client1" do |c1|
      c1.memory = 512
      c1.cpus = 1
      c1.name = "client1"
    end
    client1.vm.network "forwarded_port", guest: 80, host: 8083
    client1.vm.network "private_network", ip: "192.168.56.111"
    client1.vm.hostname = "client1"
  end

  config.vm.define "client2" do |client2|
    client2.vm.provider "client2" do |c2|
      c2.memory = 512
      c2.cpus = 1
      c2.name = "client2"
    end
    client2.vm.network "forwarded_port", guest: 80, host: 8084
    client2.vm.network "private_network", ip: "192.168.56.112"
    client2.vm.hostname = "client2"
  end
end

SHELL脚本

  • sources.sh脚本:
wget http://mirrors.163.com/.help/sources.list.trusty 
mv /etc/apt/sources.list /etc/apt/sources.list.old
mv ./sources.list.trusty /etc/apt/sources.list

-O /etc/apt/sources.list
      apt-get update
  • ansible.sh脚本(ansible使用的pip安装):
      apt-get install -y python-pip python-dev libffi-dev libssl-dev
      mkdir -p ~/.pip
      cat > ~/.pip/pip.conf <<EOF
[global]
trusted-host=mirrors.aliyun.com 

index-url=http://mirrors.aliyun.com/pypi/simple/ 

EOF
      cat >> /etc/ssh/ssh_config <<EOF   
    StrictHostKeyChecking no
    UserKnownHostsFile=/dev/null
EOF
      pip install ansible
©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值