Skip to content

Vagrantfile

ruby를 사용하는 vagrant 설정파일에 대한 내용.

초기 파일 템플릿

vagrant init 하면 만들어지는 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://vagrantcloud.com/search.
  config.vm.box = "base"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Disable the default share of the current code directory. Doing this
  # provides improved isolation between the vagrant box and your host
  # by making sure your Vagrantfile isn't accessible to the vagrant box.
  # If you use this you may want to enable additional shared subfolders as
  # shown above.
  # config.vm.synced_folder ".", "/vagrant", disabled: true

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

kubeflow 를 위한 Ubuntu 22.04

두 단계로 나눠서 진행해야 한다. 우선 아래의 vagrantfile 을 저장하자. 코드에서 {USERNAME} 부분은 너가 바꾸고 싶은 사용자 이름으로 지정해라.

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.box_version = "20241002.0.0"
  config.vm.hostname = "DESKTOP-11F854J-UBUNTU-2204"

  # config.ssh.username = "{USERNAME}"
  # config.ssh.password = "0000"

  config.vm.provider "virtualbox" do |vb|
    vb.name = "DESKTOP-11F854J-UBUNTU-2204"
    vb.gui = false
    vb.cpus = 8
    vb.memory = 16384
  end

  config.vm.provision "shell", inline: <<-SHELL
    useradd -s /bin/bash -U -m {USERNAME}
    # gpasswd -a {USERNAME} wheel
    echo "{USERNAME}:0000" | chpasswd

    echo "{USERNAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/{USERNAME}
    chmod 440 /etc/sudoers.d/{USERNAME}

    mkdir -p /home/{USERNAME}/.ssh
    cp /home/vagrant/.ssh/authorized_keys /home/{USERNAME}/.ssh/
    chown -R {USERNAME}:{USERNAME} /home/{USERNAME}/.ssh
    chmod 700 /home/{USERNAME}/.ssh
    chmod 600 /home/{USERNAME}/.ssh/authorized_keys

    # userdel -f -r vagrant
    # rm /etc/sudoers.d/vagrant
  SHELL
end

실행:

vagrant.exe up

쉘 접속:

vagrant.exe ssh

잘 접속되는거 확인한 후 SSH 설정 확인:

vagrant.exe ssh-config

아마 아래와 같은 느낌의 내용일거다:

Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/Users/{USERNAME}/Project/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  PubkeyAcceptedKeyTypes +ssh-rsa
  HostKeyAlgorithms +ssh-rsa

새로 만든 사용자로 접속 테스트:

ssh -i .vagrant/machines/default/virtualbox/private_key {USERNAME}@127.0.0.1 -p 2222

잘 접속되면 아까 vagrantfile의 주석처리된 config.ssh.* 항목의 주석을 해제하자. 그리고 다시 쉘 접속:

vagrant.exe ssh

이제 직전 사용자의 찌끄레기를 지워버리자:

sudo userdel -f -r vagrant
sudo rm /etc/sudoers.d/vagrant

Disable VT-X in Vagrantfile

config.vm.provider :virtualbox do |vb|  
  vb.customize ["modifyvm", :id, "--hwvirtex", "off"]     
end

VirtualBoxUbuntu 실행 예제

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
  config.vm.box = "alvistack/ubuntu-22.04"

  # via 127.0.0.1 to disable public access
  config.vm.network "forwarded_port", guest: 5601, host: 5601, host_ip: "127.0.0.1"
  config.vm.network "forwarded_port", guest: 9200, host: 9200, host_ip: "127.0.0.1"

  config.vm.provider "virtualbox" do |vb|
    vb.cpus = 2
    vb.memory = "8192"
  end

  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
  SHELL
end

VirtualBoxKubernetes 클러스터 구축하기

우선 VirtualBox를 설치한다:

sudo apt install virtualbox virtualbox-ext-pack

Vagrantfile:

## Vagrantfile
# Variables
K8SV = '1.33.2-1.1' # Kubernetes Version : apt list -a kubelet , ex) 1.32.5-1.1
CONTAINERDV = '1.7.27-1' # Containerd Version : apt list -a containerd.io , ex) 1.6.33-1
N = 2 # max number of worker nodes

# Base Image  https://portal.cloud.hashicorp.com/vagrant/discover/bento/ubuntu-24.04
## Rocky linux Image https://portal.cloud.hashicorp.com/vagrant/discover/rockylinux
BOX_IMAGE = "bento/ubuntu-24.04"
BOX_VERSION = "202502.21.0"

Vagrant.configure("2") do |config|
#-ControlPlane Node
  config.ssh.insert_key = false # 추가 -> insecure key 재사용
  config.vm.define "cilium-m1" do |subconfig|
    subconfig.vm.box = BOX_IMAGE
    subconfig.vm.box_version = BOX_VERSION
    subconfig.vm.provider "virtualbox" do |vb|
      vb.customize ["modifyvm", :id, "--groups", "/Cilium-Lab"]
      vb.customize ["modifyvm", :id, "--nicpromisc2", "allow-all"]
      vb.name = "cilium-m1"
      vb.cpus = 2
      vb.memory = 2048
      vb.linked_clone = true
    end
    subconfig.vm.host_name = "cilium-m1"
    subconfig.vm.network "private_network", ip: "192.168.10.100"
    subconfig.vm.network "forwarded_port", guest: 22, host: 60000, auto_correct: true, id: "ssh"
    subconfig.vm.network "forwarded_port", guest: 6443, host: 56444, auto_correct: true, id: "api-server" # 추가
    subconfig.vm.synced_folder "./", "/vagrant", disabled: true
    subconfig.vm.provision "shell", path: "init_cfg.sh", args: [ K8SV, CONTAINERDV]
    subconfig.vm.provision "shell", path: "k8s-ctr.sh", args: [ N ]
  end

#-Worker Nodes Subnet1
  (1..N).each do |i|
    config.vm.define "cilium-w#{i}" do |subconfig|
      subconfig.vm.box = BOX_IMAGE
      subconfig.vm.box_version = BOX_VERSION
      subconfig.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--groups", "/Cilium-Lab"]
        vb.customize ["modifyvm", :id, "--nicpromisc2", "allow-all"]
        vb.name = "cilium-w#{i}"
        vb.cpus = 2
        vb.memory = 1536
        vb.linked_clone = true
      end
      subconfig.vm.host_name = "cilium-w#{i}"
      subconfig.vm.network "private_network", ip: "192.168.10.10#{i}"
      subconfig.vm.network "forwarded_port", guest: 22, host: "6000#{i}", auto_correct: true, id: "ssh"
      subconfig.vm.synced_folder "./", "/vagrant", disabled: true
      subconfig.vm.provision "shell", path: "init_cfg.sh", args: [ K8SV, CONTAINERDV]
      subconfig.vm.provision "shell", path: "k8s-w.sh"
    end
  end
end

See also