亚马逊ec2 实例删除
Skill Level: Intermediate.Operating System(s): Linux.
技能水平:中级。 操作系统: Linux。
We left off at the end of part one with a scheduled snapshot running from cron once every day. In part two, we will learn how to recover a failed AWS EBS-backed EC2 instance from a snapshot with just a few commands.
在第一部分的结尾,我们每天定时从cron运行一次计划的快照。 在第二部分中,我们将学习如何仅用几个命令从快照中恢复失败的AWS EBS支持的EC2实例。
What are some of the situations which would force us to do a full instance recovery? Here are just a few:
在哪些情况下会迫使我们执行完整实例恢复? 这里仅仅是少数:
Filesystem corruption 文件系统损坏 Malicious intrusion 恶意入侵 Hardware failure 硬件故障 Extended Amazon general outage affecting your instance but not others 扩展的Amazon一般中断影响您的实例,但不影响其他实例 Human error 人为错误This is my preferred approach. Since I maintain multiple instances, they all have the cron backups running, and so therefore all have the ability to perform recovery tasks as well. If you have another instance, but it does not yet have the EC2 API tools installed and working, please review Part One for instructions on how to set this up.
这是我的首选方法。 由于我维护多个实例,因此它们都具有正在运行的cron备份,因此它们都具有执行恢复任务的能力。 如果您有另一个实例,但尚未安装并运行EC2 API工具,请查看第1部分,以获得有关如何进行设置的说明。
Below are some possible options to consider:
以下是一些可以考虑的选项:
Create an new T1.micro “admin-only” instance. They are quite inexpensive on a monthly basis and still give you full functionality. They are cheap enough to keep running full time so you don’t have to fuss during a crisis. 创建一个新的T1.micro“仅管理员”实例。 它们每月非常便宜,但仍然为您提供全部功能。 它们足够便宜,可以保持全时运行,因此您在危机期间不必大惊小怪。 Use these instructions to clone your existing single instance. This could potentially be costly if you are running a large instance and make an exact RUNNING copy of it. A less costly way would be to clone the instance and keep it powered off until needed. 使用以下说明克隆您现有的单个实例。 如果您正在运行大型实例并为其创建精确的RUNNING副本,则这可能会造成很高的成本。 成本较低的方法是克隆实例并关闭其电源,直到需要时为止。Use your home system to provide the functionality. Please see John Degner’s excellent tutorial for instructions. Detailed instructions for this solution are beyond the scope of this tutorial, but these examples can easily be used in this situation.
使用您的家庭系统来提供功能。 有关说明,请参见John Degner的出色教程。 此解决方案的详细说明超出了本教程的范围,但是在这种情况下可以轻松使用这些示例。
Included in this tutorial are both methods – GUI and CLI. Each has advantages, and I leave it up to you to pick the one you prefer…
本教程中包括两种方法– GUI和CLI。 每一种都有优点,我让您选择自己喜欢的一种…
Successful completion of Part One, specifically /opt/bin/instanceid, and /opt/bin/ec2do.
成功完成了第一部分 ,特别是/opt/bin/instanceid和/opt/bin/ec2do 。
GUI method: access to the AWS Management Console Web GUI
GUI方法:访问AWS管理控制台Web GUI
CLI method: root access to a running AWS EC2 EBS-backed Linux instance – for example:
CLI方法:对正在运行的AWS EC2 EBS支持的Linux实例的根访问权限–例如:
ssh -i AWSKeyPair.pem ec2-user@{yourInstance_FQDN_or_IP} % sudo su root@yourInstance#It is easy to customize the columns in the Instance view to display just what you need:
在“实例”视图中自定义列以显示所需内容很容易:
Click on Instances in the left navigation menu
单击左侧导航菜单中的实例
Click the Show/Hide button to the right
点击右侧的显示/隐藏按钮
Select the needed columns and click Apply
选择所需的列,然后单击“应用”
Locate and record the following information:
找到并记录以下信息:
AMI ID, Root Device Name, Type, Security Groups, Key Pair Name, and Kernel ID
AMI ID,根设备名称,类型,安全组,密钥对名称和内核ID
Click on AMIs in the left navigation menu, then enter the AMI ID from the previous step into the indicated box. That AMI will appear below. Click the Show/Hide button so that the Architecture and Root device are checked, then click Apply. Verify that the root device is EBS, and record the Architecture:
单击左侧导航菜单中的AMI,然后将上一步中的AMI ID输入到指示的框中。 该AMI将显示在下面。 单击显示/隐藏按钮,以检查体系结构和根设备,然后单击应用。 验证根设备是EBS,并记录架构:
Click on Snapshots in the left navigation menu, then click on the “Started” column title until the latest snapshots are at the top. Read through the descriptions until you find one that is a snapshot of a ROOT (/), BOOTABLE filesystem. Once located, click on it once and make sure the checkbox in the first column is checked. Also record the Capacity. Finally, click on the “Create Image” button.
单击左侧导航菜单中的快照,然后单击“开始”列标题,直到最新的快照位于顶部。 通读说明,直到找到一个是ROOT(/)BOOTABLE文件系统的快照。 找到后,单击一次,并确保选中第一列中的复选框。 同时记录容量。 最后,单击“创建图像”按钮。
Fill out the form with the information you have recorded along the way and click “Yes, Create”.
用您在此过程中记录的信息填写表格,然后单击“是,创建”。
Click on AMIs in the left navigation menu, then type in the name of your new AMI in the search box. Click once on the line and verify that the checkbox is checked at the beginning of the line.
单击左侧导航菜单中的AMI,然后在搜索框中键入新AMI的名称。 在该行上单击一次,并验证该行开头是否已选中该复选框。
At this point, the Launch button should be come enabled. Click it and the Request Instances Wizard appears. Select the instance type you desire, then click Continue:
此时,应该启用“启动”按钮。 单击它,然后出现“请求实例向导”。 选择所需的实例类型,然后单击继续:
Next, specify the kernel ID and check the Accidental Termination Prevention box, then click Continue:
接下来,指定内核ID并选中“意外终止阻止”框,然后单击“继续”:
Type in the name for your new AMI, then click Continue:
输入新AMI的名称,然后单击继续:
Choose a key pair, then click Continue:
选择一个密钥对,然后单击继续:
Choose or create a security group, then click Continue:
选择或创建一个安全组,然后单击继续:
Finally, verify all information, then click Launch:
最后,验证所有信息,然后单击启动:
If you have gotten this far, then you have successfully cloned an instance using the GUI Console method. Congratulations!
如果到此为止,那么您已经使用GUI控制台方法成功克隆了实例。 恭喜你!
The scripts found at the bottom of this tutorial help automate each step of the process, providing key bits of the workflow that allow us to quickly get the job done.
本教程底部的脚本有助于自动化流程的每个步骤,提供工作流的关键部分,使我们能够快速完成工作。
IMPORTANT: Please create each of these scripts in the /opt/bin directory, making sure to insert your specific values wherever you see curly brackets {}.
重要说明:请在/opt/bin目录中创建每个脚本,并确保在出现大括号{}的位置插入特定的值。
Also, please be sure to set execute permissions properly on all of the new scripts:
另外,请确保在所有新脚本上正确设置执行权限:
root@yourInstance# chmod 750 /opt/bin/*We need to gather a bit of data before we can proceed. Best practice is to keep a record of the instance configuration PRINTED out and EASILY accessible. The ‘-l’ (ell) option tells the script to operate in LOCAL mode, which only outputs information for the current instance, and also tells you the OS architecture. The non-local invocation of this script does NOT provide the OS arch.
在继续之前,我们需要收集一些数据。 最佳实践是保留已打印出且易于访问的实例配置的记录。 “ -l”(ell)选项告诉脚本以LOCAL模式运行,该模式仅输出当前实例的信息,还告诉您OS体系结构。 此脚本的非本地调用不提供OS架构。
# /opt/bin/instanceinfo -l ami=ami-31509058 arch=i686 device=/dev/sda1 group=Web-Server instanceid=i-29765a07 kernel=aki-407d9529 keypair=AWSKeyPair privatedns=ip-10-100-100-100.ec2.internal privateip=10.100.100.100 publicdns=ec2-100-200-200-200.compute-1.amazonaws.com publicip=100.200.200.200 region=us-east-1a type=c1.medium volume=vol-723ebc42Find available snapshots for the desired volume:
查找所需卷的可用快照:
# /opt/bin/ec2do describe-snapshots | grep {VOLUME_FROM_LAST_STEP}Choose the snapshot you wish to use as the source for the clone and proceed to the create clone step!
选择您希望用作克隆源的快照,然后继续创建克隆步骤!
-a OS architecture (/bin/arch) -n New AMI Name -d New AMI description -r Root device -s Snapshot ID -kern Kernel ID -z Size of the new volume -g Security group to use for the new instance -t Type of instance to launch -k Key pair to use for the new instance
-a OS体系结构(/ bin / arch)-n新的AMI名称-d新的AMI描述-r根设备-s快照ID -kern内核ID -z新卷的大小-g用于新实例的安全组- t要启动的实例类型-k用于新实例的密钥对
If you have gotten this far, then you have successfully cloned an instance using the Command Line method. Congratulations!
如果到此为止,那么您已经使用命令行方法成功克隆了实例。 恭喜你!
IMPORTANT: Please create each of these scripts in the /opt/bin directory, making sure to insert your specific values wherever you see curly brackets {}.Also, please be sure to set execute permissions properly on all of the new scripts:
重要说明:请在/opt/bin目录中创建每个脚本,并确保在出现大括号{}的位置插入特定的值。 另外,请确保在所有新脚本上正确设置执行权限:
root@yourInstance# chmod 750 /opt/bin/*Here is a simple Perl script to output the list of the items to find for each instance you have:
这是一个简单的Perl脚本,用于输出要为您拥有的每个实例查找的项目列表:
#!/usr/bin/perl # ## instanceinfo # use strict; $| = 1; my $args = {}; $args->{'local'} = 0; $args->{'instanceid'} = ''; while ($ARGV[0] =~ /^-/) { $_ = shift; $args->{'local'} = 1, next if /^-l$/; } if ($args->{'local'}) { $_ = `/opt/aws/bin/ec2-metadata -i`; $args->{'instanceid'} = (split)[1]; } open(IN,"/opt/bin/ec2do describe-instances $args->{instanceid} |") or die; my $settings = {}; chomp($settings->{1}->{'arch'} = `/bin/arch`) if $args->{'local'}; my $counter = 0; while (<IN>) { if (/^RESERVATION/) { $counter ++; $settings->{$counter}->{'group'} = (split)[3]; } if (/^INSTANCE/) { next unless /running/; my @line = split; $settings->{$counter}->{'ami'} = $line[2]; $settings->{$counter}->{'publicdns'} = $line[3]; $settings->{$counter}->{'privatedns'} = $line[4]; $settings->{$counter}->{'keypair'} = $line[6]; $settings->{$counter}->{'type'} = $line[8]; $settings->{$counter}->{'region'} = $line[10]; $settings->{$counter}->{'kernel'} = $line[11]; $settings->{$counter}->{'publicip'} = $line[13]; $settings->{$counter}->{'privateip'} = $line[14]; } if (/^BLOCKDEVICE/) { my @line = split; $settings->{$counter}->{'device'} = $line[1]; $settings->{$counter}->{'volume'} = $line[2]; } if (/^TAG/ and /name/i) { $settings->{$counter}->{'name'} = (split)[4]; } } foreach my $counter (sort keys %$settings) { print '='x60,"n$settings->{$counter}->{name}n",'='x60,"n"; foreach my $key (sort keys %{$settings->{$counter}}) { print "$key=$settings->{$counter}->{$key}n"; } }This script wrapper simply allows us to launch a new instance when we need to, with a proper environment at all times.
该脚本包装程序仅允许我们在需要时始终在适当的环境下启动新实例。
Please be sure to modify the example below to include your specific information.
请确保修改以下示例以包含您的特定信息。
#!/bin/bash # ## ec2run Wrapper # export EC2_HOME='/opt/aws/apitools/ec2' # Make sure you use the API tools, not the AMI tools export EC2_BIN=$EC2_HOME/bin export EC2_PRIVATE_KEY=/root/.ec2/pk-{your_32_char_rand}.pem export EC2_CERT=/root/.ec2/cert-{your_32_char_rand}.pem export PATH=$PATH:$EC2_BIN export JAVA_HOME=/usr/java/latest /opt/aws/bin/ec2run $*The main shell script for this tutorial is clone, which allows us to create an exact copy of a snapshot as a running instance.
本教程的主要shell脚本是clone ,它使我们能够创建快照的精确副本作为正在运行的实例。
Please be sure to modify the example below to include your specific information.
请确保修改以下示例以包含您的特定信息。
#!/usr/bin/perl # ## clone # use strict; $| = 1; # unbuffered output ####################################################################################################### ## some defaults - change these to match your environment, or override from the command line my $settings = {}; $settings->{'snap'} = ''; $settings->{'name'} = ''; $settings->{'desc'} = ''; $settings->{'key'} = 'AWSKeyPair'; $settings->{'type'} = 't1.micro'; $settings->{'kern'} = 'aki-427d952b'; $settings->{'arch'} = 'x86_64'; $settings->{'size'} = 10; $settings->{'group'} = 'Production'; $settings->{'root'} = '/dev/sda1'; ####################################################################################################### ## Process the command line while ($ARGV[0] =~ /^-/) { $_ = shift; $settings->{'arch'} = shift, next if /^-a$/; $settings->{'desc'} = shift, next if /^-d$/; $settings->{'key'} = shift, next if /^-k$/; $settings->{'kern'} = shift, next if /^-kern$/; $settings->{'name'} = shift, next if /^-n$/; $settings->{'snap'} = shift, next if /^-s$/; $settings->{'type'} = shift, next if /^-t$/; $settings->{'size'} = shift, next if /^-z$/; $settings->{'group'} = shift, next if /^-g$/; $settings->{'root'} = shift, next if /^-r$/; } ####################################################################################################### ## more defaults... chomp($settings->{'desc'} = 'AMI-FROM-SNAPSHOT-' . `/bin/date '+%Y%m%d'`) unless $settings->{'desc'}; ####################################################################################################### ## CREATE AN AMI LINKED TO THE SNAPSHOT my $cmd = qq|/opt/bin/ec2do register -n $settings->{name} -d $settings->{desc} --root-device-name $settings->{root} -b "$settings->{root}=$settings->{snap}" -a $settings->{arch} --kernel $settings->{kern}|; print "Executing: $cmdn"; chomp($_ = `$cmd`); my $ami = (split)[1]; print "New AMI ID: $amin"; ####################################################################################################### ## LAUNCH THE NEW AMI $cmd = qq|/opt/bin/ec2run $ami -b "$settings->{root}=snap-$settings->{snap}:$settings->{size}:true" -g $settings->{group} -t $settings->{type} -k $settings->{key}|; print "Executing: $cmdn"; system($cmd); print "Done.n";Congratulations! Great job – you have automated your recovery.
恭喜你! 干得好–您已使恢复自动化。
http://aws.amazon.com/ec2/faqs/http://docs.amazonwebservices.com/AWSEC2/latest/CommandLineReference/From Zero to Cloud: Setting up an EC2 Sandbox, Part 2
http://aws.amazon.com/ec2/faqs/ http://docs.amazonwebservices.com/AWSEC2/latest/CommandLineReference/ 从零到云:设置EC2沙箱,第2部分
AlexGul Images on Shutterstock
Shutterstock上的AlexGul图片
翻译自: https://www.sitepoint.com/automating-amazon-ec2-instance-backup-and-recovery-part-two-2/
亚马逊ec2 实例删除
相关资源:jdk-8u281-windows-x64.exe