Vagrant 学习笔记二

centos7 虚拟机

  1. Vagrantfile
 Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"

  config.vm.define "simple" do |node|

    node.vm.box = "centos/7"
    node.vm.hostname="simple"
    
    node.vm.network "private_network", ip: "192.168.99.120"

    node.vm.provider "virtualbox" do |vb|
      vb.name = "centos7" + Time.now.strftime(" %Y-%m-%d")
      vb.memory = 2048
      vb.cpus = 2
    end

    node.vm.provision "shell", path: "config/bootstrap.sh"
  end
end   
  1. bootstrap.sh
#!/bin/bash

yum update
yum upgrade -y

# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

# 关闭 selinux
setenforce 0
sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config

docker 主机

  1. Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"

  config.vm.define "docker-host" do |node|

    node.vm.box = "centos/7"
    node.vm.hostname="docker-host"
    
    node.vm.network "private_network", ip: "192.168.99.101"

    node.vm.provider "virtualbox" do |vb|
      vb.name = "centos7" + Time.now.strftime(" %Y-%m-%d")
      vb.memory = 2048
      vb.cpus = 2
    end

    node.vm.provision "file", source: "config/docker-ce.repo", destination: "/home/vagrant/docker-ce.repo"
    node.vm.provision "shell", path: "config/bootstrap.sh"
    node.vm.provision "shell", path: "config/update.sh", run: "always"
  end
end 
  1. bootstrap.sh
#!/bin/bash

yum update
yum upgrade -y

# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

# 关闭 selinux
setenforce 0
sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config

cp /home/vagrant/docker-ce.repo /etc/yum.repos.d/
yum clean all
yum update
yum install docker-ce -y

# 也可以
# curl -fsSL get.docker.com -o get-docker.sh
# sh get-docker.sh --mirror Aliyun

usermod -aG docker vagrant

# 加速
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://29c6ef5f.m.daocloud.io

systemctl restart docker
  1. update.sh
  • bootstrap.sh 只执行一次,update.sh 做更新,每次运行

ubuntu16 虚拟机(带图形界面)

  1. Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"

  config.vm.define "ubuntu" do |node|

    node.vm.box = "ubuntu/xenial64"
    node.vm.hostname="ubuntu"
    
    node.vm.network "private_network", ip: "192.168.99.130"

    node.vm.network "public_network"

    node.vm.provider "virtualbox" do |vb|
      vb.name = "ubuntu" + Time.now.strftime(" %Y-%m-%d")
      vb.memory = 2048
      vb.cpus = 2
      vb.gui = true
      vb.linked_clone = true 
    end

    node.vm.synced_folder "share", "/home/vagrant/share"

    node.vm.provision "shell", path: "config/bootstrap.sh"
  end
end  
  1. bootstrap.sh
#!/bin/bash

# 关闭防火墙
ufw disable

apt-get update
apt-get upgrade -y

多个虚拟机互通

  1. Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"

  # (1..3).each do |i|
  #   config.vm.define "node-#{i}" do |node|
  #     node.vm.hostname="node-#{i}"
  #   end
  # end

  config.vm.define "node1" do |node|
    node.vm.hostname="node1"
    
    node.vm.network "private_network", ip: "192.168.99.121"

    node.vm.provider "virtualbox" do |vb|
      vb.name = "node1" + Time.now.strftime(" %Y-%m-%d")
      vb.memory = 2048
      vb.cpus = 2
    end

    node.vm.provision "shell", path: "config/bootstrap.sh"
  end

  config.vm.define "node2" do |node|
    node.vm.hostname="node2"
    
    node.vm.network "private_network", ip: "192.168.99.122"

    node.vm.provider "virtualbox" do |vb|
      vb.name = "node2" + Time.now.strftime(" %Y-%m-%d")
      vb.memory = 2048
      vb.cpus = 2
    end

    node.vm.provision "shell", path: "config/bootstrap.sh"
  end

  config.vm.define "node3" do |node|
    node.vm.hostname="node3"
    
    node.vm.network "private_network", ip: "192.168.99.123"

    node.vm.provider "virtualbox" do |vb|
      vb.name = "node3" + Time.now.strftime(" %Y-%m-%d")
      vb.memory = 2048
      vb.cpus = 2
    end

    node.vm.provision "shell", path: "config/bootstrap.sh"
  end
end
  

GitHub 地址:https://github.com/wj5633/Easy_install

转载于:https://www.cnblogs.com/wj5633/p/11228828.html

你可能感兴趣的:(Vagrant 学习笔记二)