簡體   English   中英

如何使用 Terraform 格式化和掛載臨時磁盤?

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

我正在編寫 Packer 和 Terraform 代碼以在 aws 上創建一個不可變的基礎設施。 但是,在磁盤上安裝 ext4 並掛載它似乎不是很簡單。

步驟看起來很簡單:

  • 在包含所有軟件的 t2.micro 上使用打包程序創建 ami,首先用於測試,然后用於生產。
  • 從這個 ami 啟動一個 r3.4xlarge 實例,它有一個 300GB 的臨時磁盤。 出於性能原因,將此磁盤格式化為 ext4,掛載它並將 /var/lib/docker 重定向到新文件系統。
  • 完成其余的應用程序啟動。

首先:

最佳實踐是使用您將使用它的相同實例類型創建 ami 還是擁有一個“通用”映像並從中啟動多個實例類型? 什么哲學是最好的?

  • 打包程序(軟件版本)-> terraform(實例 + 掛載磁盤)-> 部署?
  • 打包器(軟件版本)-> 打包器(特定於實例類型的安裝)-> terraform(實例)-> 部署?
  • 打包程序(軟件版本,特定於實例的安裝)-> terraform -> 部署?

后者看起來越來越好,但每個實例類型都需要一個 ami。

到目前為止我嘗試過的:

根據這個答案,最好使用 user_data 工作方式而不是供應商方式。 所以我要走那條路。

這個答案似乎很有希望,但太舊了,不再有效。 我可以更新它,但可能有不同的更好的方法。

這個答案似乎也很有希望,但抱怨的是 ${DEVICE}。 我想知道該變量來自哪里,因為 template_file 中沒有指定變量。 如果我將自己的 DEVICE 變量設置為 xvdb,則它會運行,但不會產生結果,因為 xvdb 在 lsblk 中可見但在 blkid 中不可見。

這是我的代碼。 format_disks.sh 文件與上述文件相同。 非常感謝任何幫助。

# 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"
  }
}

讓我談談我對這個話題的看法。

我認為 cloud-init 是 AWS 的關鍵,因為您可以動態創建您想要的機器。 首先,嘗試更改一些全局腳本,將在您的機器啟動時使用。 然后,您應該將該腳本添加為用戶數據 我建議您同時使用 ec2 自動縮放,因此,如果您更改 cloud-init 腳本,您可能會終止該實例,另一個將自動創建。

我的結構目錄。

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

主文件

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"
  }
}

模板/雲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.

您想要附加一個臨時磁盤嗎? 您是否嘗試使用delete_on_termination添加一個root_block_device true delete_on_termination 這樣在銷毀aws ec2實例資源后,磁盤將被刪除。 這是在 AWS 上節省成本的好方法,但要小心,如果存儲的數據不重要或已備份,請使用它。

如果您需要在這種情況下連接外接硬盤EBS,您可以使用AWS API,請確保您有在同一機器AZ磁盤,你可以使用它。

如果您需要一些 bash 腳本,請告訴我,但這很簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM