上手Vagrant

如何基于Vagrant与Virtualbox快速在本地拉起测试虚拟机

有时候为了测试“数据库集群”这样的东西,您需要若干台虚拟机。

尽管现在的云服务已经非常方便也非常便宜,但再方便也肯定也没有本地的虚拟机用起来方便。

vagrant就是用来解决这个问题的。您不需要了解它的原理,只需要知道它能简单快捷地按照您的要求在您的笔记本、PC或Mac上拉起若干台虚拟机即可。

IMAGE_NAME = "centos/7"
N=3  # 数据库机器节点数量,可修改为0

Vagrant.configure("2") do |config|
    config.vm.box = IMAGE_NAME
    config.vm.box_check_update = false
    config.ssh.insert_key = false

    # 元节点
    config.vm.define "meta", primary: true do |meta|  # 元节点默认的ssh别名为`meta`
        meta.vm.hostname = "meta"
        meta.vm.network "private_network", ip: "10.10.10.10"
        meta.vm.provider "virtualbox" do |v|
            v.linked_clone = true
            v.customize [
                    "modifyvm", :id,
                    "--memory", 4096, "--cpus", "2",   # 元节点的内存与CPU核数:默认为2核/4GB
                    "--nictype1", "virtio", "--nictype2", "virtio",
                    "--hwvirtex", "on", "--ioapic", "on", "--rtcuseutc", "on", "--vtxvpid", "on", "--largepages", "on"
                ]
        end
        meta.vm.provision "shell", path: "provision.sh"
    end

    # 初始化N个数据库节点
    (1..N).each do |i|
        config.vm.define "node-#{i}" do |node|  # 数据库节点默认的ssh别名分别为`node-{1,2,3}`
            node.vm.box = IMAGE_NAME
            node.vm.network "private_network", ip: "10.10.10.#{i + 10}"
            node.vm.hostname = "node-#{i}"
            node.vm.provider "virtualbox" do |v|
                v.linked_clone = true
                v.customize [
                        "modifyvm", :id,
                        "--memory", 2048, "--cpus", "1", # 数据库节点的内存与CPU核数:默认为1核/2GB
                        "--nictype1", "virtio", "--nictype2", "virtio",
                        "--hwvirtex", "on", "--ioapic", "on", "--rtcuseutc", "on", "--vtxvpid", "on", "--largepages", "on"
                    ]
            end
            node.vm.provision "shell", path: "provision.sh"
        end
    end
end

Vagrant Provision [DRAFT]

If you wish to run pigsty on your laptop, consider using vagrant and virtualbox as vm provisioner

  1. Install vagrant, virtualbox and ansible on your computer. for example:
brew install virtualbox vagrant ansible 	# MacOS, other may not work this way
  1. Use vagrant with Vagrantfile, it will provision 4 nodes (via virtualbox) for this project.
make up     # pull up vm nodes. alternative: cd vagrant && vagrant up
  1. Setup nopass ssh from your host to vm nodes
make ssh		# cd vagrant && vagrant ssh-config > ~/.ssh/pigsty_config
  1. There are some vagrant shortcuts defined in Makefile
make				# launch cluster
make new    # create a new pigsty cluster
make dns		# write pigsty dns record to your /etc/hosts (sudo required)
make ssh		# write ssh config to your ~/.ssh/config
make clean	# delete current cluster
make cache	# copy local yum repo packages to your pigsty/pkg

Vagrant provision scripts tested on MacOS 10.15 Catalina.

Vagrant使用教程

如果您希望在本地环境运行Pigsty示例,可以考虑使用 vagrantvirtualbox初始化本地虚拟机。

  1. 在宿主机上安装 vagrant, virtualboxansible(可选)

    具体安装方式因平台而异,请参照软件官网文档进行,以MacOS为例,可以使用homebrew一键安装:

brew install virtualbox vagrant ansible 	# MacOS命令行
  1. 在项目主目录执行make up,系统会使用 Vagrantfile中的定义拉起四台虚拟机。
make up     # 拉起所有节点,也可以通过进入vagrant目录执行vagrant up实现
  1. 配置宿主机到虚拟机的SSH免密访问
make ssh		# 等价于执行 cd vagrant && vagrant ssh-config > ~/.ssh/pigsty_config
  1. Makefile 中定义了一些vagrant快捷方式
make				# 启动集群
make new    # 销毁并创建新集群
make dns		# 将Pigsty域名记录写入本机/etc/hosts (需要sudo权限)
make ssh		# 将虚拟机SSH配置信息写入 ~/.ssh/config
make clean	# 销毁现有本地集群
make cache	# 制作离线安装包,并拷贝至宿主机本地,加速后续集群创建
make upload # 将离线安装缓存包 pkg.tgz 上传并解压至默认目录 /www/pigsty

附带的Vagrantfile在MacOS 10.15下测试

最后修改 2021-01-06: routine update (e5292ee)