地形:安装量 [英] Terraform: Mount volume

查看:74
本文介绍了地形:安装量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,使用terraform,我可以在数字海洋上创建一个小滴:

According to documentation, using terraform, I'm able to create a droplet on digital ocean:

resource "digitalocean_volume" "foobar" {
  region      = "nyc1"
  name        = "baz"
  size        = 100
  description = "an example volume"
}

因此,我也可以为其添加一个音量:

So, I'm also able to add a volume to it:

resource "digitalocean_droplet" "foobar" {
  name       = "baz"
  size       = "1gb"
  image      = "coreos-stable"
  region     = "nyc1"
  volume_ids = ["${digitalocean_volume.foobar.id}"]
}

我想知道如何将其安装在所需的位置。
我需要自动安装它。我的意思是,当液滴滴加完时,我需要安装卷。我正在考虑使用厨师...

I'd like to know how to mount this on a desired location. I need to mount it automatically. I mean, when droplet is up I need to the volume is mounted. I was thinking about using chef...

有什么想法吗?

推荐答案

要自动安装卷,可以通过cloud init使用user_data来运行脚本,如下所示:

To mount the volume automatically, you can use user_data via cloud init to run a script as follow:

这是您的digitalocean_droplet资源应如何体现的:

This is how your digitalocean_droplet resources should reflect:

resource "digitalocean_droplet" "foobar" {
  name       = "baz"
  size       = "1gb"
  image      = "coreos-stable"
  region     = "nyc1"
  volume_ids = ["${digitalocean_volume.foobar.id}"]
   # user data
  user_data = "${data.template_cloudinit_config.cloudinit-example.rendered}"
}

然后,包含cloudinit_config的cloud.init文件应如下所示。它将引用$ {TERRAFORM_HOME} /script/disk.sh中的shell脚本,该脚本将自动安装您的卷:

Then your cloud.init file that contains the cloudinit_config should be as bellow. It will reference the shell script in ${TERRAFORM_HOME}/script/disk.sh that would mount your volume automatically:

provider "cloudinit" {}


data "template_file" "shell-script" {
  template = "${file("scripts/disk.sh")}"

}
data "template_cloudinit_config" "cloudinit-example" {

  gzip = false
  base64_encode = false

  part {
    content_type = "text/x-shellscript"
    content      = "${data.template_file.shell-script.rendered}"
  }

}

用于在启动时自动安装卷的shell脚本位于$ {TERRAFORM_HOME} /script/disk.sh

The shell script to mount the volume automatically on startup is in ${TERRAFORM_HOME}/script/disk.sh

它将首先检查文件系统是否存在。如果为true,则不会格式化磁盘

It will first check if a file system exist. If true it wouldn't format the disk if not it will

#!/bin/bash


DEVICE_FS=`blkid -o value -s TYPE ${DEVICE}`
if [ "`echo -n $DEVICE_FS`" == "" ] ; then
        mkfs.ext4 ${DEVICE}
fi
mkdir -p /data
echo '${DEVICE} /data ext4 defaults 0 0' >> /etc/fstab
mount /data

我希望这会有所帮助

这篇关于地形:安装量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆