Terraform:安装卷 [英] Terraform: Mount volume
问题描述
根据文档,使用 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...
有什么想法吗?
推荐答案
要自动挂载volume,可以通过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
希望对你有帮助
这篇关于Terraform:安装卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!