CloudSim 示例与云平台实验指南
1. CloudSim 示例
1.1 CloudSim 示例 7:容器模拟的初始示例
1.1.1 算法说明
该示例展示了如何使用 CloudSim 进行容器模拟。其输入为多个虚拟机(VM)的 ID、VM 大小和处理元素(PE)配置,输出为云任务(Cloudlet)列表。具体步骤如下:
1.
初始化 CloudSim 包
:在创建任何实体之前调用此操作。
2.
定义容器分配策略
:确定容器如何分配到数据中心的虚拟机。
3.
定义 VM 选择策略
:当主机被识别为过载时,确定哪些虚拟机应被选择进行迁移。
4.
定义主机选择策略
:确定哪些主机应被选为迁移目标。
5.
定义选择未充分利用和过度利用主机的阈值
。
6.
创建主机列表
:考虑主机数量和主机类型。
7.
定义 VM 到容器的分配策略
。
8.
定义容器分配到 VM 的超额预订因子
:代理使用此因子创建云任务、容器和 VM 列表以提交给代理。
9.
分配日志地址
:用于记录数据中心中 VM 和容器的统计信息。
10.
提交列表
:将云任务、容器和 VM 列表提交给代理。
11.
确定模拟终止时间
:根据云任务的工作量。
12.
创建实验特定名称
:用于创建日志地址文件夹。
13.
打印云任务对象
。
14.
创建虚拟机并添加到列表
。
15.
创建主机列表
:考虑规格。
16.
创建数据中心
。
17.
创建容器
:用于托管云任务并将它们绑定在一起。
18.
创建云任务列表
:将在容器上运行。
19.
打印模拟结果
:当模拟完成时。
1.1.2 源代码
public class CloudSimExample7
{
private static List<Cloudlet> CloudletList;
private static List<Vm> vmlist;
public static void main(String[] args)
{
Log.printLine("Starting CloudSimExample7...");
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; // trace events
CloudSim.init(num_user, calendar, trace_flag);
/* Defining the container allocation Policy. This policy determines how Containers are allocated to VMs in the data center. */
ContainerAllocationPolicy containerAllocationPolicy = new PowerContainerAllocationPolicySimple();
/* Defining the VM selection Policy. This policy determines which VMs should be selected for migration when a host is identified as over-loaded.*/
PowerContainerVmSelectionPolicy vmSelectionPolicy = new PowerContainerVmSelectionPolicyMaximumUsage();
/* Defining the host selection Policy. This policy determines which hosts should be selected as migration destination */
HostSelectionPolicy hostSelectionPolicy = new HostSelectionPolicyFirstFit();
/**
* Defining the thresholds for selecting the under-utilized and over-utilized hosts.
*/
double overUtilizationThreshold = 0.80;
double underUtilizationThreshold = 0.70;
} catch (Exception e) {
e.printStackTrace();
}
}
/** Prints the Cloudlet objects */
private static void printCloudletList(List<Cloudlet> list) {
int size = list.size();
Cloudlet cloudlet;
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + "STATUS" + "Data center ID" + "VM ID" + "Time" + "Start Time" + "Finish Time");
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
Log.print("" + cloudlet.getCloudletId());
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) {
Log.print("SUCCESS");
}
Log.printLine(cloudlet.getResourceId() + cloudlet.getVmId() + (cloudlet.getActualCPUTime()) + cloudlet.getExecStartTime() + " " + (cloudlet.getFinishTime()));
}
}
}
1.1.3 输出示例
| Cloudlet ID | STATUS | Data Center ID | VM ID | Time | Start Time | Finish Time |
|---|---|---|---|---|---|---|
| 4 | SUCCESS | 2 | 4 | 3 | 0.2 | 3.2 |
| 30 | SUCCESS | 3 | 6 | 3 | 0.2 | 3 |
| 38 | SUCCESS | 2 | 2 | 4 | 0.2 | 4.2 |
| 39 | SUCCESS | 2 | 3 | 4 | 0.2 | 4.2 |
1.2 CloudSim 示例 8:使用全局管理器实体在运行时创建模拟实体
1.2.1 算法说明
该示例展示了如何使用全局管理器实体(GlobalBroker)在运行时创建模拟实体(如数据中心代理)。其输入和输出与示例 7 相同,具体步骤也与示例 7 类似。
1.2.2 源代码
public class CloudSimExample8
{
/** The Cloudlet list. */
private static List<Cloudlet> CloudletList;
/** The vmList. */
private static List<Vm> vmList;
private static List<Vm> createVM(int userId, int vms, int idShift) {
LinkedList<Vm> list = new LinkedList<Vm>();
//VM Parameters
long size = 10000; //image size (MB)
int ram = 512; //vm memory (MB)
int mips = 250;
long bw = 1000;
int pesNumber = 1; //number of cpus
String vmm = "Xen"; //VMM name
//create VMs
Vm[] vm = new Vm[vms];
for(int i=0;i< vms;i++) {
vm[i] = new Vm(idShift + i, userId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
list.add(vm[i]);
}
return list;
}
private static List<Cloudlet> createCloudlet(int userId, int cloudlets, int idShift) {
LinkedList<Cloudlet> list = new LinkedList<Cloudlet>();
//Cloudlet parameters
long length = 40000;
long fileSize = 300;
long outputSize = 300;
int pesNumber = 1;
UtilizationModel utilizationModel = new UtilizationModelFull();
Cloudlet[] cloudlet = new Cloudlet[cloudlets];
for(int i=0;i<cloudlets;i++) {
cloudlet[i] = new Cloudlet(idShift + i, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
cloudlet[i].setUserId(userId);
list.add(cloudlet[i]);
}
return list;
}
public static void main(String[] args) {
Log.printLine("Starting CloudSimExample8...");
try {
int num_user = 2; // number of grid users
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false; // mean trace events
CloudSim.init(num_user, calendar, trace_flag);
GlobalBroker globalBroker = new GlobalBroker("GlobalBroker");
Datacenter datacenter0 = createDatacenter("Datacenter 0");
Datacenter datacenter1 = createDatacenter("Datacenter 1");
DatacenterBroker broker = createBroker("Broker 0");
int brokerId = broker.getId();
vmList = createVM(brokerId, 5, 0); //creating 5 vms
CloudletList = createCloudlet(brokerId, 10, 0); // creating 10 Cloudlets
broker.submitVmList(vmList);
broker.submitCloudletList(CloudletList);
// Fifth step: Starts the simulation
CloudSim.startSimulation();
// Final step: Print results when simulation is over
List<Cloudlet> newList = broker.getCloudletReceivedList();
newList.addAll(globalBroker.getBroker().getCloudletReceivedList());
CloudSim.stopSimulation();
printCloudletList(newList);
Log.printLine("CloudSimExample8 finished!");
} 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 one or more Machines
List<Host> hostList = new ArrayList<Host>();
// 2. A Machine contains one or more PEs or CPUs/Cores. Therefore, should create a list to store these PEs before creating a Machine.
List<Pe> peList1 = new ArrayList<Pe>();
int mips = 1000;
peList1.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id and MIPS Rating
peList1.add(new Pe(1, new PeProvisionerSimple(mips)));
peList1.add(new Pe(2, new PeProvisionerSimple(mips)));
peList1.add(new Pe(3, new PeProvisionerSimple(mips)));
//Another list, for a dual-core machine
List<Pe> peList2 = new ArrayList<Pe>();
peList2.add(new Pe(0, new PeProvisionerSimple(mips)));
peList2.add(new Pe(1, new PeProvisionerSimple(mips)));
//4. Create Hosts with its id and list of PEs and add them to the list of machines
int hostId=0;
int ram = 16384; //host memory (MB)
long storage = 1000000; //host storage
int bw = 10000;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList1,
new VmSchedulerTimeShared(peList1)
)
); // This is our first machine
hostId++;
hostList.add(
new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList2,
new VmSchedulerTimeShared(peList2)
)
); // Second 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.1; // the cost of using storage in this resource
double costPerBw = 0.1; // 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;
}
private static DatacenterBroker createBroker(String name) {
DatacenterBroker broker = null;
try {
broker = new DatacenterBroker(name);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return broker;
}
private static void printCloudletList(List<Cloudlet> list) {
int size = list.size();
Cloudlet cloudlet;
Log.printLine("========== OUTPUT ==========");
Log.printLine("Cloudlet ID" + "STATUS" + "Data center ID" + "VM ID" + "Time" + "Start Time" + "Finish Time");
for (int i = 0; i < size; i++) {
cloudlet = list.get(i);
Log.print("" + cloudlet.getCloudletId());
if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) {
Log.print("SUCCESS");
}
Log.printLine(cloudlet.getResourceId() + cloudlet.getVmId() + (cloudlet.getActualCPUTime()) + cloudlet.getExecStartTime() + " " + (cloudlet.getFinishTime()));
}
}
}
1.2.3 输出示例
| Cloudlet ID | STATUS | Data center ID | VM ID | Time | Start Time | Finish Time |
|---|---|---|---|---|---|---|
| 0 | SUCCESS | 3 | 0 | 320 | 0.1 | 320.1 |
| 8 | SUCCESS | 3 | 3 | 320 | 0.1 | 320.1 |
| 109 | SUCCESS | 3 | 104 | 320 | 200.1 | 520.1 |
1.3 流程图
graph TD;
A[初始化 CloudSim 包] --> B[定义容器分配策略];
B --> C[定义 VM 选择策略];
C --> D[定义主机选择策略];
D --> E[定义阈值];
E --> F[创建主机列表];
F --> G[定义 VM 到容器的分配策略];
G --> H[定义超额预订因子];
H --> I[分配日志地址];
I --> J[提交列表];
J --> K[确定模拟终止时间];
K --> L[创建实验名称];
L --> M[打印云任务对象];
M --> N[创建虚拟机];
N --> O[创建主机列表];
O --> P[创建数据中心];
P --> Q[创建容器];
Q --> R[创建云任务列表];
R --> S[打印模拟结果];
2. 云平台实验
2.1 实验概述
涵盖了多个云平台相关的实验,包括虚拟机的备份恢复、克隆,MapReduce 程序在不同文件大小下的性能评估,多虚拟机之间的通信配置,虚拟化的安装和配置,以及各种云服务的研究和实现等。
2.2 平台安装
2.2.1 VMware Workstation 安装步骤
-
以 root 或具有 sudo 权限的非 root 用户登录服务器:
-
在 RedHat 系统上:
# yum update -
在 Fedora 上:
# dnf update
-
在 RedHat 系统上:
- 从 VMware 官方网站下载 VMWare Workstation Pro 安装脚本包。
-
下载脚本文件后,进入包含脚本文件的目录并设置执行权限:
-
# chmod a+x VMware-Workstation-Full-15.5.1-15018445.x86_64.bundle
-
-
在 Linux 主机系统上运行安装脚本:
-
# ./VMware-Workstation-Full-15.5.1-15018445.x86_64.bundle或$ sudo ./VMware-Workstation-Full-15.5.1-15018445.x86_64.bundle
-
-
启动软件:在终端中输入
vmware。- 如果没有 GCC GNU C 编译器,会提示安装,按“Cancel”继续。
-
返回终端安装“Development Tools”:
-
在 RedHat 系统上:
[root@tecmint Downloads]# yum groupinstall “Development tools” -
在 Debian 系统上:
root@tecmint: # apt-get install build-essential
-
在 RedHat 系统上:
-
安装内核头文件:
-
在 RedHat 系统上:
[root@tecmint Downloads]# yum install kernel-headers -
在 Debian 系统上:
root@tecmint: # apt-get install linux-headers-‘uname -r‘
-
在 RedHat 系统上:
2.2.2 Hadoop 平台安装步骤
- 启动 Hadoop 集群,支持三种模式:本地(独立)模式、伪分布式模式和完全分布式模式。
-
检查是否可以无密码 ssh 到本地主机:
$ ssh localhost -
如果不能无密码 ssh,执行以下命令:
-
$ ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa -
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys -
$ chmod 0600 ~/.ssh/authorized_keys
-
-
格式化文件系统:
$ bin/hdfs namenode -format -
启动 NameNode 守护进程和 DataNode 守护进程:
$ sbin/start-dfs.sh -
Hadoop 守护进程日志输出到
$HADOOP_LOG_DIR目录(默认为$HADOOP_HOME/logs)。 -
浏览 NameNode 的 Web 界面:默认地址为
http://localhost:50070/ -
创建执行 MapReduce 作业所需的 HDFS 目录:
-
$ bin/hdfs dfs -mkdir /user -
$ bin/hdfs dfs -mkdir /user/<username>
-
2.2.3 Kernel-based Virtual Machine (KVM) 安装步骤
-
安装提供用户级 KVM 和磁盘映像管理器的软件包:
[root@server ]# yum install qemu-kvm qemu-img -
安装管理虚拟平台的工具:
[root@server]# yum install virt-manager libvirt libvirt-python libvirt-client -
启动虚拟化守护进程:
[root@server]#systemctl restart libvirtd -
检查守护进程状态:
[root@server ]#systemctl status libvirtd - 选择创建虚拟机的安装方法,这里以本地安装介质为例,提供“ISO”映像的路径。
- 使用虚拟磁盘安装虚拟机。
- 输入虚拟机名称和其他高级选项,点击“完成”,出现控制控制台以管理来宾操作系统。
2.2.4 OwnCloud 在 Ubuntu 上的安装步骤
-
使用“apt”命令更新系统包和仓库:
$ sudo apt update -y & sudo apt upgrade -y -
安装 Apache 网络服务器、PHP 7.2 及相关模块:
-
$ sudo apt install apache2 libapache2-mod-php7.2 openssl php-imagick php7.2-common php7.2-curl php7.2-gd php7.2-imap php7.2-intl php7.2-json php7.2-ldap php7.2-mbstring php7.2-mysql php7.2-pgsql php-smbclient php-ssh2 php7.2-sqlite3 php7.2-xml php7.2-zip
-
-
验证 Apache 是否安装:
$ sudo dpkg -l apache -
启动并设置 Apache 在开机时运行:
$ sudo systemctl enable apache2 - 在浏览器中输入服务器的 IP 地址,检查 Apache 是否运行。
-
检查 PHP 是否安装:
$ php -v -
安装 MariaDB:
$ sudo apt install mariadb-server -
安全配置 MySQL 服务器:
$ sudo mysql secure installation - 设置 root 密码并按提示操作。
-
创建 OwnCloud 数据库:
-
登录 MariaDB:
$ sudo mysql -u root -p -
执行以下命令:
-
MariaDB [(none)]> CREATE DATABASE ownCloud_db; -
MariaDB [(none)]> GRANT ALL ON ownCloud_db.* TO 'ownCloud_user'@localhost'; -
MariaDB [(none)]> FLUSH PRIVILEGES; -
MariaDB [(none)]> EXIT;
-
-
登录 MariaDB:
-
下载 OwnCloud 压缩文件:
$ sudo wget https://download.ownCloud.org/community/ownCloud-10.4.0.zip -
解压文件到
/var/www/目录:$ sudo unzip ownCloud-10.4.0.zip -d /var/www/ -
设置权限:
-
$ sudo chown -R www-data:www-data /var/www/ownCloud/ -
$ sudo chmod -R 755 /var/www/ownCloud/
-
-
配置 Apache 以支持 OwnCloud:
$ sudo vim /etc/apache2/conf-available/ownCloud.conf -
重启 Apache 网络服务器:
$ sudo systemctl restart apache2 -
在浏览器中输入服务器地址并添加
/ownCloud后缀。
2.2.5 AWS Cloud Development Kit (AWS CDK) 安装步骤
-
提供 AWS 凭证和区域信息:
-
安装 AWS CLI 并执行
aws configure,按提示输入 AWS 访问密钥 ID、秘密访问密钥和默认区域。 -
也可以手动编辑
/~/.aws/config和/~/.aws/credentials(Linux 或 Mac)或%USERPROFILE%\aws\config和%USERPROFILE%\aws\credentials(Windows)文件,格式如下:-
在
/~/.aws/config或%USERPROFILE%\aws\config中:
-
在
-
安装 AWS CLI 并执行
[default]
region=us-west-2
- 在 `/~/.aws/credentials` 或 `%USERPROFILE%\aws\credentials` 中:
aws_access_key_id=AKIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
-
使用 Node 包管理器全局安装 AWS CDK 工具包:
npm install -g aws-cdk
2.3 安装流程图
graph TD;
A[选择平台] --> B{VMware Workstation};
A --> C{Hadoop 平台};
A --> D{KVM};
A --> E{OwnCloud};
A --> F{AWS CDK};
B --> B1[更新系统];
B1 --> B2[下载安装包];
B2 --> B3[设置权限];
B3 --> B4[安装];
B4 --> B5[启动软件];
B5 --> B6[安装开发工具];
C --> C1[选择模式];
C1 --> C2[检查 ssh];
C2 --> C3[配置 ssh];
C3 --> C4[格式化文件系统];
C4 --> C5[启动守护进程];
C5 --> C6[浏览界面];
C6 --> C7[创建目录];
D --> D1[安装软件包];
D1 --> D2[安装管理工具];
D2 --> D3[启动守护进程];
D3 --> D4[检查状态];
D4 --> D5[选择安装方法];
D5 --> D6[安装虚拟机];
E --> E1[更新系统];
E1 --> E2[安装服务器和模块];
E2 --> E3[验证安装];
E3 --> E4[配置数据库];
E4 --> E5[下载解压文件];
E5 --> E6[设置权限];
E6 --> E7[配置 Apache];
E7 --> E8[重启服务器];
E8 --> E9[访问网站];
F --> F1[配置凭证];
F1 --> F2[安装工具包];
2.4 实验总结
| 实验名称 | 主要操作步骤 | 关键注意事项 |
|---|---|---|
| VMware Workstation 安装 | 系统更新、下载安装包、设置权限、安装、启动软件、安装开发工具 | 注意不同系统的命令差异,启动软件时若缺少 GCC 编译器可按“Cancel”继续 |
| Hadoop 平台安装 | 选择模式、检查 ssh、配置 ssh、格式化文件系统、启动守护进程、浏览界面、创建目录 | 确保 ssh 无密码登录,格式化文件系统操作需谨慎 |
| KVM 安装 | 安装软件包、安装管理工具、启动守护进程、检查状态、选择安装方法、安装虚拟机 | 注意守护进程的启动和状态检查 |
| OwnCloud 在 Ubuntu 上的安装 | 更新系统、安装服务器和模块、验证安装、配置数据库、下载解压文件、设置权限、配置 Apache、重启服务器、访问网站 | 数据库配置和权限设置要正确 |
| AWS CDK 安装 | 配置凭证、安装工具包 | 提供正确的 AWS 凭证和区域信息 |
2.5 实验拓展
上述实验为云平台的基础操作和使用提供了指导,但在实际应用中,还可以进行更多的拓展和优化。例如:
-
性能优化
:在 Hadoop 平台上,可以通过调整集群配置参数、优化 MapReduce 程序等方式提高性能。
-
安全加固
:在云平台中,安全是至关重要的。可以通过设置防火墙、加密数据、定期更新软件等方式加强安全防护。
-
多平台集成
:可以将不同的云平台进行集成,实现资源的共享和协同工作。
3. 总结与展望
3.1 总结
本文介绍了 CloudSim 示例和云平台实验的相关内容。CloudSim 示例展示了如何使用 CloudSim 进行容器模拟和在运行时创建模拟实体,提供了详细的算法说明、源代码和输出示例。云平台实验部分涵盖了多个云平台相关的实验,包括虚拟机的备份恢复、克隆,MapReduce 程序在不同文件大小下的性能评估,多虚拟机之间的通信配置,虚拟化的安装和配置,以及各种云服务的研究和实现等,并详细介绍了多个平台的安装步骤。
3.2 展望
随着云计算技术的不断发展,CloudSim 和云平台的应用也将越来越广泛。未来,可以进一步探索 CloudSim 的高级功能,如资源调度算法的优化、分布式系统的模拟等。在云平台方面,可以关注新兴的云服务和技术,如人工智能、大数据分析等在云平台上的应用,以及云原生技术的发展和应用。同时,也需要加强云平台的安全管理和性能优化,以满足不断增长的业务需求。
3.3 流程图总结
graph LR;
A[CloudSim 示例] --> B[示例 7: 容器模拟];
A --> C[示例 8: 运行时创建实体];
D[云平台实验] --> E[实验概述];
D --> F[平台安装];
F --> G[VMware Workstation];
F --> H[Hadoop 平台];
F --> I[KVM];
F --> J[OwnCloud];
F --> K[AWS CDK];
D --> L[实验总结];
D --> M[实验拓展];
N[总结与展望] --> O[总结];
N --> P[展望];
N --> Q[流程图总结];
通过以上的总结和展望,我们可以更好地理解 CloudSim 示例和云平台实验的内容,并为未来的学习和实践提供方向。希望本文对读者有所帮助,能够激发大家对云计算技术的兴趣和探索欲望。
超级会员免费看
113

被折叠的 条评论
为什么被折叠?



