侧边栏壁纸
博主头像
SRE实战博主等级

行动起来,活在当下

  • 累计撰写 9 篇文章
  • 累计创建 1 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Vagrant虚拟化

Miracle
2024-03-01 / 0 评论 / 0 点赞 / 10 阅读 / 7493 字

一. 什么是 Vagrant

  • Vagrant 是一个虚拟机管理软件,可以自动化虚拟机的安装和配置流程。一般我们使用虚拟机时是这样的,安装一个虚拟机软件VMware或VirtualBox,寻找我们需要的iso镜像文件,然后一步步的在VMware上安装这个镜像文件,安装好之后,再一步步配置这个这个虚拟机的开发环境或运行环境。如果我们需要安装两个或多相同的虚拟机环境怎么办?还是得这样一步步的安装?不,我们有Vagrant。
  • Vagrant 让你通过编写一个Vagrantfile配置文件来控制虚拟机的启动、销毁、与宿主机间的文件共享、虚拟机网络环境的配置,还可以编写一些虚拟机启动后的执行脚本,自动安装一些必备的开发工具或配置。并且Vagrant还允许移植的你的虚拟机,使你只需要一次搭建就可以拥有多个相同环境的虚拟机副本。
  • Vagrant 的优点:跨平台、可移动、自动化部署无需人工参与等。
  • Vagrant 与 VirtualBox都有对应Windows版本可直接下载安装,插件安装,Vagrant 使用与linux一样

Vagrant 官方文档

二. 初始化系统

# 配置YUM源(OpenEuler无需配置)
rm -f /etc/yum.repos.d/*.repo
curl -so /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -so /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i '/aliyuncs.com/d' /etc/yum.repos.d/Centos-7.repo /etc/yum.repos.d/epel-7.repo

# 禁用Selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

# 禁用Filewalld
systemctl stop firewalld
systemctl disable firewalld

# 安装常用软件
yum install -y wget vim lrzsz iftop ntpdate net-tools

# 安装 kernel-headers
kernel_ver=$(uname -a | awk '{print $3}')
yum install -y kernel-devel-${kernel_ver} kernel-headers-${kernel_ver} dkms

# 设置时区
timedatectl set-timezone Asia/Shanghai

# 同步系统时间
ntpdate ntp.aliyun.com
hwclock --systohc

# 重启服务器(OpenEuler无需重启)
reboot

三. 安装 VirtualBox

  • VirtualBox 是一个免费开源的虚拟化软件,相对 VMware 来说更加小巧
# 安装 virtualbox(centos-7)
yum install -y https://download.virtualbox.org/virtualbox/6.1.32/VirtualBox-6.1-6.1.32_149290_el7-1.x86_64.rpm

# 安装 virtualbox(openEuler)
yum install -y https://download.virtualbox.org/virtualbox/6.1.32/VirtualBox-6.1-6.1.32_149290_el8-1.x86_64.rpm

# 初始化
/sbin/vboxconfig

四. 安装 Vagrat

# 下载安装源
curl -so /etc/yum.repos.d/hashicorp.repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo

# 如果系统是 openEuler-22.03
grep -q openEuler /etc/os-release && sed -i 's#$releasever#8#g' /etc/yum.repos.d/hashicorp.repo

# 安装 vagrant
yum install -y vagrant rsync nfs-utils

五. Vagrant 插件安装

# 硬盘插件(硬盘扩容)
vagrant plugin install --plugin-clean-sources \
    --plugin-source https://gems.ruby-china.com/ vagrant-disksize

# 文件共享插件(默认文件共享插件)
vagrant plugin install --plugin-clean-sources \
    --plugin-source https://gems.ruby-china.com/ vagrant-vbguest --plugin-version 0.21

# Windows 下nfs文件共享插件(windows下nfs插件 linux只需要安装nfs-utils并启动nfs服务无需安装此插件)
vagrant plugin install --plugin-clean-sources \
    --plugin-source https://gems.ruby-china.com/ vagrant-winnfsd

# 查看已安装插件
vagrant plugin list

六. Vagrant 使用

# 添加镜像
vagrant box add --name centos/7 \
   https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box

# 查看镜像
vagrant box list

# 初始化一个 Vagrantfile
vagrant init centos7

# 启动
vagrant up

# 进入虚拟机
vagrant ssh name

# 删除当前目录下Vagrantfile 部署的虚拟机
vagrant destroy -f

# 查看 VirtualBox 虚拟机
vboxmanage list vms

七. Vagrantfile

  1. 创建 Vagrantfile
cat <<'EOF'  > Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# name    :    虚拟机名称/虚拟机主机名
# ip      :    桥接网卡IP(局域网同网段)
# mem     :   内存大小
# cpu     :    CPU核数
# nfs_ip  :   nfs存储内部网络

boxes = [
  {
    :name => "node01",
    :ip => "192.168.6.81",
    :mem => "4096",
    :cpu => "2",
    :nfs_ip => "192.168.56.81"
  },
  {
    :name => "node02",
    :ip => "192.168.6.82",
    :mem => "4096",
    :cpu => "2",
    :nfs_ip => "192.168.56.82"
  },
  {
    :name => "node03",
    :ip => "192.168.6.83",
    :mem => "4096",
    :cpu => "2",
    :nfs_ip => "192.168.56.83"
  },
  {
    :name => "node04",
    :ip => "192.168.6.84",
    :mem => "4096",
    :cpu => "2",
    :nfs_ip => "192.168.56.84"
  },
  {
    :name => "node05",
    :ip => "192.168.6.85",
    :mem => "4096",
    :cpu => "2",
    :nfs_ip => "192.168.56.85"
  },
  {
    :name => "node06",
    :ip => "192.168.6.86",
    :mem => "4096",
    :cpu => "2",
    :nfs_ip => "192.168.56.86"
  },
  {
    :name => "node07",
    :ip => "192.168.6.87",
    :mem => "16384",
    :cpu => "6",
    :nfs_ip => "192.168.56.87"
  },
]

Vagrant.configure(2) do |config|
  config.vm.box_check_update = false
  config.vm.box = "centos/7"
  config.vm.box_url = "https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box"
  boxes.each do |opts|
    config.vm.define opts[:name] do |config|
      config.vm.hostname = opts[:name]
      config.vm.provider "vmware_desktop" do |v|
        v.vmx["memsize"] = opts[:mem]
        v.vmx["numvcpus"] = opts[:cpu]
      end
      config.vm.provider "virtualbox" do |v|
        v.customize ["modifyvm", :id, "--memory", opts[:mem]]
        v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
      end

      # add eth1 bridge loacal(vboxnet0)
      config.vm.network :private_network, ip: opts[:nfs_ip]

      # add eth2 bridge loacal(eth0)
      config.vm.network :public_network, ip: opts[:ip], bridge: "eth0"

      # mount dir
      config.vm.synced_folder "./data/#{opts[:name]}", "/data", create:true, type: "nfs", nfs_version: 4, nfs_udp: false
    end
  end

  # disk size
  config.disksize.size = '200GB'

  # mount dir
  config.vm.synced_folder "./data/share", "/share", create:true, type: "nfs", nfs_version: 4, nfs_udp: false

  # run script
  config.vm.provision "shell", privileged: true, path: "./setup.sh"

  # mount dir
  config.vm.synced_folder ".", "/vagrant", create:true, type: "nfs", nfs_version: 4, nfs_udp: false

  # run shell
  # config.vm.provision "shell", run: "always", inline: "sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config"
  config.vm.provision "shell", inline: <<-SHELL
     echo "redhat" | passwd --stdin root
   SHELL

  # skip vbguest update
  config.vbguest.no_remote = true
  config.vbguest.auto_update = false

  #config.ssh.username = 'root'
  #config.ssh.insert_key = 'true'
end
EOF
EOF
  1. 创建虚拟机初始化脚本
cat <<EOF    > setup.sh
#/bin/bash

# 配置阿里云软件安装源
rm -f /etc/yum.repos.d/*.repo
curl -so /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -so /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i '/aliyuncs.com/d' /etc/yum.repos.d/Centos-7.repo /etc/yum.repos.d/epel-7.repo

# install some tools
yum install -y vim lrzsz

# add route
route add default gw 192.168.6.1

# 启用root远程登录(此操作有风险)
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
systemctl restart sshd

# 配置时区
timedatectl set-timezone Asia/Shanghai
EOF
  1. 部署
vagrant up
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区