【问题标题】:JClouds: How to attach a new volume to an AWS EC2 instanceJClouds:如何将新卷附加到 AWS EC2 实例
【发布时间】:2015-04-10 00:54:27
【问题描述】:

我正在尝试使用 JClouds 将新卷附加到我的实例。但我找不到办法。

    final String POLL_PERIOD_TWENTY_SECONDS = String.valueOf(SECONDS.toMillis(20));

    Properties overrides = new Properties();
    overrides.setProperty(ComputeServiceProperties.POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
    overrides.setProperty(ComputeServiceProperties.POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS);

    Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule(), new SLF4JLoggingModule());
    //Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule());

    ComputeServiceContext context = ContextBuilder.newBuilder("aws-ec2")
            .credentials("valid user", "valid password")
            .modules(modules)
            .overrides(overrides)
            .buildView(ComputeServiceContext.class);
    ComputeService computeService = context.getComputeService();

    // Ubuntu AMI
    Template template = computeService.templateBuilder()
            .locationId("us-east-1")
            .imageId("us-east-1/ami-7c807d14")
            .hardwareId("t1.micro")
            .build();

    // This line creates the volume but does not attach it
    template.getOptions().as(EC2TemplateOptions.class).mapNewVolumeToDeviceName("/dev/sdm", 100, true);

    Set<? extends NodeMetadata> nodes = computeService.createNodesInGroup("m456", 1, template);

    for (NodeMetadata nodeMetadata : nodes) {
        String publicAddress = nodeMetadata.getPublicAddresses().iterator().next();
        //String privateAddress = nodeMetadata.getPrivateAddresses().iterator().next();
        String username = nodeMetadata.getCredentials().getUser();
        String password = nodeMetadata.getCredentials().getPassword();
        // [...]
        System.out.println(String.format("ssh %s@%s  %s", username, publicAddress, password));
        System.out.println(nodeMetadata.getCredentials().getPrivateKey());
    }

如何创建附加卷到目录“/var”? 如何创建具有更多硬盘空间的实例?

【问题讨论】:

    标签: java amazon-web-services jclouds


    【解决方案1】:

    您可以为此使用 jclouds 中的 ElasticBlockStoreApi。您已经创建了实例,所以像这样创建并附加卷:

    // Get node
    NodeMetadata node = Iterables.getOnlyElement(nodes);
    
    // Get AWS EC2 API
    EC2Api ec2Api = computeServiceContext.unwrapApi(EC2Api.class);
    
    // Create 100 GiB Volume
    Volume volume = ec2Api.getElasticBlockStoreApi().get()
            .createVolumeInAvailabilityZone(zoneId, 100);
    
    // Attach to instance
    Attachment attachment = ec2Api.getElasticBlockStoreApi().get()
            .attachVolumeInRegion(region, volume.getId(), node.getId(), "/dev/sdx");
    

    现在,您已附加了一个 EBS 卷,并且虚拟机已为其创建了一个块设备,您只需运行正确的命令将其挂载到 /var 目录,这取决于您的特定操作系统。您可以像这样运行脚本:

    // Run script on instance
    computeService.runScriptOnNode(node.getId(),
            Statements.exec("mount /dev/sdx /var"));
    

    【讨论】:

    • Will Statements.exec("mount /dev/sdx /var" ) 是否会在重启后保留..我的意思是文件夹 /var 会在重启后指向 /dev/sdx 文件系统吗?
    猜你喜欢
    • 2018-12-29
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多