简体   繁体   English

如何使用 Terraform 格式化和挂载临时磁盘?

[英]How to format and mount an ephemeral disk with Terraform?

I'm in the process of writing Packer and Terraform code to create an immutable infra on aws.我正在编写 Packer 和 Terraform 代码以在 aws 上创建一个不可变的基础设施。 However, it does not seem very straightforward to install ext4 on a disk and mount it.但是,在磁盘上安装 ext4 并挂载它似乎不是很简单。

The steps seem simple:步骤看起来很简单:

  • Creating the ami with packer on t2.micro that contains all software, to be used first on test and afterwards on production.在包含所有软件的 t2.micro 上使用打包程序创建 ami,首先用于测试,然后用于生产。
  • Launch a r3.4xlarge instance from this ami that has a 300GB ephemeral disk.从这个 ami 启动一个 r3.4xlarge 实例,它有一个 300GB 的临时磁盘。 Format this disk as ext4, mount it and redirect /var/lib/docker to the new filesystem for performance reasons.出于性能原因,将此磁盘格式化为 ext4,挂载它并将 /var/lib/docker 重定向到新文件系统。
  • Complete the rest of the application launching.完成其余的应用程序启动。

First of all:首先:

Is it best practice to create the ami with the same instance type you will use it for or to have one 'generic' image and start multipe instance types from that?最佳实践是使用您将使用它的相同实例类型创建 ami 还是拥有一个“通用”映像并从中启动多个实例类型? What philosophy is the best?什么哲学是最好的?

  • packer(software versions) -> terraform(instance + mount disk) -> deploy?打包程序(软件版本)-> terraform(实例 + 挂载磁盘)-> 部署?
  • packer(software versions) -> packer(instancetype specific mounts) -> terraform(instance) -> deploy?打包器(软件版本)-> 打包器(特定于实例类型的安装)-> terraform(实例)-> 部署?
  • packer(software versions, instance specific mounts) -> terraform -> deploy?打包程序(软件版本,特定于实例的安装)-> terraform -> 部署?

The latter is starting to look better and better but requires an ami per instance type.后者看起来越来越好,但每个实例类型都需要一个 ami。

What I have tried so far:到目前为止我尝试过的:

According to this answer it is better to use the user_data way of working instead of the provisioners way.根据这个答案,最好使用 user_data 工作方式而不是供应商方式。 So I'm going down that road.所以我要走那条路。

This answer seemed promising but is so old it does not work anymore. 这个答案似乎很有希望,但太旧了,不再有效。 I could update it but there might be a different, better way.我可以更新它,但可能有不同的更好的方法。

This answer also seemed promising but was complaining about the ${DEVICE}. 这个答案似乎也很有希望,但抱怨的是 ${DEVICE}。 I am wondering where that variable is coming from as there are no vars specified in the template_file.我想知道该变量来自哪里,因为 template_file 中没有指定变量。 If I set my own DEVICE variable to xvdb then it runs, but does not produce a result because xvdb is visible in lsblk but not in blkid.如果我将自己的 DEVICE 变量设置为 xvdb,则它会运行,但不会产生结果,因为 xvdb 在 lsblk 中可见但在 blkid 中不可见。

Here is my code.这是我的代码。 The format_disks.sh file is the same as the one mentioned above . format_disks.sh 文件与上述文件相同。 Any help is greatly appreciated.非常感谢任何帮助。

# Create a new instance of the latest Ubuntu 16.04 on an
# t2.micro node with an AWS Tag naming it "test1"
provider "aws" {
  region = "us-east-1"
}

data "template_file" "format-disks" {
  template = "${file("format_disk.sh")}"

  vars {
    DEVICE = "xvdb"
  }
}

resource "aws_instance" "test1" {
  ami           = "ami-98181234"
  instance_type = "r3.4xlarge"
  key_name = "keypair-1"               # This needs to be changed so multiple users can use this
  subnet_id = "subnet-a0aeb123"            # maps to the vpc for the us production
  associate_public_ip_address = "true"
  vpc_security_group_ids = ["sg-f3e91234"] #backendservers
  user_data = "${data.template_file.format-disks.rendered}"
  tags {
    Name = "test1"
  }
  ephemeral_block_device {
    device_name = "xvdb"
    virtual_name = "ephemeral0"
  }
}

Let me give you my thoughts about this topic.让我谈谈我对这个话题的看法。

I think the cloud-init is the key to AWS because you can create the machine you want dynamically.我认为 cloud-init 是 AWS 的关键,因为您可以动态创建您想要的机器。 First, try to change some global script, will be used when your machine is starting.首先,尝试更改一些全局脚本,将在您的机器启动时使用。 Then, you should add that script as user data I suggest you play with ec2 autoscaling at the same time, so, if you change the cloud-init script, you may terminate the instance, another one will be created automatically.然后,您应该将该脚本添加为用户数据 我建议您同时使用 ec2 自动缩放,因此,如果您更改 cloud-init 脚本,您可能会终止该实例,另一个将自动创建。

My structure directories.我的结构目录。

.
|____main.tf
|____templates
| |____cloud-init.tpl

main.tf主文件

provider "aws" {
  region = "us-east-1"
}

data "template_file" "cloud_init" {
  template = file("${path.module}/templates/cloud-init.tpl")
}

data "aws_ami" "linux_ami" {
  most_recent = "true"
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-2.0.????????.?-x86_64-gp2"]
  }
}

resource "aws_instance" "test1" {
  ami                         = data.aws_ami.linux_ami.image_id
  instance_type               = "r3.4xlarge"
  key_name                    = "keypair-1"       
  subnet_id                   = "subnet-xxxxxx"
  associate_public_ip_address = true
  vpc_security_group_ids      = ["sg-xxxxxxx"] 
  user_data                   = data.template_file.cloud_init.rendered
  root_block_device {
      delete_on_termination = true
      encrypted             = true
      volume_size           = 10
      volume_type           = "gp2"
  }

  ebs_block_device {
      device_name = "ebs-block-device-name"
      delete_on_termination = true
      encrypted             = true
      volume_size           = 10
      volume_type           = "gp2"
  }

  network_interface {
      device_index          = 0
      network_interface_id  = var.network_interface_id
      delete_on_termination = true
  }

  tags = {
    Name = "test1"
    costCenter = "xxxxx"
    owner = "xxxxx"
  }
}

templates/cloud-init.tpl模板/云init.tpl

#!/bin/bash -x 

yum update -y
yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
systemctl enable amazon-ssm-agent
systemctl start amazon-ssm-agent

pip install aws-ssm-tunnel-agent

echo "[INFO] SSM agent has been installed!"
# More scripts here.

Would you like to have a temporal disk attached?您想要附加一个临时磁盘吗? Have you tried to add a root_block_device with delete_on_termination with a true as value?您是否尝试使用delete_on_termination添加一个root_block_device true delete_on_termination This way after destroying the aws ec2 instance resource, the disk will be deleted.这样在销毁aws ec2实例资源后,磁盘将被删除。 It's a good way to save costs on AWS but be carefull, Just use it if the data stored on isn't important or if you've backed up.这是在 AWS 上节省成本的好方法,但要小心,如果存储的数据不重要或已备份,请使用它。

If you need to attach an external ebs disk on this instance, you can use the AWS API, make sure you have the machine in the same AZ that the disk you can use it.如果您需要在这种情况下连接外接硬盘EBS,您可以使用AWS API,请确保您有在同一机器AZ磁盘,你可以使用它。

Let me know if you need some bash script but this is straightforward to do.如果您需要一些 bash 脚本,请告诉我,但这很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM