Posted in DevOps, Docker

Vagrant là gì? Làm thế nào để ứng dụng Vagrant.

Khi các bạn xài Window vài muốn chạy Linux, thường thì lựa chọn sẽ là Virtual Machine(oracle Virtual Box chẳng hạn). Khi xài virtual-box các bạn phải set memory, capacity to máy ảo bạn sử dụng, sau đó lên mạng tải ubunu images về mount vào mới chạy được, đó là 1 quá trình không quá dài nhưng cũng mệt mỏi

Trong bài này mình sẽ giới thiệu 1 cho các bạn 1 công cụ mới tên là Vagrant.

Định nghĩa: Vagrant là một công cụ đa nền tảng cho phép bạn chỉ định Máy ảo ( dưới dạng Vagrantfile ) để triển khai tới một hypervisor (như VirtualBox trên laptop của bạn).

Vargant cũng gần như docker, bạn chỉ cần tạo 1 vagrantfile, trong đó cấu hình Linux machine của bạn, ví dụ như

file sau:

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-20.04"
end

File này chỉ định bạn sẽ tạo 1 vagrant box cài ubnutu-20.04

C:\Users\Thanh\VirtualBox VMs> vagrant up
A Vagrant environment or target machine is required to run this
command. Run `vagrant init` to create a new Vagrant environment. Or,
get an ID of a target machine from `vagrant global-status` to run
this command on. A final option is to change to a directory with a
Vagrantfile and to try again.

Để bật máy lên chỉ cần dùng lệnh “vagrant up”

C:\Users\Thanh\VirtualBox VMs> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'bento/ubuntu-20.04'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'bento/ubuntu-20.04' version '202010.24.0' is up to date...
==> default: A newer version of the box 'bento/ubuntu-20.04' for provider 'virtualbox' is
==> default: available! You currently have version '202010.24.0'. The latest is version
==> default: '202012.23.0'. Run `vagrant box update` to update.
==> default: Setting the name of the VM: VirtualBoxVMs_default_1617373630300_91414
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/Thanh/VirtualBox VMs

Để access vào máy để làm việc các dùng lệnh “vagrant ssh”.

C:\Users\Thanh\VirtualBox VMs> vagrant ssh
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-52-generic x86_64)

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

 System information disabled due to load higher than 1.0

Vagrant làm cho việc setup máy ảo trên laptop của bạn đơn giản hơn rất nhiều. Chỉ cần 1 vài dòng lệnh là vagrant up là đã có 1 máy ảo chạy Ubuntu hoặc CenOS. Để access vào máy ảo chỉ cần vagrant ssh.

Ranh giới giữa máy ảo và container đang bị mờ. Bản cập nhật gần đây của Vagrant, đã có rất nhiều thứ được bổ sung, bao gồm cả Vagrant Cloud. Vagrant Cloud là một dịch vụ lữu trữ các boxes, tương tự như dockerhub, mà bạn có thể tải về và xài tương tự, ví dụ bạn cần 1 máy ảo có cài sẵn postgresql, chỉ cần lên Vagrant Cloud tải về vargrant file sau đó bật lên là được.

Phần sau mình sẽ làm về setup docker. Bên docker cũng tương tự khi bạn chỉ cần dockerfile sau đó dùng dockerun để chạy container.

Author:

My name is Truong Thanh, graduated Master of Information Technology and Artificial Intelligent in Frankfurt University,Germany. I create this Blog to share my experience about life, study, travel...with friend who have the same hobbies.

One thought on “Vagrant là gì? Làm thế nào để ứng dụng Vagrant.

Leave a comment