【问题标题】:Using existing securityGroup and pem file in jclouds - Java在 jclouds 中使用现有的 securityGroup 和 pem 文件 - Java
【发布时间】:2016-09-17 21:03:09
【问题描述】:

我开始使用 jclouds 来操作 EC2 实例。因此,我有以下方法:

private ComputeService compute;

public void add(Integer instances) {
        try {
            logger.info("------------------------------------------------------");
            logger.info(String.format(">> adding node to group %s%n", this.groupname));
            // Default template chooses the smallest size on an operating system
            // that tested to work with java, which tends to be Ubuntu or CentOS
            TemplateBuilder templateBuilder = this.compute.templateBuilder();
            // note this will create a user with the same name as you on the
            // node. ex. you can connect via ssh publicip
            Statement bootInstructions = AdminAccess.standard();

            // to run commands as root, we use the runScript option in the template.
            templateBuilder.options(runScript(bootInstructions));

            Template template = templateBuilder.build();
            // add a custom security group

            NodeMetadata node = getOnlyElement(this.compute.createNodesInGroup(this.groupname, instances, template));
            logger.info(String.format("<< node %s: %s%n", node.getId(),
                        concat(node.getPrivateAddresses(), node.getPublicAddresses())));
            logger.info("------------------------------------------------------");
        } catch (Exception e) {
            logger.error(e.getMessage());
            logger.info("------------------------------------------------------");
        }
    }

请假设验证是正确的并且 groupname = default。当我执行

this.compute.add(1); 

即使创建了一个实例,jclouds 也会每次都创建一个新的security groupkey pair。我确实有我现有的foo.pem 文件和default 安全组。例如: security group = jclouds#default。如何利用现有的安全组和密钥值?

【问题讨论】:

    标签: java amazon-web-services amazon-ec2 jclouds


    【解决方案1】:

    如果您想使用现有的安全组或密钥对,您可以通过将它们提供给模板构建器来实现:

    templateBuilder.securityGroups(<existing security groups>);
    templateBuilder.as(AWSEC2TemplateOptions.class).keyPair(<existing key pair>);
    templateBuilder.options(runScript(bootInstructions));
    

    请注意,为了使用 keypair 方法,您需要将选项强制转换为 AWS 类,因为该选项在可移植界面中不可用。

    还请注意,当使用基于密钥的访问通过 SSH(运行脚本)访问节点时,jclouds 必须向节点提供“私钥”。您有两种选择:预先将密钥加载到 ssh-agent 中,或者使用 templateOptions.overrideLoginPrivateKey 提供您的私钥,以便 jclouds 可以正确登录到节点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-27
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多