0% found this document useful (0 votes)
28 views

20dit057 Ac

Uploaded by

20dit057
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

20dit057 Ac

Uploaded by

20dit057
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 76

IT442 : ADVANCED COMPUTING 20DIT057

Practical – 1

Aim: To implement Cloud-based infrastructures and services, it is required to set up the


complete system requirements. Researchers & industry-based developers can focus on
specific system design issues that they want to investigate, without taking more concerned
about the low-level details so the Cloudsim is very much useful for these activities and it can
support simulation environment to implement cloud-based infrastructure solutions.
Overview of Cloudsim functionalities:
Support for modeling and simulation of large-scale Cloud computing data centers support for
modeling and simulation of virtualized server hosts, with customizable policies for
provisioning host resources to virtual machines support for modeling and simulation of data
center network topologies and message-passing applications support for dynamic insertion of
simulation elements, stop and resume of simulation support for user-defined policies for
allocation of hosts to virtual machines and policies for allocation of host resources to virtual
machines.
Perform Cloud Computing Set up using Cloudsim Tool:
1. Introduction to Cloudsim tool.
2. Perform Installation steps of Cloudsim on NetBeans.

Theory:
 It’s a framework for modeling and simulation of cloud computing infrastructures and
services.
 CloudSim supports modeling and simulation of cloud computing data centers,
virtualized hosts, allocation policies, network topologies and message passing
applications. Moreover, the framework can be integrated with Eclipse and NetBeans,
allowing you to code in Java.

There are few entities we need to understand before using CloudSim.


 Datacenter: Set of hosts that runs virtual machines.
 Datacenter Broker : This broker is responsible for submitting VM provisioning
requests to the datacenter and then submitting tasks to VMs.
 Host : The machine that runs and manages the virtual machine.
 VM: A software representation of a machine that can process tasks.
 Cloudlet – A task or an application that actually runs in VM.

Implementation:
1. Download NetBeans and Install it.
2. Download CloudSim and unzip it.
3. Once Netbeans IDE is installed, start creating a new Project via File > New Project.
Choose “Java” under Categories and “Java Application” under Projects.

DEPSTAR (IT) 1
IT442 : ADVANCED COMPUTING 20DIT057

Figure 1.1
Name your project and click “Finish” button.

Figure 1.2

DEPSTAR (IT) 2
IT442 : ADVANCED COMPUTING 20DIT057

On the left sidebar, right click on the project and select “Properties” as shown below:

Figure 1.3
In the properties dialog, select “Libraries” and click on “Add Library” button at the right. In
the “Add Library” dialog, select cloudsim jar file (will be located in jars folder) downloaded
in step 2. Now you can drag and drop example programs (under
examples/org/cloudbus/cloudsim) from the CloudSim directory into your application. That’s
it, your CloudSim toolkit it ready to use.

Figure 1.4

DEPSTAR (IT) 3
IT442 : ADVANCED COMPUTING 20DIT057

Expand org.cloudbus.cloudsim.examples folder to find all example programs. Select your


desired example and start running the simulation. The output will be displayed at the bottom
pane as shown below:

Figure 1.5

Conclusion:
From this practical, I learned about how to install and configure CloudSIm step by step in
Netbeans IDE.

DEPSTAR (IT) 4
IT442 : ADVANCED COMPUTING 20DIT057

Practical – 2

Aim: Cloud Computing aims for Internet based application services to deliver reliable,
secure, fault - tolerant, scalable infrastructure. It is a tremendous challenging task to model
and schedule the different applications and services on real cloud infrastructure which
requires to handle different workload and energy performance parameters. Consider the real-
world analogy into cloudsim and
Perform following Programs:
1. Write a program in cloudsim using NetBeans IDE to create a datacenter with one host
and run four cloudlets on it.
Implementation:
/*
* Title: CloudSim Toolkit
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation
* of Clouds
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009, The University of Melbourne, Australia
*/

//package org.cloudbus.cloudsim.examples;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.Datacenter;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.UtilizationModel;
import org.cloudbus.cloudsim.UtilizationModelFull;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;

DEPSTAR (IT) 5
IT442 : ADVANCED COMPUTING 20DIT057

/**
* A simple example showing how to create
* a datacenter with one host and run two
* cloudlets on it. The cloudlets run in
* VMs with the same MIPS requirements.
* The cloudlets will take the same time to
* complete the execution.
*/
public class CloudSimExample2 {

/** The cloudlet list. */


private static List<Cloudlet> cloudletList;

/** The vmlist. */


private static List<Vm> vmlist;

/**
* Creates main() to run this example
*/
public static void main(String[] args) {
Log.printLine("Starting CloudSimExample2...");
try {
// First step: Initialize the CloudSim package. It should be called
// before creating any entities.
int num_user = 1; // number of cloud users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean trace events

// Initialize the CloudSim library


CloudSim.init(num_user, calendar, trace_flag);

// Second step: Create Datacenters


//Datacenters are the resource providers in CloudSim. We need at list
one of them to run a CloudSim simulation
@SuppressWarnings("unused")
Datacenter datacenter0 =
createDatacenter("Datacenter_0");

//Third step: Create Broker


DatacenterBroker broker = createBroker();
int brokerId = broker.getId();

//Fourth step: Create one virtual machine


vmlist = new ArrayList<Vm>();

//VM description
int vmid = 0;
int mips = 250;
long size = 10000; //image size (MB)
int ram = 512; //vm memory (MB)

DEPSTAR (IT) 6
IT442 : ADVANCED COMPUTING 20DIT057

long bw = 1000;
int pesNumber = 1; //number of cpus
String vmm = "Xen"; //VMM name

//create two VMs


Vm vm1 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());

vmid++;
Vm vm2 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());

vmid++;
Vm vm3 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());

vmid++;
Vm vm4 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());

//add the VMs to the vmList


vmlist.add(vm1);
vmlist.add(vm2);
vmlist.add(vm3);
vmlist.add(vm4);

//submit vm list to the broker


broker.submitVmList(vmlist);

//Fifth step: Create two Cloudlets


cloudletList = new ArrayList<Cloudlet>();

//Cloudlet properties
int id = 0;
pesNumber=1;
long length = 250000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();

Cloudlet cloudlet1 = new Cloudlet(id, length, pesNumber, fileSize,


outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet1.setUserId(brokerId);

id++;
Cloudlet cloudlet2 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet2.setUserId(brokerId);

DEPSTAR (IT) 7
IT442 : ADVANCED COMPUTING 20DIT057

id++;
Cloudlet cloudlet3 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet3.setUserId(brokerId);

id++;
Cloudlet cloudlet4 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet4.setUserId(brokerId);

//add the cloudlets to the list


cloudletList.add(cloudlet1);
cloudletList.add(cloudlet2);
cloudletList.add(cloudlet3);
cloudletList.add(cloudlet4);

//submit cloudlet list to the broker


broker.submitCloudletList(cloudletList);

//bind the cloudlets to the vms. This way, the broker


// will submit the bound cloudlets only to the specific VM
broker.bindCloudletToVm(cloudlet1.getCloudletId(),vm1.getId());
broker.bindCloudletToVm(cloudlet2.getCloudletId(),vm2.getId());
broker.bindCloudletToVm(cloudlet3.getCloudletId(),vm3.getId());
broker.bindCloudletToVm(cloudlet4.getCloudletId(),vm4.getId());

// Sixth step: Starts the simulation


CloudSim.startSimulation();

// Final step: Print results when simulation is over


List<Cloudlet> newList = broker.getCloudletReceivedList();

CloudSim.stopSimulation();

printCloudletList(newList);

}
catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an unexpected
error");
}
}
private static Datacenter createDatacenter(String name){
// Here are the steps needed to create a PowerDatacenter:
// 1. We need to create a list to store
// our machine
List<Host> hostList = new ArrayList<Host>();

// 2. A Machine contains one or more PEs or CPUs/Cores.

DEPSTAR (IT) 8
IT442 : ADVANCED COMPUTING 20DIT057

// In this example, it will have only one core.


List<Pe> peList = new ArrayList<Pe>();

int mips = 1000;

// 3. Create PEs and add these into a list.


peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id
and MIPS Rating

//4. Create Host with its id and list of PEs and add them to the list of machines
int hostId=0;
int ram = 2048; //host memory (MB)
long storage = 1000000; //host storage
int bw = 10000;

hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList)
)
); // This is our machine

// 5. Create a DatacenterCharacteristics object that stores the


// properties of a data center: architecture, OS, list of
// Machines, allocation policy: time- or space-shared, time zone
// and its price (G$/Pe time unit).
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this
resource
double costPerStorage = 0.001; // the cost of using storage in this resource
double costPerBw = 0.0; // the cost of using bw in this resource

LinkedList<Storage> storageList = new LinkedList<Storage>(); //we are


not adding SAN devices by now

DatacenterCharacteristics characteristics = new DatacenterCharacteristics(


arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage,
costPerBw);

// 6. Finally, we need to create a PowerDatacenter object.


Datacenter datacenter = null;
try {

DEPSTAR (IT) 9
IT442 : ADVANCED COMPUTING 20DIT057

datacenter = new Datacenter(name, characteristics, new


VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}

return datacenter;
}

//We strongly encourage users to develop their own broker policies, to submit
vms and cloudlets according

//to the specific rules of the simulated scenario


private static DatacenterBroker createBroker(){

DatacenterBroker broker = null;


try {
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}

/**
* Prints the Cloudlet objects
* @param list list of Cloudlets
*/
private static void printCloudletList(List<Cloudlet> list) {
int size = list.size();
Cloudlet cloudlet;
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent +
"Data center ID" + indent + "VM ID" + indent + "Time" + indent + "Start
Time" + indent + "Finish Time");

DecimalFormat dft = new DecimalFormat("###.##");


for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
Log.print(indent + cloudlet.getCloudletId() + indent + indent);

if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS){
Log.print("SUCCESS");

Log.printLine( indent + indent + cloudlet.getResourceId() + indent +


indent + indent + cloudlet.getVmId() +
indent + indent + dft.format(cloudlet.getActualCPUTime()) + indent +

DEPSTAR (IT) 10
IT442 : ADVANCED COMPUTING 20DIT057

indent + dft.format(cloudlet.getExecStartTime())+
indent + indent + dft.format(cloudlet.getFinishTime()));
}
}
}
}

Output:

Figure 2.1

DEPSTAR (IT) 11
IT442 : ADVANCED COMPUTING 20DIT057

2. Write a program in cloudsim using NetBeans IDE to create a datacenter with three
host and run three cloudlets on it.

Implementation:
/*
* Title: CloudSim Toolkit
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation
* of Clouds
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009, The University of Melbourne, Australia
*/

//package org.cloudbus.cloudsim.examples;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.Datacenter;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.UtilizationModel;
import org.cloudbus.cloudsim.UtilizationModelFull;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;

/**
* A simple example showing how to create
* a datacenter with two hosts and run two
* cloudlets on it. The cloudlets run in
* VMs with different MIPS requirements.
* The cloudlets will take different time
* to complete the execution depending on
* the requested VM performance.
*/
public class CloudSimExample3 {

DEPSTAR (IT) 12
IT442 : ADVANCED COMPUTING 20DIT057

/** The cloudlet list. */


private static List<Cloudlet> cloudletList;
/** The vmlist. */
private static List<Vm> vmlist;
/**
* Creates main() to run this example
*/
public static void main(String[] args) {
Log.printLine("Starting CloudSimExample3...");
try {
// First step: Initialize the CloudSim package. It should be called
// before creating any entities.
int num_user = 1; // number of cloud users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean trace events
// Initialize the CloudSim library
CloudSim.init(num_user, calendar, trace_flag);
// Second step: Create Datacenters
//Datacenters are the resource providers in CloudSim. We need at list
one of them to run a CloudSim simulation
@SuppressWarnings("unused")
Datacenter datacenter0 = createDatacenter("Datacenter_0");
//Third step: Create Broker
DatacenterBroker broker = createBroker();
int brokerId = broker.getId();
//Fourth step: Create one virtual machine
vmlist = new ArrayList<Vm>();
//VM description
int vmid = 0;
int mips = 250;
long size = 10000; //image size (MB)
int ram = 2048; //vm memory (MB)
long bw = 1000;
int pesNumber = 1; //number of cpus
String vmm = "Xen"; //VMM name
//create two VMs
Vm vm1 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());
//the second VM will have twice the priority of VM1 and so will
receive twice CPU time
vmid++;
Vm vm2 = new Vm(vmid, brokerId, mips * 2, pesNumber, ram, bw,
size, vmm, new CloudletSchedulerTimeShared());
vmid++;
Vm vm3 = new Vm(vmid, brokerId, mips * 3, pesNumber, ram, bw,
size, vmm, new CloudletSchedulerTimeShared());
//add the VMs to the vmList
vmlist.add(vm1);
vmlist.add(vm2);
vmlist.add(vm3);

DEPSTAR (IT) 13
IT442 : ADVANCED COMPUTING 20DIT057

//submit vm list to the broker


broker.submitVmList(vmlist);
//Fifth step: Create two Cloudlets
cloudletList = new ArrayList<Cloudlet>();
//Cloudlet properties
int id = 0;
long length = 40000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet cloudlet1 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet1.setUserId(brokerId);
id++;
Cloudlet cloudlet2 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet2.setUserId(brokerId);
id++;
Cloudlet cloudlet3 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet3.setUserId(brokerId);
//add the cloudlets to the list
cloudletList.add(cloudlet1);
cloudletList.add(cloudlet2);
cloudletList.add(cloudlet3);
//submit cloudlet list to the broker
broker.submitCloudletList(cloudletList);
//bind the cloudlets to the vms. This way, the broker
// will submit the bound cloudlets only to the specific VM
broker.bindCloudletToVm(cloudlet1.getCloudletId(),vm1.getId());
broker.bindCloudletToVm(cloudlet2.getCloudletId(),vm2.getId());
broker.bindCloudletToVm(cloudlet3.getCloudletId(),vm3.getId());
// Sixth step: Starts the simulation
CloudSim.startSimulation();
// Final step: Print results when simulation is over
List<Cloudlet> newList = broker.getCloudletReceivedList();
CloudSim.stopSimulation();
printCloudletList(newList);
}
catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an
unexpected error");
}
}
private static Datacenter createDatacenter(String name){
// Here are the steps needed to create a PowerDatacenter:
// 1. We need to create a list to store
// our machine
List<Host> hostList = new ArrayList<Host>();

DEPSTAR (IT) 14
IT442 : ADVANCED COMPUTING 20DIT057

// 2. A Machine contains one or more PEs or CPUs/Cores.


// In this example, it will have only one core.
List<Pe> peList = new ArrayList<Pe>();
int mips = 1000;
// 3. Create PEs and add these into a list.
peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id
and MIPS Rating
//4. Create Hosts with its id and list of PEs and add them to the list of
machines
int hostId=0;
int ram = 2048; //host memory (MB)
long storage = 1000000; //host storage
int bw = 10000;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList)
)
); // This is our first machine
//create another machine in the Data center
List<Pe> peList2 = new ArrayList<Pe>();
peList2.add(new Pe(0, new PeProvisionerSimple(mips)));
hostId++;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList2,
new VmSchedulerTimeShared(peList2)
)
); // This is our second machine
List<Pe> peList3 = new ArrayList<Pe>();
peList3.add(new Pe(0, new PeProvisionerSimple(mips)));
hostId++;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList3,
new VmSchedulerTimeShared(peList3)
)
);

DEPSTAR (IT) 15
IT442 : ADVANCED COMPUTING 20DIT057

// 5. Create a DatacenterCharacteristics object that stores the


// properties of a data center: architecture, OS, list of
// Machines, allocation policy: time- or space-shared, time zone
// and its price (G$/Pe time unit).
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this
resource
double costPerStorage = 0.001; // the cost of using storage in this
resource
double costPerBw = 0.0; // the cost of using bw in this
resource
LinkedList<Storage> storageList = new LinkedList<Storage>(); //we are
not adding SAN devices by now
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage,
costPerBw);
// 6. Finally, we need to create a PowerDatacenter object.
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new
VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
//We strongly encourage users to develop their own broker policies, to submit vms
and cloudlets according
//to the specific rules of the simulated scenario
private static DatacenterBroker createBroker(){
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
/**
* Prints the Cloudlet objects
* @param list list of Cloudlets
*/
private static void printCloudletList(List<Cloudlet> list) {
int size = list.size();
Cloudlet cloudlet;

DEPSTAR (IT) 16
IT442 : ADVANCED COMPUTING 20DIT057

String indent = " ";


Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent +
"Data center ID" + indent + "VM ID" + indent + "Time" +
indent + "Start Time" + indent + "Finish Time");
DecimalFormat dft = new DecimalFormat("###.##");
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
Log.print(indent + cloudlet.getCloudletId() + indent + indent);
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS){
Log.print("SUCCESS");
Log.printLine( indent + indent + cloudlet.getResourceId() +
indent + indent + indent + cloudlet.getVmId() +
indent + indent +
dft.format(cloudlet.getActualCPUTime()) + indent + indent +
dft.format(cloudlet.getExecStartTime())+
indent + indent +
dft.format(cloudlet.getFinishTime()));
}
}
}
}
Output:

Figure 2.2

DEPSTAR (IT) 17
IT442 : ADVANCED COMPUTING 20DIT057

3. Write a program in cloudsim using NetBeans IDE to create five datacenters with five
host and run cloudlets of five users on them.

Implementation:
/*
* Title: CloudSim Toolkit
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation
* of Clouds
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.examples;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.Datacenter;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.UtilizationModel;
import org.cloudbus.cloudsim.UtilizationModelFull;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.VmSchedulerSpaceShared;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
/**
* A simple example showing how to create
* two datacenters with one host each and
* run cloudlets of two users on them.
*/
public class CloudSimExample5 {
/** The cloudlet lists. */
private static List<Cloudlet> cloudletList1;
private static List<Cloudlet> cloudletList2;
private static List<Cloudlet> cloudletList3;
private static List<Cloudlet> cloudletList4;
private static List<Cloudlet> cloudletList5;

DEPSTAR (IT) 18
IT442 : ADVANCED COMPUTING 20DIT057

/** The vmlists. */


private static List<Vm> vmlist1;
private static List<Vm> vmlist2;
private static List<Vm> vmlist3;
private static List<Vm> vmlist4;
private static List<Vm> vmlist5;
/**
* Creates main() to run this example
*/
public static void main(String[] args) {
Log.printLine("Starting CloudSimExample5...");
try {
// First step: Initialize the CloudSim package. It should be called
// before creating any entities.
int num_user = 2; // number of cloud users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean trace events
// Initialize the CloudSim library
CloudSim.init(num_user, calendar, trace_flag);
// Second step: Create Datacenters
//Datacenters are the resource providers in CloudSim. We need at list
one of them to run a CloudSim simulation
@SuppressWarnings("unused")
Datacenter datacenter0 = createDatacenter("Datacenter_0");
@SuppressWarnings("unused")
Datacenter datacenter1 = createDatacenter("Datacenter_1");
@SuppressWarnings("unused")
Datacenter datacenter2 = createDatacenter("Datacenter_2");
@SuppressWarnings("unused")
Datacenter datacenter3 = createDatacenter("Datacenter_3");
@SuppressWarnings("unused")
Datacenter datacenter4 = createDatacenter("Datacenter_4");
//Third step: Create Brokers
DatacenterBroker broker1 = createBroker(1);
int brokerId1 = broker1.getId();
DatacenterBroker broker2 = createBroker(2);
int brokerId2 = broker2.getId();

DatacenterBroker broker3 = createBroker(3);


int brokerId3 = broker3.getId();

DatacenterBroker broker4 = createBroker(4);


int brokerId4 = broker4.getId();

DatacenterBroker broker5 = createBroker(5);


int brokerId5 = broker5.getId();
//Fourth step: Create one virtual machine for each broker/user
vmlist1 = new ArrayList<Vm>();
vmlist2 = new ArrayList<Vm>();
vmlist3 = new ArrayList<Vm>();

DEPSTAR (IT) 19
IT442 : ADVANCED COMPUTING 20DIT057

vmlist4 = new ArrayList<Vm>();


vmlist5 = new ArrayList<Vm>();
//VM description
int vmid = 0;
int mips = 250;
long size = 10000; //image size (MB)
int ram = 512; //vm memory (MB)
long bw = 1000;
int pesNumber = 1; //number of cpus
String vmm = "Xen"; //VMM name
//create two VMs: the first one belongs to user1
Vm vm1 = new Vm(vmid, brokerId1, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());
//the second VM: this one belongs to user2
Vm vm2 = new Vm(vmid, brokerId2, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());
Vm vm3 = new Vm(vmid, brokerId2, mips, pesNumber, ram, bw, size, vmm,
new CloudletSchedulerTimeShared());
Vm vm4 = new Vm(vmid, brokerId2, mips, pesNumber, ram, bw, size, vmm,
new CloudletSchedulerTimeShared());
Vm vm5 = new Vm(vmid, brokerId2, mips, pesNumber, ram, bw, size, vmm,
new CloudletSchedulerTimeShared());
//add the VMs to the vmlists
vmlist1.add(vm1);
vmlist2.add(vm2);
vmlist3.add(vm3);
vmlist4.add(vm4);
vmlist5.add(vm5);
//submit vm list to the broker
broker1.submitVmList(vmlist1);
broker2.submitVmList(vmlist2);
broker3.submitVmList(vmlist3);
broker4.submitVmList(vmlist4);
broker5.submitVmList(vmlist5);
//Fifth step: Create two Cloudlets
cloudletList1 = new ArrayList<Cloudlet>();
cloudletList2 = new ArrayList<Cloudlet>();
cloudletList3 = new ArrayList<Cloudlet>();
cloudletList4 = new ArrayList<Cloudlet>();
cloudletList5 = new ArrayList<Cloudlet>();
//Cloudlet properties
int id = 0;
long length = 40000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet cloudlet1 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet1.setUserId(brokerId1);
Cloudlet cloudlet2 = new Cloudlet(id, length, pesNumber, fileSize,

DEPSTAR (IT) 20
IT442 : ADVANCED COMPUTING 20DIT057

outputSize, utilizationModel, utilizationModel, utilizationModel);


cloudlet2.setUserId(brokerId2);
Cloudlet cloudlet3 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet3.setUserId(brokerId2);

Cloudlet cloudlet4 = new Cloudlet(id, length, pesNumber, fileSize,


outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet4.setUserId(brokerId2);

Cloudlet cloudlet5 = new Cloudlet(id, length, pesNumber, fileSize,


outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet5.setUserId(brokerId2);
//add the cloudlets to the lists: each cloudlet belongs to one user
cloudletList1.add(cloudlet1);
cloudletList2.add(cloudlet2);
cloudletList3.add(cloudlet3);
cloudletList4.add(cloudlet4);
cloudletList5.add(cloudlet5);
//submit cloudlet list to the brokers
broker1.submitCloudletList(cloudletList1);
broker2.submitCloudletList(cloudletList2);
broker3.submitCloudletList(cloudletList3);
broker4.submitCloudletList(cloudletList4);
broker5.submitCloudletList(cloudletList5);
// Sixth step: Starts the simulation
CloudSim.startSimulation();
// Final step: Print results when simulation is over
List<Cloudlet> newList1 = broker1.getCloudletReceivedList();
List<Cloudlet> newList2 = broker2.getCloudletReceivedList();
List<Cloudlet> newList3 = broker3.getCloudletReceivedList();
List<Cloudlet> newList4 = broker4.getCloudletReceivedList();
List<Cloudlet> newList5 = broker5.getCloudletReceivedList();
CloudSim.stopSimulation();
Log.print("=============> User "+brokerId1+" ");
printCloudletList(newList1);
Log.print("=============> User "+brokerId2+" ");
printCloudletList(newList2);

}
catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an
unexpected error");
}
}
private static Datacenter createDatacenter(String name){
// Here are the steps needed to create a PowerDatacenter:
// 1. We need to create a list to store
// our machine

DEPSTAR (IT) 21
IT442 : ADVANCED COMPUTING 20DIT057

List<Host> hostList = new ArrayList<Host>();


// 2. A Machine contains one or more PEs or CPUs/Cores.
// In this example, it will have only one core.
List<Pe> peList = new ArrayList<Pe>();
int mips=1000;
// 3. Create PEs and add these into a list.
peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id
and MIPS Rating
//4. Create Host with its id and list of PEs and add them to the list of machines
int hostId=0;
int ram = 2048; //host memory (MB)
long storage = 1000000; //host storage
int bw = 10000;
//in this example, the VMAllocatonPolicy in use is SpaceShared. It means that
only one VM
//is allowed to run on each Pe. As each Host has only one Pe, only one VM
can run on each Host.
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerSpaceShared(peList)
)
); // This is our first machine
// 5. Create a DatacenterCharacteristics object that stores the
// properties of a data center: architecture, OS, list of
// Machines, allocation policy: time- or space-shared, time zone
// and its price (G$/Pe time unit).
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this
resource
double costPerStorage = 0.001; // the cost of using storage in this
resource
double costPerBw = 0.0; // the cost of using bw in this
resource
LinkedList<Storage> storageList = new LinkedList<Storage>(); //we are
not adding SAN devices by now
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage,
costPerBw);
// 6. Finally, we need to create a PowerDatacenter object.
Datacenter datacenter = null;
try {

DEPSTAR (IT) 22
IT442 : ADVANCED COMPUTING 20DIT057

datacenter = new Datacenter(name, characteristics, new


VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
//We strongly encourage users to develop their own broker policies, to submit vms
and cloudlets according
//to the specific rules of the simulated scenario
private static DatacenterBroker createBroker(int id){
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker"+id);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
/**
* Prints the Cloudlet objects
* @param list list of Cloudlets
*/
private static void printCloudletList(List<Cloudlet> list) {
int size = list.size();
Cloudlet cloudlet;
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent +
"Data center ID" + indent + "VM ID" + indent + "Time" +
indent + "Start Time" + indent + "Finish Time");
DecimalFormat dft = new DecimalFormat("###.##");
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
Log.print(indent + cloudlet.getCloudletId() + indent + indent);
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS){
Log.print("SUCCESS");
Log.printLine( indent + indent + cloudlet.getResourceId() +
indent + indent + indent + cloudlet.getVmId() +
indent + indent +
dft.format(cloudlet.getActualCPUTime()) + indent + indent +
dft.format(cloudlet.getExecStartTime())+
indent + indent +
dft.format(cloudlet.getFinishTime()));
}
}
}
}

DEPSTAR (IT) 23
IT442 : ADVANCED COMPUTING 20DIT057

Output:

Figure 2.3

Conclusion:
From this practical, I learned how to simulate the real-world analogy using cloudsim by
modifying the examples of the cloudsim.

DEPSTAR (IT) 24
IT442 : ADVANCED COMPUTING 20DIT057

Practical – 3

Aim: Perform following Programs on network topology using Cloudsim:


1. Write a program in cloudsim to create three datacenters with three host and a network
topology each and run three cloudlets on them.
Implementation:
/*
* Title: CloudSim Toolkit
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation
* of Clouds
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.examples.network;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.Datacenter;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.NetworkTopology;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.UtilizationModel;
import org.cloudbus.cloudsim.UtilizationModelFull;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.VmSchedulerTimeShared;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
/**
* A simple example showing how to create
* two datacenters with one host and a
* network topology each and run two cloudlets
* on them.
*/
public class NetworkExample2 {
/** The cloudlet list. */

DEPSTAR (IT) 25
IT442 : ADVANCED COMPUTING 20DIT057

private static List<Cloudlet> cloudletList;


/** The vmlist. */
private static List<Vm> vmlist;
/**
* Creates main() to run this example
*/
public static void main(String[] args) {
Log.printLine("Starting NetworkExample2...");
try {
// First step: Initialize the CloudSim package. It should be called
// before creating any entities.
int num_user = 1; // number of cloud users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean trace events
// Initialize the CloudSim library
CloudSim.init(num_user, calendar, trace_flag);
// Second step: Create Datacenters
//Datacenters are the resource providers in CloudSim. We need at list
one of them to run a CloudSim simulation
Datacenter datacenter0 = createDatacenter("Datacenter_0");
Datacenter datacenter1 = createDatacenter("Datacenter_1");
Datacenter datacenter2 = createDatacenter("Datacenter_2");
//Third step: Create Broker
DatacenterBroker broker = createBroker();
int brokerId = broker.getId();
//Fourth step: Create one virtual machine
vmlist = new ArrayList<Vm>();
//VM description
int vmid = 0;
int mips = 250;
long size = 10000; //image size (MB)
int ram = 512; //vm memory (MB)
long bw = 1000;
int pesNumber = 1; //number of cpus
String vmm = "Xen"; //VMM name
//create two VMs
Vm vm1 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());
//the second VM will have twice the priority of VM1 and so will receive
twice CPU time
vmid++;
Vm vm2 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());
vmid++;
Vm vm3 = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());
//add the VMs to the vmList
vmlist.add(vm1);
vmlist.add(vm2);
vmlist.add(vm3);

DEPSTAR (IT) 26
IT442 : ADVANCED COMPUTING 20DIT057

//submit vm list to the broker


broker.submitVmList(vmlist);
//Fifth step: Create two Cloudlets
cloudletList = new ArrayList<Cloudlet>();
//Cloudlet properties
int id = 0;
long length = 40000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet cloudlet1 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet1.setUserId(brokerId);
id++;
Cloudlet cloudlet2 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet2.setUserId(brokerId);
id++;
Cloudlet cloudlet3 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet3.setUserId(brokerId);
//add the cloudlets to the list
cloudletList.add(cloudlet1);
cloudletList.add(cloudlet2);
cloudletList.add(cloudlet3);
//submit cloudlet list to the broker
broker.submitCloudletList(cloudletList);
//bind the cloudlets to the vms. This way, the broker
// will submit the bound cloudlets only to the specific VM
broker.bindCloudletToVm(cloudlet1.getCloudletId(),vm1.getId());
broker.bindCloudletToVm(cloudlet2.getCloudletId(),vm2.getId());
broker.bindCloudletToVm(cloudlet3.getCloudletId(),vm3.getId());
//Sixth step: configure network
//load the network topology file
NetworkTopology.buildNetworkTopology("topology.brite");
//maps CloudSim entities to BRITE entities
//Datacenter0 will correspond to BRITE node 0
int briteNode=0;
NetworkTopology.mapNode(datacenter0.getId(),briteNode);
//Datacenter1 will correspond to BRITE node 2
briteNode=2;
NetworkTopology.mapNode(datacenter1.getId(),briteNode);
briteNode=3;
NetworkTopology.mapNode(datacenter2.getId(),briteNode);
//Broker will correspond to BRITE node 3
briteNode=4;
NetworkTopology.mapNode(broker.getId(),briteNode);
// Sixth step: Starts the simulation
CloudSim.startSimulation();
// Final step: Print results when simulation is over

DEPSTAR (IT) 27
IT442 : ADVANCED COMPUTING 20DIT057

List<Cloudlet> newList = broker.getCloudletReceivedList();


CloudSim.stopSimulation();
printCloudletList(newList);

}
catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an
unexpected error");
}}
private static Datacenter createDatacenter(String name){
// Here are the steps needed to create a PowerDatacenter:
// 1. We need to create a list to store
// our machine
List<Host> hostList = new ArrayList<Host>();
// 2. A Machine contains one or more PEs or CPUs/Cores.
// In this example, it will have only one core.
List<Pe> peList = new ArrayList<Pe>();
int mips = 1000;
// 3. Create PEs and add these into a list
peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id
and MIPS Rating
//4. Create Host with its id and list of PEs and add them to the list of machines
int hostId=0;
int ram = 2048; //host memory (MB)
long storage = 1000000; //host storage
int bw = 10000;
//in this example, the VMAllocatonPolicy in use is Time Shared with priorities.
It means that VMs
//receive time shares accroding to their priority.
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList)
)
); // This is our machine
List<Pe> peList2 = new ArrayList<Pe>();
peList2.add(new Pe(0, new PeProvisionerSimple(mips)));
hostId++;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList2,

DEPSTAR (IT) 28
IT442 : ADVANCED COMPUTING 20DIT057

new VmSchedulerTimeShared(peList2)
)
);
List<Pe> peList3 = new ArrayList<Pe>();
peList3.add(new Pe(0, new PeProvisionerSimple(mips)));
hostId++;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList3,
new VmSchedulerTimeShared(peList3)
) );
// 5. Create a DatacenterCharacteristics object that stores the
// properties of a data center: architecture, OS, list of
// Machines, allocation policy: time- or space-shared, time zone
// and its price (G$/Pe time unit).
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this
resource
double costPerStorage = 0.001; // the cost of using storage in this resource
double costPerBw = 0.0; // the cost of using bw in this
resource
LinkedList<Storage> storageList = new LinkedList<Storage>(); //we are not
adding SAN devices by now
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, time_zone, cost, costPerMem,
costPerStorage, costPerBw);
// 6. Finally, we need to create a PowerDatacenter object.
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new
VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
//We strongly encourage users to develop their own broker policies, to submit vms and
cloudlets according
//to the specific rules of the simulated scenario
private static DatacenterBroker createBroker(){
DatacenterBroker broker = null;
try {

DEPSTAR (IT) 29
IT442 : ADVANCED COMPUTING 20DIT057

broker = new DatacenterBroker("Broker");


} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
/**
* Prints the Cloudlet objects
* @param list list of Cloudlets
*/
private static void printCloudletList(List<Cloudlet> list) {
int size = list.size();
Cloudlet cloudlet;
String indent = " ";
Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent +
"Data center ID" + indent + "VM ID" + indent + "Time" + indent
+ "Start Time" + indent + "Finish Time");
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
Log.print(indent + cloudlet.getCloudletId() + indent + indent);
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS){
Log.print("SUCCESS");
DecimalFormat dft = new DecimalFormat("###.##");
Log.printLine( indent + indent + cloudlet.getResourceId() +
indent + indent + indent + cloudlet.getVmId() +
indent + indent +
dft.format(cloudlet.getActualCPUTime()) + indent + indent +
dft.format(cloudlet.getExecStartTime())+
indent + indent +
dft.format(cloudlet.getFinishTime()));
}}}}

DEPSTAR (IT) 30
IT442 : ADVANCED COMPUTING 20DIT057

Output:

Figure 3.1

DEPSTAR (IT) 31
IT442 : ADVANCED COMPUTING 20DIT057

2. Write a program in cloudsim to create two datacenters with one host each and run
cloudlets of three users with network topology on them.

Implementation:
/*
* Title: CloudSim Toolkit
* Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation
* of Clouds
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.examples.network;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.CloudletSchedulerTimeShared;
import org.cloudbus.cloudsim.Datacenter;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.DatacenterCharacteristics;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.NetworkTopology;
import org.cloudbus.cloudsim.Pe;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.UtilizationModel;
import org.cloudbus.cloudsim.UtilizationModelFull;
import org.cloudbus.cloudsim.Vm;
import org.cloudbus.cloudsim.VmAllocationPolicySimple;
import org.cloudbus.cloudsim.VmSchedulerSpaceShared;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
/**
* A simple example showing how to create
* two datacenters with one host each and
* run cloudlets of two users with network
* topology on them.
*/
public class NetworkExample3 {
/** The cloudlet list. */
private static List<Cloudlet> cloudletList1;
private static List<Cloudlet> cloudletList2;
private static List<Cloudlet> cloudletList3;

DEPSTAR (IT) 32
IT442 : ADVANCED COMPUTING 20DIT057

/** The vmlist. */


private static List<Vm> vmlist1;
private static List<Vm> vmlist2;
private static List<Vm> vmlist3;
/**
* Creates main() to run this example
*/
public static void main(String[] args) {
Log.printLine("Starting NetworkExample3...");
try {
// First step: Initialize the CloudSim package. It should be called
// before creating any entities.
int num_user = 2; // number of cloud users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean trace events
// Initialize the CloudSim library
CloudSim.init(num_user, calendar, trace_flag);
// Second step: Create Datacenters
//Datacenters are the resource providers in CloudSim. We need at list
one of them to run a CloudSim simulation
Datacenter datacenter0 = createDatacenter("Datacenter_0");
Datacenter datacenter1 = createDatacenter("Datacenter_1");
//Third step: Create Brokers
DatacenterBroker broker1 = createBroker(1);
int brokerId1 = broker1.getId();
DatacenterBroker broker2 = createBroker(2);
int brokerId2 = broker2.getId();
DatacenterBroker broker3 = createBroker(3);
int brokerId3 = broker3.getId();
//Fourth step: Create one virtual machine for each broker/user
vmlist1 = new ArrayList<Vm>();
vmlist2 = new ArrayList<Vm>();
vmlist3 = new ArrayList<Vm>();
//VM description
int vmid = 0;
long size = 10000; //image size (MB)
int mips = 250;
int ram = 512; //vm memory (MB)
long bw = 1000;
int pesNumber = 1; //number of cpus
String vmm = "Xen"; //VMM name
//create two VMs: the first one belongs to user1
Vm vm1 = new Vm(vmid, brokerId1, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());
//the second VM: this one belongs to user2
Vm vm2 = new Vm(vmid, brokerId2, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());
Vm vm3 = new Vm(vmid, brokerId2, mips, pesNumber, ram, bw, size,
vmm, new CloudletSchedulerTimeShared());
//add the VMs to the vmlists

DEPSTAR (IT) 33
IT442 : ADVANCED COMPUTING 20DIT057

vmlist1.add(vm1);
vmlist2.add(vm2);
vmlist3.add(vm3);
//submit vm list to the broker
broker1.submitVmList(vmlist1);
broker2.submitVmList(vmlist2);
broker3.submitVmList(vmlist3);
//Fifth step: Create two Cloudlets
cloudletList1 = new ArrayList<Cloudlet>();
cloudletList2 = new ArrayList<Cloudlet>();
cloudletList3 = new ArrayList<Cloudlet>();
//Cloudlet properties
int id = 0;
long length = 40000;
long fileSize = 300;
long outputSize = 300;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet cloudlet1 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet1.setUserId(brokerId1);
Cloudlet cloudlet2 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet2.setUserId(brokerId2);
Cloudlet cloudlet3 = new Cloudlet(id, length, pesNumber, fileSize,
outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet3.setUserId(brokerId3);
//add the cloudlets to the lists: each cloudlet belongs to one user
cloudletList1.add(cloudlet1);
cloudletList2.add(cloudlet2);
cloudletList3.add(cloudlet3);
//submit cloudlet list to the brokers
broker1.submitCloudletList(cloudletList1);
broker2.submitCloudletList(cloudletList2);
broker3.submitCloudletList(cloudletList3);
//Sixth step: configure network //load the network topology file
NetworkTopology.buildNetworkTopology("topology.brite");
//maps CloudSim entities to BRITE entities
//Datacenter0 will correspond to BRITE node 0
int briteNode=0;
NetworkTopology.mapNode(datacenter0.getId(),briteNode);
//Datacenter1 will correspond to BRITE node 2
briteNode=2;
NetworkTopology.mapNode(datacenter1.getId(),briteNode);
//Broker1 will correspond to BRITE node 3
briteNode=3;
NetworkTopology.mapNode(broker1.getId(),briteNode);
//Broker2 will correspond to BRITE node 4
briteNode=4;
NetworkTopology.mapNode(broker2.getId(),briteNode);
briteNode=5;

DEPSTAR (IT) 34
IT442 : ADVANCED COMPUTING 20DIT057

NetworkTopology.mapNode(broker3.getId(),briteNode);
// Sixth step: Starts the simulation
CloudSim.startSimulation();
// Final step: Print results when simulation is over
List<Cloudlet> newList1 = broker1.getCloudletReceivedList();
List<Cloudlet> newList2 = broker2.getCloudletReceivedList();
List<Cloudlet> newList3 = broker3.getCloudletReceivedList();
CloudSim.stopSimulation();
Log.print("=============> User "+brokerId1+" ");
printCloudletList(newList1);
Log.print("=============> User "+brokerId2+" ");
printCloudletList(newList2);
Log.print("=============> User "+brokerId3+" ");
printCloudletList(newList3);
Log.printLine("19DIT015 RUSHI DONGA");
}
catch (Exception e) {
e.printStackTrace();
Log.printLine("The simulation has been terminated due to an
unexpected error");
}}
private static Datacenter createDatacenter(String name){
// Here are the steps needed to create a PowerDatacenter:
// 1. We need to create a list to store our machine
List<Host> hostList = new ArrayList<Host>();
// 2. A Machine contains one or more PEs or CPUs/Cores.
// In this example, it will have only one core.
List<Pe> peList = new ArrayList<Pe>();
int mips = 1000;
// 3. Create PEs and add these into a list.
peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id
and MIPS Rating
//4. Create Host with its id and list of PEs and add them to the list of machines
int hostId=0;
int ram = 2048; //host memory (MB)
long storage = 1000000; //host storage
int bw = 10000;
//in this example, the VMAllocatonPolicy in use is SpaceShared. It means that
only one VM
//is allowed to run on each Pe. As each Host has only one Pe, only one VM can
run on each Host.
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerSpaceShared(peList)
)

DEPSTAR (IT) 35
IT442 : ADVANCED COMPUTING 20DIT057

); // This is our machine


// 5. Create a DatacenterCharacteristics object that stores the
// properties of a data center: architecture, OS, list of
// Machines, allocation policy: time- or space-shared, time zone
// and its price (G$/Pe time unit).
String arch = "x86"; // system architecture
String os = "Linux"; // operating system
String vmm = "Xen";
double time_zone = 10.0; // time zone this resource located
double cost = 3.0; // the cost of using processing in this resource
double costPerMem = 0.05; // the cost of using memory in this
resource
double costPerStorage = 0.001; // the cost of using storage in this resource
double costPerBw = 0.0; // the cost of using bw in this
resource
LinkedList<Storage> storageList = new LinkedList<Storage>(); //we are not
adding SAN devices by now
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, time_zone, cost, costPerMem,
costPerStorage, costPerBw);
// 6. Finally, we need to create a PowerDatacenter object.
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics, new
VmAllocationPolicySimple(hostList), storageList, 0);
} catch (Exception e) {
e.printStackTrace();
}
return datacenter;
}
//We strongly encourage users to develop their own broker policies, to submit vms and
cloudlets according
//to the specific rules of the simulated scenario
private static DatacenterBroker createBroker(int id){
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker("Broker"+id);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
/**
* Prints the Cloudlet objects
* @param list list of Cloudlets
*/
private static void printCloudletList(List<Cloudlet> list) {
int size = list.size();
Cloudlet cloudlet;

DEPSTAR (IT) 36
IT442 : ADVANCED COMPUTING 20DIT057

String indent = " ";


Log.printLine();
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + indent + "STATUS" + indent +
"Data center ID" + indent + "VM ID" + indent + "Time" + indent
+ "Start Time" + indent + "Finish Time");
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
Log.print(indent + cloudlet.getCloudletId() + indent + indent);
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS){
Log.print("SUCCESS");
DecimalFormat dft = new DecimalFormat("###.##");
Log.printLine( indent + indent + cloudlet.getResourceId() +
indent + indent + indent + cloudlet.getVmId() +
indent + indent +
dft.format(cloudlet.getActualCPUTime()) + indent + indent +
dft.format(cloudlet.getExecStartTime())+
indent + indent +
dft.format(cloudlet.getFinishTime()));
}}}}

Output:

Figure 3.2
Conclusion:
From this practical, I learned how to simulate the real-world analogy of the network topology
using cloudsim by modifying the network examples of the cloudsim.

DEPSTAR (IT) 37
IT442 : ADVANCED COMPUTING 20DIT057

Practical – 4

Aim: Perform following using Cloud Analyst:


1. Install a Cloud Analyst and Integrate with NetBeans. Monitor the performance of an
Existing Algorithms given in Cloud Analyst.
2. Modify or propose a new load balancing algorithm compatible with Cloud Analyst.

Implementation:
Step 1: Install cloud Analyst from http://www.cloudbus.org/cloudsim/CloudAnalyst.zip
Step 2: Extract the zip file and then click on run file to run the simulator.

Figure 4.1
Step 3: The Dashboard looks like as shown in the below image.

Figure 4.2

DEPSTAR (IT) 38
IT442 : ADVANCED COMPUTING 20DIT057

Step 4: Analysis of the Existing algorithm in cloud Analyst.


Configuration of existing algorithm:

Figure 4.3

Figure 4.4

Figure 4.5

DEPSTAR (IT) 39
IT442 : ADVANCED COMPUTING 20DIT057

Detailed results of the simulation:

Figure 4.6

Figure 4.7

DEPSTAR (IT) 40
IT442 : ADVANCED COMPUTING 20DIT057

Figure 4.8
Step 5: Modifying the algorithm.
Configuration of the algorithm:

Figure 4.9

Figure 4.10

DEPSTAR (IT) 41
IT442 : ADVANCED COMPUTING 20DIT057

Detailed results of the simulation:

Figure 4.11

Figure 4.12
Results of the simulation:

Figure 4.13
Conclusion:
From this practical, I learned about the Cloud Analyst. Also learned how to configure the
simulation and run the simulation.

DEPSTAR (IT) 42
IT442 : ADVANCED COMPUTING 20DIT057

PRACTICAL - 5

AIM:
1. To create a webserver using EC2 instance on AWS
2. To host a simple web page over an EC2 instance.
3. Setup AWS with RDS and SQL

IMPLEMENTATION:

PRACTICAL 1:
Step 1: Select Amazon Machine Image (AMI).

Figure 5.1

Step 2: Choose Instance Type.

Figure 5.2

Step 3: Choose your key-pair or create one key


pair.

DEPSTAR (IT) 43
IT442 : ADVANCED COMPUTING 20DIT057

Figure 5.3

Step 4: Choose network settings

Figure 5.4

Step 5: Create another volume.

Figure 5.5

Step 6: Click on Launch Instance.

DEPSTAR (IT) 44
IT442 : ADVANCED COMPUTING 20DIT057

Figure 5.6

Step 7: Click on Connect at top right corner to connect with your instance.

Figure 5.7

PRACTICAL 2:
Step 1: Run the following commands:
“sudo su”
“yum install httpd”

DEPSTAR (IT) 45
IT442 : ADVANCED COMPUTING 20DIT057

Figure 5.7

Step 2: After completion of the httpd service, enable the service using the following
commands:
“systemctl start httpd”
“systemctl enable httpd”
“systemctl status httpd”

Figure 5.8

Step 3: Copy the public IP of the EC instance and open it in the browser to see a test page
of the apache server which indicates that the server is running.

DEPSTAR (IT) 46
IT442 : ADVANCED COMPUTING 20DIT057

Figure 5.9

Step 4: Run the following command to replace the html with your own custom html on the
webserver:
“echo "<h1>Hello world from $(hostname -f)</h1>" > /var/www/html/index.html”
Refresh the server to see the changes.

Figure 5.10

PRACTICAL 3:
Step 1: Go to the RDS service and enter the Database menu, click on Create Databases.

DEPSTAR (IT) 47
IT442 : ADVANCED COMPUTING 20DIT057

Figure 5.11

Step 2: Select Standard create

Figure 5.12

Step 3: Select engine type as MySQL

Figure 5.13

DEPSTAR (IT) 48
IT442 : ADVANCED COMPUTING 20DIT057

Step 4: Select Free tier in templates

Figure 5.13
Step 5: Enter the Master username and password.

Figure 5.14

Step 6: Keep all the other options all default. Click on Create database.

Figure 5.15

DEPSTAR (IT) 49
IT442 : ADVANCED COMPUTING 20DIT057

Step 7: Go to the databases tab to see the created database.

Figure 5.16

Figure 5.17

CONCLUSION:
In this practical, I have learned about the services of AWS such as AWS EC2 and AWS RDS

DEPSTAR (IT) 50
IT442 : ADVANCED COMPUTING 20DIT057

PRACTICAL-6

AIM:
Create a Static Website and Host it on AWS using S3 bucket.

IMPLEMENTATION:

Step-1: Name the bucket and select the AWS Region for the bucket.

Figure 6.1

Step-2: Uncheck block all public access

Figure 6.2

DEPSTAR (IT) 51
IT442 : ADVANCED COMPUTING 20DIT057

Step-3: Click on Create Bucket

Figure 6.3

Step-4: Go to the bucket and click on upload

Figure 6.4

DEPSTAR (IT) 52
IT442 : ADVANCED COMPUTING 20DIT057

Step-5: Select the files to upload and click Upload.

Figure 6.5

Step-6: Go to properties and click on Edit in Static website hosting.

Figure 6.6

DEPSTAR (IT) 53
IT442 : ADVANCED COMPUTING 20DIT057

Step-7: Click enable and type the name of the html document of the website.
Finally click Save changes.

Figure 6.7

Step-8: Click on permissions and in the bucket policy section click on Edit and
add the following policy and click Save changes.
{
"Version": "2008-10-17",
"Id": "PolicyForPublicWebsiteContent",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::shreyans-website-bucket/*"
}

DEPSTAR (IT) 54
IT442 : ADVANCED COMPUTING 20DIT057

]}

Figure 6.8

Step-9: Open the link given in the static website hosting section

Figure 6.9

DEPSTAR (IT) 55
IT442 : ADVANCED COMPUTING 20DIT057

Practical – 7

Aim:
1. Create and setup monitoring service for AWS cloud resources and the applications you run
on AWS (Amazon CloudWatch).
Link: https://explore.skillbuilder.aws/learn/course/external/view/elearning/203/introduction-
to-amazon-cloudwatch

2. Create an AWS Identity and Access Management (IAM) group and user, attach a policy
and
add a user to a group.
Link: https://explore.skillbuilder.aws/learn/course/external/view/elearning/120/introduction-
to-aws-identity-and-access-management-iam

Implementation:
Step 1: Select Amazon Machine Image (AMI).

Figure 7.1
Step 2: Choose Instance Type.

DEPSTAR (IT) 56
IT442 : ADVANCED COMPUTING 20DIT057

Figure 7.2
Step 3: Choose your key-pair or create one key pair.

Figure 7.3
Step 4: Set your network settings and choose security group or create one.

Figure 7.4
Step 5: Create another volume.

Figure 7.5
Step 6: Click on Launch Instance.

DEPSTAR (IT) 57
IT442 : ADVANCED COMPUTING 20DIT057

DEPSTAR (IT) 58
IT442 : ADVANCED COMPUTING 20DIT057

Step 7: Click on Connect at top right corner to connect with your instance.

Figure 7.6
1. Create and setup monitoring service for AWS cloud resources and the applications
you run on AWS (Amazon CloudWatch).
Link:https://explore.skillbuilder.aws/learn/course/external/view/elearning/203/
introduction-to-amazon-cloudwatch

Implementation:
Step 1: Click on CloudWatch service by searching on AWS platform.
Step 2: Click on Metrics -> All metrics. Here, you can see all type of metrics.

Figure 7.7
Step 3: Click on Alarms -> All alarms. Here, you can create an alarm or you can perform
required action.

DEPSTAR (IT) 59
IT442 : ADVANCED COMPUTING 20DIT057

Figure 7.8
2. Create an AWS Identity and Access Management (IAM) group and user, attach a
policy and add a user to a group.
Link:https://explore.skillbuilder.aws/learn/course/external/view/elearning/120/
introduction-to-aws-identity-and-access-management-iam

Implementation:
Step 1: Click on IAM -> Access Management -> Users -> Add users. Write the User name.
Select AWS Access type according to requirements.

Figure 7.9
Step 2: Click on Next: Permissions -> Attach existing policies directly. Select which type of
access you want to give to the user.

DEPSTAR (IT) 60
IT442 : ADVANCED COMPUTING 20DIT057

Figure 7.10
Step 3: Click on Next: Tags. You can give the tag to the user. Then, Click on Review. Then,
Click on Create User. Then click on close after downloading the csv file.

Figure 7.11
Step 4: To create the user group, Click on IAM -> Access Management -> User Groups ->
Create Group. Give the name of the group.

Figure 7.12
Step 5: Then, Select the users under the Add users to the group and attach the policies under
the Attach permissions policies according to the requirements.

Figure 7.13

DEPSTAR (IT) 61
IT442 : ADVANCED COMPUTING 20DIT057

Step 6: Click on Create Group.

Figure 7.14
Conclusion:
In this practical, I have learned about the various services of AWS Cloud Platform like EC2,
CloudWatch, IAM.

DEPSTAR (IT) 62
IT442 : ADVANCED COMPUTING 20DIT057

Practical – 8

Aim: A Comparative Study of Docker Engine on Windows Server vs Linux Platform


Comparing the feature sets and implementations of Docker on Windows and Linux and Build
and Run Your First Docker Windows Server Container Walkthrough installing Docker on
Windows 10, building a Docker image and running a Windows container.
Link: https://github.com/docker/labs/tree/master/beginner/

Implementation:
To build a Docker image and running a Windows Container:
To get started, let's run the following in our terminal:
$ docker pull alpine

The pull command fetches the alpine image from the Docker registry and saves it in our
system. You can use the docker images command to see a list of all images on your system.
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
alpine latest c51f86c28340 4 weeks ago 1.109 MB
hello-world latest 690ed74de00f 5 months ago 960 B

Great! Let's now run a Docker container based on this image. To do that you are going to use
the docker run command.
$ docker run alpine ls -l
total 48
drwxr-xr-x 2 root root 4096 Mar 2 16:20 bin
drwxr-xr-x 5 root root 360 Mar 18 09:47 dev
drwxr-xr-x 13 root root 4096 Mar 18 09:47 etc
drwxr-xr-x 2 root root 4096 Mar 2 16:20 home
drwxr-xr-x 5 root root 4096 Mar 2 16:20 lib
......
......

Conclusion:
In this practical, I have learned about building a Docker image and running a Windows
Container.

DEPSTAR (IT) 63
IT442 : ADVANCED COMPUTING 20DIT057

Practical – 9

Aim: Perform following using Google Cloud Platform:


1. Introduction to Google Cloud.

Theory:
 Google Cloud is a suite of cloud services hosted on Google's infrastructure.
 From computing and storage, to data analytics, machine learning, and networking,
Google Cloud offers a wide variety of services and APIs that can be integrated with
any cloud-computing application or project, from personal to enterprise-grade.
Features and Components of Google Cloud Platform:
Start Lab (button):
Clicking this button creates a temporary Google Cloud environment, with all the necessary
services and credentials enabled, so you can get hands-on practice with the lab's material.
This also starts a countdown timer.
Credit:
The price of a lab. 1 Credit is usually equivalent to 1 US dollar (discounts are available when
you purchase credits in bulk). Some introductory-level labs (like this one) are free. The more
specialized labs cost more because they involve heavier computing tasks and demand more
Google Cloud resources.
Time:
Specifies the amount of time you have to complete a lab. When you click the Start Lab
button, the timer will count down until it reaches 00:00:00. When it does, your temporary
Google Cloud environment and resources are deleted. Ample time is given to complete a lab,
but make sure you don't work on something else while a lab is running: you risk losing all of
your hard work!
Score:
Many labs include a score. This feature is called "activity tracking" and ensures that you
complete specified steps in a lab. To pass a lab with activity tracking, you need to complete
all the steps in order. Only then will you receive completion credit.

DEPSTAR (IT) 64
IT442 : ADVANCED COMPUTING 20DIT057

2. Perform Google Cloud Hands-on Labs.

Implementation:
Step 1: Sign in to Google Cloud.

Figure 9.1
Step 2: View all projects.

Figure 9.2
Step 3: View your roles and permissions.

DEPSTAR (IT) 65
IT442 : ADVANCED COMPUTING 20DIT057

Figure 9.3
Step 4: View available APIs.

Figure 9.4
Step 5: On the Navigation menu (Navigation menu), click APIs & Services > Library. The
left pane, under the header CATEGORY, displays the different categories available. In the
API search bar, type Dialogflow, and then click Dialogflow API. The Dialogflow description
page opens.

Figure 9.5
Step 6: Click Enable.

DEPSTAR (IT) 66
IT442 : ADVANCED COMPUTING 20DIT057

Figure 9.6

3. Set Up Network and HTTP Load Balancers on Google Cloud Platform.


Link: https://www.cloudskillsboost.google/focuses/12007?parent=catalog

Implementation:
Task 1: Set the default region and zone for all resources
Step 1: In Cloud Shell, set the default zone:
gcloud config set compute/zone us-central1-a
Step 2: Set the default region:
gcloud config set compute/region us-central1

Figure 9.7

Task 2: Create multiple web server instances


Step 1: Create a virtual machine www1 in your default zone.
gcloud compute instances create www1 \
--zone= \
--tags=network-lb-tag \
--machine-type=e2-medium \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart

DEPSTAR (IT) 67
IT442 : ADVANCED COMPUTING 20DIT057

echo "
<h3>Web Server: www1</h3>" | tee /var/www/html/index.html'

Figure 9.8
Step 2: Create a virtual machine www2 in your default zone.
gcloud compute instances create www2 \
--zone= \
--tags=network-lb-tag \
--machine-type=e2-medium \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "
<h3>Web Server: www2</h3>" | tee /var/www/html/index.html'

Figure 9.9

DEPSTAR (IT) 68
IT442 : ADVANCED COMPUTING 20DIT057

Step 3: Create a virtual machine www3 in your default zone.


gcloud compute instances create www3 \
--zone= \
--tags=network-lb-tag \
--machine-type=e2-medium \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "
<h3>Web Server: www3</h3>" | tee /var/www/html/index.html'

Figure 9.10
Step 4: Create a firewall rule to allow external traffic to the VM instances:
gcloud compute firewall-rules create www-firewall-network-lb \
--target-tags network-lb-tag --allow tcp:80

Figure 9.11
Step 5: Run the following to list your instances. You'll see their IP addresses in the
EXTERNAL_IP column:
gcloud compute instances list

DEPSTAR (IT) 69
IT442 : ADVANCED COMPUTING 20DIT057

Step 6: Verify that each instance is running with curl, replacing [IP_ADDRESS] with the IP
address for each of your VMs:
curl http://[IP_ADDRESS]

Task 3: Configure the load balancing service


Step 1: Create a static external IP address for your load balancer:
gcloud compute addresses create network-lb-ip-1 \
--region

Figure 9.12
Step 2: Add a legacy HTTP health check resource:
gcloud compute http-health-checks create basic-check

Figure 9.13
Step 3: Add a target pool in the same region as your instances. Run the following to create
the target pool and use the health check, which is required for the service to function:
gcloud compute target-pools create www-pool \
--region --http-health-check basic-check
Step 4: Add the instances to the pool:
gcloud compute target-pools add-instances www-pool \
--instances www1,www2,www3
Step 5: Add a forwarding rule:
gcloud compute forwarding-rules create www-rule \
--region us-central1 --ports 80 --address network-lb-ip-1 --target-pool www-pool

Figure 9.14

DEPSTAR (IT) 70
IT442 : ADVANCED COMPUTING 20DIT057

Task 4: Sending traffic to your instances


Step 1: Enter the following command to view the external IP address of the www-rule
forwarding rule used by the load balancer:
gcloud compute forwarding-rules describe www-rule --region
Step 2: Access the external IP address:
IPADDRESS=$(gcloud compute forwarding-rules describe www-rule --region --
format="json" | jq -r .IPAddress)
Step 3: Show the external IP address:
echo $IPADDRESS
Step 4: Use curl command to access the external IP address, replacing IP_ADDRESS with an
external IP address from the previous command:
while true; do curl -m1 $IPADDRESS; done
Step 5: Use Ctrl + c to stop running the command.

Figure 9.15

Task 5: Create an HTTP load balancer


Step 1: First, create the load balancer template:
gcloud compute instance-templates create lb-backend-template \
--region=us-central1 --network=default --subnet=default --tags=allow-health-
check --machine-type=e2-medium --image-family=debian-11 --image-
project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
a2ensite default-ssl
a2enmod ssl
vm_hostname="$(curl -H "Metadata-Flavor:Google" \

DEPSTAR (IT) 71
IT442 : ADVANCED COMPUTING 20DIT057

http://169.254.169.254/computeMetadata/v1/instance/name)"
echo "Page served from: $vm_hostname" | \
tee /var/www/html/index.html
systemctl restart apache2'

Figure 9.16
Step 2: Create a managed instance group based on the template:
gcloud compute instance-groups managed create lb-backend-group \
--template=lb-backend-template --size=2 --zone=
Step 3: Create the fw-allow-health-check firewall rule.
gcloud compute firewall-rules create fw-allow-health-check \
--network=default \
--action=allow \
--direction=ingress \
--source-ranges=130.211.0.0/22,35.191.0.0/16 \
--target-tags=allow-health-check \
--rules=tcp:80

Figure 9.17

DEPSTAR (IT) 72
IT442 : ADVANCED COMPUTING 20DIT057

Step 4: Now that the instances are up and running, set up a global static external IP address
that your customers use to reach your load balancer:
gcloud compute addresses create lb-ipv4-1 \
--ip-version=IPV4 \
--global

gcloud compute addresses describe lb-ipv4-1 \


--format="get(address)" \
--global
Step 5: Create a health check for the load balancer:
gcloud compute health-checks create http http-basic-check \
--port 80
Step 6: Create a backend service:
gcloud compute backend-services create web-backend-service \
--protocol=HTTP \
--port-name=http \
--health-checks=http-basic-check \
--global
Step 7: Add your instance group as the backend to the backend service:
gcloud compute backend-services add-backend web-backend-service \
--instance-group=lb-backend-group \
--instance-group-zone= \
--global

Figure 9.18

DEPSTAR (IT) 73
IT442 : ADVANCED COMPUTING 20DIT057

Step 8: Create a URL map to route the incoming requests to the default backend service:
gcloud compute url-maps create web-map-http \
--default-service web-backend-service
Step 9: Create a target HTTP proxy to route requests to your URL map:
gcloud compute target-http-proxies create http-lb-proxy \
--url-map web-map-http
Step 10: Create a global forwarding rule to route incoming requests to the proxy:
gcloud compute forwarding-rules create http-content-rule \
--address=lb-ipv4-1\
--global \
--target-http-proxy=http-lb-proxy \
--ports=80

Figure 9.19

Task 6: Testing traffic sent to your instances


Step 1: In the Cloud Console, from the Navigation menu, go to Network services > Load
balancing.
Step 2: Click on the load balancer that you just created (web-map-http).
Step 3: In the Backend section, click on the name of the backend and confirm that the VMs
are Healthy. If they are not healthy, wait a few moments and try reloading the page.
Step 4: When the VMs are healthy, test the load balancer using a web browser, going to
http://IP_ADDRESS/, replacing IP_ADDRESS with the load balancer's IP address.

Conclusion:
From this practical, I learned about the Google Cloud Platform. Also learned how to enable
APIs in GCP. I also learned about setting up network and HTTP load balancer on GCP.

DEPSTAR (IT) 74
IT442 : ADVANCED COMPUTING 20DIT057

Practical – 10

Aim: Perform Cluster orchestration with Google Kubernetes Engine.


Link: https://www.cloudskillsboost.google/focuses/878?parent=catalog

Implementation:
Task 1: Set a default compute zone
Step 1: Set the default compute region.
gcloud config set compute/region
Step 2: Set the default compute zone.
gcloud config set compute/zone

Figure 10.1

Task 2: Create a GKE cluster


Step 1: Create a cluster by following command:
gcloud container clusters create --machine-type=e2-medium --zone= lab-cluster

Figure 10.2

Task 3: Get authentication credentials for the cluster


Step 1: Authenticate with the cluster by following command:
gcloud container clusters get-credentials lab-cluster

Figure 10.3

DEPSTAR (IT) 75
IT442 : ADVANCED COMPUTING 20DIT057

Task 4: Deploy an application to the cluster


Step 1: To create a new Deployment hello-server from the hello-app container image, run the
following kubectl create command:
kubectl create deployment hello-server --image=gcr.io/google-samples/hello-
app:1.0
Step 2: To create a Kubernetes Service, which is a Kubernetes resource that lets you expose
your application to external traffic, run the following kubectl expose command:
kubectl expose deployment hello-server --type=LoadBalancer --port 8080
Step 3: To inspect the hello-server Service, run kubectl get:
kubectl get service

Figure 10.4
Step 4: To view the application from your web browser, open a new tab and enter the
following address, replacing [EXTERNAL IP] with the EXTERNAL-IP for hello-server.
http://[EXTERNAL-IP]:8080

Figure 10.5

Task 5: Deleting the cluster


Step 1: To delete the cluster, run the following command:
gcloud container clusters delete lab-cluster
Step 2: When prompted, type Y to confirm.

Figure 10.6
Conclusion:
From this practical, I learned about performing Cluster orchestration with Google Kubernetes
Engine.

DEPSTAR (IT) 76

You might also like