Vagrant Operations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 44: Line 44:


=Managing a VM in an AWS Environment=
=Managing a VM in an AWS Environment=
==Describe the VM in a Vagrantfile==
<syntaxhighlight lang='ruby'>
require_relative 'helpers'
# AWS authentication is assumed to be setup in shell environment
BASTION_HOST = '52.43.31.217'
# Change the default AMI ID when a new one is built, see ./README.md##bringing-up-a-single-node-k8s-cluster
AMI_ID = ENV['AMI_ID'] || 'ami-00ae6b0dc1d1c7404'
AWS_KEYPAIR = 'sbox-ml-kp-01'
SUBNET_ID = 'subnet-0fe2965e8bf239c09' # private subnet
SECURITY_GROUP_ID = 'sg-041f9d51704199e98' # should only require ssh access
SSH_PRIVATE_KEY = "~/.ssh/#{AWS_KEYPAIR}.pem"
SSH_CONFIG_FILE = "#{ENV['HOME']}/.ssh/dev/minikube"
# Create and configure the AWS instance(s)
Vagrant.configure('2') do |config|
  ensure_plugins(%w[vagrant-aws])
  config.vm.define :minikube do |t|
  end
  config.vm.hostname = hostname
  config.vm.box = 'dummy' # Use dummy AWS box
  config.vm.synced_folder '.', '/vagrant', disabled: true
  # Specify AWS provider configuration
  config.vm.provider 'aws' do |aws, override|
    # Specify SSH keypair to use, which should match SSH_PRIVATE_KEY
    aws.keypair_name = AWS_KEYPAIR
    aws.instance_type = 'm5.4xlarge'
    aws.associate_public_ip = false
    aws.elastic_ip = false
    # Launch configuration
    aws.ami = AMI_ID
    aws.subnet_id = SUBNET_ID
    aws.security_groups = [SECURITY_GROUP_ID]
    aws.block_device_mapping = [{ 'DeviceName' => '/dev/xvda', 'Ebs.VolumeSize' => 50 }]
    aws.tags = {
      'Name' => hostname,
      'Created by' => username,
      'Environment' => 'k8s'
    }
    # Specify username and private key path
    config.ssh.forward_agent = true
    override.ssh.username = 'ec2-user'
    override.ssh.private_key_path = SSH_PRIVATE_KEY
    override.ssh.proxy_command = "ssh -o ExitOnForwardFailure=yes -W %h:%p -i #{override.ssh.private_key_path} %r@#{BASTION_HOST}"
  end
  config.trigger.after [:up] do |t|
    t.info = "Writing ssh config to #{SSH_CONFIG_FILE}"
    t.run = { path: './utils/set-ssh-config.sh', args: [SSH_CONFIG_FILE.to_s] }
  end
end
</syntaxhighlight>

Revision as of 21:29, 16 November 2019

External

Internal

Box Operations

List

vagrant box list

Instance Operations

Start and provision the vagrant environment:

vagrant up

If property configured, bringing the machine up may create a ~/.ssh/dev/vagranthostname configuration that can be used by ssh to connect directly.

Connect to machine via SSH:

vagrant ssh

How does it know to connect to the instance it was started? More about jump box.

Status of the VM:

vagrant global-status
vagrant status [name|id]

Stops and deletes all traces of the vagrant machine:

vagrant destroy
vagrant destroy [name|id]

If the corresponding VM runs in AWS EC2, this terminates the instance.

Managing a VM in an AWS Environment

Describe the VM in a Vagrantfile

require_relative 'helpers'

# AWS authentication is assumed to be setup in shell environment
BASTION_HOST = '52.43.31.217'
# Change the default AMI ID when a new one is built, see ./README.md##bringing-up-a-single-node-k8s-cluster
AMI_ID = ENV['AMI_ID'] || 'ami-00ae6b0dc1d1c7404'
AWS_KEYPAIR = 'sbox-ml-kp-01'
SUBNET_ID = 'subnet-0fe2965e8bf239c09' # private subnet
SECURITY_GROUP_ID = 'sg-041f9d51704199e98' # should only require ssh access
SSH_PRIVATE_KEY = "~/.ssh/#{AWS_KEYPAIR}.pem"
SSH_CONFIG_FILE = "#{ENV['HOME']}/.ssh/dev/minikube"

# Create and configure the AWS instance(s)
Vagrant.configure('2') do |config|
  ensure_plugins(%w[vagrant-aws])

  config.vm.define :minikube do |t|
  end
  config.vm.hostname = hostname
  config.vm.box = 'dummy' # Use dummy AWS box
  config.vm.synced_folder '.', '/vagrant', disabled: true

  # Specify AWS provider configuration
  config.vm.provider 'aws' do |aws, override|
    # Specify SSH keypair to use, which should match SSH_PRIVATE_KEY
    aws.keypair_name = AWS_KEYPAIR
    aws.instance_type = 'm5.4xlarge'
    aws.associate_public_ip = false
    aws.elastic_ip = false

    # Launch configuration
    aws.ami = AMI_ID
    aws.subnet_id = SUBNET_ID
    aws.security_groups = [SECURITY_GROUP_ID]
    aws.block_device_mapping = [{ 'DeviceName' => '/dev/xvda', 'Ebs.VolumeSize' => 50 }]
    aws.tags = {
      'Name' => hostname,
      'Created by' => username,
      'Environment' => 'k8s'
    }

    # Specify username and private key path
    config.ssh.forward_agent = true
    override.ssh.username = 'ec2-user'
    override.ssh.private_key_path = SSH_PRIVATE_KEY
    override.ssh.proxy_command = "ssh -o ExitOnForwardFailure=yes -W %h:%p -i #{override.ssh.private_key_path} %r@#{BASTION_HOST}"
  end

  config.trigger.after [:up] do |t|
    t.info = "Writing ssh config to #{SSH_CONFIG_FILE}"
    t.run = { path: './utils/set-ssh-config.sh', args: [SSH_CONFIG_FILE.to_s] }
  end
end