Git Product home page Git Product logo

aws-bare-metal-kvm-demo's Introduction

aws-bare-metal-kvm

The purpose of this repository is to show virtualization using KVM on a bare metal server on AWS

This type of EC2 Instances offer the best of both worlds, allowing the operacional system to be executed directly on the underlying hardware, at the same time that it provides acess to all of the benefits of the cloud.

Amazon EC2 Bare Metal Instances

Sumary

Prerequisites

  • Configured Amazon VPC with at least one public subnet
  • If you are going to test Windows, download Windows ISO

Creating our Amazon EC2

For this demonstration we will use an EC2 of type: i3.metal:

i3.metal

Login to AWS console and select EC2 > instances > Launch Instance

Obs: We will use Ubuntu 18.04 as the operational system

Select the instance type i3.metal > Configure Instance Details

Select the VPC and subnet where you want to do the launch of your instance

Obs: It will be necessary to accomplish SSH on your instance, therefore realize the launch on a public subnet or have mechanisms to access your instance (VPN/Bastion)

Select the amount of GB for the Root Volume (We will use this virtual machine to do virtualization, therefore select a proper ammount)

Define a Name Tag for your EC2

Obs: I will use the name kvm-virtualization-lab

Click on Configure Security Group

Create a specific Security Group for your EC2 or select one that already exists.

Obs: Remember to check the necessary ports on the Security Group to do the remote access to our virtual machines

Click on Review and Launch

Validate the informations and click on Launch

Create a private key .pem in case you don´t have or utilize one that already exists

Click on Launch Instance

Wait a few minutes for your EC2 Instance be ready to be accessed

Installing KVM

In this repository there are some scripts that will help us to accomplish all of the configuration steps.

ssh -i bare-metal-demo.pem [email protected]

Realize SSH on the server and follow the following steps:

sudo su - 
cd /opt/ && apt-get update && apt-get install git -y
git clone https://github.com/aws-samples/aws-bare-metal-kvm-demo.git

Do the KVM and the necessary components installation

cd aws-bare-metal-kvm-demo && ./install-kvm-ubuntu.sh

Creating the first Ubuntu VM

For this demonstration we will create a Ubuntu 18.04 server with 1GB of RAM and 2 vCpu

./create-ubuntu-vm.sh

Wait for the creation completion, it can take some time. After completion it will be necessary to login again in the server

A Logon screen will be shown, use the default user and password.

User: ubuntu

Pass: ubuntu

Go back to the Host OS and list the VM'ms

sudo virsh -c qemu:///system list

Defining an static IP using the Default network Nat-based networking

We will use the default network crated on the KVM instalation process

Using virsh

You can create, exclude, execute, stop and manage your virtual machines from the command line, using a tool called virsh. Virsh is mostly useful for advanced Linux administrators, interested ​​in scripts or automating some aspects of managing their virtual machines

virsh net-list
virsh net-info default

The NAT based network is commonly provided and enabled by default fot the majority of the principal linux distributions that supports KVM virtualization.

This network configuration uses a Linux brigde combined with Network Address Translation (NAT) to allow that a guest operational system gets output conectivity , independent of the network type (with wire, wireless, dial-up and goes on) used on KVM host with no need of any specific administrator configuration.

Defining an static IP for our VM

Execute the script define-static-networking-kvm.sh

./define-static-networking-kvm.sh

Put the name of the virtual machine that you want to define the IP, on our case is ubuntu-01

Copy the line that starts with <host mac='

Edit the file of network definition

sudo virsh net-edit default

Add the line that we copied above under <range

Save the file and execute the following commands

sudo virsh net-destroy default
sudo virsh net-start default
sudo virsh shutdown ubuntu-01
sudo systemctl stop libvirtd && sudo systemctl start libvirtd
sudo virsh start ubuntu-01

Test the SSH for our VM

Exposing our VM to external access via IP Tables

Since we are using the configuration of a default network of type NAT we don´t have a network interface addded on our virtual machine, we will use a rule of IP Tables based on a port to accomplish the external access to our virtual server.

We will use the Hooks of QEMU

Crieate the following file /etc/libvirt/hooks/qemu

sudo vim /etc/libvirt/hooks/qemu

Add the following content

#!/bin/bash

# Script that add iptables rule to forward traffic to VM's

if [ "${1}" = "VM NAME" ]; then

   # Update the following variables to fit your setup
   GUEST_IP=
   GUEST_PORT=
   HOST_PORT=

   if [ "${2}" = "stopped" ] || [ "${2}" = "reconnect" ]; then
	/sbin/iptables -D FORWARD -o virbr0 -p tcp -d $GUEST_IP --dport $GUEST_PORT -j ACCEPT
	/sbin/iptables -t nat -D PREROUTING -p tcp --dport $HOST_PORT -j DNAT --to $GUEST_IP:$GUEST_PORT
   fi
   if [ "${2}" = "start" ] || [ "${2}" = "reconnect" ]; then
	/sbin/iptables -I FORWARD -o virbr0 -p tcp -d $GUEST_IP --dport $GUEST_PORT -j ACCEPT
	/sbin/iptables -t nat -I PREROUTING -p tcp --dport $HOST_PORT -j DNAT --to $GUEST_IP:$GUEST_PORT
   fi
fi

Replacing the following variables for ours, in my case it was like this:

#!/bin/bash

# Script that add iptables rule to forward traffic to VM's

if [ "${1}" = "ubuntu-01" ]; then

   # Update the following variables to fit your setup
   GUEST_IP=192.168.122.3
   GUEST_PORT=22
   HOST_PORT=2222

   if [ "${2}" = "stopped" ] || [ "${2}" = "reconnect" ]; then
	/sbin/iptables -D FORWARD -o virbr0 -p tcp -d $GUEST_IP --dport $GUEST_PORT -j ACCEPT
	/sbin/iptables -t nat -D PREROUTING -p tcp --dport $HOST_PORT -j DNAT --to $GUEST_IP:$GUEST_PORT
   fi
   if [ "${2}" = "start" ] || [ "${2}" = "reconnect" ]; then
	/sbin/iptables -I FORWARD -o virbr0 -p tcp -d $GUEST_IP --dport $GUEST_PORT -j ACCEPT
	/sbin/iptables -t nat -I PREROUTING -p tcp --dport $HOST_PORT -j DNAT --to $GUEST_IP:$GUEST_PORT
   fi
fi

Where GUEST_IP is the IP of our VM, GUEST_PORT is the port that we will do the redirect of the traffic, in this case SSH port, HOST_PORT the port that we will map from the host to the guest

sudo chmod +x /etc/libvirt/hooks/qemu
sudo virsh shutdown ubuntu-01
sudo systemctl stop libvirtd && sudo systemctl start libvirtd
sudo virsh start ubuntu-01

Testing the SSH, log-off from our EC2 and do the ssh pointing for the port that we will do the forward via IP Tables.

Obs: Don´t forget to open the Security Group on our EC2 on the port 2222

ssh ubuntu@EC2_IP -p 2222

The result should be the same of loging in from inside the Host OS

References

Networking with KVM https://aboullaite.me/kvm-qemo-forward-ports-with-iptables/

Setup KVM on Ubuntu 18.04 https://blog.programster.org/set-up-ubuntu-18-04-KVM-server https://ostechnix.com/setup-headless-virtualization-server-using-kvm-ubuntu/

Helper Script to create VM https://blog.programster.org/ubuntu-18-04-getting-started-with-kvm-using-php-helper-script

Forwarding connection https://wiki.libvirt.org/page/Networking#Forwarding_Incoming_Connections

Libvirt Default Networking https://wiki.libvirt.org/page/Networking

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

aws-bare-metal-kvm-demo's People

Contributors

amazon-auto avatar gabrielmartinigit avatar isagmonteiro avatar lusoal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-bare-metal-kvm-demo's Issues

Shared vGPUs on KVM hypervisor

Will it work with g4 instances? I want to host a server that support vgpus with the help of KVM hypervisor on aws.

Also can i attached tesla T4 multiple gpus on i3 bare metal instance?

"Exposing our VM to external access via IP Tables" part not working ?

I desperately need a way to start my VMS on EC2 (i3.metal), and these VMs need to be networked. And I think your solution gives me some hope how it may happen.

Anyway, when I have followed your instructions to make your case working first:

  1. I was able to finish Defining an static IP for our VM part, and do ssh [email protected] to access this VM,

  2. But when I have followed the instruction of Exposing our VM to external access via IP Tables part, to try to make a connect as ssh ubuntu@EC2_IP -p 2222, the command hangs.

Following is my iptables picture before I am running this command, and you may find utuntu-01 is of ip 192.168.122.243 in my case.

iptables -L

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootps

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             ip-192-168-122-243.us-west-1.compute.internal  tcp dpt:ssh
ACCEPT     all  --  anywhere             ip-192-168-122-0.us-west-1.compute.internal/24  ctstate RELATED,ESTABLISHED
ACCEPT     all  --  ip-192-168-122-0.us-west-1.compute.internal/24  anywhere
ACCEPT     all  --  anywhere             anywhere
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootpc

And my EC2 instance is open to all traffic to my desktop ip.

Do you know from where I can tell the port 2222 is open ? What could be the reason for this hanging ?

Thanks a lot for the help

Chun Ji

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.