Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 通过AWS控制台为EC2执行根值交换_Amazon Web Services_Amazon Ec2 - Fatal编程技术网

Amazon web services 通过AWS控制台为EC2执行根值交换

Amazon web services 通过AWS控制台为EC2执行根值交换,amazon-web-services,amazon-ec2,Amazon Web Services,Amazon Ec2,我最近一直在使用Swap root volume方法来创建一个持久性Spot实例,如前所述(方法2)。通常,完成Spot实例和交换需要2-5分钟。然而,有些日子,这个过程永远不会结束(或者至少我在等了20分钟到一个小时后变得不耐烦!) 明确地说,创建了实例,但交换从未发生:我可以ssh到服务器中,但我的持久性文件不在那里。我也可以在AWS控制台上看到这一点,注意到“spotter”(我的持久存储)没有附件信息: 由于我使用的交换脚本从未给我任何错误,因此很难看到失败的地方。因此,我想知道,根据

我最近一直在使用Swap root volume方法来创建一个持久性Spot实例,如前所述(方法2)。通常,完成Spot实例和交换需要2-5分钟。然而,有些日子,这个过程永远不会结束(或者至少我在等了20分钟到一个小时后变得不耐烦!)

明确地说,创建了实例,但交换从未发生:我可以ssh到服务器中,但我的持久性文件不在那里。我也可以在AWS控制台上看到这一点,注意到“spotter”(我的持久存储)没有附件信息:

由于我使用的交换脚本从未给我任何错误,因此很难看到失败的地方。因此,我想知道,根据我的屏幕截图,我是否可以使用AWS EC2管理控制台“手动”执行交换,如果可以,我将如何实现这一点

如果这有助于@vorspring

我通过运行以下脚本启动该过程:

    # The config file was created in ondemand_to_spot.sh
export config_file=my.conf
cd "$(dirname ${BASH_SOURCE[0]})"

. ../$config_file || exit -1

export request_id=`../ec2spotter-launch $config_file`
echo Spot request ID: $request_id

echo Waiting for spot request to be fulfilled...
aws ec2 wait spot-instance-request-fulfilled --spot-instance-request-ids $request_id

export instance_id=`aws ec2 describe-spot-instance-requests --spot-instance-request-ids $request_id --query="SpotInstanceRequests[*].InstanceId" --output="text"`

echo Waiting for spot instance to start up...
aws ec2 wait instance-running --instance-ids $instance_id

echo Spot instance ID: $instance_id

echo 'Please allow the root volume swap script a few minutes to finish.'
if [ "x$ec2spotter_elastic_ip" = "x" ]
then
        # Non elastic IP
        export ip=`aws ec2 describe-instances --instance-ids $instance_id --filter Name=instance-state-name,Values=running --query "Reservations[*].Instances[*].PublicIpAddress" --output=text`
else
        # Elastic IP
        export ip=`aws ec2 describe-addresses --allocation-ids $ec2spotter_elastic_ip --output text --query 'Addresses[0].PublicIp'`
fi

export name=fast-ai
if [ "$ec2spotter_key_name" = "aws-key-$name" ]
then    function aws-ssh-spot {
        ssh -i ~/.ssh/aws-key-$name.pem ubuntu@$ip
        }
        function aws-terminate-spot {
        aws ec2 terminate-instances --instance-ids $instance_id
        }
        echo  Jupyter Notebook -- $ip:8888
fi
其中my.conf是:

# Name of root volume.
ec2spotter_volume_name=spotter
# Location (zone) of root volume. If not the same as ec2spotter_launch_zone,
# a copy will be created in ec2spotter_launch_zone.
# Can be left blank, if the same as ec2spotter_launch_zone
ec2spotter_volume_zone=us-west-2b

ec2spotter_launch_zone=us-west-2b
ec2spotter_key_name=aws-key-fast-ai
ec2spotter_instance_type=p2.xlarge
# Some instance types require a subnet to be specified:
ec2spotter_subnet=subnet-c9cba8af

ec2spotter_bid_price=0.55

# uncomment and update the value if you want an Elastic IP
# ec2spotter_elastic_ip=eipalloc-64d5890a

# Security group
ec2spotter_security_group=sg-2be79356

# The AMI to be used as the pre-boot environment. This is NOT your target system installation.
# Do Not Modify this unless you have a need for a different Kernel version from what's supplied.
# ami-6edd3078 is ubuntu-xenial-16.04-amd64-server-20170113
ec2spotter_preboot_image_id=ami-bc508adc
ec2spotter启动脚本是:

    #!/bin/bash

    # "Phase 1" this is the user-facing script for launching a new spot istance

    if [ "$1" = "" ]; then echo "USER ERROR: please specify a configuration file"; exit -1; fi

    cd $(dirname $0)

    . $1 || exit -1

    # New instance:
    # Desired launch zone
    LAUNCH_ZONE=$ec2spotter_launch_zone
    # Region is LAUNCH_ZONE minus the last character
    LAUNCH_REGION=$(echo $LAUNCH_ZONE | sed -e 's/.$//')
    PUB_KEY=$ec2spotter_key_name

    # Existing Volume:
    # If no volume zone
    if [ "$ec2spotter_volume_zone" = "" ]
    then # Use instance zone
            ec2spotter_volume_zone=$LAUNCH_ZONE
    fi

    # Name of volume (find it by name later)
    ROOT_VOL_NAME=$ec2spotter_volume_name
    # zone of volume (needed if different than instance zone)
    ROOT_ZONE=$ec2spotter_volume_zone
    # Region is Zone minus the last character
    ROOT_REGION=$(echo $ROOT_ZONE | sed -e 's/.$//')


    #echo "ROOT_VOL_NAME=${ROOT_VOL_NAME}; ROOT_ZONE=${ROOT_ZONE}; ROOT_REGION=${ROOT_REGION}; "
    #echo "LAUNCH_ZONE=${LAUNCH_ZONE}; LAUNCH_REGION=${LAUNCH_REGION}; PUB_KEY=${PUB_KEY}"

    AWS_ACCESS_KEY=`aws configure get aws_access_key_id`
    AWS_SECRET_KEY=`aws configure get aws_secret_access_key`

    aws ec2 describe-volumes \
            --filters Name=tag-key,Values="Name" Name=tag-value,Values="$ROOT_VOL_NAME" \
            --region ${ROOT_REGION} --output=json > volumes.tmp || exit -1

    ROOT_VOL=$(jq -r '.Volumes[0].VolumeId' volumes.tmp)
    ROOT_TYPE=$(jq -r '.Volumes[0].VolumeType' volumes.tmp)

    #echo "ROOT_TYPE=$ROOT_TYPE; ROOT_VOL=$ROOT_VOL";
    if [ "$ROOT_VOL_NAME" = "" ]
then
  echo "root volume lacks a Name tag";
  exit -1;
fi

cat >user-data.tmp <<EOF
#!/bin/sh
echo AWSAccessKeyId=$AWS_ACCESS_KEY > /root/.aws.creds
echo AWSSecretKey=$AWS_SECRET_KEY >> /root/.aws.creds

apt-get update
apt-get install -y jq
apt-get install -y python-pip python-setuptools
apt-get install -y git

pip install awscli

cd /root
git clone --depth=1 https://github.com/slavivanov/ec2-spotter.git
echo Got spotter scripts from github.

cd ec2-spotter

echo Swapping root volume
./ec2spotter-remount-root  --force 1 --vol_name ${ROOT_VOL_NAME} --vol_region ${ROOT_REGION} --elastic_ip $ec2spotter_elastic_ip
EOF

userData=$(base64 user-data.tmp | tr -d '\n');

cat >specs.tmp <<EOF
{
  "ImageId" : "$ec2spotter_preboot_image_id",
  "InstanceType": "$ec2spotter_instance_type",
  "KeyName" : "$PUB_KEY",
  "EbsOptimized": true,
  "Placement": {
     "AvailabilityZone": "$LAUNCH_ZONE"
  },
  "BlockDeviceMappings": [
    {
      "DeviceName": "/dev/sda1",
      "Ebs": {
        "DeleteOnTermination": true,
        "VolumeType": "gp2",
        "VolumeSize": 128
      }
    }
  ],
  "NetworkInterfaces": [
      {
        "DeviceIndex": 0,
        "SubnetId": "${ec2spotter_subnet}",
        "Groups": [ "${ec2spotter_security_group}" ],
        "AssociatePublicIpAddress": true
      }
  ],
  "UserData" : "${userData}"
}
EOF

SPOT_REQUEST_ID=$(aws ec2 request-spot-instances --launch-specification file://specs.tmp --spot-price $ec2spotter_bid_price --output="text" --query="SpotInstanceRequests[*].SpotInstanceRequestId" --region ${LAUNCH_REGION})
echo $SPOT_REQUEST_ID
# Clean up
rm user-data.tmp
rm specs.tmp
rm volumes.tmp
#/bin/bash
#“阶段1”这是一个面向用户的脚本,用于启动新的spot istance
如果[“$1”=”;然后回显“用户错误:请指定配置文件”;出口-1;fi
cd$(目录名$0)
. $1 | |出口-1
#新实例:
#理想发射区
发射区=$EC2SPOUTER发射区
#区域是启动区域减去最后一个字符
LAUNCH_REGION=$(echo$LAUNCH_ZONE | sed-e's/$/'))
发布密钥=$ec2spotter\u密钥\u名称
#现有卷:
#如果没有卷区域
如果[“$ec2spotter\u volume\u zone”=“”]
然后#使用实例区域
ec2spotter\u volume\u zone=$LAUNCH\u zone
fi
#卷的名称(稍后按名称查找)
ROOT\u VOL\u NAME=$ec2spotter\u volume\u NAME
#卷的分区(如果与实例分区不同,则需要)
根分区=$ec2spotter\u卷分区
#区域是区域减去最后一个字符
ROOT_REGION=$(echo$ROOT_ZONE | sed-e's/$/'))
#echo“ROOT\u VOL\u NAME=${ROOT\u VOL\u NAME};ROOT\u ZONE=${ROOT\u ZONE};ROOT\u REGION=${ROOT\u REGION}
#echo“LAUNCH_ZONE=${LAUNCH_ZONE};LAUNCH_REGION=${LAUNCH_REGION};PUB_KEY=${PUB_KEY}”
AWS\u ACCESS\u KEY=`AWS configure get AWS\u ACCESS\u KEY\u id`
AWS\u SECRET\u KEY=`AWS configure get AWS\u SECRET\u access\u KEY`
aws ec2描述卷\
--过滤器名称=标记键,Values=“Name”名称=标记值,Values=“$ROOT\u VOL\u Name”\
--region${ROOT_region}--output=json>volumes.tmp | | exit-1
ROOT_VOL=$(jq-r'.Volumes[0].VolumeId'Volumes.tmp)
根目录类型=$(jq-r'.Volumes[0].VolumeType'Volumes.tmp)
#回显“根目录类型=$ROOT目录类型;根目录卷=$ROOT目录卷”;
如果[“$ROOT\u VOL\u NAME”=“”]
然后
echo“根卷缺少名称标记”;
出口-1;
fi
cat>user-data.tmp>/root/.aws.creds
更新源
apt-get-install-y jq
apt get install-y python pip python setuptools
apt get安装-y git
pip安装awscli
cd/根目录
git克隆--深度=1https://github.com/slavivanov/ec2-spotter.git
echo从github获得了观察员脚本。
cd ec2观测仪
回显交换根卷
./ec2spotter重新安装root--force 1--vol_name${root_vol_name}--vol_region${root_region}--elastic_ip$ec2spotter_elastic_ip
EOF
userData=$(base64 user-data.tmp | tr-d'\n');

cat>specs.tmp这不是一个确切的答案,但它可以帮助您找到调试问题的方法。 据我所知,这是负责卷交换的
ec2spotter启动
脚本中设置的一部分:

...
cat >specs.tmp <<EOF
{
  "ImageId" : "$ec2spotter_preboot_image_id",
  ...
  "UserData" : "${userData}"
}
EOF

SPOT_REQUEST_ID=$(aws ec2 request-spot-instances --launch-specification file://specs.tmp --spot-price $ec2spotter_bid_price --output="text" --query="SpotInstanceRequests[*].SpotInstanceRequestId" --region ${LAUNCH_REGION})
交换根卷的实际工作由
ec2sporteremount root
脚本执行,该脚本为

该脚本中有许多
echo
语句,因此我认为如果您找到了输出的位置,您将能够理解错误所在。 因此,当出现问题时,您将使用ssh连接到实例并检查日志文件。 问题是要检查哪个文件(以及脚本输出是否被记录到某个文件中)

以下是我的建议:

  • 检查实例启动时生成的
    /var/log
    下的标准日志(cloud-init.log、syslog等),查看是否可以找到
    ec2spotter remount root
    输出

  • 尝试自己启用日志记录,类似的内容也在讨论中

  • 我会尝试通过以下方式修改
    es2spotter launch
    中的
    user data.tmp
    部分:

    #!/bin/bash
    set -x
    exec > >(tee /var/log/user-data.log|logger -t user-data ) 2>&1
    echo AWSAccessKeyId=$AWS_ACCESS_KEY > /root/.aws.creds
    ...
    echo Swapping root volume
    ./ec2spotter-remount-root  --force 1 --vol_name ${ROOT_VOL_NAME} --vol_region ${ROOT_REGION} --elastic_ip $ec2spotter_elastic_ip
    EOF
    
    在这里,我更改了前三行以启用登录到
    /var/log/user data.log

  • 如果1和2不起作用,我会尝试询问github上的脚本作者。由于脚本中有大量的
    echo
    s,作者应该知道在哪里查找该输出
  • 希望这对您有所帮助,您也不需要等待问题出现才尝试执行此操作,而是在成功运行时查找脚本输出。
    或者,如果您能够进行一些测试运行,那么就这样做,并确保您可以找到带有脚本输出的日志。

    我认为您不需要再对spot实例执行此操作。你看过这个新功能吗?这项功能似乎只能在“当容量不再以您的出价或低于您的出价时”使用,这不是我的使用案例。我转到了您提到的链接页面。你需要再问一次这个问题,但是脚本的全部细节(我认为是“start_spot.sh”)是失败的。我相信有人可以找到一种方法,从脚本中获取诊断信息,以理解为什么它不起作用,或者以某种方式修复它。要手动附加一个不同的根卷,请停止实例,分离当前根卷,并使用完全相同的参数(通常是/dev/sda1作为装入点)附加您要使用的根卷@vorspring我已为相关脚本添加了代码。而且,是的,从脚本中获取诊断将非常有用!您在哪里查找错误@虽然这不是完整的答案,但提供的信息肯定会让我走上一条无法解决问题的道路
    #!/bin/bash
    set -x
    exec > >(tee /var/log/user-data.log|logger -t user-data ) 2>&1
    echo AWSAccessKeyId=$AWS_ACCESS_KEY > /root/.aws.creds
    ...
    echo Swapping root volume
    ./ec2spotter-remount-root  --force 1 --vol_name ${ROOT_VOL_NAME} --vol_region ${ROOT_REGION} --elastic_ip $ec2spotter_elastic_ip
    EOF