본문 바로가기

* 리눅스

[리눅스] vagrant 패키지 설치 및 ubuntu 배포

반응형

vagrant 패키지 설치 및 ubuntu 배포

테스트 환경

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

vagrant 패키지 설치

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt update && sudo apt install vagrant

vagrant 버전 정보

vagrant --version
$ vagrant --version
Vagrant 2.3.0

box 이미지 다운로드

vagrant box add ubuntu/focal64

서버 구성 현황

호스트명 IP 역할 CPU Memory 비고
kube-control1 192.168.56.11 Control Plane 4 4096  
kube-node1 192.168.56.21 Node 2 4096  
kube-node2 192.168.56.22 Node 2 4096  
kube-node3 192.168.56.23 Node 2 4096  

vagrant 파일 작성

cd ~
mkdir kube
cd kube
vim Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  $vm_provider = "virtualbox"
  $box_image = "ubuntu/focal64"

  $vm_name_prefix = "kube"

  $number_of_control_planes = 1
  $number_of_nodes = 3

  $vm_subnet = "192.168.56"

  $vm_control_plane_cpus = 4
  $vm_control_plane_memory = 4096
  $vm_node_cpus = 2
  $vm_node_memory = 4096

  # Kubernetes Control Planes
  (1..$number_of_control_planes).each do |i|
    config.vm.define "#{$vm_name_prefix}-control#{i}" do |node|
      node.vm.box = $box_image
      node.vm.provider $vm_provider do |vm|
        vm.name = "#{$vm_name_prefix}-control#{i}"
        vm.cpus = $vm_control_plane_cpus
        vm.memory = $vm_control_plane_memory
      end
      node.vm.hostname = "#{$vm_name_prefix}-control#{i}"
      node.vm.network "private_network", ip: "#{$vm_subnet}.1#{i}", nic_type: "virtio"
    end
  end

  # Kubernetes Nodes
  (1..$number_of_nodes).each do |i|
    config.vm.define "#{$vm_name_prefix}-node#{i}" do |node|
      node.vm.box = $box_image
      node.vm.provider $vm_provider do |vm|
        vm.name = "#{$vm_name_prefix}-node#{i}"
        vm.cpus = $vm_node_cpus
        vm.memory = $vm_node_memory
      end
      node.vm.hostname = "#{$vm_name_prefix}-node#{i}"
      node.vm.network "private_network", ip: "#{$vm_subnet}.2#{i}", nic_type: "virtio"
    end
  end

  # Disable Synced Folder
  config.vm.synced_folder ".", "/vagrant", disabled: true

  # Enable SSH Password Authentication
  config.vm.provision "shell", inline: <<-SHELL
    sed -i 's/archive.ubuntu.com/mirror.kakao.com/g' /etc/apt/sources.list
    sed -i 's/security.ubuntu.com/mirror.kakao.com/g' /etc/apt/sources.list
    sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
    systemctl restart ssh
  SHELL
end

vm 배포

vagrant up
$ vagrant up
Bringing machine 'kube-control1' up with 'virtualbox' provider...
Bringing machine 'kube-node1' up with 'virtualbox' provider...
Bringing machine 'kube-node2' up with 'virtualbox' provider...
Bringing machine 'kube-node3' up with 'virtualbox' provider...
==> kube-control1: Importing base box 'ubuntu/focal64'...
==> kube-control1: Matching MAC address for NAT networking...
==> kube-control1: Checking if box 'ubuntu/focal64' version '20220913.0.0' is up to date...
==> kube-control1: Setting the name of the VM: kube-control1
==> kube-control1: Clearing any previously set network interfaces...
==> kube-control1: Preparing network interfaces based on configuration...
    kube-control1: Adapter 1: nat
    kube-control1: Adapter 2: hostonly
==> kube-control1: Forwarding ports...
    kube-control1: 22 (guest) => 2222 (host) (adapter 1)
==> kube-control1: Running 'pre-boot' VM customizations...
==> kube-control1: Booting VM...
==> kube-control1: Waiting for machine to boot. This may take a few minutes...
    kube-control1: SSH address: 127.0.0.1:2222
    kube-control1: SSH username: vagrant
    kube-control1: SSH auth method: private key
    kube-control1: Warning: Connection reset. Retrying...
    kube-control1: Warning: Connection reset. Retrying...
    kube-control1:
    kube-control1: Vagrant insecure key detected. Vagrant will automatically replace
    kube-control1: this with a newly generated keypair for better security.
    kube-control1:
    kube-control1: Inserting generated public key within guest...
    kube-control1: Removing insecure key from the guest if it's present...
    kube-control1: Key inserted! Disconnecting and reconnecting using new SSH key...
==> kube-control1: Machine booted and ready!
==> kube-control1: Checking for guest additions in VM...
==> kube-control1: Setting hostname...
==> kube-control1: Configuring and enabling network interfaces...
==> kube-control1: Running provisioner: shell...
    kube-control1: Running: inline script
...

vm 상태 확인

vagrant status
$ vagrant status
Current machine states:

kube-control1             running (virtualbox)
kube-node1                running (virtualbox)
kube-node2                running (virtualbox)
kube-node3                running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

vm 접속(ssh 연결)

vagrant ssh kube-control1
$ vagrant ssh kube-control1
Welcome to Ubuntu 20.04.5 LTS (GNU/Linux 5.4.0-125-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Mon Sep 19 12:49:12 UTC 2022

  System load:  0.02              Processes:               134
  Usage of /:   3.5% of 38.70GB   Users logged in:         0
  Memory usage: 6%                IPv4 address for enp0s3: 10.0.2.15
  Swap usage:   0%                IPv4 address for enp0s8: 192.168.56.11

 * Super-optimized for small spaces - read how we shrank the memory
   footprint of MicroK8s to make it the smallest full K8s around.

   https://ubuntu.com/blog/microk8s-memory-optimisation

0 updates can be applied immediately.

New release '22.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

vagrant@kube-control1:~$

vm 종료

vagrant halt

vm 삭제

vagrant destroy
728x90
반응형